Class: Volt::AttributeSection

Inherits:
BaseSection show all
Defined in:
lib/volt/page/targets/attribute_section.rb

Instance Method Summary collapse

Methods inherited from BaseSection

#remove_anchors, #set_content_to_template, #set_template

Constructor Details

#initialize(target, binding_name) ⇒ AttributeSection

Returns a new instance of AttributeSection.



8
9
10
11
# File 'lib/volt/page/targets/attribute_section.rb', line 8

def initialize(target, binding_name)
  @target       = target
  @binding_name = binding_name
end

Instance Method Details

#html=(value) ⇒ Object



17
18
19
# File 'lib/volt/page/targets/attribute_section.rb', line 17

def html=(value)
  set_content_and_rezero_bindings(value, {})
end

#insert_anchor_before_end(binding_name) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/volt/page/targets/attribute_section.rb', line 21

def insert_anchor_before_end(binding_name)
  end_node = @target.find_by_binding_id(@binding_name)
  if end_node.is_a?(ComponentNode)
    component_node = ComponentNode.new(binding_name, end_node, end_node.root || end_node)
    end_node.insert(-1, component_node)
  else
    raise "can not insert on HtmlNode"
  end
end

#removeObject



80
81
82
83
84
# File 'lib/volt/page/targets/attribute_section.rb', line 80

def remove
  # TODO: is this getting run for no reason?
  node = @target.find_by_binding_id(@binding_name)
  node.remove if node
end

#rezero_bindings(html, bindings) ⇒ Object

When using bindings, we have to change the binding id so we don’t reuse the same id when rendering a binding multiple times.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/volt/page/targets/attribute_section.rb', line 33

def rezero_bindings(html, bindings)
  @@base_binding_id ||= 20_000
  # rezero
  parts  = html.split(/(\<\!\-\- \$\/?[0-9]+ \-\-\>)/).reject { |v| v == '' }

  new_html = []
  new_bindings = {}
  id_map = {}

  parts.each do |part|
    case part
      when /\<\!\-\- \$[0-9]+ \-\-\>/
        # Open
        binding_id = part.match(/\<\!\-\- \$([0-9]+) \-\-\>/)[1].to_i
        binding = bindings[binding_id]
        new_bindings[@@base_binding_id] = binding if binding

        new_html << "<!-- $#{@@base_binding_id} -->"
        id_map[binding_id] = @@base_binding_id
        @@base_binding_id += 1
      when /\<\!\-\- \$\/[0-9]+ \-\-\>/
        # Close
        binding_id = part.match(/\<\!\-\- \$\/([0-9]+) \-\-\>/)[1].to_i
        new_html << "<!-- $/#{id_map[binding_id]} -->"
      else
        # html string
        new_html << part
    end
  end

  return new_html.join(''), new_bindings
end

#set_content_and_rezero_bindings(html, bindings) ⇒ Object

Takes in our html and bindings, and rezero’s the comment names, and the bindings. Returns an updated bindings hash



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/volt/page/targets/attribute_section.rb', line 68

def set_content_and_rezero_bindings(html, bindings)
  html, bindings = rezero_bindings(html, bindings)

  if @binding_name == 'main'
    @target.html = html
  else
    @target.find_by_binding_id(@binding_name).html = html
  end

  bindings
end

#text=(text) ⇒ Object



13
14
15
# File 'lib/volt/page/targets/attribute_section.rb', line 13

def text=(text)
  set_content_and_rezero_bindings(text, {})
end