-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpablo.js
1123 lines (915 loc) · 35.6 KB
/
pablo.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
/*!
Pablo <http://pablojs.com>
by Premasagar Rose <http://premasagar.com>,
Dharmafly <http://dharmafly.com>
Repo: <https://github.com/dharmafly/pablo>
MIT license: http://opensource.org/licenses/mit-license.php
*/
/*jshint newcap:false */
var Pablo = (function(document, Array, Element, SVGElement, NodeList, HTMLDocument){
'use strict';
var /* SETTINGS */
pabloVersion = '0.2.4',
svgVersion = 1.1,
svgns = 'http://www.w3.org/2000/svg',
xlinkns = 'http://www.w3.org/1999/xlink',
vendorPrefixes = ['', '-moz-', '-webkit-', '-khtml-', '-o-', '-ms-'],
testElement, arrayProto, supportsClassList, hyphensToCamelCase, cssClassApi, pabloCollectionApi;
function make(elementName){
return typeof elementName === 'string' &&
document.createElementNS(svgns, elementName) ||
null;
}
/////
// TEST BROWSER COMPATIBILITY
testElement = document && document.createElementNS && make('svg');
// Incompatible browser
if (!(
testElement && testElement.createSVGRect &&
document.querySelectorAll &&
Array && Array.isArray && Array.prototype.forEach &&
Element && SVGElement && NodeList && HTMLDocument &&
document.body.children && document.body.previousElementSibling
)){
// Return a simplified version of the Pablo API
return {
v: pabloVersion,
isSupported: false
};
}
supportsClassList = !!testElement.classList;
arrayProto = Array.prototype;
/////
// UTILITIES
function extend(target/*, any number of source objects*/){
var len = arguments.length,
withPrototype = arguments[len-1] === true,
i, obj, prop;
if (!target){
target = {};
}
for (i = 1; i < len; i++){
obj = arguments[i];
if (typeof obj === 'object'){
for (prop in obj){
if (withPrototype || obj.hasOwnProperty(prop)){
target[prop] = obj[prop];
}
}
}
}
return target;
}
function toArray(obj){
return arrayProto.slice.call(obj);
}
function getAttributes(el){
var ret = {},
attr, len, i;
if (el){
attr = el.attributes;
for (i = 0, len = attr.length; i<len; i++){
ret[attr[i].name] = attr[i].value;
}
}
return ret;
}
function isArrayLike(obj){
return !!(obj && typeof obj === 'object' && typeof obj.length === 'number');
}
function isElement(obj){
return obj instanceof Element;
}
function isNodeList(obj){
return obj instanceof NodeList;
}
function isHTMLDocument(obj){
return obj instanceof HTMLDocument;
}
function hasSvgNamespace(obj){
return obj && obj.namespaceURI && obj.namespaceURI === svgns;
}
function isSvg(obj){
return obj instanceof SVGElement;
}
function canBeWrapped(obj){
return Pablo.isPablo(obj) ||
isElement(obj) ||
isNodeList(obj) ||
isHTMLDocument(obj) ||
Array.isArray(obj) ||
isArrayLike(obj) ||
hasSvgNamespace(obj);
}
// Return node (with attributes) if a Pablo collection, otherwise create one
function toPablo(node, attr){
if (Pablo.isPablo(node)){
return attr ? node.attr(attr) : node;
}
return Pablo(node, attr);
}
function addElementIfUnique(node, collection, prepend){
var toPush;
// Create new element from elementName
if (typeof node === 'string'){
node = make(node);
}
// Is an existing element; check if already in collection
else if (isElement(node) || isHTMLDocument(node) || hasSvgNamespace(node)){
if (collection.indexOf(node) >= 0){
return;
}
// If the element is not yet in the collection, it will be added below
}
// Probably some kind of list of elements
else {
// A Pablo collection
if (Pablo.isPablo(node)){
// See extensions/functional.js for example usage of node.collection
// The check for node.collection is for extenstions/functional.js
toPush = node.collection || node;
}
// An array of elements
else if (Array.isArray(node)){
toPush = node;
}
// A nodeList (e.g. result of a selector query, or childNodes)
// or is an object like an array, e.g. a jQuery collection
else if (isNodeList(node) || isArrayLike(node)){
toPush = toArray(node);
}
// Whatever it is, it isn't supported
else {
return;
}
// Add each element in the list
return toPush.forEach(function(el){
addElementIfUnique(el, collection);
});
}
// Add element to collection
arrayProto[prepend ? 'unshift' : 'push'].call(collection, node);
}
// Return CSS styles with browser vendor prefixes
// e.g. cssPrefix({transform:'rotate(45deg)'}) will return the styles object, with additional properties containing CSS properties prefixed with the browser vendor prefixes - see vendorPrefixes
// e.g. cssPrefix('transform', 'rotate(45deg)') will return a string sequence of prefixed CSS properties, each assigned the same value
// e.g. cssPrefix('transform') will return a string sequence of CSS properties
function cssPrefix(styles, value){
var vendorPrefixes = Pablo.vendorPrefixes,
prop, res, rule, setStyle;
if (typeof styles === 'object'){
setStyle = function(prefix){
res[prefix + prop] = styles[prop];
};
res = {};
for (prop in styles){
if (styles.hasOwnProperty(prop)){
vendorPrefixes.forEach(setStyle);
}
}
}
if (typeof styles === 'string'){
prop = styles;
// e.g. cssPrefix('transform') -> 'transform,-webkit-transform,...'
// useful for adding prefixed properties when setting active properties in a CSS transition
if (typeof value === 'undefined'){
res = vendorPrefixes.join(prop + ',') + prop;
}
// e.g. cssPrefix('transform', 'rotate(45deg)') -> 'transform:rotate(45deg);-webkit-transform:rotate(45deg);...'
else {
rule = prop + ':' + value + ';';
res = vendorPrefixes.join(rule) + rule;
}
}
return res;
}
// e.g. 'font-color' -> 'fontColor'
hyphensToCamelCase = (function(){
var firstLetter = /-([a-z])/g;
return function (str){
return str.replace(firstLetter, function(match, letter){
return letter.toUpperCase();
});
};
}());
/*
// e.g. 'fontColor' -> 'font-color'
// NOTE: does not check for blank spaces, i.e. for multiple words 'font Color'
var camelCaseToHyphens = (function(){
var capitalLetters = /[A-Z]/g;
return function (str){
return str.replace(capitalLetters, function(letter){
return '-' + letter.toLowerCase();
});
};
});
*/
// Determine a value passed to attr(), css(), content()
function getValue(val, el, i, collection){
if (typeof val === 'function'){
val = val.call(collection, el, i);
}
else if (Array.isArray(val)){
val = val[i];
}
return val;
}
// `behaviour` can be 'some', 'every' or 'filter'
function matchSelectors(collection, selectors, behaviour){
var i, len, node, ancestor, ancestors, matches, matchesCache, ancestorsLength, isMatch, filtered;
if (behaviour === 'filter'){
filtered = Pablo();
}
for (i=0, len=collection.length; i<len; i++){
node = collection.eq(i);
// Get the root ancestor
// `null` is passed as the 2nd arg to parents() to include `document` in ancestors
ancestor = node.parents(null, null).last();
// Use ancestor to find element via a selector query
if (ancestor.length){
// Have we previously cached the result of the query?
matches = matchesCache && matchesCache[ancestors.indexOf(ancestor)];
if (!matches){
matches = ancestor.find(selectors);
// If more than one element in the collection, then
// we cache the result of the query
if (len > 1){
// Create the cache containers
if (!matchesCache){
ancestors = [];
matchesCache = [];
}
// Cache the result
ancestorsLength = ancestors.push(ancestor);
matchesCache[ancestorsLength-1] = matches;
}
}
}
// Element has no parent
// If it is an element, clone it and append to a temporary element
else if (!isElement(node[0])) {
node = node.clone();
ancestor = Pablo.g().append(node);
matches = ancestor.find(selectors);
}
isMatch = matches && matches.indexOf(node) >= 0;
if (isMatch){
if (behaviour === 'some'){
return true;
}
if (behaviour === 'filter'){
filtered.push(node);
}
}
else if (behaviour === 'every'){
return false;
}
}
// If filtering, return the collection; other boolean
return behaviour === 'filter' ? filtered :
(behaviour === 'every' ? true : false);
}
function traverse(prop){
return function(selectors){
var collection = this.map(function(el){
return el[prop];
});
return selectors ? collection.select(selectors) : collection;
};
}
/////
// ELEMENT API
function PabloCollection(node, attr){
if (node){
this.push(node);
// Apply attributes
if (attr){
this.attr(attr);
}
}
}
// Node prototype
pabloCollectionApi = PabloCollection.prototype = [];
extend(pabloCollectionApi, {
collection: null,
constructor: PabloCollection,
pablo: pabloVersion,
get: function(index){
return this[index];
},
toArray: function(){
return toArray(this);
},
eq: function(index){
return index >= 0 ?
// Return zero-indexed node
Pablo(this[index]) :
// Return node, counting backwards from end of elements array
(index < -1 ? this.slice(index, index + 1) : this.slice(index));
},
size: function(){
return this.length;
},
first: function(){
return this.eq(0);
},
last: function(){
return this.eq(this.length-1);
},
// Add new node(s) to the collection; accepts arrays or nodeLists
push: function(node){
addElementIfUnique(node, this);
return this;
},
// Add new node(s) to the collection; accepts arrays or nodeLists
unshift: function(node){
addElementIfUnique(node, this, true);
return this;
},
// Remove node from end of the collection
pop: function(){
return Pablo(arrayProto.pop.call(this));
},
shift: function(){
return Pablo(arrayProto.shift.call(this));
},
slice: function(begin, end){
return Pablo(arrayProto.slice.call(this, begin, end));
},
reverse: function(){
arrayProto.reverse.call(this);
return this;
},
sort: function(fn){
arrayProto.sort.call(this, fn);
return this;
},
each: function(fn){
arrayProto.forEach.call(this, fn, this);
return this;
},
map: function(fn){
return Pablo(arrayProto.map.call(this, fn));
},
some: function(fnOrSelector){
return typeof fnOrSelector === 'string' ?
matchSelectors(this, fnOrSelector, 'some') :
arrayProto.some.call(this, fnOrSelector);
},
every: function(fnOrSelector){
return typeof fnOrSelector === 'string' ?
matchSelectors(this, fnOrSelector, 'every') :
arrayProto.every.call(this, fnOrSelector);
},
// Note: this method is analogous to Array.filter but is called `select`
// here (as in Underscore.js) because Pablo's filter() method is used to
// create a `<filter>` SVG element
select: function(fnOrSelector){
return typeof fnOrSelector === 'string' ?
matchSelectors(this, fnOrSelector, 'filter') :
Pablo(arrayProto.filter.call(this, fnOrSelector));
},
indexOf: function(element){
if (Pablo.isPablo(element)){
element = element[0];
}
return arrayProto.indexOf.call(this, element);
},
/////
// TRAVERSAL
children: traverse('childNodes'),
next: traverse('nextElementSibling'),
prev: traverse('previousElementSibling'),
parent: traverse('parentNode'),
// NOTE: `parent` and `parents` matches jQuery's behaviour:
// the former allows `document` in the response; the latter excludes it.
// `checkType` is for internal use; it is a comparison function and stops
// the loop when it returns false; see parentsSvg() and matchSelectors()
parents: function(selectors, checkType){
var parents = Pablo();
if (typeof checkType === 'undefined'){
checkType = isElement;
}
this.each(function(el){
el = el.parentNode;
// Check element has parent and, if `checkType` isn't falsey, use it to
// check the element's type
while (el && (checkType ? checkType(el) : true)){
parents.push(el);
el = el.parentNode;
}
});
return selectors ? parents.select(selectors) : parents;
},
// Same as parents() but only returns SVG nodes
parentsSvg: function(selectors){
return this.parents(selectors, isSvg);
},
// Find each element's SVG root element
root: function(selectors){
// Find the SVG element ancestors and return those that have a non-SVG parent
return this.parentsSvg(selectors).select(function(el){
return !isSvg(el.parentNode);
});
},
siblings: function(selectors){
var siblings = Pablo();
this.each(function(el){
siblings.push(
Pablo(el).parent().children().select(function(child){
return el !== child;
})
);
});
return selectors ? siblings.select(selectors) : siblings;
},
find: function(selectors){
var found = Pablo();
this.each(function(el){
found.push(el.querySelectorAll(selectors));
});
return found;
},
/////
// MANIPULATION
empty: function(){
return this.each(function(el){
while (el.firstChild) {
el.removeChild(el.firstChild);
}
});
},
remove: function(){
return this.each(function(el){
var parentNode = el.parentNode;
if (parentNode){
parentNode.removeChild(el);
}
});
},
// NOTE: the following append-related functions all require attr to exist, even as a blank object, if a new element is to be created. Otherwise, if the first argument is a string, then a selection operation will be performed.
// TODO: If appending pre-existing elements, then append to just first element in the collection?
append: function(node, attr){
this.each(function(el){
toPablo(node, attr).each(function(child){
el.appendChild(child);
});
});
return this;
},
appendTo: function(node, attr){
toPablo(node, attr).append(this);
return this;
},
/*
child: function(node, attr){
return toPablo(node, attr).appendTo(this);
},
*/
before: function(node, attr){
return this.each(function(el){
var parentNode = el.parentNode;
if (parentNode){
toPablo(node, attr).each(function(toInsert){
parentNode.insertBefore(toInsert, el);
});
}
});
},
after: function(node, attr){
return this.each(function(el){
var parentNode = el.parentNode;
if (parentNode){
toPablo(node, attr).each(function(toInsert){
parentNode.insertBefore(toInsert, el.nextSibling);
});
}
});
},
// Insert every element in the set of matched elements after the target.
insertAfter: function(node, attr){
toPablo(node, attr).after(this);
return this;
},
// Insert every element in the set of matched elements before the target.
insertBefore: function(node, attr){
toPablo(node, attr).before(this);
return this;
},
prepend: function(node, attr){
return this.each(function(el){
var first = el.firstChild;
toPablo(node, attr).each(function(child){
el.insertBefore(child, first);
});
});
},
prependTo: function(node, attr){
toPablo(node, attr).prepend(this);
return this;
},
clone: function(deep){
deep = (deep || false);
return this.map(function(el){
return el.cloneNode(deep);
});
},
duplicate: function(repeats){
var duplicates;
if (repeats !== 0){
duplicates = Pablo();
if (typeof repeats !== 'number' || repeats < 0){
repeats = 1;
}
// Clone the collection
while (repeats --){
duplicates.push(this.clone(true)[0]);
}
// Insert in the DOM after the collection
this.after(duplicates)
// Add new elements the collection
.push(duplicates);
}
return this;
},
attr: function(attr, value){
var el, attributeName, colonIndex, nsPrefix, nsURI;
if (typeof attr === 'undefined'){
return getAttributes(this[0]);
}
if (typeof attr === 'string'){
// Get attribute
if (typeof value === 'undefined'){
el = this[0];
// Namespaced attributes
colonIndex = attr.indexOf(':');
if (colonIndex >= 0){
nsPrefix = attr.slice(0, colonIndex);
nsURI = Pablo.ns[nsPrefix];
attr = attr.slice(colonIndex+1);
}
return el && el.getAttributeNS(nsURI || null, attr);
}
// Create attributes object
attributeName = attr;
attr = {};
attr[attributeName] = value;
}
// Set multiple attributes
this.each(function(el, i){
var prop, val;
for (prop in attr){
if (attr.hasOwnProperty(prop)){
val = getValue(attr[prop], el, i, this);
// Namespaced attributes, e.g. 'xlink:href'
colonIndex = prop.indexOf(':');
if (colonIndex >= 0){
nsPrefix = prop.slice(0, colonIndex);
nsURI = Pablo.ns[nsPrefix];
}
el.setAttributeNS(nsURI || null, prop, val);
}
}
});
return this;
},
/*
pluck: function(prop){
return toArray(this).map(function(el){
return Pablo(el).attr(prop) || el[prop];
});
},
*/
transform: function(functionName, value/* , additional values*/){
// Additional arguments are space-delimited as part of the value
if (arguments.length > 2){
value = toArray(arguments).slice(1).join(' ');
}
return this.each(function(el, i){
var node = Pablo(el),
transformAttr = node.attr('transform'),
newTransformAttr, pos, posEnd, transformAttrEnd,
functionString = functionName + '(' + getValue(value, el, i, this) + ')';
// There's already a transform attribute
if (transformAttr){
// Start position for the function
pos = (' ' + transformAttr).indexOf(' ' + functionName + '(');
// Function name already present
if (pos >= 0){
transformAttrEnd = transformAttr.slice(pos);
// End position for the function
posEnd = transformAttrEnd.indexOf(')');
// Insert modified function
// TODO: use splice() instead?
newTransformAttr = transformAttr.slice(0, pos) +
functionString + transformAttrEnd.slice(posEnd + 1);
}
// Function not yet present
else {
newTransformAttr = transformAttr + ' ' + functionString;
}
}
// Set transform attribute
node.attr('transform', newTransformAttr || functionString);
});
},
removeAttr: function (attr) {
var colonIndex = attr.indexOf(':'),
nsPrefix, nsURI;
if (colonIndex >= 0){
nsPrefix = attr.slice(0, colonIndex);
nsURI = Pablo.ns[nsPrefix];
attr = attr.slice(colonIndex+1);
}
return this.each(function (el){
el.removeAttributeNS(nsURI || null, attr);
});
},
content: function(text){
var el;
// Get first element's textContent
if (typeof text === 'undefined'){
el = this[0];
return el && el.textContent || '';
}
// Set every element's textContent
return this.each(function(el, i){
el.textContent = getValue(text, el, i, this);
});
},
css: function(styles, value){
var el, styleProperty;
if (typeof styles === 'string'){
// Get style
if (typeof value === 'undefined'){
el = this.get(0);
return el && el.style.getPropertyValue(styles);
// or document.defaultView.getComputedStyle(el, null).getPropertyValue(styles);
}
// Create styles object
styleProperty = styles;
styles = {};
styles[styleProperty] = value;
}
return this.each(function(el, i){
var style = el.style,
prop, val;
for (prop in styles){
if (styles.hasOwnProperty(prop)){
val = getValue(styles[prop], el, i, this);
style.setProperty(prop, val, '');
}
}
});
},
// Add prefixed CSS styles to elements in collection
cssPrefix: function(styles, value){
var styleProperty;
if (typeof styles === 'string' && typeof value !== 'undefined'){
// Create styles object
styleProperty = styles;
styles = {};
styles[styleProperty] = value;
}
return this.css(cssPrefix(styles));
}
});
/////
// DOM EVENT METHODS
(function(){
// Allow either single or multiple events to be triggered
function eventMethod(method){
return function(type, listener, useCapture){
// Multiple events
if (type.indexOf(' ') > 0){
type.split(' ').forEach(function(type){
method.call(this, type, listener, useCapture);
}, this);
}
// Single event
else {
method.call(this, type, listener, useCapture);
}
return this;
};
}
extend(pabloCollectionApi, {
on: eventMethod(function(type, listener, useCapture){
this.each(function(el){
el.addEventListener(type, listener, useCapture || false);
});
}),
off: eventMethod(function(type, listener, useCapture){
this.each(function(el){
el.removeEventListener(type, listener, useCapture || false);
});
}),
// Trigger listener once per collection
one: eventMethod(function(type, listener, useCapture){
var thisNode = this;
this.on(type, function addListener(){
// Remove listener, then trigger
thisNode.off(type, addListener, useCapture);
listener.apply(thisNode, arguments);
}, useCapture);
}),
// Trigger listener once per element in the collection
oneEach: eventMethod(function(type, listener, useCapture){
this.each(function(el){
var node = Pablo(el);
node.on(type, function addListener(){
// Remove listener, then trigger
node.off(type, addListener, useCapture);
listener.apply(node, arguments);
}, useCapture);
});
})
});
}());
// Pablo Collection API Aliases
extend(pabloCollectionApi, {
elements: pabloCollectionApi.toArray,
add: pabloCollectionApi.push,
concat: pabloCollectionApi.push,
forEach: pabloCollectionApi.each,
is: pabloCollectionApi.some
});
/////
// CSS CLASS METHODS
cssClassApi = supportsClassList ?
// Browser supports native classLists in SVG
// e.g. Firefox
{
// Return true if _any_ element has className
hasClass: function(className){
return this.some(function(el){
return el.classList.contains(className);
});
},
addClass: function(className){
return this.each(function(el){
el.classList.add(className);
});
},
removeClass: function(className){
return this.each(function(el){
el.classList.remove(className);
});
},
toggleClass: function(className){
return this.each(function(el){
el.classList.toggle(className);
});
}
} :
// Browser doesn't support native classLists in SVG
// e.g. Internet Explorer 9, Chrome 21
{
// Return true if _any_ element has className
hasClass: function(className){
return this.some(function(el){
var node = Pablo(el),
classString = node.attr('class');
return classString && (' ' + classString + ' ')
.indexOf(' ' + className + ' ') >= 0;
});
},
addClass: function(className){
return this.each(function(el){
var node = Pablo(el),
classString;
if (!node.hasClass(className)){
classString = node.attr('class');
classString = classString ? (classString + ' ') : '';
node.attr('class', classString + className);
}
});
},
removeClass: function(className){
var classPattern = new RegExp('(^|\\s)' + className + '(\\s|$)');
return this.each(function(el){
var node = Pablo(el),
classString;
if (node.hasClass(className)){
classString = node.attr('class');
classString = classString.replace(classPattern, '$2');
node.attr('class', classString);
}
});
},
toggleClass: function(className){
return this.each(function(el){
var node = Pablo(el);
if (node.hasClass(className)){
node.removeClass(className);
}
else {
node.addClass(className);
}
});
}
};
extend(pabloCollectionApi, cssClassApi);
/* For alternative implementations of class manipulation, see:
* https://gist.github.com/1319121
* https://developer.mozilla.org/media/uploads/demos/p/a/paulrouget/8bfba7f0b6c62d877a2b82dd5e10931e/hacksmozillaorg-achi_1334270447_demo_package/classList/classList.js
* https://hacks.mozilla.org/2010/01/classlist-in-firefox-3-6/