Class: Browser::DOM::Document

Inherits:
Element show all
Includes:
DocumentOrShadowRoot
Defined in:
opal/browser/effects.rb,
opal/browser/location.rb,
opal/browser/dom/document.rb

Constant Summary

Constants inherited from Element

Element::Img

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ENTITY_NODE, Node::ENTITY_REFERENCE_NOCE, Node::NOTATION_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary collapse

Attributes included from DocumentOrShadowRoot

#style_sheets

Attributes inherited from Element

#attribute_nodes, #attributes, #class_name, #class_names, #editable, #height, #id, #inner_html, #offset, #outer_html, #position, #scroll, #size, #style!, #width

Attributes inherited from Node

#child, #children, #element_children, #first_element_child, #last_element_child, #name, #namespace, #next, #next_element, #node_type, #outer_html, #parent, #previous, #previous_element, #value

Instance Method Summary collapse

Methods inherited from Element

#/, #=~, #[]=, #add_class, #animate, #animation_queue, #at_css, #at_xpath, #blur, #click, create, #css, #data, def_selector, #edit, #editable?, #fade_in, #fade_out, #fade_toggle, #focus, #focused?, #hide, #inner_dom, #inner_dom=, native_is?, native_matches?, new, #remove_attribute, #remove_class, #search, selector, #shadow, #shadow?, #show, #slide_down, #slide_toggle, #slide_up, #style, subclasses, tag_name, #toggle, #toggle_class, #visible?, #xpath

Methods included from Event::Target

#off, #on, #on!, #one, #trigger, #trigger!

Methods inherited from Node

#<<, #==, #>>, #add_child, #add_next_sibling, #add_previous_sibling, #ancestors, #append_to, #attached?, #blank?, #cdata?, #clear, #comment?, #content, #content=, #custom?, #document?, #elem?, #fragment?, #initialize, #initialize_copy, new, #parse, #path, #prepend_to, #remove, #remove_child, #replace, #text?, #traverse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Constructor Details

This class inherits a constructor from Browser::DOM::Node

Instance Attribute Details

#active_elementElement (readonly)



8
9
10
# File 'opal/browser/effects.rb', line 8

def active_element
  DOM(`#@native.activeElement`)
end

#bodyElement? (readonly)



27
28
29
30
31
# File 'opal/browser/dom/document.rb', line 27

def body
  DOM(`#@native.body`)
rescue ArgumentError
  raise '$document.body is not defined; try to wrap your code in $document.ready{}'
end

#headElement? (readonly)



116
117
118
# File 'opal/browser/dom/document.rb', line 116

def head
  DOM(`#@native.getElementsByTagName("head")[0]`)
end

#hidden?Boolean (readonly)



190
191
192
# File 'opal/browser/dom/document.rb', line 190

def hidden?
  `#@native.hidden`
end

#locationLocation (readonly)



88
89
90
# File 'opal/browser/location.rb', line 88

def location
  Location.new(`#@native.location`) if `#@native.location`
end

#referrerString



164
165
166
# File 'opal/browser/dom/document.rb', line 164

def referrer
  `#@native.referrer`
end

#rootElement?



170
171
172
# File 'opal/browser/dom/document.rb', line 170

def root
  DOM(`#@native.documentElement`)
end

#titleString



180
181
182
# File 'opal/browser/dom/document.rb', line 180

def title
  `#@native.title`
end

#visibilityString (readonly)



196
197
198
# File 'opal/browser/dom/document.rb', line 196

def visibility
  `#@native.visibilityState`
end

#windowWindow (readonly)

Returns the window for the document.

Raises:

  • (NotImplementedError)


211
212
213
# File 'opal/browser/dom/document.rb', line 211

def window
  Window.new(`#@native.defaultView`)
end

Instance Method Details

#[](what) ⇒ Element? Also known as: at

Get the first element matching the given ID, CSS selector or XPath.



11
12
13
14
15
16
17
18
19
20
21
# File 'opal/browser/dom/document.rb', line 11

def [](what)
  %x{
    var result = #@native.getElementById(what);

    if (result) {
      return #{DOM(`result`)};
    }
  }

  css(what).first || xpath(what).first
end

#create_comment(content) ⇒ Comment

Create a new comment node for the document.



106
107
108
# File 'opal/browser/dom/document.rb', line 106

def create_comment(content)
  DOM(`#@native.createComment(#{content})`)
end

#create_document_fragmentDocumentFragment

Create a new document fragment.



88
89
90
# File 'opal/browser/dom/document.rb', line 88

def create_document_fragment
  DOM(`#@native.createDocumentFragment()`)
end

#create_element(name, builder = nil, **options, &block) ⇒ Element

Create a new element for the document.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'opal/browser/dom/document.rb', line 44

def create_element(name, builder=nil, **options, &block)
  opts = {}

  if options[:is] ||= (options.dig(:attrs, :is))
    opts[:is] = options[:is]
  end

  if ns = options[:namespace]
    elem = `#@native.createElementNS(#{ns}, #{name}, #{opts.to_n})`
  else
    elem = `#@native.createElement(name, #{opts.to_n})`
  end

  if options[:classes]
    `#{elem}.className = #{Array(options[:classes]).join(" ")}`
  end

  if options[:id]
    `#{elem}.id = #{options[:id]}`
  end

  if options[:attrs]
    options[:attrs].each do |k,v|
      next unless v
      `#{elem}.setAttribute(#{k}, #{v})`
    end
  end

  dom = DOM(elem)

  if block_given?
    dom.inner_dom(builder, &block)
  end

  if builder
    builder << dom
  end

  dom
end

#create_text(content) ⇒ Text

Create a new text node for the document.



97
98
99
# File 'opal/browser/dom/document.rb', line 97

def create_text(content)
  DOM(`#@native.createTextNode(#{content})`)
end

#documentObject



110
111
112
# File 'opal/browser/dom/document.rb', line 110

def document
  self
end

#inspectObject



120
121
122
# File 'opal/browser/dom/document.rb', line 120

def inspect
  "#<DOM::Document>"
end

#ready(&block) ⇒ Object

Wait for the document to be ready and call the block.

Raises:

  • (NotImplementedError)


152
153
154
155
156
157
158
159
160
161
162
# File 'opal/browser/dom/document.rb', line 152

def ready(&block)
  raise ArgumentError, 'no block given' unless block

  return block.call if ready?

  on 'dom:load' do |e|
    e.off

    block.call
  end
end

#ready?Boolean

Check if the document is ready.



158
159
160
# File 'opal/browser/dom/document.rb', line 158

def ready?
  `#@native.readyState === "complete" || #@native.readyState === "interactive"`
end