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
45
46
47
48
49
50
51
52
53
54
55
# File 'app/builders/releaf/content/builders/tree.rb', line 27

def build_tree
  stack = []
  result = []

  collection.each do |node|
    if stack.empty?
      stack.push({ node: node, children: [] })
      result << stack.last
      next
    end

    if stack.last[:node].lft < node.lft && node.lft < stack.last[:node].rgt
      child = { node: node, children: [] }
      stack.last[:children] << child

      if node.rgt + 1 == stack.last[:node].rgt
        stack.pop
      end

      unless node.leaf?
        stack.push(child)
      end
    else
      stack.pop
    end
  end

  result
end

#empty_bodyObject



68
69
70
71
72
# File 'app/builders/releaf/content/builders/tree.rb', line 68

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

#root_levelObject



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

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



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

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

#tree_level(list, level) ⇒ Object



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

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



82
83
84
85
86
87
88
89
90
91
# File 'app/builders/releaf/content/builders/tree.rb', line 82

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



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

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



105
106
107
108
# File 'app/builders/releaf/content/builders/tree.rb', line 105

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

#tree_resource_collapser(resource, expanded) ⇒ Object



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

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



110
111
112
113
114
115
116
117
# File 'app/builders/releaf/content/builders/tree.rb', line 110

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



119
120
121
122
123
124
# File 'app/builders/releaf/content/builders/tree.rb', line 119

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