Class: DomSection
- Inherits:
-
BaseSection
- Object
- BaseSection
- DomSection
- Defined in:
- lib/volt/templates/targets/dom_section.rb
Instance Method Summary collapse
- #find_by_comment(text, in_node = `document`) ⇒ Object
-
#initialize(binding_name) ⇒ DomSection
constructor
A new instance of DomSection.
- #insert_anchor_before_end(binding_name) ⇒ Object
-
#nodes=(nodes) ⇒ Object
Takes in an array of dom nodes and replaces the current content with the new nodes.
- #remove ⇒ Object
- #remove_anchors ⇒ Object
-
#set_content_and_rezero_bindings(html, bindings) ⇒ Object
Takes in our html and bindings, and rezero’s the comment names, and the bindings.
- #text=(value) ⇒ Object
Constructor Details
#initialize(binding_name) ⇒ DomSection
Returns a new instance of DomSection.
4 5 6 7 |
# File 'lib/volt/templates/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/templates/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 |
#insert_anchor_before_end(binding_name) ⇒ Object
42 43 44 |
# File 'lib/volt/templates/targets/dom_section.rb', line 42 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/volt/templates/targets/dom_section.rb', line 48 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 |
#remove ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/volt/templates/targets/dom_section.rb', line 25 def remove range = self.range() %x{ range.deleteContents(); } end |
#remove_anchors ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/volt/templates/targets/dom_section.rb', line 33 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
65 66 67 68 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 |
# File 'lib/volt/templates/targets/dom_section.rb', line 65 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); 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/templates/targets/dom_section.rb', line 18 def text=(value) %x{ this.$range().deleteContents(); this.$range().insertNode(document.createTextNode(#{value})); } end |