Class: DOM::NODE

Inherits:
Object show all
Includes:
Comparable, Events
Defined in:
opal/fron/dom/node.rb

Overview

Low level wrapper for the Node class

:reek:TooManyMethods

Direct Known Subclasses

Element, Fragment, Text

Instance Attribute Summary

Attributes included from Events

#listeners

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Events

#add_listener, #delegate, #off, #old_trigger, #on, #on!, #remove_listeners, #trigger

Constructor Details

#initialize(node = nil) ⇒ NODE

Initializes the node with the given native node

Parameters:

  • node (Native) (defaults to: nil)

    The native node



27
28
29
30
31
32
# File 'opal/fron/dom/node.rb', line 27

def initialize(node = nil)
  raise 'A node must be provided!' unless node
  raise 'Not a HTML Node given!' unless `#{node} instanceof Node`
  @el = node
  `#{@el}._instance = #{self}`
end

Class Method Details

.from_node(node) ⇒ DOM::NODE

Returns the ruby instance associated to the node, or creates a new instance if there is none.

Parameters:

  • node (Native)

    The node

Returns:



17
18
19
20
21
22
# File 'opal/fron/dom/node.rb', line 17

def self.from_node(node)
  return nil unless node
  instance = `#{node}._instance || Opal.nil`
  return instance if instance && instance.is_a?(DOM::NODE)
  new node
end

.get_element(obj) ⇒ Native

Gets the native node from the given object

Parameters:

Returns:

  • (Native)

    The native node



169
170
171
172
173
174
175
176
177
# File 'opal/fron/dom/node.rb', line 169

def self.get_element(obj)
  if `#{obj} instanceof Node`
    obj
  elsif obj.is_a?(NODE)
    obj.instance_variable_get('@el')
  else
    nil
  end
end

Instance Method Details

#<<(other) ⇒ Object

Inserts other node into self

Parameters:



97
98
99
# File 'opal/fron/dom/node.rb', line 97

def <<(other)
  `#{@el}.appendChild(#{NODE.get_element other})`
end

#<=>(other) ⇒ Boolean

Compars self position with other node

Parameters:

Returns:

  • (Boolean)

    The compareable with the other node



150
151
152
153
154
# File 'opal/fron/dom/node.rb', line 150

def <=>(other)
  return 0 if other == self
  raise 'Nodes not Comparable!' if other.parent != parent
  other.index <=> index
end

#==(other) ⇒ Boolean

Compars self with other node

Parameters:

Returns:

  • (Boolean)

    True if the same false if not



141
142
143
# File 'opal/fron/dom/node.rb', line 141

def ==(other)
  `#{NODE.get_element(other)} === #{@el}`
end

#>>(other) ⇒ Object

Inserts self into other node

Parameters:



104
105
106
# File 'opal/fron/dom/node.rb', line 104

def >>(other)
  `#{NODE.get_element other}.appendChild(#{@el})`
end

#childrenDOM::NodeList

Returns the child nodes as a NodeList

Returns:



77
78
79
# File 'opal/fron/dom/node.rb', line 77

def children
  NodeList.new `Array.prototype.slice.call(#{@el}.childNodes)`
end

#dupDOM::NODE

Clones the node without child nodes

Returns:



37
38
39
# File 'opal/fron/dom/node.rb', line 37

def dup
  self.class.new `#{@el}.cloneNode()`
end

#dup!DOM::NODE

Clones the node with child nodes

Returns:



44
45
46
# File 'opal/fron/dom/node.rb', line 44

def dup!
  self.class.new `#{@el}.cloneNode(true)`
end

#emptyObject

Removes all the child nodes



63
64
65
# File 'opal/fron/dom/node.rb', line 63

def empty
  children.each(&:remove!)
end

#empty?Boolean

Returns if the node is empty or not

Returns:

  • (Boolean)

    True if empty false if not



70
71
72
# File 'opal/fron/dom/node.rb', line 70

def empty?
  `#{@el}.childNodes.length === 0`
end

#indexInteger

Returns the index of the node

Returns:

  • (Integer)

    Zero based index



159
160
161
162
# File 'opal/fron/dom/node.rb', line 159

def index
  return nil unless parent
  NodeList.new(`Array.prototype.slice.call(#{@el}.parentNode.children)`).index self
end

#insert_before(what, where) ⇒ Object

Inserts the given node before the other given node

Parameters:



112
113
114
115
# File 'opal/fron/dom/node.rb', line 112

def insert_before(what, where)
  return what >> self unless where # Fix for firefox...
  `#{@el}.insertBefore(#{NODE.get_element what},#{NODE.get_element where})`
end

#normalize!Object

Normalizes the node removing extra text nodes



132
133
134
# File 'opal/fron/dom/node.rb', line 132

def normalize!
  `#{@el}.normalize()`
end

#parentDOM::NODE

Returns the parent element

Returns:



58
59
60
# File 'opal/fron/dom/node.rb', line 58

def parent
  DOM::Element.from_node `#{@el}.parentElement || Opal.nil`
end

#parent_nodeDOM::NODE

Returns the parent node

Returns:



51
52
53
# File 'opal/fron/dom/node.rb', line 51

def parent_node
  DOM::Element.from_node `#{@el}.parentNode || Opal.nil`
end

#remove(el) ⇒ Object

Removes the given elment from the node

Parameters:



84
85
86
# File 'opal/fron/dom/node.rb', line 84

def remove(el)
  `#{@el}.removeChild(#{NODE.get_element el})`
end

#remove!Object

Removes self from parent node



89
90
91
92
# File 'opal/fron/dom/node.rb', line 89

def remove!
  return unless parent
  parent.remove self
end

#textString

Returns the text content of the node

Returns:

  • (String)

    The text content



120
121
122
# File 'opal/fron/dom/node.rb', line 120

def text
  `#{@el}.textContent`
end

#text=(text) ⇒ Object

Sets the text content of the node with the given value

Parameters:

  • text (String)

    The new value



127
128
129
# File 'opal/fron/dom/node.rb', line 127

def text=(text)
  `#{@el}.textContent = #{text}`
end