class Volt::DomSection

Public Class Methods

new(binding_name) click to toggle source
# 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

Public Instance Methods

container_node() click to toggle source

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

# File lib/volt/page/targets/dom_section.rb, line 73
def container_node
  range = self.range
  `range.commonAncestorContainer`
end
html=(value) click to toggle source
# 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) click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 48
def insert_anchor_before(binding_name, insert_after_binding)
  node = find_by_comment("$#{insert_after_binding}")

  binding_str = "<!-- $#{binding_name} --><!-- $/#{binding_name} -->"
  `$(#{node}).before(#{binding_str})`
end
insert_anchor_before_end(binding_name) click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 43
def insert_anchor_before_end(binding_name)
  binding_str = "<!-- $#{binding_name} --><!-- $/#{binding_name} -->"
  `$(#{@end_node}).before(#{binding_str})`
end
inspect() click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 111
def inspect
  "<#{self.class}>"
end
nodes=(nodes) click to toggle source

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

# File lib/volt/page/targets/dom_section.rb, line 57
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
range() click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 96
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
remove() click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 26
def remove
  range = self.range

  `
    range.deleteContents();
  `
end
remove_anchors() click to toggle source
# 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) click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 78
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) click to toggle source
# File lib/volt/page/targets/dom_section.rb, line 13
def text=(value)
  `
    this.$range().deleteContents();
    this.$range().insertNode(document.createTextNode(#{value}));
  `
end