1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Style.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,445 @@ 1.4 +/*** 1.5 + 1.6 +MochiKit.Style 1.4 1.7 + 1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc. 1.9 + 1.10 +(c) 2005-2006 Bob Ippolito, Beau Hartshorne. All rights Reserved. 1.11 + 1.12 +***/ 1.13 + 1.14 +if (typeof(dojo) != 'undefined') { 1.15 + dojo.provide('MochiKit.Style'); 1.16 + dojo.require('MochiKit.Base'); 1.17 + dojo.require('MochiKit.DOM'); 1.18 +} 1.19 +if (typeof(JSAN) != 'undefined') { 1.20 + JSAN.use('MochiKit.Base', []); 1.21 + JSAN.use('MochiKit.DOM', []); 1.22 +} 1.23 + 1.24 +try { 1.25 + if (typeof(MochiKit.Base) == 'undefined') { 1.26 + throw ''; 1.27 + } 1.28 +} catch (e) { 1.29 + throw 'MochiKit.Style depends on MochiKit.Base!'; 1.30 +} 1.31 + 1.32 +try { 1.33 + if (typeof(MochiKit.DOM) == 'undefined') { 1.34 + throw ''; 1.35 + } 1.36 +} catch (e) { 1.37 + throw 'MochiKit.Style depends on MochiKit.DOM!'; 1.38 +} 1.39 + 1.40 + 1.41 +if (typeof(MochiKit.Style) == 'undefined') { 1.42 + MochiKit.Style = {}; 1.43 +} 1.44 + 1.45 +MochiKit.Style.NAME = 'MochiKit.Style'; 1.46 +MochiKit.Style.VERSION = '1.4'; 1.47 +MochiKit.Style.__repr__ = function () { 1.48 + return '[' + this.NAME + ' ' + this.VERSION + ']'; 1.49 +}; 1.50 +MochiKit.Style.toString = function () { 1.51 + return this.__repr__(); 1.52 +}; 1.53 + 1.54 +MochiKit.Style.EXPORT_OK = []; 1.55 + 1.56 +MochiKit.Style.EXPORT = [ 1.57 + 'setStyle', 1.58 + 'setOpacity', 1.59 + 'getStyle', 1.60 + 'getElementDimensions', 1.61 + 'elementDimensions', // deprecated 1.62 + 'setElementDimensions', 1.63 + 'getElementPosition', 1.64 + 'elementPosition', // deprecated 1.65 + 'setElementPosition', 1.66 + 'setDisplayForElement', 1.67 + 'hideElement', 1.68 + 'showElement', 1.69 + 'getViewportDimensions', 1.70 + 'getViewportPosition', 1.71 + 'Dimensions', 1.72 + 'Coordinates' 1.73 +]; 1.74 + 1.75 + 1.76 +/* 1.77 + 1.78 + Dimensions 1.79 + 1.80 +*/ 1.81 +/** @id MochiKit.Style.Dimensions */ 1.82 +MochiKit.Style.Dimensions = function (w, h) { 1.83 + this.w = w; 1.84 + this.h = h; 1.85 +}; 1.86 + 1.87 +MochiKit.Style.Dimensions.prototype.__repr__ = function () { 1.88 + var repr = MochiKit.Base.repr; 1.89 + return '{w: ' + repr(this.w) + ', h: ' + repr(this.h) + '}'; 1.90 +}; 1.91 + 1.92 +MochiKit.Style.Dimensions.prototype.toString = function () { 1.93 + return this.__repr__(); 1.94 +}; 1.95 + 1.96 + 1.97 +/* 1.98 + 1.99 + Coordinates 1.100 + 1.101 +*/ 1.102 +/** @id MochiKit.Style.Coordinates */ 1.103 +MochiKit.Style.Coordinates = function (x, y) { 1.104 + this.x = x; 1.105 + this.y = y; 1.106 +}; 1.107 + 1.108 +MochiKit.Style.Coordinates.prototype.__repr__ = function () { 1.109 + var repr = MochiKit.Base.repr; 1.110 + return '{x: ' + repr(this.x) + ', y: ' + repr(this.y) + '}'; 1.111 +}; 1.112 + 1.113 +MochiKit.Style.Coordinates.prototype.toString = function () { 1.114 + return this.__repr__(); 1.115 +}; 1.116 + 1.117 + 1.118 +MochiKit.Base.update(MochiKit.Style, { 1.119 + 1.120 + /** @id MochiKit.Style.getStyle */ 1.121 + getStyle: function (elem, cssProperty) { 1.122 + var dom = MochiKit.DOM; 1.123 + var d = dom._document; 1.124 + 1.125 + elem = dom.getElement(elem); 1.126 + cssProperty = MochiKit.Base.camelize(cssProperty); 1.127 + 1.128 + if (!elem || elem == d) { 1.129 + return undefined; 1.130 + } 1.131 + if (cssProperty == 'opacity' && elem.filters) { 1.132 + var opacity = (MochiKit.Style.getStyle(elem, 'filter') || '').match(/alpha\(opacity=(.*)\)/); 1.133 + if (opacity && opacity[1]) { 1.134 + return parseFloat(opacity[1]) / 100; 1.135 + } 1.136 + return 1.0; 1.137 + } 1.138 + var value = elem.style ? elem.style[cssProperty] : null; 1.139 + if (!value) { 1.140 + if (d.defaultView && d.defaultView.getComputedStyle) { 1.141 + var css = d.defaultView.getComputedStyle(elem, null); 1.142 + cssProperty = cssProperty.replace(/([A-Z])/g, '-$1' 1.143 + ).toLowerCase(); // from dojo.style.toSelectorCase 1.144 + value = css ? css.getPropertyValue(cssProperty) : null; 1.145 + } else if (elem.currentStyle) { 1.146 + value = elem.currentStyle[cssProperty]; 1.147 + } 1.148 + } 1.149 + if (cssProperty == 'opacity') { 1.150 + value = parseFloat(value); 1.151 + } 1.152 + 1.153 + if (/Opera/.test(navigator.userAgent) && (MochiKit.Base.find(['left', 'top', 'right', 'bottom'], cssProperty) != -1)) { 1.154 + if (MochiKit.Style.getStyle(elem, 'position') == 'static') { 1.155 + value = 'auto'; 1.156 + } 1.157 + } 1.158 + 1.159 + return value == 'auto' ? null : value; 1.160 + }, 1.161 + 1.162 + /** @id MochiKit.Style.setStyle */ 1.163 + setStyle: function (elem, style) { 1.164 + elem = MochiKit.DOM.getElement(elem); 1.165 + for (var name in style) { 1.166 + if (name == 'opacity') { 1.167 + MochiKit.Style.setOpacity(elem, style[name]); 1.168 + } else { 1.169 + elem.style[MochiKit.Base.camelize(name)] = style[name]; 1.170 + } 1.171 + } 1.172 + }, 1.173 + 1.174 + /** @id MochiKit.Style.setOpacity */ 1.175 + setOpacity: function (elem, o) { 1.176 + elem = MochiKit.DOM.getElement(elem); 1.177 + var self = MochiKit.Style; 1.178 + if (o == 1) { 1.179 + var toSet = /Gecko/.test(navigator.userAgent) && !(/Konqueror|AppleWebKit|KHTML/.test(navigator.userAgent)); 1.180 + elem.style["opacity"] = toSet ? 0.999999 : 1.0; 1.181 + if (/MSIE/.test(navigator.userAgent)) { 1.182 + elem.style['filter'] = 1.183 + self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, ''); 1.184 + } 1.185 + } else { 1.186 + if (o < 0.00001) { 1.187 + o = 0; 1.188 + } 1.189 + elem.style["opacity"] = o; 1.190 + if (/MSIE/.test(navigator.userAgent)) { 1.191 + elem.style['filter'] = 1.192 + self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + o * 100 + ')'; 1.193 + } 1.194 + } 1.195 + }, 1.196 + 1.197 + /* 1.198 + 1.199 + getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0. 1.200 + Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved. 1.201 + License: BSD, http://developer.yahoo.net/yui/license.txt 1.202 + 1.203 + */ 1.204 + 1.205 + /** @id MochiKit.Style.getElementPosition */ 1.206 + getElementPosition: function (elem, /* optional */relativeTo) { 1.207 + var self = MochiKit.Style; 1.208 + var dom = MochiKit.DOM; 1.209 + elem = dom.getElement(elem); 1.210 + 1.211 + if (!elem || 1.212 + (!(elem.x && elem.y) && 1.213 + (!elem.parentNode === null || 1.214 + self.getStyle(elem, 'display') == 'none'))) { 1.215 + return undefined; 1.216 + } 1.217 + 1.218 + var c = new self.Coordinates(0, 0); 1.219 + var box = null; 1.220 + var parent = null; 1.221 + 1.222 + var d = MochiKit.DOM._document; 1.223 + var de = d.documentElement; 1.224 + var b = d.body; 1.225 + 1.226 + if (!elem.parentNode && elem.x && elem.y) { 1.227 + /* it's just a MochiKit.Style.Coordinates object */ 1.228 + c.x += elem.x || 0; 1.229 + c.y += elem.y || 0; 1.230 + } else if (elem.getBoundingClientRect) { // IE shortcut 1.231 + /* 1.232 + 1.233 + The IE shortcut can be off by two. We fix it. See: 1.234 + http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp 1.235 + 1.236 + This is similar to the method used in 1.237 + MochiKit.Signal.Event.mouse(). 1.238 + 1.239 + */ 1.240 + box = elem.getBoundingClientRect(); 1.241 + 1.242 + c.x += box.left + 1.243 + (de.scrollLeft || b.scrollLeft) - 1.244 + (de.clientLeft || 0); 1.245 + 1.246 + c.y += box.top + 1.247 + (de.scrollTop || b.scrollTop) - 1.248 + (de.clientTop || 0); 1.249 + 1.250 + } else if (elem.offsetParent) { 1.251 + c.x += elem.offsetLeft; 1.252 + c.y += elem.offsetTop; 1.253 + parent = elem.offsetParent; 1.254 + 1.255 + if (parent != elem) { 1.256 + while (parent) { 1.257 + c.x += parent.offsetLeft; 1.258 + c.y += parent.offsetTop; 1.259 + parent = parent.offsetParent; 1.260 + } 1.261 + } 1.262 + 1.263 + /* 1.264 + 1.265 + Opera < 9 and old Safari (absolute) incorrectly account for 1.266 + body offsetTop and offsetLeft. 1.267 + 1.268 + */ 1.269 + var ua = navigator.userAgent.toLowerCase(); 1.270 + if ((typeof(opera) != 'undefined' && 1.271 + parseFloat(opera.version()) < 9) || 1.272 + (ua.indexOf('AppleWebKit') != -1 && 1.273 + self.getStyle(elem, 'position') == 'absolute')) { 1.274 + 1.275 + c.x -= b.offsetLeft; 1.276 + c.y -= b.offsetTop; 1.277 + 1.278 + } 1.279 + } 1.280 + 1.281 + if (typeof(relativeTo) != 'undefined') { 1.282 + relativeTo = arguments.callee(relativeTo); 1.283 + if (relativeTo) { 1.284 + c.x -= (relativeTo.x || 0); 1.285 + c.y -= (relativeTo.y || 0); 1.286 + } 1.287 + } 1.288 + 1.289 + if (elem.parentNode) { 1.290 + parent = elem.parentNode; 1.291 + } else { 1.292 + parent = null; 1.293 + } 1.294 + 1.295 + while (parent) { 1.296 + var tagName = parent.tagName.toUpperCase(); 1.297 + if (tagName === 'BODY' || tagName === 'HTML') { 1.298 + break; 1.299 + } 1.300 + var disp = self.getStyle(parent, 'display'); 1.301 + // Handle strange Opera bug for some display 1.302 + if (disp != 'inline' && disp != 'table-row') { 1.303 + c.x -= parent.scrollLeft; 1.304 + c.y -= parent.scrollTop; 1.305 + } 1.306 + if (parent.parentNode) { 1.307 + parent = parent.parentNode; 1.308 + } else { 1.309 + parent = null; 1.310 + } 1.311 + } 1.312 + 1.313 + return c; 1.314 + }, 1.315 + 1.316 + /** @id MochiKit.Style.setElementPosition */ 1.317 + setElementPosition: function (elem, newPos/* optional */, units) { 1.318 + elem = MochiKit.DOM.getElement(elem); 1.319 + if (typeof(units) == 'undefined') { 1.320 + units = 'px'; 1.321 + } 1.322 + var newStyle = {}; 1.323 + var isUndefNull = MochiKit.Base.isUndefinedOrNull; 1.324 + if (!isUndefNull(newPos.x)) { 1.325 + newStyle['left'] = newPos.x + units; 1.326 + } 1.327 + if (!isUndefNull(newPos.y)) { 1.328 + newStyle['top'] = newPos.y + units; 1.329 + } 1.330 + MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle}); 1.331 + }, 1.332 + 1.333 + /** @id MochiKit.Style.getElementDimensions */ 1.334 + getElementDimensions: function (elem) { 1.335 + var self = MochiKit.Style; 1.336 + var dom = MochiKit.DOM; 1.337 + if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') { 1.338 + return new self.Dimensions(elem.w || 0, elem.h || 0); 1.339 + } 1.340 + elem = dom.getElement(elem); 1.341 + if (!elem) { 1.342 + return undefined; 1.343 + } 1.344 + var disp = self.getStyle(elem, 'display'); 1.345 + // display can be empty/undefined on WebKit/KHTML 1.346 + if (disp != 'none' && disp !== '' && typeof(disp) != 'undefined') { 1.347 + return new self.Dimensions(elem.offsetWidth || 0, 1.348 + elem.offsetHeight || 0); 1.349 + } 1.350 + var s = elem.style; 1.351 + var originalVisibility = s.visibility; 1.352 + var originalPosition = s.position; 1.353 + s.visibility = 'hidden'; 1.354 + s.position = 'absolute'; 1.355 + s.display = ''; 1.356 + var originalWidth = elem.offsetWidth; 1.357 + var originalHeight = elem.offsetHeight; 1.358 + s.display = 'none'; 1.359 + s.position = originalPosition; 1.360 + s.visibility = originalVisibility; 1.361 + return new self.Dimensions(originalWidth, originalHeight); 1.362 + }, 1.363 + 1.364 + /** @id MochiKit.Style.setElementDimensions */ 1.365 + setElementDimensions: function (elem, newSize/* optional */, units) { 1.366 + elem = MochiKit.DOM.getElement(elem); 1.367 + if (typeof(units) == 'undefined') { 1.368 + units = 'px'; 1.369 + } 1.370 + var newStyle = {}; 1.371 + var isUndefNull = MochiKit.Base.isUndefinedOrNull; 1.372 + if (!isUndefNull(newSize.w)) { 1.373 + newStyle['width'] = newSize.w + units; 1.374 + } 1.375 + if (!isUndefNull(newSize.h)) { 1.376 + newStyle['height'] = newSize.h + units; 1.377 + } 1.378 + MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle}); 1.379 + }, 1.380 + 1.381 + /** @id MochiKit.Style.setDisplayForElement */ 1.382 + setDisplayForElement: function (display, element/*, ...*/) { 1.383 + var elements = MochiKit.Base.extend(null, arguments, 1); 1.384 + var getElement = MochiKit.DOM.getElement; 1.385 + for (var i = 0; i < elements.length; i++) { 1.386 + element = getElement(elements[i]); 1.387 + if (element) { 1.388 + element.style.display = display; 1.389 + } 1.390 + } 1.391 + }, 1.392 + 1.393 + /** @id MochiKit.Style.getViewportDimensions */ 1.394 + getViewportDimensions: function () { 1.395 + var d = new MochiKit.Style.Dimensions(); 1.396 + 1.397 + var w = MochiKit.DOM._window; 1.398 + var b = MochiKit.DOM._document.body; 1.399 + 1.400 + if (w.innerWidth) { 1.401 + d.w = w.innerWidth; 1.402 + d.h = w.innerHeight; 1.403 + } else if (b.parentElement.clientWidth) { 1.404 + d.w = b.parentElement.clientWidth; 1.405 + d.h = b.parentElement.clientHeight; 1.406 + } else if (b && b.clientWidth) { 1.407 + d.w = b.clientWidth; 1.408 + d.h = b.clientHeight; 1.409 + } 1.410 + return d; 1.411 + }, 1.412 + 1.413 + /** @id MochiKit.Style.getViewportPosition */ 1.414 + getViewportPosition: function () { 1.415 + var c = new MochiKit.Style.Coordinates(0, 0); 1.416 + var d = MochiKit.DOM._document; 1.417 + var de = d.documentElement; 1.418 + var db = d.body; 1.419 + if (de && (de.scrollTop || de.scrollLeft)) { 1.420 + c.x = de.scrollLeft; 1.421 + c.y = de.scrollTop; 1.422 + } else if (db) { 1.423 + c.x = db.scrollLeft; 1.424 + c.y = db.scrollTop; 1.425 + } 1.426 + return c; 1.427 + }, 1.428 + 1.429 + __new__: function () { 1.430 + var m = MochiKit.Base; 1.431 + 1.432 + this.elementPosition = this.getElementPosition; 1.433 + this.elementDimensions = this.getElementDimensions; 1.434 + 1.435 + this.hideElement = m.partial(this.setDisplayForElement, 'none'); 1.436 + this.showElement = m.partial(this.setDisplayForElement, 'block'); 1.437 + 1.438 + this.EXPORT_TAGS = { 1.439 + ':common': this.EXPORT, 1.440 + ':all': m.concat(this.EXPORT, this.EXPORT_OK) 1.441 + }; 1.442 + 1.443 + m.nameFunctions(this); 1.444 + } 1.445 +}); 1.446 + 1.447 +MochiKit.Style.__new__(); 1.448 +MochiKit.Base._exportSymbols(this, MochiKit.Style);