diff options
Diffstat (limited to 'node_modules/montage-user/ui/dom.js')
-rwxr-xr-x | node_modules/montage-user/ui/dom.js | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/node_modules/montage-user/ui/dom.js b/node_modules/montage-user/ui/dom.js new file mode 100755 index 00000000..f629ff18 --- /dev/null +++ b/node_modules/montage-user/ui/dom.js | |||
@@ -0,0 +1,278 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /** | ||
8 | Provides DOM | ||
9 | @module montage/ui/dom | ||
10 | @requires montage/core/geometry/point | ||
11 | */ | ||
12 | |||
13 | var Point = require("core/geometry/point").Point, | ||
14 | NodePrototype = Node.prototype, | ||
15 | ElementPrototype = Element.prototype; | ||
16 | |||
17 | /** | ||
18 | @function external:Element#set | ||
19 | @param {type-TODO} string | ||
20 | @param {type-TODO} string | ||
21 | @param {type-TODO} string | ||
22 | */ | ||
23 | Object.defineProperty(ElementPrototype, "set", { | ||
24 | value: function(aPropertyPath, value, currentIndex) { | ||
25 | var dotIndex = aPropertyPath.indexOf(".", currentIndex), | ||
26 | result, | ||
27 | currentKeyComponent, | ||
28 | indexEnd, | ||
29 | styleKey, | ||
30 | classKey; | ||
31 | |||
32 | currentIndex = currentIndex || 0; | ||
33 | |||
34 | currentKeyComponent = aPropertyPath.substring(currentIndex, (dotIndex === -1 ? aPropertyPath.length : dotIndex)); | ||
35 | |||
36 | if (dotIndex === -1) { | ||
37 | //This is only using properties. | ||
38 | this.setAttribute(currentKeyComponent, value); | ||
39 | } | ||
40 | else { | ||
41 | indexEnd = aPropertyPath.lastIndexOf("."); | ||
42 | if (currentKeyComponent === "style") { | ||
43 | styleKey = aPropertyPath.substring(dotIndex + 1, aPropertyPath.length); | ||
44 | this.style[styleKey] = value; | ||
45 | } else if (currentKeyComponent === "classList") { | ||
46 | classKey = aPropertyPath.substring(dotIndex + 1, aPropertyPath.length); | ||
47 | if (value) { | ||
48 | this.classList.add(classKey); | ||
49 | } else { | ||
50 | this.classList.remove(classKey); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | else if ((result = this.get(aPropertyPath.substring(0, indexEnd)))) { | ||
55 | (result[aPropertyPath.substring(indexEnd + 1, aPropertyPath.length)] = value); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | }, | ||
60 | enumerable: false | ||
61 | }); | ||
62 | |||
63 | NodePrototype.get = function(key) { | ||
64 | return this.getAttribute(key) || this[key]; | ||
65 | }; | ||
66 | |||
67 | Object.getPrototypeOf(document).addRule = function(selectorText, definition) { | ||
68 | var styleSheet, cssRule; | ||
69 | if ((styleSheet = document.styleSheets[0]) == null) { | ||
70 | var style = document.createElement("style"); | ||
71 | style.type = "text/css"; | ||
72 | document.head.appendChild(style); | ||
73 | styleSheet = document.styleSheets[0]; | ||
74 | } else { | ||
75 | cssRule = document.getRule(selectorText, styleSheet); | ||
76 | } | ||
77 | if (!cssRule) { | ||
78 | styleSheet.insertRule(selectorText + " " + definition, styleSheet.cssRules.length); | ||
79 | } | ||
80 | }; | ||
81 | |||
82 | Object.getPrototypeOf(document).getRule = function(ruleKey, styleSheet) { | ||
83 | var cssRule; | ||
84 | if (styleSheet.cssRules) { | ||
85 | for (var j = 0; (cssRule = styleSheet.cssRules[j]); j++) { | ||
86 | if (cssRule.name && cssRule.name === ruleKey) { | ||
87 | // keyframe animation | ||
88 | return cssRule; | ||
89 | } else if (cssRule.selectorText === ruleKey) { | ||
90 | return cssRule; | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | }; | ||
95 | |||
96 | /* | ||
97 | * classList.js | ||
98 | * | ||
99 | * Implements a cross-browser element.classList getter. | ||
100 | * By Eli Grey, http://eligrey.com | ||
101 | */ | ||
102 | |||
103 | "use strict"; | ||
104 | |||
105 | if (typeof Element !== "undefined") { | ||
106 | |||
107 | (function () { | ||
108 | |||
109 | var classListProp = "classList"; | ||
110 | |||
111 | if (!Element.prototype.hasOwnProperty(classListProp)) { | ||
112 | var trim = /^\s+|\s+$/g, | ||
113 | setClasses = function (elem, classes) { | ||
114 | elem.setAttribute("class", classes.join(" ")); | ||
115 | }, | ||
116 | checkAndGetIndex = function (classes, token) { | ||
117 | if (token === "") { | ||
118 | throw "SYNTAX_ERR"; | ||
119 | } | ||
120 | if (/\s/.test(token)) { | ||
121 | throw "INVALID_CHARACTER_ERR"; | ||
122 | } | ||
123 | |||
124 | return classes.indexOf(token); | ||
125 | }, | ||
126 | classListGetter = function () { | ||
127 | var elem = this, | ||
128 | classes = elem.getAttribute("class") || ""; | ||
129 | classes = classes.replace(trim, "").split(/\s+/); | ||
130 | return { | ||
131 | length: classes.length, | ||
132 | item: function (i) { | ||
133 | return classes[i] || null; | ||
134 | }, | ||
135 | contains: function (token) { | ||
136 | return checkAndGetIndex(classes, token) !== -1; | ||
137 | }, | ||
138 | add: function (token) { | ||
139 | if (checkAndGetIndex(classes, token) === -1) { | ||
140 | classes.push(token); | ||
141 | this.length = classes.length; | ||
142 | setClasses(elem, classes); | ||
143 | } | ||
144 | }, | ||
145 | remove: function (token) { | ||
146 | var index = checkAndGetIndex(classes, token); | ||
147 | if (index !== -1) { | ||
148 | classes.splice(index, 1); | ||
149 | this.length = classes.length; | ||
150 | setClasses(elem, classes); | ||
151 | } | ||
152 | }, | ||
153 | toggle: function (token) { | ||
154 | if (checkAndGetIndex(classes, token) === -1) { | ||
155 | this.add(token); | ||
156 | } else { | ||
157 | this.remove(token); | ||
158 | } | ||
159 | }, | ||
160 | toString: function () { | ||
161 | return (elem.getAttribute("class") || ""); | ||
162 | } | ||
163 | }; | ||
164 | }; | ||
165 | |||
166 | if (Object.defineProperty) { | ||
167 | Object.defineProperty(Element.prototype, classListProp, { get: classListGetter, enumerable: true }); | ||
168 | } else if (Object.prototype.__defineGetter__) { | ||
169 | Element.prototype.__defineGetter__(classListProp, classListGetter); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | }()); | ||
174 | |||
175 | } | ||
176 | |||
177 | NodePrototype.parentOf = function(child) { | ||
178 | while ((child = child.parentNode) && child !== this) {}; | ||
179 | //If child is defined then we didn't walk all the way up to the root | ||
180 | return child ? true : false; | ||
181 | |||
182 | }; | ||
183 | |||
184 | var _offsetForElement = function(element) { | ||
185 | var boundingClientRect, | ||
186 | elementsDocument = element.ownerDocument, | ||
187 | elementsDocumentElement, | ||
188 | elementsBody, | ||
189 | elementsWindow; | ||
190 | |||
191 | if ( element && elementsDocument ) { | ||
192 | elementsDocumentElement = elementsDocument.documentElement; | ||
193 | elementsBody = elementsDocument.body; | ||
194 | elementsWindow = elementsDocument.defaultView; | ||
195 | |||
196 | if ( element !== elementsBody ) { | ||
197 | boundingClientRect = element.getBoundingClientRect(); | ||
198 | if ( elementsDocumentElement.parentOf(element) ) { | ||
199 | var clientTop = elementsDocumentElement.clientTop || elementsBody.clientTop || 0, | ||
200 | clientLeft = elementsDocumentElement.clientLeft || elementsBody.clientLeft || 0, | ||
201 | scrollTop = elementsWindow.pageYOffset || elementsDocumentElement.scrollTop || elementsBody.scrollTop, | ||
202 | scrollLeft = elementsWindow.pageXOffset || elementsDocumentElement.scrollLeft || elementsBody.scrollLeft, | ||
203 | top = boundingClientRect.top + scrollTop - clientTop, | ||
204 | left = boundingClientRect.left + scrollLeft - clientLeft; | ||
205 | return { top: top, left: left }; | ||
206 | } else { | ||
207 | return { top: boundingClientRect.top, left: boundingClientRect.left }; | ||
208 | } | ||
209 | |||
210 | } else { | ||
211 | return { top: elementsBody.offsetTop, left: elementsBody.offsetLeft }; | ||
212 | } | ||
213 | } else { | ||
214 | return null; | ||
215 | } | ||
216 | }; | ||
217 | |||
218 | var _webKitPoint = null; | ||
219 | try { | ||
220 | _webKitPoint = new WebKitPoint(0,0); | ||
221 | } catch (e) {} | ||
222 | |||
223 | var webkitImplementation = function() { | ||
224 | exports.convertPointFromNodeToPage = function(element, point) { | ||
225 | if(point) { | ||
226 | _webKitPoint.x = point.x; | ||
227 | _webKitPoint.y = point.y; | ||
228 | } else { | ||
229 | _webKitPoint.x = 0; | ||
230 | _webKitPoint.y = 0; | ||
231 | } | ||
232 | point = webkitConvertPointFromNodeToPage(element, _webKitPoint); | ||
233 | return point ? Point.create().init(point.x, point.y) : null; | ||
234 | }; | ||