forked from druids/werkzeug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDom.coffee
235 lines (196 loc) · 5.34 KB
/
Dom.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
goog.provide 'wzk.dom.Dom'
goog.require 'goog.dom.DomHelper'
goog.require 'goog.style'
class wzk.dom.Dom extends goog.dom.DomHelper
###*
@constructor
@extends {goog.dom.DomHelper}
@param {Document=} doc
###
constructor: (doc) ->
super(doc)
###*
Creates new Element according to a given tag.
@param {string} tag
@param {Object|string=} attrs If a second argument is an object, then a map of name-value pairs for attributes.
If a string, then this is the className of the new element.
@param {(Node|string|Array.<Node>)=} parentOrTxt If a third argument is a Node,
then new element will be append as a child.
If is a string, then this is the text content of the new element.
If is an array it's items will be appended as children into the new created element.
@return {Element}
###
el: (tag, attrs = null, parentOrTxt = null) ->
el = @createDom(tag, attrs)
if goog.isString(parentOrTxt)
@setTextContent(el, parentOrTxt)
else if @isNode(parentOrTxt)
parent = (`/** @type {Node} */`) parentOrTxt
parent.appendChild(el)
else if goog.isArray(parentOrTxt)
for child in parentOrTxt
el.appendChild(child)
el
###*
Returns true if a given element is Node, otherwise return false
@param {*} el
@return {boolean}
###
isNode: (el) ->
el? and el.nodeType?
###*
A shortcut for {@link goog.style.setElementShown}
@param {Element} el
###
show: (el) ->
goog.style.setElementShown el, true
###*
A shortcut for {@link goog.style.setElementShown}
@param {Element} el
###
hide: (el) ->
goog.style.setElementShown el, false
###*
Inserts a child as first element of a parent
@param {Element} parent
@param {Element} child
###
prependChild: (parent, child) ->
@insertChildAt parent, child, 0
###*
Returns index of given element in document according to selector, returns -1 if selector does not match tested element.
@param {Element} testedEl
@param {string} selector
@return {number}
###
getIndexOf: (testedEl, selector) ->
for el, i in @all selector
if el is testedEl
return i
-1
###*
Calls a given callback with a given element, forces to return an Element instance or null.
@protected
@param {function(Element)} clb
@param {Element} el
@return {Element}
###
elementOrNull: (clb, el) ->
wanted = clb el if el?
wanted = null if wanted is undefined
wanted
###*
Returns first sibling if exists
@param {Element} el
@return {Element}
###
getFirstSibling: (el) ->
first = @elementOrNull @getFirstElementChild, @getParentElement(el)
if first is el then null else first
###*
Returns last sibling if exists
@param {Element} el
@return {Element}
###
getLastSibling: (el) ->
last = @elementOrNull @getLastElementChild, @getParentElement(el)
if last is el then null else last
###*
@param {Element} el
@return {boolean}
###
hasChildren: (el) ->
el.children? and el.children.length > 0
###*
Alias for document.querySelector
@param {string} selector
@param {?Element|Document|null|undefined=} el
@return {Element|null}
###
one: (selector, el = null) ->
unless el?
el = @getDocument()
el.querySelector selector
###*
Alias for document.querySelectorAll
@param {string} selector
@param {?Element|Document|null|undefined=} el
@return {NodeList}
###
all: (selector, el = null) ->
unless el?
el = @getDocument()
el.querySelectorAll selector
###*
Alias for goog.dom.DomHelper.getElementByClass
@param {string} cls
@param {?Element|Document|null|undefined=} el
@return {Element|null}
###
cls: (cls, el = null) ->
@getElementByClass cls, el
###*
Alias for goog.dom.DomHelper.getElementsByClass
@param {string} cls
@param {?Element|Document|null|undefined=} el
@return { {length: number} }
###
clss: (cls, el = null) ->
@getElementsByClass cls, el
###*
@param {Element} select
@param {*} val
@param {function(Element):boolean} clbk
###
iterOverOptions: (select, val, clbk) ->
val = String val
for opt in @all 'option', select
if opt.value is val
if clbk opt
break
###*
Selects given value in a HTMLSelectElement
@param {Element} select
@param {*} val
###
select: (select, val) ->
@iterOverOptions select, val, (opt) ->
opt.selected = true
###*
Unselects given value in a HTMLSelectElement
@param {Element} select
@param {*} val
###
unselect: (select, val) ->
@iterOverOptions select, val, (opt) ->
opt.selected = false
###*
@param {Element} el
@param {string} elementName
@return {Node}
###
lastChildOfType: (el, elementName) ->
elements = @all(elementName, el)
elements.item(elements.length - 1)
###*
@param {NodeList} list
@return {Array}
###
nodeList2Array: (list) ->
(el for el in list)
###*
Clears dom element
Use instead of element.innerHTML = ''
@param {Element} el
###
clearElement: (el) ->
unless @isIE()
el.innerHTML = ''
else
while el.hasChildNodes()
el.removeChild el.lastChild
###*
@return {boolean} true if user agent is Internet Explorer
###
isIE: ->
return goog.userAgent.IE