Debian Codegraph @

src/js/Rickshaw.Compat.ClassList.js

raw
1 Rickshaw.namespace('Rickshaw.Compat.ClassList');
2
3 Rickshaw.Compat.ClassList = function() {
4
5 /* adapted from http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
6
7 if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) {
8
9 (function (view) {
10
11 "use strict";
12
13 var
14 classListProp = "classList"
15 , protoProp = "prototype"
16 , elemCtrProto = (view.HTMLElement || view.Element)[protoProp]
17 , objCtr = Object
18 , strTrim = String[protoProp].trim || function () {
19 return this.replace(/^\s+|\s+$/g, "");
20 }
21 , arrIndexOf = Array[protoProp].indexOf || function (item) {
22 var
23 i = 0
24 , len = this.length
25 ;
26 for (; i < len; i++) {
27 if (i in this && this[i] === item) {
28 return i;
29 }
30 }
31 return -1;
32 }
33 // Vendors: please allow content code to instantiate DOMExceptions
34 , DOMEx = function (type, message) {
35 this.name = type;
36 this.code = DOMException[type];
37 this.message = message;
38 }
39 , checkTokenAndGetIndex = function (classList, token) {
40 if (token === "") {
41 throw new DOMEx(
42 "SYNTAX_ERR"
43 , "An invalid or illegal string was specified"
44 );
45 }
46 if (/\s/.test(token)) {
47 throw new DOMEx(
48 "INVALID_CHARACTER_ERR"
49 , "String contains an invalid character"
50 );
51 }
52 return arrIndexOf.call(classList, token);
53 }
54 , ClassList = function (elem) {
55 var
56 trimmedClasses = strTrim.call(elem.className)
57 , classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
58 , i = 0
59 , len = classes.length
60 ;
61 for (; i < len; i++) {
62 this.push(classes[i]);
63 }
64 this._updateClassName = function () {
65 elem.className = this.toString();
66 };
67 }
68 , classListProto = ClassList[protoProp] = []
69 , classListGetter = function () {
70 return new ClassList(this);
71 }
72 ;
73 // Most DOMException implementations don't allow calling DOMException's toString()
74 // on non-DOMExceptions. Error's toString() is sufficient here.
75 DOMEx[protoProp] = Error[protoProp];
76 classListProto.item = function (i) {
77 return this[i] || null;
78 };
79 classListProto.contains = function (token) {
80 token += "";
81 return checkTokenAndGetIndex(this, token) !== -1;
82 };
83 classListProto.add = function (token) {
84 token += "";
85 if (checkTokenAndGetIndex(this, token) === -1) {
86 this.push(token);
87 this._updateClassName();
88 }
89 };
90 classListProto.remove = function (token) {
91 token += "";
92 var index = checkTokenAndGetIndex(this, token);
93 if (index !== -1) {
94 this.splice(index, 1);
95 this._updateClassName();
96 }
97 };
98 classListProto.toggle = function (token) {
99 token += "";
100 if (checkTokenAndGetIndex(this, token) === -1) {
101 this.add(token);
102 } else {
103 this.remove(token);
104 }
105 };
106 classListProto.toString = function () {
107 return this.join(" ");
108 };
109
110 if (objCtr.defineProperty) {
111 var classListPropDesc = {
112 get: classListGetter
113 , enumerable: true
114 , configurable: true
115 };
116 try {
117 objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
118 } catch (ex) { // IE 8 doesn't support enumerable:true
119 if (ex.number === -0x7FF5EC54) {
120 classListPropDesc.enumerable = false;
121 objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
122 }
123 }
124 } else if (objCtr[protoProp].__defineGetter__) {
125 elemCtrProto.__defineGetter__(classListProp, classListGetter);
126 }
127
128 }(window));
129
130 }
131 };
132
133 if ( (typeof RICKSHAW_NO_COMPAT !== "undefined" && !RICKSHAW_NO_COMPAT) || typeof RICKSHAW_NO_COMPAT === "undefined") {
134 new Rickshaw.Compat.ClassList();
135 }