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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
|
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 3.0.0pr1
*/
YUI.add('node', function(Y) {
/**
* The Node Utility provides a DOM-like interface for interacting with DOM nodes.
* @module node
* @submodule node-base
*/
/**
* The Node class provides a wrapper for manipulating DOM Nodes.
* Node properties can be accessed via the set/get methods.
* Use Y.get() to retrieve Node instances.
*
* <strong>NOTE:</strong> All node properties are accessed using the
* <code>set</code> and <code>get</code> methods.
*
* @class Node
* @constructor
*/
var BASE_NODE = 0,
ELEMENT_NODE = 1,
//ATTRIBUTE_NODE = 2,
//TEXT_NODE = 3,
//CDATA_SECTION_NODE = 4,
//ENTITY_REFERENCE_NODE = 5,
//ENTITY_NODE = 6,
//PROCESSING_INSTRUCTION_NODE = 7,
//COMMENT_NODE = 8,
DOCUMENT_NODE = 9; //,
//DOCUMENT_TYPE_NODE = 10,
//DOCUMENT_FRAGMENT_NODE = 11,
//NOTATION_NODE = 12;
var OWNER_DOCUMENT = 'ownerDocument',
TAG_NAME = 'tagName',
NODE_NAME = 'nodeName',
NODE_TYPE = 'nodeType';
var RE_VALID_PROP_TYPES = /(?:string|boolean|number)/;
var Selector = Y.Selector;
var _instances = {};
var _nodes = {};
var _nodelists = {};
var _restrict = null;
var slice = [].slice;
var wrapDOM = function(node) {
var ret = null,
yuid = (node) ? node._yuid : null,
instance = _instances[yuid],
existingNode = _nodes[yuid];
if (node) {
if (NODE_TYPE in node) {
if (instance && existingNode && node === existingNode) {
ret = instance; // reuse existing Nodes if nodes match
} else {
ret = new Node(node);
}
} else if ('item' in node || 'push' in node) {
ret = new Y.NodeList(node);
}
}
return ret;
};
var wrapFn = function(fn) {
var ret = null;
if (fn) {
ret = (typeof fn === 'string') ?
function(n) {
return Y.Selector.test(n, fn);
} :
function(n) {
return fn(_instances[n._yuid]);
};
}
return ret;
};
var getDoc = function(node) {
node = _nodes[node._yuid];
return (node[NODE_TYPE] === 9) ? node : node[OWNER_DOCUMENT];
};
// returns HTMLElement
var getDOMNode = function(node) {
if (node && !node.nodeType && node._yuid) {
node = _nodes[node._yuid];
}
return node || null;
};
/*
* Wraps the input and outputs of a node instance
*/
var nodeInOut = function(method, a, b, c, d, e) {
if (a) { // first 2 may be Node instances or nodes (TODO: or strings?)
a = getDOMNode(a);
if (b) {
b = getDOMNode(b);
}
}
return wrapDOM(_nodes[this._yuid][method](a, b, c, d, e));
};
/*
* Wraps the return value in a node instance
*/
var nodeOut = function(method, a, b, c, d, e) {
return wrapDOM(_nodes[this._yuid][method](a, b, c, d, e));
};
/*
* Returns directy from node method call
*/
var rawOut = function(method, a, b, c, d, e) {
return _nodes[this._yuid][method](a, b, c, d, e);
};
var noOut = function(method, a, b, c, d, e) {
_nodes[this._yuid][method](a, b, c, d, e);
return this;
};
var PROPS_WRAP = {
/**
* Returns a Node instance.
* @property parentNode
* @type Node
*/
'parentNode': BASE_NODE,
/**
* Returns a NodeList instance.
* @property childNodes
* @type NodeList
*/
'childNodes': BASE_NODE,
/**
* Returns a NodeList instance.
* @property children
* @type NodeList
*/
'children': function(node) {
node = _nodes[node._yuid];
var children = node.children;
if (children === undefined) {
var childNodes = node.childNodes;
children = [];
for (var i = 0, len = childNodes.length; i < len; ++i) {
if (childNodes[i][TAG_NAME]) {
children[children.length] = childNodes[i];
}
}
}
return children;
},
/**
* Returns a Node instance.
* @property firstChild
* @type Node
*/
'firstChild': BASE_NODE,
/**
* Returns a Node instance.
* @property lastChild
* @type Node
*/
'lastChild': BASE_NODE,
/**
* Returns a Node instance.
* @property previousSibling
* @type Node
*/
'previousSibling': BASE_NODE,
/**
* Returns a Node instance.
* @property previousSibling
* @type Node
*/
'nextSibling': BASE_NODE,
/**
* Returns a Node instance.
* @property ownerDocument
* @type Doc
*/
'ownerDocument': BASE_NODE,
/**
* Returns a Node instance.
* @property offsetParent
* @type Node
*/
'offsetParent': ELEMENT_NODE,
/**
* Returns a Node instance.
* @property documentElement
* @type Node
*/
'documentElement': DOCUMENT_NODE,
/**
* Returns a Node instance.
* @property body
* @type Node
*/
'body': DOCUMENT_NODE,
// form
/**
* Returns a NodeList instance.
* @property elements
* @type NodeList
*/
'elements': ELEMENT_NODE,
// table
/**
* Returns a NodeList instance.
* @property rows
* @type NodeList
*/
'rows': ELEMENT_NODE,
/**
* Returns a NodeList instance.
* @property cells
* @type NodeList
*/
'cells': ELEMENT_NODE,
/**
* Returns a Node instance.
* @property tHead
* @type Node
*/
'tHead': ELEMENT_NODE,
/**
* Returns a Node instance.
* @property tFoot
* @type Node
*/
'tFoot': ELEMENT_NODE,
/**
* Returns a NodeList instance.
* @property tBodies
* @type NodeList
*/
'tBodies': ELEMENT_NODE
};
var METHODS = {
/**
* Passes through to DOM method.
* @method replaceChild
* @param {HTMLElement | Node} node Node to be inserted
* @param {HTMLElement | Node} refNode Node to be replaced
* @return {Node} The replaced node
*/
replaceChild: nodeInOut,
/**
* Passes through to DOM method.
* @method appendChild
* @param {HTMLElement | Node} node Node to be appended
* @return {Node} The appended node
*/
appendChild: nodeInOut,
/**
* Passes through to DOM method.
* @method insertBefore
* @param {HTMLElement | Node} newNode Node to be appended
* @param {HTMLElement | Node} refNode Node to be inserted before
* @return {Node} The inserted node
*/
insertBefore: nodeInOut,
/**
* Passes through to DOM method.
* @method removeChild
* @param {HTMLElement | Node} node Node to be removed
* @return {Node} The removed node
*/
removeChild: nodeInOut,
/**
* Passes through to DOM method.
* @method hasChildNodes
* @return {Boolean} Whether or not the node has any childNodes
*/
hasChildNodes: rawOut,
/**
* Passes through to DOM method.
* @method cloneNode
* @param {HTMLElement | Node} node Node to be cloned
* @return {Node} The clone
*/
cloneNode: nodeOut,
/**
* Passes through to DOM method.
* @method getAttribute
* @param {String} attribute The attribute to retrieve
* @return {String} The current value of the attribute
*/
getAttribute: rawOut,
/**
* Passes through to DOM method.
* @method setAttribute
* @param {String} attribute The attribute to set
* @param {String} The value to apply to the attribute
* @chainable
*/
setAttribute: noOut,
/**
* Passes through to DOM method.
* @method hasAttribute
* @param {String} attribute The attribute to test for
* @return {Boolean} Whether or not the attribute is present
*/
hasAttribute: rawOut,
/**
* Passes through to DOM method.
* @method scrollIntoView
* @chainable
*/
scrollIntoView: noOut,
/**
* Passes through to DOM method.
* @method getElementsByTagName
* @param {String} tagName The tagName to collect
* @return {NodeList} A NodeList representing the HTMLCollection
*/
getElementsByTagName: nodeOut,
/**
* Passes through to DOM method.
* @method focus
* @chainable
*/
focus: noOut,
/**
* Passes through to DOM method.
* @method blur
* @chainable
*/
blur: noOut,
/**
* Passes through to DOM method.
* Only valid on FORM elements
* @method submit
* @chainable
*/
submit: noOut,
/**
* Passes through to DOM method.
* Only valid on FORM elements
* @method reset
* @chainable
*/
reset: noOut
};
var addNodeListMethod = function(name) {
NodeList.prototype[name] = function() {
var a = [],
nodes = _nodelists[this._yuid],
ret;
for (var i = 0, len = nodes.length; i < len; ++i) {
_nodes[_tmpNode._yuid] = nodes[i];
ret = _tmpNode[name].apply(_tmpNode, arguments);
if (ret !== _tmpNode) {
a[i] = ret;
}
}
return a.length ? a : this;
};
};
var METHODS_INVOKE = {
'getBoundingClientRect': true
};
var Node = function(node) {
if (!node || !node[NODE_TYPE]) {
Y.log('invalid node:' + node, 'error', 'Node');
return null;
}
var yuid = Y.guid();
try { // IE errors on non-element expandos (cant be reused)
node._yuid = yuid;
} catch(e) {}
this._yuid = yuid;
_nodes[yuid] = node;
_instances[yuid] = this;
};
var SETTERS = {};
var GETTERS = {
/**
* Normalizes nodeInnerText and textContent.
* @property text
* @type String
*/
'text': function(node) {
return Y.DOM.getText(_nodes[node._yuid]);
},
/**
* Returns a nodeList of option elements
* @property options
* @type String
*/
'options': function(node) {
return (node) ? node.getElementsByTagName('option') : [];
}
};
Node.setters = function(prop, fn) {
if (typeof prop == 'string') {
SETTERS[prop] = fn;
} else { // assume object
Y.each(prop, function(fn, prop) {
Node.setters(prop, fn);
});
}
};
Node.getters = function(prop, fn) {
if (typeof prop == 'string') {
GETTERS[prop] = fn;
} else { // assume object
Y.each(prop, function(fn, prop) {
Node.getters(prop, fn);
});
}
};
Node.methods = function(name, fn) {
if (typeof name == 'string') {
Node.prototype[name] = function() {
var args = slice.call(arguments);
args.unshift(this);
var ret = fn.apply(null, args);
if (ret === undefined) {
ret = this;
}
return ret;
};
addNodeListMethod(name);
} else { // assume object
Y.each(name, function(fn, name) {
Node.methods(name, fn);
});
}
};
Node.getDOMNode = function(node) {
var ret;
if (node.nodeType) {
ret = node;
} else if (typeof node === 'string') {
ret = Selector.query(node, null, true);
} else {
ret = _nodes[node._yuid];
}
return ret || null;
};
Node.wrapDOMMethod = function(name) {
return function() {
var args = slice.call(arguments);
args.unshift(Y.Node.getDOMNode(args.shift()));
return Y.DOM[name].apply(Y.DOM, args);
};
};
Node.addDOMMethods = function(methods) {
var add = {};
Y.each(methods, function(v, n) {
add[v] = Y.Node.wrapDOMMethod(v);
});
Y.Node.methods(add);
};
Node.prototype = {
/**
* Set the value of the property/attribute on the HTMLElement bound to this Node.
* Only strings/numbers/booleans are passed through unless a SETTER exists.
* @method set
* @param {String} prop Property to set
* @param {any} val Value to apply to the given property
* @chainable
*/
set: function(prop, val) {
var node = _nodes[this._yuid];
if (prop in SETTERS) { // use custom setter
SETTERS[prop](this, prop, val); // passing Node instance
} else if (RE_VALID_PROP_TYPES.test(typeof node[prop])) { // safe to write
node[prop] = val;
}
return this;
},
/**
* Get the value of the property/attribute on the HTMLElement bound to this Node.
* Only strings/numbers/booleans are passed through unless a GETTER exists.
* @method get
* @param {String} prop Property to get
* @return {any} Current value of the property
*/
get: function(prop) {
var val;
var node = _nodes[this._yuid];
if (prop in GETTERS) { // use custom getter
val = GETTERS[prop](this, prop);
} else if (prop in PROPS_WRAP) { // wrap DOM object (HTMLElement, HTMLCollection, Document)
if (typeof PROPS_WRAP[prop] === 'function') {
val = PROPS_WRAP[prop](this);
} else {
val = node[prop];
}
if (_restrict && _restrict[this._yuid] && !Y.DOM.contains(node, val)) {
val = null; // not allowed to go outside of root node
} else {
val = wrapDOM(val);
}
} else if (RE_VALID_PROP_TYPES.test(typeof node[prop])) { // safe to read
val = node[prop];
}
return val;
},
invoke: function(method, a, b, c, d, e) {
if (a) { // first 2 may be Node instances or strings
a = (a[NODE_TYPE]) ? a : getDOMNode(a);
if (b) {
b = (b[NODE_TYPE]) ? b : getDOMNode(b);
}
}
var node = _nodes[this._yuid];
if (node && METHODS_INVOKE[method] && node[method]) {
return node[method](a, b, c, d, e);
}
return null;
},
hasMethod: function(method) {
return !!(METHODS_INVOKE[method] && _nodes[this._yuid][method]);
},
//normalize: function() {},
//isSupported: function(feature, version) {},
toString: function() {
var node = _nodes[this._yuid] || {};
return node.id || node[NODE_NAME] || 'undefined node';
},
/**
* Retrieves a single node based on the given CSS selector.
* @method query
*
* @param {string} selector The CSS selector to test against.
* @return {Node} A Node instance for the matching HTMLElement.
*/
query: function(selector) {
return wrapDOM(Selector.query(selector, _nodes[this._yuid], true));
},
/**
* Retrieves a nodeList based on the given CSS selector.
* @method queryAll
*
* @param {string} selector The CSS selector to test against.
* @return {NodeList} A NodeList instance for the matching HTMLCollection/Array.
*/
queryAll: function(selector) {
return wrapDOM(Selector.query(selector, _nodes[this._yuid]));
},
/**
* Test if the supplied node matches the supplied selector.
* @method test
*
* @param {string} selector The CSS selector to test against.
* @return {boolean} Whether or not the node matches the selector.
*/
test: function(selector) {
return Selector.test(_nodes[this._yuid], selector);
},
/**
* Compares nodes to determine if they match.
* Node instances can be compared to each other and/or HTMLElements.
* @method compareTo
* @param {HTMLElement | Node} refNode The reference node to compare to the node.
* @return {Boolean} True if the nodes match, false if they do not.
*/
compareTo: function(refNode) {
refNode = refNode[NODE_TYPE] ? refNode : _nodes[refNode._yuid];
return _nodes[this._yuid] === refNode;
},
/*
* Returns the nearest ancestor that passes the test applied by supplied boolean method.
* @method ancestor
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @return {Node} The matching Node instance or null if not found
*/
ancestor: function(fn) {
return wrapDOM(Y.DOM.elementByAxis(_nodes[this._yuid], 'parentNode', wrapFn(fn)));
},
/**
* Returns the previous matching sibling.
* Returns the nearest element node sibling if no method provided.
* @method previous
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
* @return {Node} Node instance or null if not found
*/
previous: function(fn, all) {
return wrapDOM(Y.DOM.elementByAxis(_nodes[this._yuid], 'previousSibling', wrapFn(fn)), all);
},
/**
* Returns the next matching sibling.
* Returns the nearest element node sibling if no method provided.
* @method next
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
* @return {Node} Node instance or null if not found
*/
next: function(fn, all) {
return wrapDOM(Y.DOM.elementByAxis(_nodes[this._yuid], 'nextSibling', wrapFn(fn)), all);
},
/**
* Attaches a DOM event handler.
* @method attach
* @param {String} type The type of DOM Event to listen for
* @param {Function} fn The handler to call when the event fires
* @param {Object} arg An argument object to pass to the handler
*/
attach: function(type, fn, arg) {
var args = slice.call(arguments, 0);
args.unshift(_nodes[this._yuid]);
return Y.Event.addListener.apply(Y.Event, args);
},
/**
* Alias for attach.
* @method on
* @param {String} type The type of DOM Event to listen for
* @param {Function} fn The handler to call when the event fires
* @param {Object} arg An argument object to pass to the handler
* @see attach
*/
on: function(type, fn, arg) {
return this.attach.apply(this, arguments);
},
addEventListener: function(type, fn, arg) {
return Y.Event.nativeAdd(_nodes[this._yuid], type, fn, arg);
},
/**
* Detaches a DOM event handler.
* @method detach
* @param {String} type The type of DOM Event
* @param {Function} fn The handler to call when the event fires
*/
detach: function(type, fn) {
var args = slice.call(arguments, 0);
args.unshift(_nodes[this._yuid]);
return Y.Event.removeListener.apply(Y.Event, args);
},
removeEventListener: function(type, fn) {
return Y.Event.nativeRemove(_nodes[this._yuid], type, fn);
},
/**
* Creates a Node instance from HTML string
* @method create
* @param {String|Array} html The string of html to create
* @return {Node} A new Node instance
*/
create: function(html) {
return Y.Node.create(html);
},
/**
* Determines whether the ndoe is an ancestor of another HTML element in the DOM hierarchy.
* @method contains
* @param {Node | HTMLElement} needle The possible node or descendent
* @return {Boolean} Whether or not this node is the needle its ancestor
*/
contains: function(needle) {
return Y.DOM.contains(_nodes[this._yuid], getDOMNode(needle));
},
/**
* Applies the supplied plugin to the node.
* @method plug
* @param {Function} The plugin Class to apply
* @param {Object} config An optional config to pass to the constructor
* @chainable
*/
plug: function(PluginClass, config) {
config = config || {};
config.owner = this;
if (PluginClass && PluginClass.NS) {
this[PluginClass.NS] = new PluginClass(config);
}
return this;
},
/**
* Determines whether the node is appended to the document.
* @method inDoc
* @param {Node|HTMLElement} doc optional An optional document to check against.
* Defaults to current document.
* @return {Boolean} Whether or not this node is appended to the document.
*/
inDoc: function(doc) {
var node = _nodes[this._yuid];
doc = (doc) ? getDoc(doc) : node.ownerDocument;
if (doc.documentElement) {
return Y.DOM.contains(doc.documentElement, node);
}
}
};
Y.each(METHODS, function(fn, method) {
Node.prototype[method] = function() {
return fn.apply(this, [method].concat(slice.call(arguments)));
};
});
/**
* Creates a Node instance from an HTML string
* @method create
* @param {String} html HTML string
*/
Node.create = function(html) {
return wrapDOM(Y.DOM.create(html));
};
Node.getById = function(id, doc) {
doc = (doc && doc[NODE_TYPE]) ? doc : Y.config.doc;
return wrapDOM(doc.getElementById(id));
};
/**
* Retrieves a Node instance for the given object/string.
* Note: Use 'document' string to retrieve document Node instance from string
* @method get
* @static
* @param {document|HTMLElement|HTMLCollection|Array|String} node The object to wrap.
* @param {document|Node} doc optional The document containing the node. Defaults to current document.
* @param {boolean} isRoot optional Whether or not this node should be treated as a root node. Root nodes
* aren't allowed to traverse outside their DOM tree.
* @return {Node} A wrapper instance for the supplied object.
*/
Node.get = function(node, doc, isRoot) {
if (node instanceof Node) {
return node;
}
if (!doc) {
doc = Y.config.doc;
} else if (doc._yuid && _nodes[doc._yuid]) {
doc = _nodes[doc._yuid];
}
if (node && typeof node === 'string') {
if (node === 'document') {
node = Y.config.doc;
} else {
node = Y.Selector.query(node, doc, true);
}
}
node = wrapDOM(node);
if (isRoot) {
_restrict = _restrict || {};
_restrict[node._yuid] = node;
}
return node;
};
/**
* Retrieves a NodeList instance for the given object/string.
* @method all
* @static
* @param {HTMLCollection|Array|String} node The object to wrap.
* @param {document|Node} doc optional The document containing the node. Defaults to current document.
* @return {NodeList} A NodeList instance for the supplied nodes.
*/
Node.all = function(nodes, doc) {
if (nodes instanceof NodeList) {
return nodes;
}
if (!doc) {
doc = Y.config.doc;
} else if (doc._yuid && _nodes[doc._yuid]) {
doc = _nodes[doc._yuid];
}
if (nodes && typeof nodes == 'string') {
nodes = Selector.query(nodes, doc);
}
return wrapDOM(nodes);
};
/**
* A wrapper for manipulating multiple DOM elements
* @class NodeList
* @extends Node
* @constructor
*/
var NodeList = function(nodes) {
// TODO: input validation
_nodelists[Y.stamp(this)] = nodes;
};
// used to call Node methods against NodeList nodes
var _tmpNode = Node.create('<div></div>');
NodeList.prototype = {};
Y.each(Node.prototype, function(fn, name) {
if (typeof Node.prototype[name] == 'function') {
addNodeListMethod(name);
}
});
Y.mix(NodeList.prototype, {
/**
* Retrieves the Node instance at the given index.
* @method item
*
* @param {Number} index The index of the target Node.
* @return {Node} The Node instance at the given index.
*/
item: function(index) {
var node = _nodelists[this._yuid][index];
return (node && node[TAG_NAME]) ? wrapDOM(node) : (node && node.get) ? node : null;
},
/**
* Set the value of the property/attribute on all HTMLElements bound to this NodeList.
* Only strings/numbers/booleans are passed through unless a SETTER exists.
* @method set
* @param {String} prop Property to set
* @param {any} val Value to apply to the given property
* @see Node
* @chainable
*/
set: function(name, val) {
var nodes = _nodelists[this._yuid];
for (var i = 0, len = nodes.length; i < len; ++i) {
_nodes[_tmpNode._yuid] = nodes[i];
_tmpNode.set(name, val);
}
return this;
},
/**
* Get the value of the property/attribute for each of the HTMLElements bound to this NodeList.
* Only strings/numbers/booleans are passed through unless a GETTER exists.
* @method get
* @param {String} prop Property to get
* @return {Array} Array containing the current values mapped to the Node indexes
* @see Node
*/
get: function(name) {
if (name == 'length') { // TODO: remove
Y.log('the length property is deprecated; use size()', 'warn', 'NodeList');
return _nodelists[this._yuid].length;
}
var nodes = _nodelists[this._yuid];
var ret = [];
for (var i = 0, len = nodes.length; i < len; ++i) {
_nodes[_tmpNode._yuid] = nodes[i];
ret[i] = _tmpNode.get(name);
}
return ret;
},
/**
* Filters the NodeList instance down to only nodes matching the given selector.
* @method filter
* @param {String} selector The selector to filter against
* @return {NodeList} NodeList containing the updated collection
* @see Selector
*/
filter: function(selector) {
return wrapDOM(Selector.filter(_nodelists[this._yuid], selector));
},
/**
* Applies the given function to each Node in the NodeList.
* @method each
* @param {Function} fn The function to apply
* @param {Object} context optional An optional context to apply the function with
* Default context is the NodeList instance
* @return {NodeList} NodeList containing the updated collection
* @chainable
*/
each: function(fn, context) {
context = context || this;
var nodes = _nodelists[this._yuid];
for (var i = 0, len = nodes.length; i < len; ++i) {
fn.call(context, Y.Node.get(nodes[i]), i, this);
}
return this;
},
/**
* Returns the current number of items in the NodeList.
* @method size
* @return {Int} The number of items in the NodeList.
*/
size: function() {
return _nodelists[this._yuid].length;
},
toString: function() {
var nodes = _nodelists[this._yuid] || [];
return 'NodeList (' + nodes.length + ' items)';
}
}, true);
Y.Node = Node;
Y.NodeList = NodeList;
Y.all = Y.Node.all;
Y.get = Y.Node.get;
/**
* Extended Node interface for managing node styles.
* @module node
* @submodule node-style
* @for Node
*/
Y.Node.addDOMMethods([
/**
* Returns the style's current value.
* @method getStyle
* @param {String} attr The style attribute to retrieve.
* @return {String} The current value of the style property for the element.
*/
'getStyle',
/**
* Returns the computed value for the given style property.
* @method getComputedStyle
* @param {String} attr The style attribute to retrieve.
* @return {String} The computed value of the style property for the element.
*/
'getComputedStyle',
/**
* Sets a style property of the node.
* @method setStyle
* @param {String} attr The style attribute to set.
* @param {String|Number} val The value.
* @chainable
*/
'setStyle',
/**
* Sets multiple style properties on the node.
* @method setStyles
* @param {Object} hash An object literal of property:value pairs.
* @chainable
*/
'setStyles'
]);
/**
* Extended Node interface for managing classNames.
* @module node
* @for Node
*/
Y.Node.addDOMMethods([
/**
* Determines whether the node has the given className.
* @method hasClass
* @param {String} className the class name to search for
* @return {Boolean} Whether or not the node has the given class.
*/
'hasClass',
/**
* Adds a class name to the node.
* @method addClass
* @param {String} className the class name to add to the node's class attribute
* @chainable
*/
'addClass',
/**
* Removes a class name from the node.
* @method removeClass
* @param {String} className the class name to remove from the node's class attribute
* @chainable
*/
'removeClass',
/**
* Replace a class with another class.
* If no oldClassName is present, the newClassName is simply added.
* @method replaceClass
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
* @chainable
*/
'replaceClass',
/**
* If the className exists on the node it is removed, if it doesn't exist it is added.
* @method toggleClass
* @param {String} className the class name to be toggled
* @chainable
*/
'toggleClass'
]);
/**
* Extended Node interface for managing regions and screen positioning.
* Adds support for positioning elements and normalizes window size and scroll detection.
* @module node
* @submodule node-screen
* @for Node
*/
var ATTR = [
/**
* Returns a region object for the node
* @property region
* @type Node
*/
'region',
/**
* Returns a region object for the node's viewport
* @property viewportRegion
* @type Node
*/
'viewportRegion'
],
getNode = Y.Node.getDOMNode;
Y.each(ATTR, function(v, n) {
Y.Node.getters(v, Y.Node.wrapDOMMethod(v));
});
Y.Node.addDOMMethods([
/**
* Determines whether or not the node is currently visible in the viewport.
* @method inViewportRegion
* @return {Boolean} Whether or not the node is currently positioned
* within the viewport's region
*/
'inViewportRegion'
]);
// these need special treatment to extract 2nd node arg
Y.Node.methods({
/**
* Compares the intersection of the node with another node or region
* @method intersect
* @param {Node|Object} node2 The node or region to compare with.
* @param {Object} altRegion An alternate region to use (rather than this node's).
* @return {Object} An object representing the intersection of the regions.
*/
intersect: function(node1, node2, altRegion) {
if (node2 instanceof Y.Node) { // might be a region object
node2 = getNode(node2);
}
return Y.DOM.intersect(getNode(node1), node2, altRegion);
},
/**
* Determines whether or not the node is within the giving region.
* @method inRegion
* @param {Node|Object} node2 The node or region to compare with.
* @param {Boolean} all Whether or not all of the node must be in the region.
* @param {Object} altRegion An alternate region to use (rather than this node's).
* @return {Object} An object representing the intersection of the regions.
*/
inRegion: function(node1, node2, all, altRegion) {
if (node2 instanceof Y.Node) { // might be a region object
node2 = getNode(node2);
}
return Y.DOM.inRegion(getNode(node1), node2, all, altRegion);
}
});
/**
* Extended Node interface for managing regions and screen positioning.
* Adds support for positioning elements and normalizes window size and scroll detection.
* @module node
* @submodule node-screen
* @for Node
*/
Y.each([
/**
* Returns the inner width of the viewport (exludes scrollbar).
* @property winWidth
* @type {Int}
*/
'winWidth',
/**
* Returns the inner height of the viewport (exludes scrollbar).
* @property winHeight
* @type {Int}
*/
'winHeight',
/**
* Document width
* @property winHeight
* @type {Int}
*/
'docWidth',
/**
* Document height
* @property docHeight
* @type {Int}
*/
'docHeight',
/**
* Amount page has been scroll vertically
* @property docScrollX
* @type {Int}
*/
'docScrollX',
/**
* Amount page has been scroll horizontally
* @property docScrollY
* @type {Int}
*/
'docScrollY'
],
function(v, n) {
Y.Node.getters(v, Y.Node.wrapDOMMethod(v));
}
);
Y.Node.addDOMMethods([
/**
* Gets the current position of the node in page coordinates.
* Nodes must be part of the DOM tree to have page coordinates
* (display:none or nodes not appended return false).
* @method getXY
* @return {Array} The XY position of the node
*/
'getXY',
/**
* Set the position of the node in page coordinates, regardless of how the node is positioned.
* The node must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setXY
* @param {Array} xy Contains X & Y values for new position (coordinates are page-based)
* @chainable
*/
'setXY',
/**
* Gets the current position of the node in page coordinates.
* Nodes must be part of the DOM tree to have page coordinates
* (display:none or nodes not appended return false).
* @method getX
* @return {Int} The X position of the node
*/
'getX',
/**
* Set the position of the node in page coordinates, regardless of how the node is positioned.
* The node must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setX
* @param {Int} x X value for new position (coordinates are page-based)
* @chainable
*/
'setX',
/**
* Gets the current position of the node in page coordinates.
* Nodes must be part of the DOM tree to have page coordinates
* (display:none or nodes not appended return false).
* @method getY
* @return {Int} The Y position of the node
*/
'getY',
/**
* Set the position of the node in page coordinates, regardless of how the node is positioned.
* The node must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setY
* @param {Int} y Y value for new position (coordinates are page-based)
* @chainable
*/
'setY'
]);
}, '3.0.0pr1' ,{requires:['dom']});
|