Class: DomSection

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

Instance Method Summary collapse

Constructor Details

#initialize(binding_name) ⇒ DomSection

Returns a new instance of DomSection.



4
5
6
7
# File 'lib/volt/page/targets/dom_section.rb', line 4

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

Instance Method Details

#find_by_comment(text, in_node = `document`) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/volt/page/targets/dom_section.rb', line 9

def find_by_comment(text, in_node=`document`)
	node = nil

	%x{
		node = document.evaluate("//comment()[. = ' " + text + " ']", in_node, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null).iterateNext();
	}
	return node
end

#html=(value) ⇒ Object



25
26
27
# File 'lib/volt/page/targets/dom_section.rb', line 25

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

#insert_anchor_before_end(binding_name) ⇒ Object



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

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

#nodes=(nodes) ⇒ Object

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



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

def nodes=(nodes)
  range = self.range()

  %x{
    range.deleteContents();

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

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

#removeObject



29
30
31
32
33
34
35
# File 'lib/volt/page/targets/dom_section.rb', line 29

def remove
  range = self.range()

  %x{
    range.deleteContents();
  }
end

#remove_anchorsObject



37
38
39
40
41
42
43
44
# File 'lib/volt/page/targets/dom_section.rb', line 37

def remove_anchors    
  %x{
    this.start_node.parentNode.removeChild(this.start_node);
    this.end_node.parentNode.removeChild(this.end_node);
  }
  @start_node = nil
  @end_node = nil
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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/volt/page/targets/dom_section.rb', line 69

def set_content_and_rezero_bindings(html, bindings)
	sub_nodes = nil
   temp_div = nil

   %x{
     temp_div = document.createElement('div');
     var doc = jQuery.parseHTML(html);
     
     if (doc) {
       for (var i=0;i < doc.length;i++) {
         temp_div.appendChild(doc[i]);
       }
     }
   }

   new_bindings = {}
   # Loop through the bindings, and rezero.
   bindings.each_pair do |name,binding|
     new_name = @@binding_number

     if name.cur.is_a?(String)
       if name[0..1] == 'id'
       # Find by id
         %x{
           var node = temp_div.querySelector('#' + name);
           node.setAttribute('id', 'id' +new_name);
         }
         
         new_bindings["id#{new_name}"] = binding
       else
         # Assume a fixed id
         # TODO: We should raise an exception if this id is already on the page
         new_bindings[name] = binding
       end
     else
       # Change the comment ids
       start_comment = find_by_comment("$#{name}", temp_div)
       end_comment = find_by_comment("$/#{name}", temp_div)

       %x{
         start_comment.textContent = " $" + new_name + " ";
         end_comment.textContent = " $/" + new_name + " ";
       }

       new_bindings[new_name] = binding
     end


     @@binding_number += 1
   end


   children = nil
	%x{
	  children = temp_div.childNodes;
	}
   
   # Update the nodes
   self.nodes = children
   
   %x{
     temp_div = null;
   }

	return new_bindings
end

#text=(value) ⇒ Object



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

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