Completed
Push — master ( 175dcd...faed65 )
by Yoh
01:23
created

Document.ready()   A

Complexity

Conditions 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 3

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
module Hyalite::DOM
2
  class Document
3
    include Native
4
    include Node
5
6
    def create_element(tag)
7
      Element.new `self.native.createElement(tag)`
8
    end
9
10
    def create_text(text)
11
      Text.new `self.native.createTextNode(text)`
12
    end
13
14
    def body
15
      Body.new `self.native.body`
16
    end
17
18
    def ready(&block)
19
      `self.native.addEventListener('DOMContentLoaded', block)`
20
    end
21
22
    def self.singleton
23
      @singleton ||= self.new(`window.document`)
24
    end
25
26
    def document?
27
      true
28
    end
29
30
    def [](q)
31
      elements = []
32
      %x(
33
        var nodeList = self.native.querySelectorAll(#{q});
34
        for (var i = 0; i < nodeList.length; i++) {
35
          elements.$push(nodeList.item(i));
36
        }
37
      )
38
      elements.map!{|el| Element.new(el) }
39
    end
40
  end
41
end
42