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