Module: Releaf::Content::Builders::Tree

Included in:
Dialog, Nodes::IndexBuilder
Defined in:
app/builders/releaf/content/builders/tree.rb

Instance Method Summary collapse

Instance Method Details

#body_classesObject



9
10
11
12
13
# File 'app/builders/releaf/content/builders/tree.rb', line 9

def body_classes
  classes = [:body]
  classes << :empty if collection.size < 1
  classes
end

#build_treeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/builders/releaf/content/builders/tree.rb', line 27

def build_tree
  object_hash = collection.inject({}) do |result, node|
    result[node.id] = { node: node, children: [] }
    result
  end

  object_hash[nil] = { root: true, children: [] }

  object_hash.each_value do |node|
    next if node[:root]
    next if node[:node].parent_id && !object_hash[node[:node].parent_id]

    children = object_hash[node[:node].parent_id][:children]
    children << node
  end

  object_hash[nil][:children]
end

#empty_bodyObject



57
58
59
60
61
# File 'app/builders/releaf/content/builders/tree.rb', line 57

def empty_body
  tag(:div, class: "nothing-found") do
    t("Nothing found")
  end
end

#root_levelObject



52
53
54
55
# File 'app/builders/releaf/content/builders/tree.rb', line 52

def root_level
  return empty_body if collection.size < 1
  tree_level(sorted_tree, 1)
end

#section_bodyObject



3
4
5
6
7
# File 'app/builders/releaf/content/builders/tree.rb', line 3

def section_body
  tag(:div, class: body_classes) do
    tree
  end
end

#sort_tree(nodes) ⇒ Object



19
20
21
22
23
24
25
# File 'app/builders/releaf/content/builders/tree.rb', line 19

def sort_tree(nodes)
  nodes.sort_by! { |item| item[:node].item_position }

  nodes.each do |node|
    sort_tree(node[:children]) if node[:children].present?
  end
end

#sorted_treeObject



15
16
17
# File 'app/builders/releaf/content/builders/tree.rb', line 15

def sorted_tree
  sort_tree(build_tree)
end

#treeObject



46
47
48
49
50
# File 'app/builders/releaf/content/builders/tree.rb', line 46

def tree
  tag(:div, class: "collection") do
    root_level
  end
end

#tree_level(list, level) ⇒ Object



63
64
65
66
67
68
69
# File 'app/builders/releaf/content/builders/tree.rb', line 63

def tree_level(list, level)
  tag(:ul, "data-level" => level) do
    list.collect do |resource|
      tree_resource(resource, level)
    end
  end
end

#tree_resource(resource, level) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/builders/releaf/content/builders/tree.rb', line 71

def tree_resource(resource, level)
  expanded = (layout_settings("content.tree.expanded.#{resource[:node].id}") == true)
  classes = []
  classes << 'collapsed' unless expanded
  classes << 'has-children' unless resource[:children].empty?

  tag(:li, class: classes, data: {level: level, id: resource[:node].id}) do
    tree_resource_blocks(resource, level, expanded)
  end
end

#tree_resource_blocks(resource, level, expanded) ⇒ Object



82
83
84
85
# File 'app/builders/releaf/content/builders/tree.rb', line 82

def tree_resource_blocks(resource, level, expanded)
  [tree_resource_collapser(resource, expanded),
   tree_resource_name(resource[:node]), tree_resource_children(resource, level)]
end

#tree_resource_children(resource, level) ⇒ Object



94
95
96
97
# File 'app/builders/releaf/content/builders/tree.rb', line 94

def tree_resource_children(resource, level)
  return if resource[:children].empty?
  tree_level(resource[:children], level + 1)
end

#tree_resource_collapser(resource, expanded) ⇒ Object



87
88
89
90
91
92
# File 'app/builders/releaf/content/builders/tree.rb', line 87

def tree_resource_collapser(resource, expanded)
  return if resource[:children].empty?
  tag(:div, class: "collapser-cell") do
    button(nil, (expanded ? 'chevron-down' : 'chevron-right'), class: %w(secondary collapser trigger), title: t(expanded ? "Collapse" : "Expand"))
  end
end

#tree_resource_name(resource) ⇒ Object



99
100
101
102
103
104
105
106
# File 'app/builders/releaf/content/builders/tree.rb', line 99

def tree_resource_name(resource)
  classes = ["node-cell"]
  classes << "active" if resource.active?

  tag(:div, class: classes) do
    tree_resource_name_button(resource)
  end
end

#tree_resource_name_button(resource) ⇒ Object



108
109
110
111
112
113
# File 'app/builders/releaf/content/builders/tree.rb', line 108

def tree_resource_name_button(resource)
  title = resource.content_id.present? ? "#{resource.content_type} ##{resource.content_id}" : resource.content_type
  tag(:a, class: "trigger", href: url_for(action: "edit", id: resource.id), title: title) do
    tag(:span, resource.name)
  end
end