testing/mochitest/MochiKit/Style.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Style 1.4
michael@0 4
michael@0 5 See <http://mochikit.com/> for documentation, downloads, license, etc.
michael@0 6
michael@0 7 (c) 2005-2006 Bob Ippolito, Beau Hartshorne. All rights Reserved.
michael@0 8
michael@0 9 ***/
michael@0 10
michael@0 11 if (typeof(dojo) != 'undefined') {
michael@0 12 dojo.provide('MochiKit.Style');
michael@0 13 dojo.require('MochiKit.Base');
michael@0 14 dojo.require('MochiKit.DOM');
michael@0 15 }
michael@0 16 if (typeof(JSAN) != 'undefined') {
michael@0 17 JSAN.use('MochiKit.Base', []);
michael@0 18 }
michael@0 19
michael@0 20 try {
michael@0 21 if (typeof(MochiKit.Base) == 'undefined') {
michael@0 22 throw '';
michael@0 23 }
michael@0 24 } catch (e) {
michael@0 25 throw 'MochiKit.Style depends on MochiKit.Base!';
michael@0 26 }
michael@0 27
michael@0 28 try {
michael@0 29 if (typeof(MochiKit.DOM) == 'undefined') {
michael@0 30 throw '';
michael@0 31 }
michael@0 32 } catch (e) {
michael@0 33 throw 'MochiKit.Style depends on MochiKit.DOM!';
michael@0 34 }
michael@0 35
michael@0 36
michael@0 37 if (typeof(MochiKit.Style) == 'undefined') {
michael@0 38 MochiKit.Style = {};
michael@0 39 }
michael@0 40
michael@0 41 MochiKit.Style.NAME = 'MochiKit.Style';
michael@0 42 MochiKit.Style.VERSION = '1.4';
michael@0 43 MochiKit.Style.__repr__ = function () {
michael@0 44 return '[' + this.NAME + ' ' + this.VERSION + ']';
michael@0 45 };
michael@0 46 MochiKit.Style.toString = function () {
michael@0 47 return this.__repr__();
michael@0 48 };
michael@0 49
michael@0 50 MochiKit.Style.EXPORT_OK = [];
michael@0 51
michael@0 52 MochiKit.Style.EXPORT = [
michael@0 53 'setOpacity',
michael@0 54 'getOpacity',
michael@0 55 'setStyle',
michael@0 56 'getStyle', // temporary
michael@0 57 'computedStyle',
michael@0 58 'getElementDimensions',
michael@0 59 'elementDimensions', // deprecated
michael@0 60 'setElementDimensions',
michael@0 61 'getElementPosition',
michael@0 62 'elementPosition', // deprecated
michael@0 63 'setElementPosition',
michael@0 64 'setDisplayForElement',
michael@0 65 'hideElement',
michael@0 66 'showElement',
michael@0 67 'getViewportDimensions',
michael@0 68 'getViewportPosition',
michael@0 69 'Dimensions',
michael@0 70 'Coordinates'
michael@0 71 ];
michael@0 72
michael@0 73
michael@0 74 /*
michael@0 75
michael@0 76 Dimensions
michael@0 77
michael@0 78 */
michael@0 79 /** @id MochiKit.Style.Dimensions */
michael@0 80 MochiKit.Style.Dimensions = function (w, h) {
michael@0 81 this.w = w;
michael@0 82 this.h = h;
michael@0 83 };
michael@0 84
michael@0 85 MochiKit.Style.Dimensions.prototype.__repr__ = function () {
michael@0 86 var repr = MochiKit.Base.repr;
michael@0 87 return '{w: ' + repr(this.w) + ', h: ' + repr(this.h) + '}';
michael@0 88 };
michael@0 89
michael@0 90 MochiKit.Style.Dimensions.prototype.toString = function () {
michael@0 91 return this.__repr__();
michael@0 92 };
michael@0 93
michael@0 94
michael@0 95 /*
michael@0 96
michael@0 97 Coordinates
michael@0 98
michael@0 99 */
michael@0 100 /** @id MochiKit.Style.Coordinates */
michael@0 101 MochiKit.Style.Coordinates = function (x, y) {
michael@0 102 this.x = x;
michael@0 103 this.y = y;
michael@0 104 };
michael@0 105
michael@0 106 MochiKit.Style.Coordinates.prototype.__repr__ = function () {
michael@0 107 var repr = MochiKit.Base.repr;
michael@0 108 return '{x: ' + repr(this.x) + ', y: ' + repr(this.y) + '}';
michael@0 109 };
michael@0 110
michael@0 111 MochiKit.Style.Coordinates.prototype.toString = function () {
michael@0 112 return this.__repr__();
michael@0 113 };
michael@0 114
michael@0 115
michael@0 116 MochiKit.Base.update(MochiKit.Style, {
michael@0 117
michael@0 118 /** @id MochiKit.Style.computedStyle */
michael@0 119 computedStyle: function (elem, cssProperty) {
michael@0 120 var dom = MochiKit.DOM;
michael@0 121 var d = dom._document;
michael@0 122
michael@0 123 elem = dom.getElement(elem);
michael@0 124 cssProperty = MochiKit.Base.camelize(cssProperty);
michael@0 125
michael@0 126 if (!elem || elem == d) {
michael@0 127 return undefined;
michael@0 128 }
michael@0 129
michael@0 130 /* from YUI 0.10.0 */
michael@0 131 if (cssProperty == 'opacity' && elem.filters) { // IE opacity
michael@0 132 try {
michael@0 133 return elem.filters.item('DXImageTransform.Microsoft.Alpha'
michael@0 134 ).opacity / 100;
michael@0 135 } catch(e) {
michael@0 136 try {
michael@0 137 return elem.filters.item('alpha').opacity / 100;
michael@0 138 } catch(e) {}
michael@0 139 }
michael@0 140 }
michael@0 141
michael@0 142 if (elem.currentStyle) {
michael@0 143 return elem.currentStyle[cssProperty];
michael@0 144 }
michael@0 145 if (typeof(d.defaultView) == 'undefined') {
michael@0 146 return undefined;
michael@0 147 }
michael@0 148 if (d.defaultView === null) {
michael@0 149 return undefined;
michael@0 150 }
michael@0 151 var style = d.defaultView.getComputedStyle(elem, null);
michael@0 152 if (typeof(style) == 'undefined' || style === null) {
michael@0 153 return undefined;
michael@0 154 }
michael@0 155
michael@0 156 var selectorCase = cssProperty.replace(/([A-Z])/g, '-$1'
michael@0 157 ).toLowerCase(); // from dojo.style.toSelectorCase
michael@0 158
michael@0 159 return style.getPropertyValue(selectorCase);
michael@0 160 },
michael@0 161
michael@0 162 /** @id MochiKit.Style.getStyle */
michael@0 163 getStyle: function (elem, style) {
michael@0 164 elem = MochiKit.DOM.getElement(elem);
michael@0 165 var value = elem.style[MochiKit.Base.camelize(style)];
michael@0 166 if (!value) {
michael@0 167 if (document.defaultView && document.defaultView.getComputedStyle) {
michael@0 168 var css = document.defaultView.getComputedStyle(elem, null);
michael@0 169 value = css ? css.getPropertyValue(style) : null;
michael@0 170 } else if (elem.currentStyle) {
michael@0 171 value = elem.currentStyle[MochiKit.Base.camelize(style)];
michael@0 172 }
michael@0 173 }
michael@0 174
michael@0 175 if (/Opera/.test(navigator.userAgent) && (MochiKit.Base.find(['left', 'top', 'right', 'bottom'], style) != -1)) {
michael@0 176 if (MochiKit.Style.getStyle(elem, 'position') == 'static') {
michael@0 177 value = 'auto';
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 return value == 'auto' ? null : value;
michael@0 182 },
michael@0 183
michael@0 184 /** @id MochiKit.Style.setStyle */
michael@0 185 setStyle: function (elem, style) {
michael@0 186 elem = MochiKit.DOM.getElement(elem);
michael@0 187 for (name in style) {
michael@0 188 elem.style[MochiKit.Base.camelize(name)] = style[name];
michael@0 189 }
michael@0 190 },
michael@0 191
michael@0 192 /** @id MochiKit.Style.getOpacity */
michael@0 193 getOpacity: function (elem) {
michael@0 194 var opacity;
michael@0 195 if (opacity = MochiKit.Style.getStyle(elem, 'opacity')) {
michael@0 196 return parseFloat(opacity);
michael@0 197 }
michael@0 198 if (opacity = (MochiKit.Style.getStyle(elem, 'filter') || '').match(/alpha\(opacity=(.*)\)/)) {
michael@0 199 if (opacity[1]) {
michael@0 200 return parseFloat(opacity[1]) / 100;
michael@0 201 }
michael@0 202 }
michael@0 203 return 1.0;
michael@0 204 },
michael@0 205 /** @id MochiKit.Style.setOpacity */
michael@0 206 setOpacity: function(elem, o) {
michael@0 207 elem = MochiKit.DOM.getElement(elem);
michael@0 208 var self = MochiKit.Style;
michael@0 209 if (o == 1) {
michael@0 210 var toSet = /Gecko/.test(navigator.userAgent) && !(/Konqueror|Safari|KHTML/.test(navigator.userAgent));
michael@0 211 self.setStyle(elem, {opacity: toSet ? 0.999999 : 1.0});
michael@0 212 if (/MSIE/.test(navigator.userAgent)) {
michael@0 213 self.setStyle(elem, {filter:
michael@0 214 self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '')});
michael@0 215 }
michael@0 216 } else {
michael@0 217 if (o < 0.00001) {
michael@0 218 o = 0;
michael@0 219 }
michael@0 220 self.setStyle(elem, {opacity: o});
michael@0 221 if (/MSIE/.test(navigator.userAgent)) {
michael@0 222 self.setStyle(elem,
michael@0 223 {filter: self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + o * 100 + ')' });
michael@0 224 }
michael@0 225 }
michael@0 226 },
michael@0 227
michael@0 228 /*
michael@0 229
michael@0 230 getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0.
michael@0 231 Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved.
michael@0 232 License: BSD, http://developer.yahoo.net/yui/license.txt
michael@0 233
michael@0 234 */
michael@0 235
michael@0 236 /** @id MochiKit.Style.getElementPosition */
michael@0 237 getElementPosition: function (elem, /* optional */relativeTo) {
michael@0 238 var self = MochiKit.Style;
michael@0 239 var dom = MochiKit.DOM;
michael@0 240 elem = dom.getElement(elem);
michael@0 241
michael@0 242 if (!elem ||
michael@0 243 (!(elem.x && elem.y) &&
michael@0 244 (!elem.parentNode == null ||
michael@0 245 self.computedStyle(elem, 'display') == 'none'))) {
michael@0 246 return undefined;
michael@0 247 }
michael@0 248
michael@0 249 var c = new self.Coordinates(0, 0);
michael@0 250 var box = null;
michael@0 251 var parent = null;
michael@0 252
michael@0 253 var d = MochiKit.DOM._document;
michael@0 254 var de = d.documentElement;
michael@0 255 var b = d.body;
michael@0 256
michael@0 257 if (!elem.parentNode && elem.x && elem.y) {
michael@0 258 /* it's just a MochiKit.Style.Coordinates object */
michael@0 259 c.x += elem.x || 0;
michael@0 260 c.y += elem.y || 0;
michael@0 261 } else if (elem.getBoundingClientRect) { // IE shortcut
michael@0 262 /*
michael@0 263
michael@0 264 The IE shortcut can be off by two. We fix it. See:
michael@0 265 http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp
michael@0 266
michael@0 267 This is similar to the method used in
michael@0 268 MochiKit.Signal.Event.mouse().
michael@0 269
michael@0 270 */
michael@0 271 box = elem.getBoundingClientRect();
michael@0 272
michael@0 273 c.x += box.left +
michael@0 274 (de.scrollLeft || b.scrollLeft) -
michael@0 275 (de.clientLeft || 0);
michael@0 276
michael@0 277 c.y += box.top +
michael@0 278 (de.scrollTop || b.scrollTop) -
michael@0 279 (de.clientTop || 0);
michael@0 280
michael@0 281 } else if (elem.offsetParent) {
michael@0 282 c.x += elem.offsetLeft;
michael@0 283 c.y += elem.offsetTop;
michael@0 284 parent = elem.offsetParent;
michael@0 285
michael@0 286 if (parent != elem) {
michael@0 287 while (parent) {
michael@0 288 c.x += parent.offsetLeft;
michael@0 289 c.y += parent.offsetTop;
michael@0 290 parent = parent.offsetParent;
michael@0 291 }
michael@0 292 }
michael@0 293
michael@0 294 /*
michael@0 295
michael@0 296 Opera < 9 and old Safari (absolute) incorrectly account for
michael@0 297 body offsetTop and offsetLeft.
michael@0 298
michael@0 299 */
michael@0 300 var ua = navigator.userAgent.toLowerCase();
michael@0 301 if ((typeof(opera) != 'undefined' &&
michael@0 302 parseFloat(opera.version()) < 9) ||
michael@0 303 (ua.indexOf('safari') != -1 &&
michael@0 304 self.computedStyle(elem, 'position') == 'absolute')) {
michael@0 305
michael@0 306 c.x -= b.offsetLeft;
michael@0 307 c.y -= b.offsetTop;
michael@0 308
michael@0 309 }
michael@0 310 }
michael@0 311
michael@0 312 if (typeof(relativeTo) != 'undefined') {
michael@0 313 relativeTo = arguments.callee(relativeTo);
michael@0 314 if (relativeTo) {
michael@0 315 c.x -= (relativeTo.x || 0);
michael@0 316 c.y -= (relativeTo.y || 0);
michael@0 317 }
michael@0 318 }
michael@0 319
michael@0 320 if (elem.parentNode) {
michael@0 321 parent = elem.parentNode;
michael@0 322 } else {
michael@0 323 parent = null;
michael@0 324 }
michael@0 325
michael@0 326 while (parent) {
michael@0 327 var tagName = parent.tagName.toUpperCase();
michael@0 328 if (tagName === 'BODY' || tagName === 'HTML') {
michael@0 329 break;
michael@0 330 }
michael@0 331 c.x -= parent.scrollLeft;
michael@0 332 c.y -= parent.scrollTop;
michael@0 333 if (parent.parentNode) {
michael@0 334 parent = parent.parentNode;
michael@0 335 } else {
michael@0 336 parent = null;
michael@0 337 }
michael@0 338 }
michael@0 339
michael@0 340 return c;
michael@0 341 },
michael@0 342
michael@0 343 /** @id MochiKit.Style.setElementPosition */
michael@0 344 setElementPosition: function (elem, newPos/* optional */, units) {
michael@0 345 elem = MochiKit.DOM.getElement(elem);
michael@0 346 if (typeof(units) == 'undefined') {
michael@0 347 units = 'px';
michael@0 348 }
michael@0 349 var newStyle = {};
michael@0 350 var isUndefNull = MochiKit.Base.isUndefinedOrNull;
michael@0 351 if (!isUndefNull(newPos.x)) {
michael@0 352 newStyle['left'] = newPos.x + units;
michael@0 353 }
michael@0 354 if (!isUndefNull(newPos.y)) {
michael@0 355 newStyle['top'] = newPos.y + units;
michael@0 356 }
michael@0 357 MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
michael@0 358 },
michael@0 359
michael@0 360 /** @id MochiKit.Style.getElementDimensions */
michael@0 361 getElementDimensions: function (elem) {
michael@0 362 var self = MochiKit.Style;
michael@0 363 var dom = MochiKit.DOM;
michael@0 364 if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') {
michael@0 365 return new self.Dimensions(elem.w || 0, elem.h || 0);
michael@0 366 }
michael@0 367 elem = dom.getElement(elem);
michael@0 368 if (!elem) {
michael@0 369 return undefined;
michael@0 370 }
michael@0 371 var disp = self.computedStyle(elem, 'display');
michael@0 372 // display can be empty/undefined on WebKit/KHTML
michael@0 373 if (disp != 'none' && disp != '' && typeof(disp) != 'undefined') {
michael@0 374 return new self.Dimensions(elem.offsetWidth || 0,
michael@0 375 elem.offsetHeight || 0);
michael@0 376 }
michael@0 377 var s = elem.style;
michael@0 378 var originalVisibility = s.visibility;
michael@0 379 var originalPosition = s.position;
michael@0 380 s.visibility = 'hidden';
michael@0 381 s.position = 'absolute';
michael@0 382 s.display = '';
michael@0 383 var originalWidth = elem.offsetWidth;
michael@0 384 var originalHeight = elem.offsetHeight;
michael@0 385 s.display = 'none';
michael@0 386 s.position = originalPosition;
michael@0 387 s.visibility = originalVisibility;
michael@0 388 return new self.Dimensions(originalWidth, originalHeight);
michael@0 389 },
michael@0 390
michael@0 391 /** @id MochiKit.Style.setElementDimensions */
michael@0 392 setElementDimensions: function (elem, newSize/* optional */, units) {
michael@0 393 elem = MochiKit.DOM.getElement(elem);
michael@0 394 if (typeof(units) == 'undefined') {
michael@0 395 units = 'px';
michael@0 396 }
michael@0 397 var newStyle = {};
michael@0 398 var isUndefNull = MochiKit.Base.isUndefinedOrNull;
michael@0 399 if (!isUndefNull(newSize.w)) {
michael@0 400 newStyle['width'] = newSize.w + units;
michael@0 401 }
michael@0 402 if (!isUndefNull(newSize.h)) {
michael@0 403 newStyle['height'] = newSize.h + units;
michael@0 404 }
michael@0 405 MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
michael@0 406 },
michael@0 407
michael@0 408 /** @id MochiKit.Style.setDisplayForElement */
michael@0 409 setDisplayForElement: function (display, element/*, ...*/) {
michael@0 410 var elements = MochiKit.Base.extend(null, arguments, 1);
michael@0 411 var getElement = MochiKit.DOM.getElement;
michael@0 412 for (var i = 0; i < elements.length; i++) {
michael@0 413 var element = getElement(elements[i]);
michael@0 414 if (element) {
michael@0 415 element.style.display = display;
michael@0 416 }
michael@0 417 }
michael@0 418 },
michael@0 419
michael@0 420 /** @id MochiKit.Style.getViewportDimensions */
michael@0 421 getViewportDimensions: function () {
michael@0 422 var d = new MochiKit.Style.Dimensions();
michael@0 423
michael@0 424 var w = MochiKit.DOM._window;
michael@0 425 var b = MochiKit.DOM._document.body;
michael@0 426
michael@0 427 if (w.innerWidth) {
michael@0 428 d.w = w.innerWidth;
michael@0 429 d.h = w.innerHeight;
michael@0 430 } else if (b.parentElement.clientWidth) {
michael@0 431 d.w = b.parentElement.clientWidth;
michael@0 432 d.h = b.parentElement.clientHeight;
michael@0 433 } else if (b && b.clientWidth) {
michael@0 434 d.w = b.clientWidth;
michael@0 435 d.h = b.clientHeight;
michael@0 436 }
michael@0 437 return d;
michael@0 438 },
michael@0 439
michael@0 440 /** @id MochiKit.Style.getViewportPosition */
michael@0 441 getViewportPosition: function () {
michael@0 442 var c = new MochiKit.Style.Coordinates(0, 0);
michael@0 443 var d = MochiKit.DOM._document;
michael@0 444 var de = d.documentElement;
michael@0 445 var db = d.body;
michael@0 446 if (de && (de.scrollTop || de.scrollLeft)) {
michael@0 447 c.x = de.scrollLeft;
michael@0 448 c.y = de.scrollTop;
michael@0 449 } else if (db) {
michael@0 450 c.x = db.scrollLeft;
michael@0 451 c.y = db.scrollTop;
michael@0 452 }
michael@0 453 return c;
michael@0 454 },
michael@0 455
michael@0 456 __new__: function () {
michael@0 457 var m = MochiKit.Base;
michael@0 458
michael@0 459 this.elementPosition = this.getElementPosition;
michael@0 460 this.elementDimensions = this.getElementDimensions;
michael@0 461
michael@0 462 this.hideElement = m.partial(this.setDisplayForElement, 'none');
michael@0 463 this.showElement = m.partial(this.setDisplayForElement, 'block');
michael@0 464
michael@0 465 this.EXPORT_TAGS = {
michael@0 466 ':common': this.EXPORT,
michael@0 467 ':all': m.concat(this.EXPORT, this.EXPORT_OK)
michael@0 468 };
michael@0 469
michael@0 470 m.nameFunctions(this);
michael@0 471 }
michael@0 472 });
michael@0 473
michael@0 474 MochiKit.Style.__new__();
michael@0 475 MochiKit.Base._exportSymbols(this, MochiKit.Style);

mercurial