1 |
efrain |
1 |
YUI.add('node-base', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* @module node
|
|
|
5 |
* @submodule node-base
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
var methods = [
|
|
|
9 |
/**
|
|
|
10 |
* Determines whether the node has the given className.
|
|
|
11 |
* @method hasClass
|
|
|
12 |
* @for Node
|
|
|
13 |
* @param {String} className the class name to search for
|
|
|
14 |
* @return {Boolean} Whether or not the node has the specified class
|
|
|
15 |
*/
|
|
|
16 |
'hasClass',
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Adds a class name to the node.
|
|
|
20 |
* @method addClass
|
|
|
21 |
* @param {String} className the class name to add to the node's class attribute
|
|
|
22 |
* @chainable
|
|
|
23 |
*/
|
|
|
24 |
'addClass',
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Removes a class name from the node.
|
|
|
28 |
* @method removeClass
|
|
|
29 |
* @param {String} className the class name to remove from the node's class attribute
|
|
|
30 |
* @chainable
|
|
|
31 |
*/
|
|
|
32 |
'removeClass',
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Replace a class with another class on the node.
|
|
|
36 |
* If no oldClassName is present, the newClassName is simply added.
|
|
|
37 |
* @method replaceClass
|
|
|
38 |
* @param {String} oldClassName the class name to be replaced
|
|
|
39 |
* @param {String} newClassName the class name that will be replacing the old class name
|
|
|
40 |
* @chainable
|
|
|
41 |
*/
|
|
|
42 |
'replaceClass',
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* If the className exists on the node it is removed, if it doesn't exist it is added.
|
|
|
46 |
* @method toggleClass
|
|
|
47 |
* @param {String} className the class name to be toggled
|
|
|
48 |
* @param {Boolean} force Option to force adding or removing the class.
|
|
|
49 |
* @chainable
|
|
|
50 |
*/
|
|
|
51 |
'toggleClass'
|
|
|
52 |
];
|
|
|
53 |
|
|
|
54 |
Y.Node.importMethod(Y.DOM, methods);
|
|
|
55 |
/**
|
|
|
56 |
* Determines whether each node has the given className.
|
|
|
57 |
* @method hasClass
|
|
|
58 |
* @see Node.hasClass
|
|
|
59 |
* @for NodeList
|
|
|
60 |
* @param {String} className the class name to search for
|
|
|
61 |
* @return {Array} An array of booleans for each node bound to the NodeList.
|
|
|
62 |
*/
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Adds a class name to each node.
|
|
|
66 |
* @method addClass
|
|
|
67 |
* @see Node.addClass
|
|
|
68 |
* @param {String} className the class name to add to each node's class attribute
|
|
|
69 |
* @chainable
|
|
|
70 |
*/
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Removes a class name from each node.
|
|
|
74 |
* @method removeClass
|
|
|
75 |
* @see Node.removeClass
|
|
|
76 |
* @param {String} className the class name to remove from each node's class attribute
|
|
|
77 |
* @chainable
|
|
|
78 |
*/
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Replace a class with another class for each node.
|
|
|
82 |
* If no oldClassName is present, the newClassName is simply added.
|
|
|
83 |
* @method replaceClass
|
|
|
84 |
* @see Node.replaceClass
|
|
|
85 |
* @param {String} oldClassName the class name to be replaced
|
|
|
86 |
* @param {String} newClassName the class name that will be replacing the old class name
|
|
|
87 |
* @chainable
|
|
|
88 |
*/
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* For each node, if the className exists on the node it is removed, if it doesn't exist it is added.
|
|
|
92 |
* @method toggleClass
|
|
|
93 |
* @see Node.toggleClass
|
|
|
94 |
* @param {String} className the class name to be toggled
|
|
|
95 |
* @chainable
|
|
|
96 |
*/
|
|
|
97 |
Y.NodeList.importMethod(Y.Node.prototype, methods);
|
|
|
98 |
/**
|
|
|
99 |
* @module node
|
|
|
100 |
* @submodule node-base
|
|
|
101 |
*/
|
|
|
102 |
|
|
|
103 |
var Y_Node = Y.Node,
|
|
|
104 |
Y_DOM = Y.DOM;
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Returns a new dom node using the provided markup string.
|
|
|
108 |
* @method create
|
|
|
109 |
* @static
|
|
|
110 |
* @param {String} html The markup used to create the element.
|
|
|
111 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
112 |
* to escape html content.
|
|
|
113 |
* @param {HTMLDocument} doc An optional document context
|
|
|
114 |
* @return {Node} A Node instance bound to a DOM node or fragment
|
|
|
115 |
* @for Node
|
|
|
116 |
*/
|
|
|
117 |
Y_Node.create = function(html, doc) {
|
|
|
118 |
if (doc && doc._node) {
|
|
|
119 |
doc = doc._node;
|
|
|
120 |
}
|
|
|
121 |
return Y.one(Y_DOM.create(html, doc));
|
|
|
122 |
};
|
|
|
123 |
|
|
|
124 |
Y.mix(Y_Node.prototype, {
|
|
|
125 |
/**
|
|
|
126 |
* Creates a new Node using the provided markup string.
|
|
|
127 |
* @method create
|
|
|
128 |
* @param {String} html The markup used to create the element.
|
|
|
129 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
130 |
* to escape html content.
|
|
|
131 |
* @param {HTMLDocument} doc An optional document context
|
|
|
132 |
* @return {Node} A Node instance bound to a DOM node or fragment
|
|
|
133 |
*/
|
|
|
134 |
create: Y_Node.create,
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Inserts the content before the reference node.
|
|
|
138 |
* @method insert
|
|
|
139 |
* @param {String | Node | HTMLElement | NodeList | HTMLCollection} content The content to insert.
|
|
|
140 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
141 |
* to escape html content.
|
|
|
142 |
* @param {Int | Node | HTMLElement | String} where The position to insert at.
|
|
|
143 |
* Possible "where" arguments
|
|
|
144 |
* <dl>
|
|
|
145 |
* <dt>Y.Node</dt>
|
|
|
146 |
* <dd>The Node to insert before</dd>
|
|
|
147 |
* <dt>HTMLElement</dt>
|
|
|
148 |
* <dd>The element to insert before</dd>
|
|
|
149 |
* <dt>Int</dt>
|
|
|
150 |
* <dd>The index of the child element to insert before</dd>
|
|
|
151 |
* <dt>"replace"</dt>
|
|
|
152 |
* <dd>Replaces the existing HTML</dd>
|
|
|
153 |
* <dt>"before"</dt>
|
|
|
154 |
* <dd>Inserts before the existing HTML</dd>
|
|
|
155 |
* <dt>"before"</dt>
|
|
|
156 |
* <dd>Inserts content before the node</dd>
|
|
|
157 |
* <dt>"after"</dt>
|
|
|
158 |
* <dd>Inserts content after the node</dd>
|
|
|
159 |
* </dl>
|
|
|
160 |
* @chainable
|
|
|
161 |
*/
|
|
|
162 |
insert: function(content, where) {
|
|
|
163 |
this._insert(content, where);
|
|
|
164 |
return this;
|
|
|
165 |
},
|
|
|
166 |
|
|
|
167 |
_insert: function(content, where) {
|
|
|
168 |
var node = this._node,
|
|
|
169 |
ret = null;
|
|
|
170 |
|
|
|
171 |
if (typeof where == 'number') { // allow index
|
|
|
172 |
where = this._node.childNodes[where];
|
|
|
173 |
} else if (where && where._node) { // Node
|
|
|
174 |
where = where._node;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
if (content && typeof content != 'string') { // allow Node or NodeList/Array instances
|
|
|
178 |
content = content._node || content._nodes || content;
|
|
|
179 |
}
|
|
|
180 |
ret = Y_DOM.addHTML(node, content, where);
|
|
|
181 |
|
|
|
182 |
return ret;
|
|
|
183 |
},
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Inserts the content as the firstChild of the node.
|
|
|
187 |
* @method prepend
|
|
|
188 |
* @param {String | Node | HTMLElement} content The content to insert.
|
|
|
189 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
190 |
* to escape html content.
|
|
|
191 |
* @chainable
|
|
|
192 |
*/
|
|
|
193 |
prepend: function(content) {
|
|
|
194 |
return this.insert(content, 0);
|
|
|
195 |
},
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Inserts the content as the lastChild of the node.
|
|
|
199 |
* @method append
|
|
|
200 |
* @param {String | Node | HTMLElement} content The content to insert.
|
|
|
201 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
202 |
* to escape html content.
|
|
|
203 |
* @chainable
|
|
|
204 |
*/
|
|
|
205 |
append: function(content) {
|
|
|
206 |
return this.insert(content, null);
|
|
|
207 |
},
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* @method appendChild
|
|
|
211 |
* @param {String | HTMLElement | Node} node Node to be appended.
|
|
|
212 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
213 |
* to escape html content.
|
|
|
214 |
* @return {Node} The appended node
|
|
|
215 |
*/
|
|
|
216 |
appendChild: function(node) {
|
|
|
217 |
return Y_Node.scrubVal(this._insert(node));
|
|
|
218 |
},
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* @method insertBefore
|
|
|
222 |
* @param {String | HTMLElement | Node} newNode Node to be appended
|
|
|
223 |
* @param {HTMLElement | Node} refNode Node to be inserted before.
|
|
|
224 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
225 |
* to escape html content.
|
|
|
226 |
* @return {Node} The inserted node
|
|
|
227 |
*/
|
|
|
228 |
insertBefore: function(newNode, refNode) {
|
|
|
229 |
return Y.Node.scrubVal(this._insert(newNode, refNode));
|
|
|
230 |
},
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Appends the node to the given node.
|
|
|
234 |
* @example
|
|
|
235 |
* // appendTo returns the node that has been created beforehand
|
|
|
236 |
* Y.Node.create('<p></p>').appendTo('body').set('text', 'hello world!');
|
|
|
237 |
* @method appendTo
|
|
|
238 |
* @param {Node | HTMLElement | String} node The node to append to.
|
|
|
239 |
* If `node` is a string it will be considered as a css selector and only the first matching node will be used.
|
|
|
240 |
* @chainable
|
|
|
241 |
*/
|
|
|
242 |
appendTo: function(node) {
|
|
|
243 |
Y.one(node).append(this);
|
|
|
244 |
return this;
|
|
|
245 |
},
|
|
|
246 |
|
|
|
247 |
// This method is deprecated, and is intentionally left undocumented.
|
|
|
248 |
// Use `setHTML` instead.
|
|
|
249 |
setContent: function(content) {
|
|
|
250 |
this._insert(content, 'replace');
|
|
|
251 |
return this;
|
|
|
252 |
},
|
|
|
253 |
|
|
|
254 |
// This method is deprecated, and is intentionally left undocumented.
|
|
|
255 |
// Use `getHTML` instead.
|
|
|
256 |
getContent: function() {
|
|
|
257 |
var node = this;
|
|
|
258 |
|
|
|
259 |
if (node._node.nodeType === 11) { // 11 === Node.DOCUMENT_FRAGMENT_NODE
|
|
|
260 |
// "this", when it is a document fragment, must be cloned because
|
|
|
261 |
// the nodes contained in the fragment actually disappear once
|
|
|
262 |
// the fragment is appended anywhere
|
|
|
263 |
node = node.create("<div/>").append(node.cloneNode(true));
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
return node.get("innerHTML");
|
|
|
267 |
}
|
|
|
268 |
});
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Replaces the node's current html content with the content provided.
|
|
|
272 |
* Note that this passes to innerHTML and is not escaped.
|
|
|
273 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
274 |
* to escape html content or `set('text')` to add as text.
|
|
|
275 |
* @method setHTML
|
|
|
276 |
* @param {String | Node | HTMLElement | NodeList | HTMLCollection} content The content to insert
|
|
|
277 |
* @chainable
|
|
|
278 |
*/
|
|
|
279 |
Y.Node.prototype.setHTML = Y.Node.prototype.setContent;
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Returns the node's current html content (e.g. innerHTML)
|
|
|
283 |
* @method getHTML
|
|
|
284 |
* @return {String} The html content
|
|
|
285 |
*/
|
|
|
286 |
Y.Node.prototype.getHTML = Y.Node.prototype.getContent;
|
|
|
287 |
|
|
|
288 |
Y.NodeList.importMethod(Y.Node.prototype, [
|
|
|
289 |
/**
|
|
|
290 |
* Called on each Node instance
|
|
|
291 |
* @for NodeList
|
|
|
292 |
* @method append
|
|
|
293 |
* @see Node.append
|
|
|
294 |
*/
|
|
|
295 |
'append',
|
|
|
296 |
|
|
|
297 |
/**
|
|
|
298 |
* Called on each Node instance
|
|
|
299 |
* @for NodeList
|
|
|
300 |
* @method insert
|
|
|
301 |
* @see Node.insert
|
|
|
302 |
*/
|
|
|
303 |
'insert',
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Called on each Node instance
|
|
|
307 |
* @for NodeList
|
|
|
308 |
* @method appendChild
|
|
|
309 |
* @see Node.appendChild
|
|
|
310 |
*/
|
|
|
311 |
'appendChild',
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Called on each Node instance
|
|
|
315 |
* @for NodeList
|
|
|
316 |
* @method insertBefore
|
|
|
317 |
* @see Node.insertBefore
|
|
|
318 |
*/
|
|
|
319 |
'insertBefore',
|
|
|
320 |
|
|
|
321 |
/**
|
|
|
322 |
* Called on each Node instance
|
|
|
323 |
* @for NodeList
|
|
|
324 |
* @method prepend
|
|
|
325 |
* @see Node.prepend
|
|
|
326 |
*/
|
|
|
327 |
'prepend',
|
|
|
328 |
|
|
|
329 |
'setContent',
|
|
|
330 |
|
|
|
331 |
'getContent',
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Called on each Node instance
|
|
|
335 |
* Note that this passes to innerHTML and is not escaped.
|
|
|
336 |
* Use <a href="../classes/Escape.html#method_html">`Y.Escape.html()`</a>
|
|
|
337 |
* to escape html content or `set('text')` to add as text.
|
|
|
338 |
* @for NodeList
|
|
|
339 |
* @method setHTML
|
|
|
340 |
* @see Node.setHTML
|
|
|
341 |
*/
|
|
|
342 |
'setHTML',
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Called on each Node instance
|
|
|
346 |
* @for NodeList
|
|
|
347 |
* @method getHTML
|
|
|
348 |
* @see Node.getHTML
|
|
|
349 |
*/
|
|
|
350 |
'getHTML'
|
|
|
351 |
]);
|
|
|
352 |
/**
|
|
|
353 |
* @module node
|
|
|
354 |
* @submodule node-base
|
|
|
355 |
*/
|
|
|
356 |
|
|
|
357 |
var Y_Node = Y.Node,
|
|
|
358 |
Y_DOM = Y.DOM;
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Static collection of configuration attributes for special handling
|
|
|
362 |
* @property ATTRS
|
|
|
363 |
* @static
|
|
|
364 |
* @type object
|
|
|
365 |
*/
|
|
|
366 |
Y_Node.ATTRS = {
|
|
|
367 |
/**
|
|
|
368 |
* Allows for getting and setting the text of an element.
|
|
|
369 |
* Formatting is preserved and special characters are treated literally.
|
|
|
370 |
* @config text
|
|
|
371 |
* @type String
|
|
|
372 |
*/
|
|
|
373 |
text: {
|
|
|
374 |
getter: function() {
|
|
|
375 |
return Y_DOM.getText(this._node);
|
|
|
376 |
},
|
|
|
377 |
|
|
|
378 |
setter: function(content) {
|
|
|
379 |
Y_DOM.setText(this._node, content);
|
|
|
380 |
return content;
|
|
|
381 |
}
|
|
|
382 |
},
|
|
|
383 |
|
|
|
384 |
/**
|
|
|
385 |
* Allows for getting and setting the text of an element.
|
|
|
386 |
* Formatting is preserved and special characters are treated literally.
|
|
|
387 |
* @config for
|
|
|
388 |
* @type String
|
|
|
389 |
*/
|
|
|
390 |
'for': {
|
|
|
391 |
getter: function() {
|
|
|
392 |
return Y_DOM.getAttribute(this._node, 'for');
|
|
|
393 |
},
|
|
|
394 |
|
|
|
395 |
setter: function(val) {
|
|
|
396 |
Y_DOM.setAttribute(this._node, 'for', val);
|
|
|
397 |
return val;
|
|
|
398 |
}
|
|
|
399 |
},
|
|
|
400 |
|
|
|
401 |
'options': {
|
|
|
402 |
getter: function() {
|
|
|
403 |
return this._node.getElementsByTagName('option');
|
|
|
404 |
}
|
|
|
405 |
},
|
|
|
406 |
|
|
|
407 |
/**
|
|
|
408 |
* Returns a NodeList instance of all HTMLElement children.
|
|
|
409 |
* @readOnly
|
|
|
410 |
* @config children
|
|
|
411 |
* @type NodeList
|
|
|
412 |
*/
|
|
|
413 |
'children': {
|
|
|
414 |
getter: function() {
|
|
|
415 |
var node = this._node,
|
|
|
416 |
children = node.children,
|
|
|
417 |
childNodes, i, len;
|
|
|
418 |
|
|
|
419 |
if (!children) {
|
|
|
420 |
childNodes = node.childNodes;
|
|
|
421 |
children = [];
|
|
|
422 |
|
|
|
423 |
for (i = 0, len = childNodes.length; i < len; ++i) {
|
|
|
424 |
if (childNodes[i].tagName) {
|
|
|
425 |
children[children.length] = childNodes[i];
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
}
|
|
|
429 |
return Y.all(children);
|
|
|
430 |
}
|
|
|
431 |
},
|
|
|
432 |
|
|
|
433 |
value: {
|
|
|
434 |
getter: function() {
|
|
|
435 |
return Y_DOM.getValue(this._node);
|
|
|
436 |
},
|
|
|
437 |
|
|
|
438 |
setter: function(val) {
|
|
|
439 |
Y_DOM.setValue(this._node, val);
|
|
|
440 |
return val;
|
|
|
441 |
}
|
|
|
442 |
}
|
|
|
443 |
};
|
|
|
444 |
|
|
|
445 |
Y.Node.importMethod(Y.DOM, [
|
|
|
446 |
/**
|
|
|
447 |
* Allows setting attributes on DOM nodes, normalizing in some cases.
|
|
|
448 |
* This passes through to the DOM node, allowing for custom attributes.
|
|
|
449 |
* @method setAttribute
|
|
|
450 |
* @for Node
|
|
|
451 |
* @for NodeList
|
|
|
452 |
* @chainable
|
|
|
453 |
* @param {string} name The attribute name
|
|
|
454 |
* @param {string} value The value to set
|
|
|
455 |
*/
|
|
|
456 |
'setAttribute',
|
|
|
457 |
/**
|
|
|
458 |
* Allows getting attributes on DOM nodes, normalizing in some cases.
|
|
|
459 |
* This passes through to the DOM node, allowing for custom attributes.
|
|
|
460 |
* @method getAttribute
|
|
|
461 |
* @for Node
|
|
|
462 |
* @for NodeList
|
|
|
463 |
* @param {string} name The attribute name
|
|
|
464 |
* @return {string} The attribute value
|
|
|
465 |
*/
|
|
|
466 |
'getAttribute'
|
|
|
467 |
|
|
|
468 |
]);
|
|
|
469 |
/**
|
|
|
470 |
* @module node
|
|
|
471 |
* @submodule node-base
|
|
|
472 |
*/
|
|
|
473 |
|
|
|
474 |
var Y_Node = Y.Node;
|
|
|
475 |
var Y_NodeList = Y.NodeList;
|
|
|
476 |
/**
|
|
|
477 |
* List of events that route to DOM events
|
|
|
478 |
* @static
|
|
|
479 |
* @property DOM_EVENTS
|
|
|
480 |
* @for Node
|
|
|
481 |
*/
|
|
|
482 |
|
|
|
483 |
Y_Node.DOM_EVENTS = {
|
|
|
484 |
abort: 1,
|
|
|
485 |
beforeunload: 1,
|
|
|
486 |
blur: 1,
|
|
|
487 |
change: 1,
|
|
|
488 |
click: 1,
|
|
|
489 |
close: 1,
|
|
|
490 |
command: 1,
|
|
|
491 |
contextmenu: 1,
|
|
|
492 |
copy: 1,
|
|
|
493 |
cut: 1,
|
|
|
494 |
dblclick: 1,
|
|
|
495 |
DOMMouseScroll: 1,
|
|
|
496 |
drag: 1,
|
|
|
497 |
dragstart: 1,
|
|
|
498 |
dragenter: 1,
|
|
|
499 |
dragover: 1,
|
|
|
500 |
dragleave: 1,
|
|
|
501 |
dragend: 1,
|
|
|
502 |
drop: 1,
|
|
|
503 |
error: 1,
|
|
|
504 |
focus: 1,
|
|
|
505 |
key: 1,
|
|
|
506 |
keydown: 1,
|
|
|
507 |
keypress: 1,
|
|
|
508 |
keyup: 1,
|
|
|
509 |
load: 1,
|
|
|
510 |
message: 1,
|
|
|
511 |
mousedown: 1,
|
|
|
512 |
mouseenter: 1,
|
|
|
513 |
mouseleave: 1,
|
|
|
514 |
mousemove: 1,
|
|
|
515 |
mousemultiwheel: 1,
|
|
|
516 |
mouseout: 1,
|
|
|
517 |
mouseover: 1,
|
|
|
518 |
mouseup: 1,
|
|
|
519 |
mousewheel: 1,
|
|
|
520 |
orientationchange: 1,
|
|
|
521 |
paste: 1,
|
|
|
522 |
reset: 1,
|
|
|
523 |
resize: 1,
|
|
|
524 |
select: 1,
|
|
|
525 |
selectstart: 1,
|
|
|
526 |
submit: 1,
|
|
|
527 |
scroll: 1,
|
|
|
528 |
textInput: 1,
|
|
|
529 |
unload: 1,
|
|
|
530 |
invalid: 1
|
|
|
531 |
};
|
|
|
532 |
|
|
|
533 |
// Add custom event adaptors to this list. This will make it so
|
|
|
534 |
// that delegate, key, available, contentready, etc all will
|
|
|
535 |
// be available through Node.on
|
|
|
536 |
Y.mix(Y_Node.DOM_EVENTS, Y.Env.evt.plugins);
|
|
|
537 |
|
|
|
538 |
Y.augment(Y_Node, Y.EventTarget);
|
|
|
539 |
|
|
|
540 |
Y.mix(Y_Node.prototype, {
|
|
|
541 |
/**
|
|
|
542 |
* Removes event listeners from the node and (optionally) its subtree
|
|
|
543 |
* @method purge
|
|
|
544 |
* @param {Boolean} recurse (optional) Whether or not to remove listeners from the
|
|
|
545 |
* node's subtree
|
|
|
546 |
* @param {String} type (optional) Only remove listeners of the specified type
|
|
|
547 |
* @chainable
|
|
|
548 |
*
|
|
|
549 |
*/
|
|
|
550 |
purge: function(recurse, type) {
|
|
|
551 |
Y.Event.purgeElement(this._node, recurse, type);
|
|
|
552 |
return this;
|
|
|
553 |
}
|
|
|
554 |
|
|
|
555 |
});
|
|
|
556 |
|
|
|
557 |
Y.mix(Y.NodeList.prototype, {
|
|
|
558 |
_prepEvtArgs: function(type, fn, context) {
|
|
|
559 |
// map to Y.on/after signature (type, fn, nodes, context, arg1, arg2, etc)
|
|
|
560 |
var args = Y.Array(arguments, 0, true);
|
|
|
561 |
|
|
|
562 |
if (args.length < 2) { // type only (event hash) just add nodes
|
|
|
563 |
args[2] = this._nodes;
|
|
|
564 |
} else {
|
|
|
565 |
args.splice(2, 0, this._nodes);
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
args[3] = context || this; // default to NodeList instance as context
|
|
|
569 |
|
|
|
570 |
return args;
|
|
|
571 |
},
|
|
|
572 |
|
|
|
573 |
/**
|
|
|
574 |
Subscribe a callback function for each `Node` in the collection to execute
|
|
|
575 |
in response to a DOM event.
|
|
|
576 |
|
|
|
577 |
NOTE: Generally, the `on()` method should be avoided on `NodeLists`, in
|
|
|
578 |
favor of using event delegation from a parent Node. See the Event user
|
|
|
579 |
guide for details.
|
|
|
580 |
|
|
|
581 |
Most DOM events are associated with a preventable default behavior, such as
|
|
|
582 |
link clicks navigating to a new page. Callbacks are passed a
|
|
|
583 |
`DOMEventFacade` object as their first argument (usually called `e`) that
|
|
|
584 |
can be used to prevent this default behavior with `e.preventDefault()`. See
|
|
|
585 |
the `DOMEventFacade` API for all available properties and methods on the
|
|
|
586 |
object.
|
|
|
587 |
|
|
|
588 |
By default, the `this` object will be the `NodeList` that the subscription
|
|
|
589 |
came from, <em>not the `Node` that received the event</em>. Use
|
|
|
590 |
`e.currentTarget` to refer to the `Node`.
|
|
|
591 |
|
|
|
592 |
Returning `false` from a callback is supported as an alternative to calling
|
|
|
593 |
`e.preventDefault(); e.stopPropagation();`. However, it is recommended to
|
|
|
594 |
use the event methods.
|
|
|
595 |
|
|
|
596 |
@example
|
|
|
597 |
|
|
|
598 |
Y.all(".sku").on("keydown", function (e) {
|
|
|
599 |
if (e.keyCode === 13) {
|
|
|
600 |
e.preventDefault();
|
|
|
601 |
|
|
|
602 |
// Use e.currentTarget to refer to the individual Node
|
|
|
603 |
var item = Y.MyApp.searchInventory( e.currentTarget.get('value') );
|
|
|
604 |
// etc ...
|
|
|
605 |
}
|
|
|
606 |
});
|
|
|
607 |
|
|
|
608 |
@method on
|
|
|
609 |
@param {String} type The name of the event
|
|
|
610 |
@param {Function} fn The callback to execute in response to the event
|
|
|
611 |
@param {Object} [context] Override `this` object in callback
|
|
|
612 |
@param {Any} [arg*] 0..n additional arguments to supply to the subscriber
|
|
|
613 |
@return {EventHandle} A subscription handle capable of detaching that
|
|
|
614 |
subscription
|
|
|
615 |
@for NodeList
|
|
|
616 |
**/
|
|
|
617 |
on: function(type, fn, context) {
|
|
|
618 |
return Y.on.apply(Y, this._prepEvtArgs.apply(this, arguments));
|
|
|
619 |
},
|
|
|
620 |
|
|
|
621 |
/**
|
|
|
622 |
* Applies an one-time event listener to each Node bound to the NodeList.
|
|
|
623 |
* @method once
|
|
|
624 |
* @param {String} type The event being listened for
|
|
|
625 |
* @param {Function} fn The handler to call when the event fires
|
|
|
626 |
* @param {Object} context The context to call the handler with.
|
|
|
627 |
* Default is the NodeList instance.
|
|
|
628 |
* @return {EventHandle} A subscription handle capable of detaching that
|
|
|
629 |
* subscription
|
|
|
630 |
* @for NodeList
|
|
|
631 |
*/
|
|
|
632 |
once: function(type, fn, context) {
|
|
|
633 |
return Y.once.apply(Y, this._prepEvtArgs.apply(this, arguments));
|
|
|
634 |
},
|
|
|
635 |
|
|
|
636 |
/**
|
|
|
637 |
* Applies an event listener to each Node bound to the NodeList.
|
|
|
638 |
* The handler is called only after all on() handlers are called
|
|
|
639 |
* and the event is not prevented.
|
|
|
640 |
* @method after
|
|
|
641 |
* @param {String} type The event being listened for
|
|
|
642 |
* @param {Function} fn The handler to call when the event fires
|
|
|
643 |
* @param {Object} context The context to call the handler with.
|
|
|
644 |
* Default is the NodeList instance.
|
|
|
645 |
* @return {EventHandle} A subscription handle capable of detaching that
|
|
|
646 |
* subscription
|
|
|
647 |
* @for NodeList
|
|
|
648 |
*/
|
|
|
649 |
after: function(type, fn, context) {
|
|
|
650 |
return Y.after.apply(Y, this._prepEvtArgs.apply(this, arguments));
|
|
|
651 |
},
|
|
|
652 |
|
|
|
653 |
/**
|
|
|
654 |
* Applies an one-time event listener to each Node bound to the NodeList
|
|
|
655 |
* that will be called only after all on() handlers are called and the
|
|
|
656 |
* event is not prevented.
|
|
|
657 |
*
|
|
|
658 |
* @method onceAfter
|
|
|
659 |
* @param {String} type The event being listened for
|
|
|
660 |
* @param {Function} fn The handler to call when the event fires
|
|
|
661 |
* @param {Object} context The context to call the handler with.
|
|
|
662 |
* Default is the NodeList instance.
|
|
|
663 |
* @return {EventHandle} A subscription handle capable of detaching that
|
|
|
664 |
* subscription
|
|
|
665 |
* @for NodeList
|
|
|
666 |
*/
|
|
|
667 |
onceAfter: function(type, fn, context) {
|
|
|
668 |
return Y.onceAfter.apply(Y, this._prepEvtArgs.apply(this, arguments));
|
|
|
669 |
}
|
|
|
670 |
});
|
|
|
671 |
|
|
|
672 |
Y_NodeList.importMethod(Y.Node.prototype, [
|
|
|
673 |
/**
|
|
|
674 |
* Called on each Node instance
|
|
|
675 |
* @method detach
|
|
|
676 |
* @see Node.detach
|
|
|
677 |
* @for NodeList
|
|
|
678 |
*/
|
|
|
679 |
'detach',
|
|
|
680 |
|
|
|
681 |
/** Called on each Node instance
|
|
|
682 |
* @method detachAll
|
|
|
683 |
* @see Node.detachAll
|
|
|
684 |
* @for NodeList
|
|
|
685 |
*/
|
|
|
686 |
'detachAll'
|
|
|
687 |
]);
|
|
|
688 |
|
|
|
689 |
/**
|
|
|
690 |
Subscribe a callback function to execute in response to a DOM event or custom
|
|
|
691 |
event.
|
|
|
692 |
|
|
|
693 |
Most DOM events are associated with a preventable default behavior such as
|
|
|
694 |
link clicks navigating to a new page. Callbacks are passed a `DOMEventFacade`
|
|
|
695 |
object as their first argument (usually called `e`) that can be used to
|
|
|
696 |
prevent this default behavior with `e.preventDefault()`. See the
|
|
|
697 |
`DOMEventFacade` API for all available properties and methods on the object.
|
|
|
698 |
|
|
|
699 |
If the event name passed as the first parameter is not a whitelisted DOM event,
|
|
|
700 |
it will be treated as a custom event subscriptions, allowing
|
|
|
701 |
`node.fire('customEventName')` later in the code. Refer to the Event user guide
|
|
|
702 |
for the full DOM event whitelist.
|
|
|
703 |
|
|
|
704 |
By default, the `this` object in the callback will refer to the subscribed
|
|
|
705 |
`Node`.
|
|
|
706 |
|
|
|
707 |
Returning `false` from a callback is supported as an alternative to calling
|
|
|
708 |
`e.preventDefault(); e.stopPropagation();`. However, it is recommended to use
|
|
|
709 |
the event methods.
|
|
|
710 |
|
|
|
711 |
@example
|
|
|
712 |
|
|
|
713 |
Y.one("#my-form").on("submit", function (e) {
|
|
|
714 |
e.preventDefault();
|
|
|
715 |
|
|
|
716 |
// proceed with ajax form submission instead...
|
|
|
717 |
});
|
|
|
718 |
|
|
|
719 |
@method on
|
|
|
720 |
@param {String} type The name of the event
|
|
|
721 |
@param {Function} fn The callback to execute in response to the event
|
|
|
722 |
@param {Object} [context] Override `this` object in callback
|
|
|
723 |
@param {Any} [arg*] 0..n additional arguments to supply to the subscriber
|
|
|
724 |
@return {EventHandle} A subscription handle capable of detaching that
|
|
|
725 |
subscription
|
|
|
726 |
@for Node
|
|
|
727 |
**/
|
|
|
728 |
|
|
|
729 |
Y.mix(Y.Node.ATTRS, {
|
|
|
730 |
offsetHeight: {
|
|
|
731 |
setter: function(h) {
|
|
|
732 |
Y.DOM.setHeight(this._node, h);
|
|
|
733 |
return h;
|
|
|
734 |
},
|
|
|
735 |
|
|
|
736 |
getter: function() {
|
|
|
737 |
return this._node.offsetHeight;
|
|
|
738 |
}
|
|
|
739 |
},
|
|
|
740 |
|
|
|
741 |
offsetWidth: {
|
|
|
742 |
setter: function(w) {
|
|
|
743 |
Y.DOM.setWidth(this._node, w);
|
|
|
744 |
return w;
|
|
|
745 |
},
|
|
|
746 |
|
|
|
747 |
getter: function() {
|
|
|
748 |
return this._node.offsetWidth;
|
|
|
749 |
}
|
|
|
750 |
}
|
|
|
751 |
});
|
|
|
752 |
|
|
|
753 |
Y.mix(Y.Node.prototype, {
|
|
|
754 |
sizeTo: function(w, h) {
|
|
|
755 |
var node;
|
|
|
756 |
if (arguments.length < 2) {
|
|
|
757 |
node = Y.one(w);
|
|
|
758 |
w = node.get('offsetWidth');
|
|
|
759 |
h = node.get('offsetHeight');
|
|
|
760 |
}
|
|
|
761 |
|
|
|
762 |
this.setAttrs({
|
|
|
763 |
offsetWidth: w,
|
|
|
764 |
offsetHeight: h
|
|
|
765 |
});
|
|
|
766 |
}
|
|
|
767 |
});
|
|
|
768 |
|
|
|
769 |
if (!Y.config.doc.documentElement.hasAttribute) { // IE < 8
|
|
|
770 |
Y.Node.prototype.hasAttribute = function(attr) {
|
|
|
771 |
if (attr === 'value') {
|
|
|
772 |
if (this.get('value') !== "") { // IE < 8 fails to populate specified when set in HTML
|
|
|
773 |
return true;
|
|
|
774 |
}
|
|
|
775 |
}
|
|
|
776 |
return !!(this._node.attributes[attr] &&
|
|
|
777 |
this._node.attributes[attr].specified);
|
|
|
778 |
};
|
|
|
779 |
}
|
|
|
780 |
|
|
|
781 |
// IE throws an error when calling focus() on an element that's invisible, not
|
|
|
782 |
// displayed, or disabled.
|
|
|
783 |
Y.Node.prototype.focus = function () {
|
|
|
784 |
try {
|
|
|
785 |
this._node.focus();
|
|
|
786 |
} catch (e) {
|
|
|
787 |
}
|
|
|
788 |
|
|
|
789 |
return this;
|
|
|
790 |
};
|
|
|
791 |
|
|
|
792 |
// IE throws error when setting input.type = 'hidden',
|
|
|
793 |
// input.setAttribute('type', 'hidden') and input.attributes.type.value = 'hidden'
|
|
|
794 |
Y.Node.ATTRS.type = {
|
|
|
795 |
setter: function(val) {
|
|
|
796 |
if (val === 'hidden') {
|
|
|
797 |
try {
|
|
|
798 |
this._node.type = 'hidden';
|
|
|
799 |
} catch(e) {
|
|
|
800 |
this._node.style.display = 'none';
|
|
|
801 |
this._inputType = 'hidden';
|
|
|
802 |
}
|
|
|
803 |
} else {
|
|
|
804 |
try { // IE errors when changing the type from "hidden'
|
|
|
805 |
this._node.type = val;
|
|
|
806 |
} catch (e) {
|
|
|
807 |
}
|
|
|
808 |
}
|
|
|
809 |
return val;
|
|
|
810 |
},
|
|
|
811 |
|
|
|
812 |
getter: function() {
|
|
|
813 |
return this._inputType || this._node.type;
|
|
|
814 |
},
|
|
|
815 |
|
|
|
816 |
_bypassProxy: true // don't update DOM when using with Attribute
|
|
|
817 |
};
|
|
|
818 |
|
|
|
819 |
if (Y.config.doc.createElement('form').elements.nodeType) {
|
|
|
820 |
// IE: elements collection is also FORM node which trips up scrubVal.
|
|
|
821 |
Y.Node.ATTRS.elements = {
|
|
|
822 |
getter: function() {
|
|
|
823 |
return this.all('input, textarea, button, select');
|
|
|
824 |
}
|
|
|
825 |
};
|
|
|
826 |
}
|
|
|
827 |
/**
|
|
|
828 |
* Provides methods for managing custom Node data.
|
|
|
829 |
*
|
|
|
830 |
* @module node
|
|
|
831 |
* @main node
|
|
|
832 |
* @submodule node-data
|
|
|
833 |
*/
|
|
|
834 |
|
|
|
835 |
Y.mix(Y.Node.prototype, {
|
|
|
836 |
_initData: function() {
|
|
|
837 |
if (! ('_data' in this)) {
|
|
|
838 |
this._data = {};
|
|
|
839 |
}
|
|
|
840 |
},
|
|
|
841 |
|
|
|
842 |
/**
|
|
|
843 |
* @method getData
|
|
|
844 |
* @for Node
|
|
|
845 |
* @description Retrieves arbitrary data stored on a Node instance.
|
|
|
846 |
* If no data is associated with the Node, it will attempt to retrieve
|
|
|
847 |
* a value from the corresponding HTML data attribute. (e.g. node.getData('foo')
|
|
|
848 |
* will check node.getAttribute('data-foo')).
|
|
|
849 |
* @param {string} name Optional name of the data field to retrieve.
|
|
|
850 |
* If no name is given, all data is returned.
|
|
|
851 |
* @return {any | Object} Whatever is stored at the given field,
|
|
|
852 |
* or an object hash of all fields.
|
|
|
853 |
*/
|
|
|
854 |
getData: function(name) {
|
|
|
855 |
this._initData();
|
|
|
856 |
var data = this._data,
|
|
|
857 |
ret = data;
|
|
|
858 |
|
|
|
859 |
if (arguments.length) { // single field
|
|
|
860 |
if (name in data) {
|
|
|
861 |
ret = data[name];
|
|
|
862 |
} else { // initialize from HTML attribute
|
|
|
863 |
ret = this._getDataAttribute(name);
|
|
|
864 |
}
|
|
|
865 |
} else if (typeof data == 'object' && data !== null) { // all fields
|
|
|
866 |
ret = {};
|
|
|
867 |
Y.Object.each(data, function(v, n) {
|
|
|
868 |
ret[n] = v;
|
|
|
869 |
});
|
|
|
870 |
|
|
|
871 |
ret = this._getDataAttributes(ret);
|
|
|
872 |
}
|
|
|
873 |
|
|
|
874 |
return ret;
|
|
|
875 |
|
|
|
876 |
},
|
|
|
877 |
|
|
|
878 |
_getDataAttributes: function(ret) {
|
|
|
879 |
ret = ret || {};
|
|
|
880 |
var i = 0,
|
|
|
881 |
attrs = this._node.attributes,
|
|
|
882 |
len = attrs.length,
|
|
|
883 |
prefix = this.DATA_PREFIX,
|
|
|
884 |
prefixLength = prefix.length,
|
|
|
885 |
name;
|
|
|
886 |
|
|
|
887 |
while (i < len) {
|
|
|
888 |
name = attrs[i].name;
|
|
|
889 |
if (name.indexOf(prefix) === 0) {
|
|
|
890 |
name = name.substr(prefixLength);
|
|
|
891 |
if (!(name in ret)) { // only merge if not already stored
|
|
|
892 |
ret[name] = this._getDataAttribute(name);
|
|
|
893 |
}
|
|
|
894 |
}
|
|
|
895 |
|
|
|
896 |
i += 1;
|
|
|
897 |
}
|
|
|
898 |
|
|
|
899 |
return ret;
|
|
|
900 |
},
|
|
|
901 |
|
|
|
902 |
_getDataAttribute: function(name) {
|
|
|
903 |
name = this.DATA_PREFIX + name;
|
|
|
904 |
|
|
|
905 |
var node = this._node,
|
|
|
906 |
attrs = node.attributes,
|
|
|
907 |
data = attrs && attrs[name] && attrs[name].value;
|
|
|
908 |
|
|
|
909 |
return data;
|
|
|
910 |
},
|
|
|
911 |
|
|
|
912 |
/**
|
|
|
913 |
* @method setData
|
|
|
914 |
* @for Node
|
|
|
915 |
* @description Stores arbitrary data on a Node instance.
|
|
|
916 |
* This is not stored with the DOM node.
|
|
|
917 |
* @param {string} name The name of the field to set. If no val
|
|
|
918 |
* is given, name is treated as the data and overrides any existing data.
|
|
|
919 |
* @param {any} val The value to be assigned to the field.
|
|
|
920 |
* @chainable
|
|
|
921 |
*/
|
|
|
922 |
setData: function(name, val) {
|
|
|
923 |
this._initData();
|
|
|
924 |
if (arguments.length > 1) {
|
|
|
925 |
this._data[name] = val;
|
|
|
926 |
} else {
|
|
|
927 |
this._data = name;
|
|
|
928 |
}
|
|
|
929 |
|
|
|
930 |
return this;
|
|
|
931 |
},
|
|
|
932 |
|
|
|
933 |
/**
|
|
|
934 |
* @method clearData
|
|
|
935 |
* @for Node
|
|
|
936 |
* @description Clears internally stored data.
|
|
|
937 |
* @param {string} name The name of the field to clear. If no name
|
|
|
938 |
* is given, all data is cleared.
|
|
|
939 |
* @chainable
|
|
|
940 |
*/
|
|
|
941 |
clearData: function(name) {
|
|
|
942 |
if ('_data' in this) {
|
|
|
943 |
if (typeof name != 'undefined') {
|
|
|
944 |
delete this._data[name];
|
|
|
945 |
} else {
|
|
|
946 |
delete this._data;
|
|
|
947 |
}
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
return this;
|
|
|
951 |
}
|
|
|
952 |
});
|
|
|
953 |
|
|
|
954 |
Y.mix(Y.NodeList.prototype, {
|
|
|
955 |
/**
|
|
|
956 |
* @method getData
|
|
|
957 |
* @for NodeList
|
|
|
958 |
* @description Retrieves arbitrary data stored on each Node instance
|
|
|
959 |
* bound to the NodeList.
|
|
|
960 |
* @see Node
|
|
|
961 |
* @param {string} name Optional name of the data field to retrieve.
|
|
|
962 |
* If no name is given, all data is returned.
|
|
|
963 |
* @return {Array} An array containing all of the data for each Node instance.
|
|
|
964 |
* or an object hash of all fields.
|
|
|
965 |
*/
|
|
|
966 |
getData: function(name) {
|
|
|
967 |
var args = (arguments.length) ? [name] : [];
|
|
|
968 |
return this._invoke('getData', args, true);
|
|
|
969 |
},
|
|
|
970 |
|
|
|
971 |
/**
|
|
|
972 |
* @method setData
|
|
|
973 |
* @for NodeList
|
|
|
974 |
* @description Stores arbitrary data on each Node instance bound to the
|
|
|
975 |
* NodeList. This is not stored with the DOM node.
|
|
|
976 |
* @param {string} name The name of the field to set. If no name
|
|
|
977 |
* is given, name is treated as the data and overrides any existing data.
|
|
|
978 |
* @param {any} val The value to be assigned to the field.
|
|
|
979 |
* @chainable
|
|
|
980 |
*/
|
|
|
981 |
setData: function(name, val) {
|
|
|
982 |
var args = (arguments.length > 1) ? [name, val] : [name];
|
|
|
983 |
return this._invoke('setData', args);
|
|
|
984 |
},
|
|
|
985 |
|
|
|
986 |
/**
|
|
|
987 |
* @method clearData
|
|
|
988 |
* @for NodeList
|
|
|
989 |
* @description Clears data on all Node instances bound to the NodeList.
|
|
|
990 |
* @param {string} name The name of the field to clear. If no name
|
|
|
991 |
* is given, all data is cleared.
|
|
|
992 |
* @chainable
|
|
|
993 |
*/
|
|
|
994 |
clearData: function(name) {
|
|
|
995 |
var args = (arguments.length) ? [name] : [];
|
|
|
996 |
return this._invoke('clearData', [name]);
|
|
|
997 |
}
|
|
|
998 |
});
|
|
|
999 |
|
|
|
1000 |
|
|
|
1001 |
}, '3.18.1', {"requires": ["event-base", "node-core", "dom-base", "dom-style"]});
|