-
Notifications
You must be signed in to change notification settings - Fork 0
/
pega_yui_dom_profile.js
1266 lines (1094 loc) · 50.1 KB
/
pega_yui_dom_profile.js
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
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.5.1
*/
/**
* The dom module provides helper methods for manipulating Dom elements.
* @module dom
*
*/
(function() {
var Y = pega.util, // internal shorthand
getStyle, // for load time browser branching
setStyle, // ditto
propertyCache = {}, // for faster hyphen converts
reClassNameCache = {}, // cache regexes for className
document = window.document; // cache for faster lookups
pega.env._id_counter = pega.env._id_counter || 0; // for use with generateId (global to save state if Dom is overwritten)
// brower detection
var isOpera = pega.env.ua.opera,
isSafari = pega.env.ua.webkit,
isGecko = pega.env.ua.gecko,
isIE = pega.env.ua.ie;
// regex cache
var patterns = {
HYPHEN: /(-[a-z])/i, // to normalize get/setStyle
ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
OP_SCROLL:/^(?:inline|table-row)$/i
};
var toCamel = function(property) {
if ( !patterns.HYPHEN.test(property) ) {
return property; // no hyphens
}
if (propertyCache[property]) { // already converted
return propertyCache[property];
}
var converted = property;
while( patterns.HYPHEN.exec(converted) ) {
converted = converted.replace(RegExp.$1,
RegExp.$1.substr(1).toUpperCase());
}
propertyCache[property] = converted;
return converted;
//return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug
};
var getClassRegEx = function(className) {
var re = reClassNameCache[className];
if (!re) {
re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
reClassNameCache[className] = re;
}
return re;
};
// branching at load instead of runtime
if (document.defaultView && document.defaultView.getComputedStyle) { // W3C DOM method
getStyle = function(el, property) {
var value = null;
if (property == 'float') { // fix reserved word
property = 'cssFloat';
}
var computed = el.ownerDocument.defaultView.getComputedStyle(el, '');
if (computed) { // test computed before touching for safari
value = computed[toCamel(property)];
}
return el.style[property] || value;
};
} else if (document.documentElement.currentStyle && isIE) { // IE method
getStyle = function(el, property) {
switch( toCamel(property) ) {
case 'opacity' :// IE opacity uses filter
var val = 100;
try { // will error if no DXImageTransform
val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
} catch(e) {
try { // make sure its in the document
val = el.filters('alpha').opacity;
} catch(e) {
pega.log('getStyle: IE filter failed',
'error', 'Dom');
}
}
return val / 100;
case 'float': // fix reserved word
property = 'styleFloat'; // fall through
default:
// test currentStyle before touching
var value = el.currentStyle ? el.currentStyle[property] : null;
return ( el.style[property] || value );
}
};
} else { // default to inline only
getStyle = function(el, property) { return el.style[property]; };
}
if (isIE) {
setStyle = function(el, property, val) {
switch (property) {
case 'opacity':
if ( pega.lang.isString(el.style.filter) ) { // in case not appended
el.style.filter = 'alpha(opacity=' + val * 100 + ')';
if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
}
break;
case 'float':
property = 'styleFloat';
default:
el.style[property] = val;
}
};
} else {
setStyle = function(el, property, val) {
if (property == 'float') {
property = 'cssFloat';
}
el.style[property] = val;
};
}
var testElement = function(node, method) {
return node && node.nodeType == 1 && ( !method || method(node) );
};
/**
* Provides helper methods for DOM elements.
* @namespace pega.util
* @class Dom
*/
pega.util.Dom = {
/**
* Returns an HTMLElement reference.
* @method get
* @param {String | HTMLElement |Array} el Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
*/
get: function(el) {
if (el && (el.nodeType || el.item)) { // Node, or NodeList
return el;
}
if (pega.lang.isString(el) || !el) { // id or null
return document.getElementById(el);
}
if (el.length !== undefined) { // array-like
var c = [];
for (var i = 0, len = el.length; i < len; ++i) {
c[c.length] = Y.Dom.get(el[i]);
}
return c;
}
return el; // some other object, just pass it back
},
/**
* Normalizes currentStyle and ComputedStyle.
* @method getStyle
* @param {String | HTMLElement |Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @param {String} property The style property whose value is returned.
* @return {String | Array} The current value of the style property for the element(s).
*/
getStyle: function(el, property) {
property = toCamel(property);
var f = function(element) {
return getStyle(element, property);
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
* @method setStyle
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @param {String} property The style property to be set.
* @param {String} val The value to apply to the given property.
*/
setStyle: function(el, property, val) {
property = toCamel(property);
var f = function(element) {
setStyle(element, property, val);
pega.log('setStyle setting ' + property + ' to ' + val, 'info', 'Dom');
};
Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method getXY
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
* @return {Array} The XY position of the element(s)
*/
getXY: function(el) {
var f = function(el) {
// has to be part of document to have pageXY
if ( (el.parentNode === null || el.offsetParent === null ||
this.getStyle(el, 'display') == 'none') && el != el.ownerDocument.body) {
pega.log('getXY failed: element not available', 'error', 'Dom');
return false;
}
pega.log('getXY returning ' + getXY(el), 'info', 'Dom');
return getXY(el);
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Gets the current X position of an element based on page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method getX
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
* @return {Number | Array} The X position of the element(s)
*/
getX: function(el) {
var f = function(el) {
return Y.Dom.getXY(el)[0];
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method getY
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
* @return {Number | Array} The Y position of the element(s)
*/
getY: function(el) {
var f = function(el) {
return Y.Dom.getXY(el)[1];
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Set the position of an html element in page coordinates, regardless of how the element is positioned.
* The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setXY
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
* @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
* @param {Boolean} noRetry By default we try and set the position a second time if the first fails
*/
setXY: function(el, pos, noRetry) {
var f = function(el) {
var style_pos = this.getStyle(el, 'position');
if (style_pos == 'static') { // default to relative
this.setStyle(el, 'position', 'relative');
style_pos = 'relative';
}
var pageXY = this.getXY(el);
if (pageXY === false) { // has to be part of doc to have pageXY
pega.log('setXY failed: element not available', 'error', 'Dom');
return false;
}
var delta = [ // assuming pixels; if not we will have to retry
parseInt( this.getStyle(el, 'left'), 10 ),
parseInt( this.getStyle(el, 'top'), 10 )
];
if ( isNaN(delta[0]) ) {// in case of 'auto'
delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
}
if ( isNaN(delta[1]) ) { // in case of 'auto'
delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
}
if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
if (!noRetry) {
var newXY = this.getXY(el);
// if retry is true, try one more time if we miss
if ( (pos[0] !== null && newXY[0] != pos[0]) ||
(pos[1] !== null && newXY[1] != pos[1]) ) {
this.setXY(el, pos, true);
}
}
pega.log('setXY setting position to ' + pos, 'info', 'Dom');
};
Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Set the X position of an html element in page coordinates, regardless of how the element is positioned.
* The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setX
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @param {Int} x The value to use as the X coordinate for the element(s).
*/
setX: function(el, x) {
Y.Dom.setXY(el, [x, null]);
},
/**
* Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
* The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @method setY
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @param {Int} x To use as the Y coordinate for the element(s).
*/
setY: function(el, y) {
Y.Dom.setXY(el, [null, y]);
},
/**
* Returns the region position of the given element.
* The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
* @method getRegion
* @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
* @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
*/
getRegion: function(el) {
var f = function(el) {
if ( (el.parentNode === null || el.offsetParent === null ||
this.getStyle(el, 'display') == 'none') && el != el.ownerDocument.body) {
pega.log('getRegion failed: element not available', 'error', 'Dom');
return false;
}
var region = Y.Region.getRegion(el);
pega.log('getRegion returning ' + region, 'info', 'Dom');
return region;
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Returns the width of the client (viewport).
* @method getClientWidth
* @deprecated Now using getViewportWidth. This interface left intact for back compat.
* @return {Int} The width of the viewable area of the page.
*/
getClientWidth: function() {
return Y.Dom.getViewportWidth();
},
/**
* Returns the height of the client (viewport).
* @method getClientHeight
* @deprecated Now using getViewportHeight. This interface left intact for back compat.
* @return {Int} The height of the viewable area of the page.
*/
getClientHeight: function() {
return Y.Dom.getViewportHeight();
},
/**
* Returns a array of HTMLElements with the given class.
* For optimized performance, include a tag and/or root node when possible.
* @method getElementsByClassName
* @param {String} className The class name to match against
* @param {String} tag (optional) The tag name of the elements being collected
* @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
* @param {Function} apply (optional) A function to apply to each element when found
* @return {Array} An array of elements that have the given class name
*/
getElementsByClassName: function(className, tag, root, apply) {
tag = tag || '*';
root = (root) ? Y.Dom.get(root) : null || document;
if (!root) {
return [];
}
var nodes = [],
elements = root.getElementsByTagName(tag),
re = getClassRegEx(className);
for (var i = 0, len = elements.length; i < len; ++i) {
if ( re.test(elements[i].className) ) {
nodes[nodes.length] = elements[i];
if (apply) {
apply.call(elements[i], elements[i]);
}
}
}
return nodes;
},
/**
* Determines whether an HTMLElement has the given className.
* @method hasClass
* @param {String | HTMLElement | Array} el The element or collection to test
* @param {String} className the class name to search for
* @return {Boolean | Array} A boolean value or array of boolean values
*/
hasClass: function(el, className) {
var re = getClassRegEx(className);
var f = function(el) {
pega.log('hasClass returning ' + re.test(el.className), 'info', 'Dom');
return re.test(el.className);
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Adds a class name to a given element or collection of elements.
* @method addClass
* @param {String | HTMLElement | Array} el The element or collection to add the class to
* @param {String} className the class name to add to the class attribute
* @return {Boolean | Array} A pass/fail boolean or array of booleans
*/
addClass: function(el, className) {
var f = function(el) {
if (this.hasClass(el, className)) {
return false; // already present
}
pega.log('addClass adding ' + className, 'info', 'Dom');
el.className = pega.lang.trim([el.className, className].join(' '));
return true;
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Removes a class name from a given element or collection of elements.
* @method removeClass
* @param {String | HTMLElement | Array} el The element or collection to remove the class from
* @param {String} className the class name to remove from the class attribute
* @return {Boolean | Array} A pass/fail boolean or array of booleans
*/
removeClass: function(el, className) {
var re = getClassRegEx(className);
var f = function(el) {
if (!className || !this.hasClass(el, className)) {
return false; // not present
}
pega.log('removeClass removing ' + className, 'info', 'Dom');
var c = el.className;
el.className = c.replace(re, ' ');
if ( this.hasClass(el, className) ) { // in case of multiple adjacent
this.removeClass(el, className);
}
el.className = pega.lang.trim(el.className); // remove any trailing spaces
return true;
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Replace a class with another class for a given element or collection of elements.
* If no oldClassName is present, the newClassName is simply added.
* @method replaceClass
* @param {String | HTMLElement | Array} el The element or collection to remove the class from
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
* @return {Boolean | Array} A pass/fail boolean or array of booleans
*/
replaceClass: function(el, oldClassName, newClassName) {
if (!newClassName || oldClassName === newClassName) { // avoid infinite loop
return false;
}
var re = getClassRegEx(oldClassName);
var f = function(el) {
pega.log('replaceClass replacing ' + oldClassName + ' with ' + newClassName, 'info', 'Dom');
if ( !this.hasClass(el, oldClassName) ) {
this.addClass(el, newClassName); // just add it if nothing to replace
return true; // NOTE: return
}
el.className = el.className.replace(re, ' ' + newClassName + ' ');
if ( this.hasClass(el, oldClassName) ) { // in case of multiple adjacent
this.replaceClass(el, oldClassName, newClassName);
}
el.className = pega.lang.trim(el.className); // remove any trailing spaces
return true;
};
return Y.Dom.batch(el, f, Y.Dom, true);
},
/**
* Returns an ID and applies it to the element "el", if provided.
* @method generateId
* @param {String | HTMLElement | Array} el (optional) An optional element array of elements to add an ID to (no ID is added if one is already present).
* @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
* @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
*/
generateId: function(el, prefix) {
prefix = prefix || 'yui-gen';
var f = function(el) {
if (el && el.id) { // do not override existing ID
pega.log('generateId returning existing id ' + el.id, 'info', 'Dom');
return el.id;
}
var id = prefix + pega.env._id_counter++;
pega.log('generateId generating ' + id, 'info', 'Dom');
if (el) {
el.id = id;
}
return id;
};
// batch fails when no element, so just generate and return single ID
return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
},
/**
* Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
* @method isAncestor
* @param {String | HTMLElement} haystack The possible ancestor
* @param {String | HTMLElement} needle The possible descendent
* @return {Boolean} Whether or not the haystack is an ancestor of needle
*/
isAncestor: function(haystack, needle) {
haystack = Y.Dom.get(haystack);
needle = Y.Dom.get(needle);
if (!haystack || !needle) {
return false;
}
if (haystack.contains && needle.nodeType && !isSafari) { // safari contains is broken
pega.log('isAncestor returning ' + haystack.contains(needle), 'info', 'Dom');
return haystack.contains(needle);
}
else if ( haystack.compareDocumentPosition && needle.nodeType ) {
pega.log('isAncestor returning ' + !!(haystack.compareDocumentPosition(needle) & 16), 'info', 'Dom');
return !!(haystack.compareDocumentPosition(needle) & 16);
} else if (needle.nodeType) {
// fallback to crawling up (safari)
return !!this.getAncestorBy(needle, function(el) {
return el == haystack;
});
}
pega.log('isAncestor failed; most likely needle is not an HTMLElement', 'error', 'Dom');
return false;
},
/**
* Determines whether an HTMLElement is present in the current document.
* @method inDocument
* @param {String | HTMLElement} el The element to search for
* @return {Boolean} Whether or not the element is present in the current document
*/
inDocument: function(el) {
return this.isAncestor(document.documentElement, el);
},
/**
* Returns a array of HTMLElements that pass the test applied by supplied boolean method.
* For optimized performance, include a tag and/or root node when possible.
* @method getElementsBy
* @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
* @param {String} tag (optional) The tag name of the elements being collected
* @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
* @param {Function} apply (optional) A function to apply to each element when found
* @return {Array} Array of HTMLElements
*/
getElementsBy: function(method, tag, root, apply) {
tag = tag || '*';
root = (root) ? Y.Dom.get(root) : null || document;
if (!root) {
return [];
}
var nodes = [],
elements = root.getElementsByTagName(tag);
for (var i = 0, len = elements.length; i < len; ++i) {
if ( method(elements[i]) ) {
nodes[nodes.length] = elements[i];
if (apply) {
apply(elements[i]);
}
}
}
pega.log('getElementsBy returning ' + nodes, 'info', 'Dom');
return nodes;
},
/**
* Runs the supplied method against each item in the Collection/Array.
* The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
* @method batch
* @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
* @param {Function} method The method to apply to the element(s)
* @param {Any} o (optional) An optional arg that is passed to the supplied method
* @param {Boolean} override (optional) Whether or not to override the scope of "method" with "o"
* @return {Any | Array} The return value(s) from the supplied method
*/
batch: function(el, method, o, override) {
el = (el && (el.tagName || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
if (!el || !method) {
pega.log('batch failed: invalid arguments', 'error', 'Dom');
return false;
}
var scope = (override) ? o : window;
if (el.tagName || el.length === undefined) { // element or not array-like
return method.call(scope, el, o);
}
var collection = [];
for (var i = 0, len = el.length; i < len; ++i) {
collection[collection.length] = method.call(scope, el[i], o);
}
return collection;
},
/**
* Returns the height of the document.
* @method getDocumentHeight
* @return {Int} The height of the actual document (which includes the body and its margin).
*/
getDocumentHeight: function() {
var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;
var h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
pega.log('getDocumentHeight returning ' + h, 'info', 'Dom');
return h;
},
/**
* Returns the width of the document.
* @method getDocumentWidth
* @return {Int} The width of the actual document (which includes the body and its margin).
*/
getDocumentWidth: function() {
var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
var w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
pega.log('getDocumentWidth returning ' + w, 'info', 'Dom');
return w;
},
/**
* Returns the current height of the viewport.
* @method getViewportHeight
* @return {Int} The height of the viewable area of the page (excludes scrollbars).
*/
getViewportHeight: function() {
var height = self.innerHeight; // Safari, Opera
var mode = document.compatMode;
if ( (mode || isIE) && !isOpera ) { // IE, Gecko
height = (mode == 'CSS1Compat') ?
document.documentElement.clientHeight : // Standards
document.body.clientHeight; // Quirks
}
pega.log('getViewportHeight returning ' + height, 'info', 'Dom');
return height;
},
/**
* Returns the current width of the viewport.
* @method getViewportWidth
* @return {Int} The width of the viewable area of the page (excludes scrollbars).
*/
getViewportWidth: function() {
var width = self.innerWidth; // Safari
var mode = document.compatMode;
if (mode || isIE) { // IE, Gecko, Opera
width = (mode == 'CSS1Compat') ?
document.documentElement.clientWidth : // Standards
document.body.clientWidth; // Quirks
}
pega.log('getViewportWidth returning ' + width, 'info', 'Dom');
return width;
},
/**
* Returns the nearest ancestor that passes the test applied by supplied boolean method.
* For performance reasons, IDs are not accepted and argument validation omitted.
* @method getAncestorBy
* @param {HTMLElement} node The HTMLElement to use as the starting point
* @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
* @return {Object} HTMLElement or null if not found
*/
getAncestorBy: function(node, method) {
while (node = node.parentNode) { // NOTE: assignment
if ( testElement(node, method) ) {
pega.log('getAncestorBy returning ' + node, 'info', 'Dom');
return node;
}
}
pega.log('getAncestorBy returning null (no ancestor passed test)', 'error', 'Dom');
return null;
},
/**
* Returns the nearest ancestor with the given className.
* @method getAncestorByClassName
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @param {String} className
* @return {Object} HTMLElement
*/
getAncestorByClassName: function(node, className) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getAncestorByClassName failed: invalid node argument', 'error', 'Dom');
return null;
}
var method = function(el) { return Y.Dom.hasClass(el, className); };
return Y.Dom.getAncestorBy(node, method);
},
/**
* Returns the nearest ancestor with the given tagName.
* @method getAncestorByTagName
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @param {String} tagName
* @return {Object} HTMLElement
*/
getAncestorByTagName: function(node, tagName) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getAncestorByTagName failed: invalid node argument', 'error', 'Dom');
return null;
}
var method = function(el) {
return el.tagName && el.tagName.toUpperCase() == tagName.toUpperCase();
};
return Y.Dom.getAncestorBy(node, method);
},
/**
* Returns the previous sibling that is an HTMLElement.
* For performance reasons, IDs are not accepted and argument validation omitted.
* Returns the nearest HTMLElement sibling if no method provided.
* @method getPreviousSiblingBy
* @param {HTMLElement} node The HTMLElement to use as the starting point
* @param {Function} method A boolean function used to test siblings
* that receives the sibling node being tested as its only argument
* @return {Object} HTMLElement or null if not found
*/
getPreviousSiblingBy: function(node, method) {
while (node) {
node = node.previousSibling;
if ( testElement(node, method) ) {
return node;
}
}
return null;
},
/**
* Returns the previous sibling that is an HTMLElement
* @method getPreviousSibling
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @return {Object} HTMLElement or null if not found
*/
getPreviousSibling: function(node) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getPreviousSibling failed: invalid node argument', 'error', 'Dom');
return null;
}
return Y.Dom.getPreviousSiblingBy(node);
},
/**
* Returns the next HTMLElement sibling that passes the boolean method.
* For performance reasons, IDs are not accepted and argument validation omitted.
* Returns the nearest HTMLElement sibling if no method provided.
* @method getNextSiblingBy
* @param {HTMLElement} node The HTMLElement to use as the starting point
* @param {Function} method A boolean function used to test siblings
* that receives the sibling node being tested as its only argument
* @return {Object} HTMLElement or null if not found
*/
getNextSiblingBy: function(node, method) {
while (node) {
node = node.nextSibling;
if ( testElement(node, method) ) {
return node;
}
}
return null;
},
/**
* Returns the next sibling that is an HTMLElement
* @method getNextSibling
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @return {Object} HTMLElement or null if not found
*/
getNextSibling: function(node) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getNextSibling failed: invalid node argument', 'error', 'Dom');
return null;
}
return Y.Dom.getNextSiblingBy(node);
},
/**
* Returns the first HTMLElement child that passes the test method.
* @method getFirstChildBy
* @param {HTMLElement} node The HTMLElement to use as the starting point
* @param {Function} method A boolean function used to test children
* that receives the node being tested as its only argument
* @return {Object} HTMLElement or null if not found
*/
getFirstChildBy: function(node, method) {
var child = ( testElement(node.firstChild, method) ) ? node.firstChild : null;
return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
},
/**
* Returns the first HTMLElement child.
* @method getFirstChild
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @return {Object} HTMLElement or null if not found
*/
getFirstChild: function(node, method) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getFirstChild failed: invalid node argument', 'error', 'Dom');
return null;
}
return Y.Dom.getFirstChildBy(node);
},
/**
* Returns the last HTMLElement child that passes the test method.
* @method getLastChildBy
* @param {HTMLElement} node The HTMLElement to use as the starting point
* @param {Function} method A boolean function used to test children
* that receives the node being tested as its only argument
* @return {Object} HTMLElement or null if not found
*/
getLastChildBy: function(node, method) {
if (!node) {
pega.log('getLastChild failed: invalid node argument', 'error', 'Dom');
return null;
}
var child = ( testElement(node.lastChild, method) ) ? node.lastChild : null;
return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
},
/**
* Returns the last HTMLElement child.
* @method getLastChild
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @return {Object} HTMLElement or null if not found
*/
getLastChild: function(node) {
node = Y.Dom.get(node);
return Y.Dom.getLastChildBy(node);
},
/**
* Returns an array of HTMLElement childNodes that pass the test method.
* @method getChildrenBy
* @param {HTMLElement} node The HTMLElement to start from
* @param {Function} method A boolean function used to test children
* that receives the node being tested as its only argument
* @return {Array} A static array of HTMLElements
*/
getChildrenBy: function(node, method) {
var child = Y.Dom.getFirstChildBy(node, method);
var children = child ? [child] : [];
Y.Dom.getNextSiblingBy(child, function(node) {
if ( !method || method(node) ) {
children[children.length] = node;
}
return false; // fail test to collect all children
});
return children;
},
/**
* Returns an array of HTMLElement childNodes.
* @method getChildren
* @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
* @return {Array} A static array of HTMLElements
*/
getChildren: function(node) {
node = Y.Dom.get(node);
if (!node) {
pega.log('getChildren failed: invalid node argument', 'error', 'Dom');
}
return Y.Dom.getChildrenBy(node);
},
/**
* Returns the left scroll value of the document
* @method getDocumentScrollLeft
* @param {HTMLDocument} document (optional) The document to get the scroll value of
* @return {Int} The amount that the document is scrolled to the left
*/
getDocumentScrollLeft: function(doc) {
doc = doc || document;
return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
},
/**
* Returns the top scroll value of the document
* @method getDocumentScrollTop
* @param {HTMLDocument} document (optional) The document to get the scroll value of
* @return {Int} The amount that the document is scrolled to the top
*/
getDocumentScrollTop: function(doc) {
doc = doc || document;
return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
},
/**
* Inserts the new node as the previous sibling of the reference node
* @method insertBefore
* @param {String | HTMLElement} newNode The node to be inserted
* @param {String | HTMLElement} referenceNode The node to insert the new node before
* @return {HTMLElement} The node that was inserted (or null if insert fails)
*/
insertBefore: function(newNode, referenceNode) {
newNode = Y.Dom.get(newNode);
referenceNode = Y.Dom.get(referenceNode);
if (!newNode || !referenceNode || !referenceNode.parentNode) {
pega.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
return null;
}
return referenceNode.parentNode.insertBefore(newNode, referenceNode);
},
/**
* Inserts the new node as the next sibling of the reference node
* @method insertAfter
* @param {String | HTMLElement} newNode The node to be inserted
* @param {String | HTMLElement} referenceNode The node to insert the new node after
* @return {HTMLElement} The node that was inserted (or null if insert fails)
*/
insertAfter: function(newNode, referenceNode) {
newNode = Y.Dom.get(newNode);
referenceNode = Y.Dom.get(referenceNode);
if (!newNode || !referenceNode || !referenceNode.parentNode) {
pega.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
return null;
}
if (referenceNode.nextSibling) {