dom/tests/mochitest/ajax/mochikit/MochiKit/Position.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Position 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 and others. 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.Position');
michael@0 13 dojo.require('MochiKit.Base');
michael@0 14 dojo.require('MochiKit.DOM');
michael@0 15 dojo.require('MochiKit.Style');
michael@0 16 }
michael@0 17 if (typeof(JSAN) != 'undefined') {
michael@0 18 JSAN.use('MochiKit.Base', []);
michael@0 19 JSAN.use('MochiKit.DOM', []);
michael@0 20 JSAN.use('MochiKit.Style', []);
michael@0 21 }
michael@0 22
michael@0 23 try {
michael@0 24 if (typeof(MochiKit.Base) == 'undefined' ||
michael@0 25 typeof(MochiKit.Style) == 'undefined' ||
michael@0 26 typeof(MochiKit.DOM) == 'undefined') {
michael@0 27 throw '';
michael@0 28 }
michael@0 29 } catch (e) {
michael@0 30 throw 'MochiKit.Style depends on MochiKit.Base, MochiKit.DOM, and MochiKit.Style!';
michael@0 31 }
michael@0 32
michael@0 33 if (typeof(MochiKit.Position) == 'undefined') {
michael@0 34 MochiKit.Position = {};
michael@0 35 }
michael@0 36
michael@0 37 MochiKit.Position.NAME = 'MochiKit.Position';
michael@0 38 MochiKit.Position.VERSION = '1.4';
michael@0 39 MochiKit.Position.__repr__ = function () {
michael@0 40 return '[' + this.NAME + ' ' + this.VERSION + ']';
michael@0 41 };
michael@0 42 MochiKit.Position.toString = function () {
michael@0 43 return this.__repr__();
michael@0 44 };
michael@0 45
michael@0 46 MochiKit.Position.EXPORT_OK = [];
michael@0 47
michael@0 48 MochiKit.Position.EXPORT = [
michael@0 49 ];
michael@0 50
michael@0 51
michael@0 52 MochiKit.Base.update(MochiKit.Position, {
michael@0 53 // set to true if needed, warning: firefox performance problems
michael@0 54 // NOT neeeded for page scrolling, only if draggable contained in
michael@0 55 // scrollable elements
michael@0 56 includeScrollOffsets: false,
michael@0 57
michael@0 58 /** @id MochiKit.Position.prepare */
michael@0 59 prepare: function () {
michael@0 60 var deltaX = window.pageXOffset
michael@0 61 || document.documentElement.scrollLeft
michael@0 62 || document.body.scrollLeft
michael@0 63 || 0;
michael@0 64 var deltaY = window.pageYOffset
michael@0 65 || document.documentElement.scrollTop
michael@0 66 || document.body.scrollTop
michael@0 67 || 0;
michael@0 68 this.windowOffset = new MochiKit.Style.Coordinates(deltaX, deltaY);
michael@0 69 },
michael@0 70
michael@0 71 /** @id MochiKit.Position.cumulativeOffset */
michael@0 72 cumulativeOffset: function (element) {
michael@0 73 var valueT = 0;
michael@0 74 var valueL = 0;
michael@0 75 do {
michael@0 76 valueT += element.offsetTop || 0;
michael@0 77 valueL += element.offsetLeft || 0;
michael@0 78 element = element.offsetParent;
michael@0 79 } while (element);
michael@0 80 return new MochiKit.Style.Coordinates(valueL, valueT);
michael@0 81 },
michael@0 82
michael@0 83 /** @id MochiKit.Position.realOffset */
michael@0 84 realOffset: function (element) {
michael@0 85 var valueT = 0;
michael@0 86 var valueL = 0;
michael@0 87 do {
michael@0 88 valueT += element.scrollTop || 0;
michael@0 89 valueL += element.scrollLeft || 0;
michael@0 90 element = element.parentNode;
michael@0 91 } while (element);
michael@0 92 return new MochiKit.Style.Coordinates(valueL, valueT);
michael@0 93 },
michael@0 94
michael@0 95 /** @id MochiKit.Position.within */
michael@0 96 within: function (element, x, y) {
michael@0 97 if (this.includeScrollOffsets) {
michael@0 98 return this.withinIncludingScrolloffsets(element, x, y);
michael@0 99 }
michael@0 100 this.xcomp = x;
michael@0 101 this.ycomp = y;
michael@0 102 this.offset = this.cumulativeOffset(element);
michael@0 103 if (element.style.position == "fixed") {
michael@0 104 this.offset.x += this.windowOffset.x;
michael@0 105 this.offset.y += this.windowOffset.y;
michael@0 106 }
michael@0 107
michael@0 108 return (y >= this.offset.y &&
michael@0 109 y < this.offset.y + element.offsetHeight &&
michael@0 110 x >= this.offset.x &&
michael@0 111 x < this.offset.x + element.offsetWidth);
michael@0 112 },
michael@0 113
michael@0 114 /** @id MochiKit.Position.withinIncludingScrolloffsets */
michael@0 115 withinIncludingScrolloffsets: function (element, x, y) {
michael@0 116 var offsetcache = this.realOffset(element);
michael@0 117
michael@0 118 this.xcomp = x + offsetcache.x - this.windowOffset.x;
michael@0 119 this.ycomp = y + offsetcache.y - this.windowOffset.y;
michael@0 120 this.offset = this.cumulativeOffset(element);
michael@0 121
michael@0 122 return (this.ycomp >= this.offset.y &&
michael@0 123 this.ycomp < this.offset.y + element.offsetHeight &&
michael@0 124 this.xcomp >= this.offset.x &&
michael@0 125 this.xcomp < this.offset.x + element.offsetWidth);
michael@0 126 },
michael@0 127
michael@0 128 // within must be called directly before
michael@0 129 /** @id MochiKit.Position.overlap */
michael@0 130 overlap: function (mode, element) {
michael@0 131 if (!mode) {
michael@0 132 return 0;
michael@0 133 }
michael@0 134 if (mode == 'vertical') {
michael@0 135 return ((this.offset.y + element.offsetHeight) - this.ycomp) /
michael@0 136 element.offsetHeight;
michael@0 137 }
michael@0 138 if (mode == 'horizontal') {
michael@0 139 return ((this.offset.x + element.offsetWidth) - this.xcomp) /
michael@0 140 element.offsetWidth;
michael@0 141 }
michael@0 142 },
michael@0 143
michael@0 144 /** @id MochiKit.Position.absolutize */
michael@0 145 absolutize: function (element) {
michael@0 146 element = MochiKit.DOM.getElement(element);
michael@0 147 if (element.style.position == 'absolute') {
michael@0 148 return;
michael@0 149 }
michael@0 150 MochiKit.Position.prepare();
michael@0 151
michael@0 152 var offsets = MochiKit.Position.positionedOffset(element);
michael@0 153 var width = element.clientWidth;
michael@0 154 var height = element.clientHeight;
michael@0 155
michael@0 156 var oldStyle = {
michael@0 157 'position': element.style.position,
michael@0 158 'left': offsets.x - parseFloat(element.style.left || 0),
michael@0 159 'top': offsets.y - parseFloat(element.style.top || 0),
michael@0 160 'width': element.style.width,
michael@0 161 'height': element.style.height
michael@0 162 };
michael@0 163
michael@0 164 element.style.position = 'absolute';
michael@0 165 element.style.top = offsets.y + 'px';
michael@0 166 element.style.left = offsets.x + 'px';
michael@0 167 element.style.width = width + 'px';
michael@0 168 element.style.height = height + 'px';
michael@0 169
michael@0 170 return oldStyle;
michael@0 171 },
michael@0 172
michael@0 173 /** @id MochiKit.Position.positionedOffset */
michael@0 174 positionedOffset: function (element) {
michael@0 175 var valueT = 0, valueL = 0;
michael@0 176 do {
michael@0 177 valueT += element.offsetTop || 0;
michael@0 178 valueL += element.offsetLeft || 0;
michael@0 179 element = element.offsetParent;
michael@0 180 if (element) {
michael@0 181 p = MochiKit.Style.getStyle(element, 'position');
michael@0 182 if (p == 'relative' || p == 'absolute') {
michael@0 183 break;
michael@0 184 }
michael@0 185 }
michael@0 186 } while (element);
michael@0 187 return new MochiKit.Style.Coordinates(valueL, valueT);
michael@0 188 },
michael@0 189
michael@0 190 /** @id MochiKit.Position.relativize */
michael@0 191 relativize: function (element, oldPos) {
michael@0 192 element = MochiKit.DOM.getElement(element);
michael@0 193 if (element.style.position == 'relative') {
michael@0 194 return;
michael@0 195 }
michael@0 196 MochiKit.Position.prepare();
michael@0 197
michael@0 198 var top = parseFloat(element.style.top || 0) -
michael@0 199 (oldPos['top'] || 0);
michael@0 200 var left = parseFloat(element.style.left || 0) -
michael@0 201 (oldPos['left'] || 0);
michael@0 202
michael@0 203 element.style.position = oldPos['position'];
michael@0 204 element.style.top = top + 'px';
michael@0 205 element.style.left = left + 'px';
michael@0 206 element.style.width = oldPos['width'];
michael@0 207 element.style.height = oldPos['height'];
michael@0 208 },
michael@0 209
michael@0 210 /** @id MochiKit.Position.clone */
michael@0 211 clone: function (source, target) {
michael@0 212 source = MochiKit.DOM.getElement(source);
michael@0 213 target = MochiKit.DOM.getElement(target);
michael@0 214 target.style.position = 'absolute';
michael@0 215 var offsets = this.cumulativeOffset(source);
michael@0 216 target.style.top = offsets.y + 'px';
michael@0 217 target.style.left = offsets.x + 'px';
michael@0 218 target.style.width = source.offsetWidth + 'px';
michael@0 219 target.style.height = source.offsetHeight + 'px';
michael@0 220 },
michael@0 221
michael@0 222 /** @id MochiKit.Position.page */
michael@0 223 page: function (forElement) {
michael@0 224 var valueT = 0;
michael@0 225 var valueL = 0;
michael@0 226
michael@0 227 var element = forElement;
michael@0 228 do {
michael@0 229 valueT += element.offsetTop || 0;
michael@0 230 valueL += element.offsetLeft || 0;
michael@0 231
michael@0 232 // Safari fix
michael@0 233 if (element.offsetParent == document.body && MochiKit.Style.getStyle(element, 'position') == 'absolute') {
michael@0 234 break;
michael@0 235 }
michael@0 236 } while (element = element.offsetParent);
michael@0 237
michael@0 238 element = forElement;
michael@0 239 do {
michael@0 240 valueT -= element.scrollTop || 0;
michael@0 241 valueL -= element.scrollLeft || 0;
michael@0 242 } while (element = element.parentNode);
michael@0 243
michael@0 244 return new MochiKit.Style.Coordinates(valueL, valueT);
michael@0 245 }
michael@0 246 });
michael@0 247
michael@0 248 MochiKit.Position.__new__ = function (win) {
michael@0 249 var m = MochiKit.Base;
michael@0 250 this.EXPORT_TAGS = {
michael@0 251 ':common': this.EXPORT,
michael@0 252 ':all': m.concat(this.EXPORT, this.EXPORT_OK)
michael@0 253 };
michael@0 254
michael@0 255 m.nameFunctions(this);
michael@0 256 };
michael@0 257
michael@0 258 MochiKit.Position.__new__(this);

mercurial