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 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /*global Node,Element, */ /** * @module montage/core/dom * @requires montage/core/geometry/point */ // Extend Node.prototype Eif (typeof Node !== "undefined") { var NodePrototype = Node.prototype; NodePrototype.get = function (key) { return this.getAttribute(key) || this[key]; }; NodePrototype.parentOf = function (child) { while ((child = child.parentNode) && child !== this) {} //If child is defined then we didn't walk all the way up to the root return child ? true : false; }; } // Extend Element.prototype Eif (typeof Element !== "undefined") { var ElementPrototype = Element.prototype; /** * @function external:Element#set * @param {string} string * @param {string} string * @param {number} string */ Object.defineProperty(ElementPrototype, "set", { value: function (aPropertyPath, value, currentIndex) { var dotIndex = aPropertyPath.indexOf(".", currentIndex), result, currentKeyComponent, indexEnd, styleKey, classKey; currentIndex = currentIndex || 0; currentKeyComponent = aPropertyPath.substring(currentIndex, (dotIndex === -1 ? aPropertyPath.length : dotIndex)); if (dotIndex === -1) { //This is only using properties. this.setAttribute(currentKeyComponent, value); } else { indexEnd = aPropertyPath.lastIndexOf("."); if (currentKeyComponent === "style") { styleKey = aPropertyPath.substring(dotIndex + 1, aPropertyPath.length); this.style[styleKey] = value; } else if (currentKeyComponent === "classList") { classKey = aPropertyPath.substring(dotIndex + 1, aPropertyPath.length); if (value) { this.classList.add(classKey); } else { this.classList.remove(classKey); } } else if ((result = this.get(aPropertyPath.substring(0, indexEnd)))) { (result[aPropertyPath.substring(indexEnd + 1, aPropertyPath.length)] = value); } } }, enumerable: false }); var classListProp = "classList"; /** * @function external:Element#set * classList.js * * Implements a cross-browser element.classList getter. */ Eif (!ElementPrototype.hasOwnProperty(classListProp)) { var trim = /^\s+|\s+$/g, setClasses = function (elem, classes) { elem.setAttribute("class", classes.join(" ")); }, checkAndGetIndex = function (classes, token) { if (token === "") { throw "SYNTAX_ERR"; } if (/\s/.test(token)) { throw "INVALID_CHARACTER_ERR"; } return classes.indexOf(token); }, classListGetter = function () { var elem = this, classes = elem.getAttribute("class") || ""; classes = classes.replace(trim, "").split(/\s+/); return { length: classes.length, item: function (i) { return classes[i] || null; }, contains: function (token) { return checkAndGetIndex(classes, token) !== -1; }, add: function (token) { if (checkAndGetIndex(classes, token) === -1) { classes.push(token); this.length = classes.length; setClasses(elem, classes); } }, remove: function (token) { var index = checkAndGetIndex(classes, token); if (index !== -1) { classes.splice(index, 1); this.length = classes.length; setClasses(elem, classes); } }, toggle: function (token) { if (checkAndGetIndex(classes, token) === -1) { this.add(token); } else { this.remove(token); } }, toString: function () { return (elem.getAttribute("class") || ""); } }; }; Eif (Object.defineProperty) { Object.defineProperty(ElementPrototype, classListProp, { get: classListGetter, enumerable: true }); } else if (Object.prototype.__defineGetter__) { ElementPrototype.__defineGetter__(classListProp, classListGetter); } } } // Extend Node.prototype Eif (typeof Node !== "undefined") { var DocumentPrototype = Object.getPrototypeOf(document); DocumentPrototype.addRule = function (selectorText, definition) { var styleSheet, cssRule; if ((styleSheet = document.styleSheets[0]) == null) { var style = document.createElement("style"); style.type = "text/css"; document.head.appendChild(style); styleSheet = document.styleSheets[0]; } else { cssRule = document.getRule(selectorText, styleSheet); } if (!cssRule) { styleSheet.insertRule(selectorText + " " + definition, styleSheet.cssRules.length); } }; DocumentPrototype.getRule = function (ruleKey, styleSheet) { var cssRule; if (styleSheet.cssRules) { for (var j = 0; (cssRule = styleSheet.cssRules[j]); j++) { if (cssRule.name && cssRule.name === ruleKey) { // keyframe animation return cssRule; } else if (cssRule.selectorText === ruleKey) { return cssRule; } } } }; } |