Class: Volt::DomSection

Inherits:
BaseSection show all
Includes:
CommentSearchers
Defined in:
lib/volt/page/targets/dom_section.rb

Constant Summary

Constants included from CommentSearchers

CommentSearchers::NO_XPATH

Instance Method Summary collapse

Methods included from CommentSearchers

#build_from_html, #find_by_comment, #find_by_comment_without_xml

Methods inherited from BaseSection

#set_content_to_template

Constructor Details

#initialize(binding_name) ⇒ DomSection

Returns a new instance of DomSection.



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

def initialize(binding_name)
  @start_node = find_by_comment("$#{binding_name}")
  @end_node   = find_by_comment("$/#{binding_name}")
end

Instance Method Details

#container_nodeObject

Returns the nearest DOM node that contains all of the section.



70
71
72
73
# File 'lib/volt/page/targets/dom_section.rb', line 70

def container_node
  range = self.range
  `range.commonAncestorContainer`
end

#html=(value) ⇒ Object



20
21
22
23
24
# File 'lib/volt/page/targets/dom_section.rb', line 20

def html=(value)
  new_nodes = build_from_html(value)

  self.nodes = `new_nodes.childNodes`
end

#insert_anchor_before(binding_name, insert_after_binding) ⇒ Object



47
48
49
50
# File 'lib/volt/page/targets/dom_section.rb', line 47

def insert_anchor_before(binding_name, insert_after_binding)
  node = find_by_comment("$#{insert_after_binding}")
  Element.find(node).before("<!-- $#{binding_name} --><!-- $/#{binding_name} -->")
end

#insert_anchor_before_end(binding_name) ⇒ Object



43
44
45
# File 'lib/volt/page/targets/dom_section.rb', line 43

def insert_anchor_before_end(binding_name)
  Element.find(@end_node).before("<!-- $#{binding_name} --><!-- $/#{binding_name} -->")
end

#inspectObject



108
109
110
# File 'lib/volt/page/targets/dom_section.rb', line 108

def inspect
  "<#{self.class}>"
end

#nodes=(nodes) ⇒ Object

Takes in an array of dom nodes and replaces the current content with the new nodes



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/volt/page/targets/dom_section.rb', line 54

def nodes=(nodes)
  range = self.range

  `
    range.deleteContents();

    for (var i=nodes.length-1; i >= 0; i--) {
      var node = nodes[i];

      node.parentNode.removeChild(node);
      range.insertNode(node);
    }
  `
end

#rangeObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/volt/page/targets/dom_section.rb', line 93

def range
  return @range if @range

  range = nil
  `
    range = document.createRange();
    range.setStartAfter(this.start_node);
    range.setEndBefore(this.end_node);
  `

  @range = range

  range
end

#removeObject



26
27
28
29
30
31
32
# File 'lib/volt/page/targets/dom_section.rb', line 26

def remove
  range = self.range

  `
    range.deleteContents();
  `
end

#remove_anchorsObject



34
35
36
37
38
39
40
41
# File 'lib/volt/page/targets/dom_section.rb', line 34

def remove_anchors
  `
    this.start_node.parentNode.removeChild(this.start_node);
    this.end_node.parentNode.removeChild(this.end_node);
  `
  @start_node = nil
  @end_node   = nil
end

#set_template(dom_template) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/volt/page/targets/dom_section.rb', line 75

def set_template(dom_template)
  dom_nodes, bindings = dom_template.make_new

  children = nil
  `
  children = dom_nodes.childNodes;
    `

  # Update the nodes
  self.nodes = children

  `
  dom_nodes = null;
    `

  bindings
end

#text=(value) ⇒ Object



13
14
15
16
17
18
# File 'lib/volt/page/targets/dom_section.rb', line 13

def text=(value)
  `
    this.$range().deleteContents();
    this.$range().insertNode(document.createTextNode(#{value}));
  `
end