From b89a7ee8b956c96a1dcee995ea840feddc5d4b27 Mon Sep 17 00:00:00 2001 From: Pierre Frisch Date: Thu, 22 Dec 2011 07:25:50 -0800 Subject: First commit of Ninja to ninja-internal Signed-off-by: Valerio Virgillito --- node_modules/montage/ui/dom.js | 268 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100755 node_modules/montage/ui/dom.js (limited to 'node_modules/montage/ui/dom.js') diff --git a/node_modules/montage/ui/dom.js b/node_modules/montage/ui/dom.js new file mode 100755 index 00000000..158b8a8f --- /dev/null +++ b/node_modules/montage/ui/dom.js @@ -0,0 +1,268 @@ +/* + This file contains proprietary software owned by Motorola Mobility, Inc.
+ No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+ (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +/** + Provides DOM + @module montage/ui/dom + @requires montage/core/geometry/point +*/ + +var Point = require("core/geometry/point").Point, + NodePrototype = Node.prototype, + ElementPrototype = Element.prototype; + +/** + @function external:Element#set + @param {type-TODO} string + @param {type-TODO} string + @param {type-TODO} 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 +}); + +NodePrototype.get = function(key) { + return this.getAttribute(key) || this[key]; +}; + +Object.getPrototypeOf(document).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); + } +}; + +Object.getPrototypeOf(document).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; + } + } + } +}; + +/* + * classList.js + * + * Implements a cross-browser element.classList getter. + * By Eli Grey, http://eligrey.com + */ + +"use strict"; + +if (typeof Element !== "undefined") { + + (function () { + + var classListProp = "classList"; + + if (!Element.prototype.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") || ""); + } + }; + }; + + if (Object.defineProperty) { + Object.defineProperty(Element.prototype, classListProp, { get: classListGetter, enumerable: true }); + } else if (Object.prototype.__defineGetter__) { + Element.prototype.__defineGetter__(classListProp, classListGetter); + } + } + + }()); + +} + +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; + +}; + +var _offsetForElement = function(element) { + var boundingClientRect, + elementsDocument = element.ownerDocument, + elementsDocumentElement, + elementsBody, + elementsWindow; + + if ( element && elementsDocument ) { + elementsDocumentElement = elementsDocument.documentElement; + elementsBody = elementsDocument.body; + elementsWindow = elementsDocument.defaultView; + + if ( element !== elementsBody ) { + boundingClientRect = element.getBoundingClientRect(); + if ( elementsDocumentElement.parentOf(element) ) { + var clientTop = elementsDocumentElement.clientTop || elementsBody.clientTop || 0, + clientLeft = elementsDocumentElement.clientLeft || elementsBody.clientLeft || 0, + scrollTop = elementsWindow.pageYOffset || elementsDocumentElement.scrollTop || elementsBody.scrollTop, + scrollLeft = elementsWindow.pageXOffset || elementsDocumentElement.scrollLeft || elementsBody.scrollLeft, + top = boundingClientRect.top + scrollTop - clientTop, + left = boundingClientRect.left + scrollLeft - clientLeft; + return { top: top, left: left }; + } else { + return { top: boundingClientRect.top, left: boundingClientRect.left }; + } + + } else { + return { top: elementsBody.offsetTop, left: elementsBody.offsetLeft }; + } + } else { + return null; + } +}; + +var _webKitPoint = null; +try { + _webKitPoint = new WebKitPoint(0,0); +} catch (e) {} + +if (_webKitPoint) { + + exports.convertPointFromNodeToPage = function(element, point) { + if(point) { + _webKitPoint.x = point.x; + _webKitPoint.y = point.y; + } else { + _webKitPoint.x = 0; + _webKitPoint.y = 0; + } + _webKitPoint = webkitConvertPointFromNodeToPage(element, _webKitPoint); + return Point.create().init(_webKitPoint.x, _webKitPoint.y); + }; + + exports.convertPointFromPageToNode = function(element, point) { + if(point) { + _webKitPoint.x = point.x; + _webKitPoint.y = point.y; + } else { + _webKitPoint.x = 0; + _webKitPoint.y = 0; + } + _webKitPoint = webkitConvertPointFromPageToNode(element, _webKitPoint); + return Point.create().init(_webKitPoint.x, _webKitPoint.y); + }; + +} else { + + exports.convertPointFromNodeToPage = function(element, point) { + var offset; + if (offset =_offsetForElement(element)) { + return Point.create().init((point ? point.x:0)+offset.left, (point ? point.y:0)+offset.top); + } else { + return Point.create().init((point ? point.x:0), (point ? point.y:0)); + } + }; + + exports.convertPointFromPageToNode = function(element, point) { + var offset; + if (offset =_offsetForElement(element)) { + return Point.create().init((point ? point.x:0)-offset.left, (point ? point.y:0)-offset.top); + } else { + return Point.create().init((point ? point.x:0), (point ? point.y:0)); + } + }; +} \ No newline at end of file -- cgit v1.2.3