Class: DOM::NODE

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

Overview

Node

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

#addListener, #delegate, #off, #on, #on!, #removeListeners, #trigger

Constructor Details

#initialize(node = nil) ⇒ NODE

Initializes the node with the given native node

Parameters:

  • node (Native) (defaults to: nil)

    The native node


24
25
26
27
28
29
# File 'opal/fron/dom/node.rb', line 24

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

Class Method Details

.fromNode(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:


15
16
17
18
19
# File 'opal/fron/dom/node.rb', line 15

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

.getElement(obj) ⇒ Native

Gets the native node from the given object

Parameters:

Returns:

  • (Native)

    The native node


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

def self.getElement(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:


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

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

#<=>(other) ⇒ Boolean

Compars self position with other node

Parameters:

Returns:

  • (Boolean)

    The compareable with the other node


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

def <=>(other)
  return 0 if other == self
  fail '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


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

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

#>>(other) ⇒ Object

Inserts self into other node

Parameters:


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

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

#childrenDOM::NodeList

Returns the child nodes as a NodeList

Returns:


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

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

#dupDOM::NODE

Clones the node without child nodes

Returns:


34
35
36
# File 'opal/fron/dom/node.rb', line 34

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

#dup!DOM::NODE

Clones the node with child nodes

Returns:


41
42
43
# File 'opal/fron/dom/node.rb', line 41

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

#emptyObject

Removes all the child nodes


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

def empty
  children.each { |node| node.remove! }
end

#empty?Boolean

Returns if the node is empty or not

Returns:

  • (Boolean)

    True if empty false if not


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

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

#indexInteger

Returns the index of the node

Returns:

  • (Integer)

    Zero based index


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

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

#insertBefore(what, where) ⇒ Object

Inserts the given node before the other given node

Parameters:


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

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

#normalize!Object

Normalizes the node removing extra text nodes


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

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

#parentDOM::NODE

Returns the parent element

Returns:


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

def parent
  el = `#{@el}.parentElement || false`
  el ? DOM::Element.fromNode(el) : nil
end

#parentNodeDOM::NODE

Returns the parent node

Returns:


48
49
50
51
# File 'opal/fron/dom/node.rb', line 48

def parentNode
  el = `#{@el}.parentNode || false`
  el ? DOM::NODE.fromNode(el) : nil
end

#remove(el) ⇒ Object

Removes the given elment from the node

Parameters:


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

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

#remove!Object

Removes self from parent node


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

def remove!
  return unless parent
  parent.remove self
end

#textString

Returns the text content of the node

Returns:

  • (String)

    The text content


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

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


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

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