Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /*** |
michael@0 | 2 | MochiKit.DragAndDrop 1.4 |
michael@0 | 3 | |
michael@0 | 4 | See <http://mochikit.com/> for documentation, downloads, license, etc. |
michael@0 | 5 | |
michael@0 | 6 | Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) |
michael@0 | 7 | Mochi-ized By Thomas Herve (_firstname_@nimail.org) |
michael@0 | 8 | |
michael@0 | 9 | ***/ |
michael@0 | 10 | |
michael@0 | 11 | if (typeof(dojo) != 'undefined') { |
michael@0 | 12 | dojo.provide('MochiKit.DragAndDrop'); |
michael@0 | 13 | dojo.require('MochiKit.Base'); |
michael@0 | 14 | dojo.require('MochiKit.DOM'); |
michael@0 | 15 | dojo.require('MochiKit.Iter'); |
michael@0 | 16 | dojo.require('MochiKit.Visual'); |
michael@0 | 17 | dojo.require('MochiKit.Signal'); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | if (typeof(JSAN) != 'undefined') { |
michael@0 | 21 | JSAN.use("MochiKit.Base", []); |
michael@0 | 22 | JSAN.use("MochiKit.DOM", []); |
michael@0 | 23 | JSAN.use("MochiKit.Visual", []); |
michael@0 | 24 | JSAN.use("MochiKit.Iter", []); |
michael@0 | 25 | JSAN.use("MochiKit.Signal", []); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | try { |
michael@0 | 29 | if (typeof(MochiKit.Base) == 'undefined' || |
michael@0 | 30 | typeof(MochiKit.DOM) == 'undefined' || |
michael@0 | 31 | typeof(MochiKit.Visual) == 'undefined' || |
michael@0 | 32 | typeof(MochiKit.Signal) == 'undefined' || |
michael@0 | 33 | typeof(MochiKit.Iter) == 'undefined') { |
michael@0 | 34 | throw ""; |
michael@0 | 35 | } |
michael@0 | 36 | } catch (e) { |
michael@0 | 37 | throw "MochiKit.DragAndDrop depends on MochiKit.Base, MochiKit.DOM, MochiKit.Visual, MochiKit.Signal and MochiKit.Iter!"; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | if (typeof(MochiKit.DragAndDrop) == 'undefined') { |
michael@0 | 41 | MochiKit.DragAndDrop = {}; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | MochiKit.DragAndDrop.NAME = 'MochiKit.DragAndDrop'; |
michael@0 | 45 | MochiKit.DragAndDrop.VERSION = '1.4'; |
michael@0 | 46 | |
michael@0 | 47 | MochiKit.DragAndDrop.__repr__ = function () { |
michael@0 | 48 | return '[' + this.NAME + ' ' + this.VERSION + ']'; |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | MochiKit.DragAndDrop.toString = function () { |
michael@0 | 52 | return this.__repr__(); |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | MochiKit.DragAndDrop.EXPORT = [ |
michael@0 | 56 | "Droppable", |
michael@0 | 57 | "Draggable" |
michael@0 | 58 | ]; |
michael@0 | 59 | |
michael@0 | 60 | MochiKit.DragAndDrop.EXPORT_OK = [ |
michael@0 | 61 | "Droppables", |
michael@0 | 62 | "Draggables" |
michael@0 | 63 | ]; |
michael@0 | 64 | |
michael@0 | 65 | MochiKit.DragAndDrop.Droppables = { |
michael@0 | 66 | /*** |
michael@0 | 67 | |
michael@0 | 68 | Manage all droppables. Shouldn't be used, use the Droppable object instead. |
michael@0 | 69 | |
michael@0 | 70 | ***/ |
michael@0 | 71 | drops: [], |
michael@0 | 72 | |
michael@0 | 73 | remove: function (element) { |
michael@0 | 74 | this.drops = MochiKit.Base.filter(function (d) { |
michael@0 | 75 | return d.element != MochiKit.DOM.getElement(element); |
michael@0 | 76 | }, this.drops); |
michael@0 | 77 | }, |
michael@0 | 78 | |
michael@0 | 79 | register: function (drop) { |
michael@0 | 80 | this.drops.push(drop); |
michael@0 | 81 | }, |
michael@0 | 82 | |
michael@0 | 83 | unregister: function (drop) { |
michael@0 | 84 | this.drops = MochiKit.Base.filter(function (d) { |
michael@0 | 85 | return d != drop; |
michael@0 | 86 | }, this.drops); |
michael@0 | 87 | }, |
michael@0 | 88 | |
michael@0 | 89 | prepare: function (element) { |
michael@0 | 90 | MochiKit.Base.map(function (drop) { |
michael@0 | 91 | if (drop.isAccepted(element)) { |
michael@0 | 92 | if (drop.options.activeclass) { |
michael@0 | 93 | MochiKit.DOM.addElementClass(drop.element, |
michael@0 | 94 | drop.options.activeclass); |
michael@0 | 95 | } |
michael@0 | 96 | drop.options.onactive(drop.element, element); |
michael@0 | 97 | } |
michael@0 | 98 | }, this.drops); |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | findDeepestChild: function (drops) { |
michael@0 | 102 | deepest = drops[0]; |
michael@0 | 103 | |
michael@0 | 104 | for (i = 1; i < drops.length; ++i) { |
michael@0 | 105 | if (MochiKit.DOM.isParent(drops[i].element, deepest.element)) { |
michael@0 | 106 | deepest = drops[i]; |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | return deepest; |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | show: function (point, element) { |
michael@0 | 113 | if (!this.drops.length) { |
michael@0 | 114 | return; |
michael@0 | 115 | } |
michael@0 | 116 | var affected = []; |
michael@0 | 117 | |
michael@0 | 118 | if (this.last_active) { |
michael@0 | 119 | this.last_active.deactivate(); |
michael@0 | 120 | } |
michael@0 | 121 | MochiKit.Iter.forEach(this.drops, function (drop) { |
michael@0 | 122 | if (drop.isAffected(point, element)) { |
michael@0 | 123 | affected.push(drop); |
michael@0 | 124 | } |
michael@0 | 125 | }); |
michael@0 | 126 | if (affected.length > 0) { |
michael@0 | 127 | drop = this.findDeepestChild(affected); |
michael@0 | 128 | MochiKit.Position.within(drop.element, point.page.x, point.page.y); |
michael@0 | 129 | drop.options.onhover(element, drop.element, |
michael@0 | 130 | MochiKit.Position.overlap(drop.options.overlap, drop.element)); |
michael@0 | 131 | drop.activate(); |
michael@0 | 132 | } |
michael@0 | 133 | }, |
michael@0 | 134 | |
michael@0 | 135 | fire: function (event, element) { |
michael@0 | 136 | if (!this.last_active) { |
michael@0 | 137 | return; |
michael@0 | 138 | } |
michael@0 | 139 | MochiKit.Position.prepare(); |
michael@0 | 140 | |
michael@0 | 141 | if (this.last_active.isAffected(event.mouse(), element)) { |
michael@0 | 142 | this.last_active.options.ondrop(element, |
michael@0 | 143 | this.last_active.element, event); |
michael@0 | 144 | } |
michael@0 | 145 | }, |
michael@0 | 146 | |
michael@0 | 147 | reset: function (element) { |
michael@0 | 148 | MochiKit.Base.map(function (drop) { |
michael@0 | 149 | if (drop.options.activeclass) { |
michael@0 | 150 | MochiKit.DOM.removeElementClass(drop.element, |
michael@0 | 151 | drop.options.activeclass); |
michael@0 | 152 | } |
michael@0 | 153 | drop.options.ondesactive(drop.element, element); |
michael@0 | 154 | }, this.drops); |
michael@0 | 155 | if (this.last_active) { |
michael@0 | 156 | this.last_active.deactivate(); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | /** @id MochiKit.DragAndDrop.Droppable */ |
michael@0 | 162 | MochiKit.DragAndDrop.Droppable = function (element, options) { |
michael@0 | 163 | var cls = arguments.callee; |
michael@0 | 164 | if (!(this instanceof cls)) { |
michael@0 | 165 | return new cls(element, options); |
michael@0 | 166 | } |
michael@0 | 167 | this.__init__(element, options); |
michael@0 | 168 | }; |
michael@0 | 169 | |
michael@0 | 170 | MochiKit.DragAndDrop.Droppable.prototype = { |
michael@0 | 171 | /*** |
michael@0 | 172 | |
michael@0 | 173 | A droppable object. Simple use is to create giving an element: |
michael@0 | 174 | |
michael@0 | 175 | new MochiKit.DragAndDrop.Droppable('myelement'); |
michael@0 | 176 | |
michael@0 | 177 | Generally you'll want to define the 'ondrop' function and maybe the |
michael@0 | 178 | 'accept' option to filter draggables. |
michael@0 | 179 | |
michael@0 | 180 | ***/ |
michael@0 | 181 | __class__: MochiKit.DragAndDrop.Droppable, |
michael@0 | 182 | |
michael@0 | 183 | __init__: function (element, /* optional */options) { |
michael@0 | 184 | var d = MochiKit.DOM; |
michael@0 | 185 | var b = MochiKit.Base; |
michael@0 | 186 | this.element = d.getElement(element); |
michael@0 | 187 | this.options = b.update({ |
michael@0 | 188 | |
michael@0 | 189 | /** @id MochiKit.DragAndDrop.greedy */ |
michael@0 | 190 | greedy: true, |
michael@0 | 191 | |
michael@0 | 192 | /** @id MochiKit.DragAndDrop.hoverclass */ |
michael@0 | 193 | hoverclass: null, |
michael@0 | 194 | |
michael@0 | 195 | /** @id MochiKit.DragAndDrop.activeclass */ |
michael@0 | 196 | activeclass: null, |
michael@0 | 197 | |
michael@0 | 198 | /** @id MochiKit.DragAndDrop.hoverfunc */ |
michael@0 | 199 | hoverfunc: b.noop, |
michael@0 | 200 | |
michael@0 | 201 | /** @id MochiKit.DragAndDrop.accept */ |
michael@0 | 202 | accept: null, |
michael@0 | 203 | |
michael@0 | 204 | /** @id MochiKit.DragAndDrop.onactive */ |
michael@0 | 205 | onactive: b.noop, |
michael@0 | 206 | |
michael@0 | 207 | /** @id MochiKit.DragAndDrop.ondesactive */ |
michael@0 | 208 | ondesactive: b.noop, |
michael@0 | 209 | |
michael@0 | 210 | /** @id MochiKit.DragAndDrop.onhover */ |
michael@0 | 211 | onhover: b.noop, |
michael@0 | 212 | |
michael@0 | 213 | /** @id MochiKit.DragAndDrop.ondrop */ |
michael@0 | 214 | ondrop: b.noop, |
michael@0 | 215 | |
michael@0 | 216 | /** @id MochiKit.DragAndDrop.containment */ |
michael@0 | 217 | containment: [], |
michael@0 | 218 | tree: false |
michael@0 | 219 | }, options || {}); |
michael@0 | 220 | |
michael@0 | 221 | // cache containers |
michael@0 | 222 | this.options._containers = []; |
michael@0 | 223 | b.map(MochiKit.Base.bind(function (c) { |
michael@0 | 224 | this.options._containers.push(d.getElement(c)); |
michael@0 | 225 | }, this), this.options.containment); |
michael@0 | 226 | |
michael@0 | 227 | d.makePositioned(this.element); // fix IE |
michael@0 | 228 | |
michael@0 | 229 | MochiKit.DragAndDrop.Droppables.register(this); |
michael@0 | 230 | }, |
michael@0 | 231 | |
michael@0 | 232 | /** @id MochiKit.DragAndDrop.isContained */ |
michael@0 | 233 | isContained: function (element) { |
michael@0 | 234 | if (this.options._containers.length) { |
michael@0 | 235 | var containmentNode; |
michael@0 | 236 | if (this.options.tree) { |
michael@0 | 237 | containmentNode = element.treeNode; |
michael@0 | 238 | } else { |
michael@0 | 239 | containmentNode = element.parentNode; |
michael@0 | 240 | } |
michael@0 | 241 | return MochiKit.Iter.some(this.options._containers, function (c) { |
michael@0 | 242 | return containmentNode == c; |
michael@0 | 243 | }); |
michael@0 | 244 | } else { |
michael@0 | 245 | return true; |
michael@0 | 246 | } |
michael@0 | 247 | }, |
michael@0 | 248 | |
michael@0 | 249 | /** @id MochiKit.DragAndDrop.isAccepted */ |
michael@0 | 250 | isAccepted: function (element) { |
michael@0 | 251 | return ((!this.options.accept) || MochiKit.Iter.some( |
michael@0 | 252 | this.options.accept, function (c) { |
michael@0 | 253 | return MochiKit.DOM.hasElementClass(element, c); |
michael@0 | 254 | })); |
michael@0 | 255 | }, |
michael@0 | 256 | |
michael@0 | 257 | /** @id MochiKit.DragAndDrop.isAffected */ |
michael@0 | 258 | isAffected: function (point, element) { |
michael@0 | 259 | return ((this.element != element) && |
michael@0 | 260 | this.isContained(element) && |
michael@0 | 261 | this.isAccepted(element) && |
michael@0 | 262 | MochiKit.Position.within(this.element, point.page.x, |
michael@0 | 263 | point.page.y)); |
michael@0 | 264 | }, |
michael@0 | 265 | |
michael@0 | 266 | /** @id MochiKit.DragAndDrop.deactivate */ |
michael@0 | 267 | deactivate: function () { |
michael@0 | 268 | /*** |
michael@0 | 269 | |
michael@0 | 270 | A droppable is deactivate when a draggable has been over it and left. |
michael@0 | 271 | |
michael@0 | 272 | ***/ |
michael@0 | 273 | if (this.options.hoverclass) { |
michael@0 | 274 | MochiKit.DOM.removeElementClass(this.element, |
michael@0 | 275 | this.options.hoverclass); |
michael@0 | 276 | } |
michael@0 | 277 | this.options.hoverfunc(this.element, false); |
michael@0 | 278 | MochiKit.DragAndDrop.Droppables.last_active = null; |
michael@0 | 279 | }, |
michael@0 | 280 | |
michael@0 | 281 | /** @id MochiKit.DragAndDrop.activate */ |
michael@0 | 282 | activate: function () { |
michael@0 | 283 | /*** |
michael@0 | 284 | |
michael@0 | 285 | A droppable is active when a draggable is over it. |
michael@0 | 286 | |
michael@0 | 287 | ***/ |
michael@0 | 288 | if (this.options.hoverclass) { |
michael@0 | 289 | MochiKit.DOM.addElementClass(this.element, this.options.hoverclass); |
michael@0 | 290 | } |
michael@0 | 291 | this.options.hoverfunc(this.element, true); |
michael@0 | 292 | MochiKit.DragAndDrop.Droppables.last_active = this; |
michael@0 | 293 | }, |
michael@0 | 294 | |
michael@0 | 295 | /** @id MochiKit.DragAndDrop.destroy */ |
michael@0 | 296 | destroy: function () { |
michael@0 | 297 | /*** |
michael@0 | 298 | |
michael@0 | 299 | Delete this droppable. |
michael@0 | 300 | |
michael@0 | 301 | ***/ |
michael@0 | 302 | MochiKit.DragAndDrop.Droppables.unregister(this); |
michael@0 | 303 | }, |
michael@0 | 304 | |
michael@0 | 305 | /** @id MochiKit.DragAndDrop.repr */ |
michael@0 | 306 | repr: function () { |
michael@0 | 307 | return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]"; |
michael@0 | 308 | } |
michael@0 | 309 | }; |
michael@0 | 310 | |
michael@0 | 311 | MochiKit.DragAndDrop.Draggables = { |
michael@0 | 312 | /*** |
michael@0 | 313 | |
michael@0 | 314 | Manage draggables elements. Not intended to direct use. |
michael@0 | 315 | |
michael@0 | 316 | ***/ |
michael@0 | 317 | drags: [], |
michael@0 | 318 | |
michael@0 | 319 | register: function (draggable) { |
michael@0 | 320 | if (this.drags.length === 0) { |
michael@0 | 321 | var conn = MochiKit.Signal.connect; |
michael@0 | 322 | this.eventMouseUp = conn(document, 'onmouseup', this, this.endDrag); |
michael@0 | 323 | this.eventMouseMove = conn(document, 'onmousemove', this, |
michael@0 | 324 | this.updateDrag); |
michael@0 | 325 | this.eventKeypress = conn(document, 'onkeypress', this, |
michael@0 | 326 | this.keyPress); |
michael@0 | 327 | } |
michael@0 | 328 | this.drags.push(draggable); |
michael@0 | 329 | }, |
michael@0 | 330 | |
michael@0 | 331 | unregister: function (draggable) { |
michael@0 | 332 | this.drags = MochiKit.Base.filter(function (d) { |
michael@0 | 333 | return d != draggable; |
michael@0 | 334 | }, this.drags); |
michael@0 | 335 | if (this.drags.length === 0) { |
michael@0 | 336 | var disc = MochiKit.Signal.disconnect; |
michael@0 | 337 | disc(this.eventMouseUp); |
michael@0 | 338 | disc(this.eventMouseMove); |
michael@0 | 339 | disc(this.eventKeypress); |
michael@0 | 340 | } |
michael@0 | 341 | }, |
michael@0 | 342 | |
michael@0 | 343 | activate: function (draggable) { |
michael@0 | 344 | // allows keypress events if window is not currently focused |
michael@0 | 345 | // fails for Safari |
michael@0 | 346 | window.focus(); |
michael@0 | 347 | this.activeDraggable = draggable; |
michael@0 | 348 | }, |
michael@0 | 349 | |
michael@0 | 350 | deactivate: function () { |
michael@0 | 351 | this.activeDraggable = null; |
michael@0 | 352 | }, |
michael@0 | 353 | |
michael@0 | 354 | updateDrag: function (event) { |
michael@0 | 355 | if (!this.activeDraggable) { |
michael@0 | 356 | return; |
michael@0 | 357 | } |
michael@0 | 358 | var pointer = event.mouse(); |
michael@0 | 359 | // Mozilla-based browsers fire successive mousemove events with |
michael@0 | 360 | // the same coordinates, prevent needless redrawing (moz bug?) |
michael@0 | 361 | if (this._lastPointer && (MochiKit.Base.repr(this._lastPointer.page) == |
michael@0 | 362 | MochiKit.Base.repr(pointer.page))) { |
michael@0 | 363 | return; |
michael@0 | 364 | } |
michael@0 | 365 | this._lastPointer = pointer; |
michael@0 | 366 | this.activeDraggable.updateDrag(event, pointer); |
michael@0 | 367 | }, |
michael@0 | 368 | |
michael@0 | 369 | endDrag: function (event) { |
michael@0 | 370 | if (!this.activeDraggable) { |
michael@0 | 371 | return; |
michael@0 | 372 | } |
michael@0 | 373 | this._lastPointer = null; |
michael@0 | 374 | this.activeDraggable.endDrag(event); |
michael@0 | 375 | this.activeDraggable = null; |
michael@0 | 376 | }, |
michael@0 | 377 | |
michael@0 | 378 | keyPress: function (event) { |
michael@0 | 379 | if (this.activeDraggable) { |
michael@0 | 380 | this.activeDraggable.keyPress(event); |
michael@0 | 381 | } |
michael@0 | 382 | }, |
michael@0 | 383 | |
michael@0 | 384 | notify: function (eventName, draggable, event) { |
michael@0 | 385 | MochiKit.Signal.signal(this, eventName, draggable, event); |
michael@0 | 386 | } |
michael@0 | 387 | }; |
michael@0 | 388 | |
michael@0 | 389 | /** @id MochiKit.DragAndDrop.Draggable */ |
michael@0 | 390 | MochiKit.DragAndDrop.Draggable = function (element, options) { |
michael@0 | 391 | var cls = arguments.callee; |
michael@0 | 392 | if (!(this instanceof cls)) { |
michael@0 | 393 | return new cls(element, options); |
michael@0 | 394 | } |
michael@0 | 395 | this.__init__(element, options); |
michael@0 | 396 | }; |
michael@0 | 397 | |
michael@0 | 398 | MochiKit.DragAndDrop.Draggable.prototype = { |
michael@0 | 399 | /*** |
michael@0 | 400 | |
michael@0 | 401 | A draggable object. Simple instantiate : |
michael@0 | 402 | |
michael@0 | 403 | new MochiKit.DragAndDrop.Draggable('myelement'); |
michael@0 | 404 | |
michael@0 | 405 | ***/ |
michael@0 | 406 | __class__ : MochiKit.DragAndDrop.Draggable, |
michael@0 | 407 | |
michael@0 | 408 | __init__: function (element, /* optional */options) { |
michael@0 | 409 | var v = MochiKit.Visual; |
michael@0 | 410 | var b = MochiKit.Base; |
michael@0 | 411 | options = b.update({ |
michael@0 | 412 | |
michael@0 | 413 | /** @id MochiKit.DragAndDrop.handle */ |
michael@0 | 414 | handle: false, |
michael@0 | 415 | |
michael@0 | 416 | /** @id MochiKit.DragAndDrop.starteffect */ |
michael@0 | 417 | starteffect: function (innerelement) { |
michael@0 | 418 | this._savedOpacity = MochiKit.Style.getStyle(innerelement, 'opacity') || 1.0; |
michael@0 | 419 | new v.Opacity(innerelement, {duration:0.2, from:this._savedOpacity, to:0.7}); |
michael@0 | 420 | }, |
michael@0 | 421 | /** @id MochiKit.DragAndDrop.reverteffect */ |
michael@0 | 422 | reverteffect: function (innerelement, top_offset, left_offset) { |
michael@0 | 423 | var dur = Math.sqrt(Math.abs(top_offset^2) + |
michael@0 | 424 | Math.abs(left_offset^2))*0.02; |
michael@0 | 425 | return new v.Move(innerelement, |
michael@0 | 426 | {x: -left_offset, y: -top_offset, duration: dur}); |
michael@0 | 427 | }, |
michael@0 | 428 | |
michael@0 | 429 | /** @id MochiKit.DragAndDrop.endeffect */ |
michael@0 | 430 | endeffect: function (innerelement) { |
michael@0 | 431 | new v.Opacity(innerelement, {duration:0.2, from:0.7, to:this._savedOpacity}); |
michael@0 | 432 | }, |
michael@0 | 433 | |
michael@0 | 434 | /** @id MochiKit.DragAndDrop.onchange */ |
michael@0 | 435 | onchange: b.noop, |
michael@0 | 436 | |
michael@0 | 437 | /** @id MochiKit.DragAndDrop.zindex */ |
michael@0 | 438 | zindex: 1000, |
michael@0 | 439 | |
michael@0 | 440 | /** @id MochiKit.DragAndDrop.revert */ |
michael@0 | 441 | revert: false, |
michael@0 | 442 | |
michael@0 | 443 | /** @id MochiKit.DragAndDrop.scroll */ |
michael@0 | 444 | scroll: false, |
michael@0 | 445 | |
michael@0 | 446 | /** @id MochiKit.DragAndDrop.scrollSensitivity */ |
michael@0 | 447 | scrollSensitivity: 20, |
michael@0 | 448 | |
michael@0 | 449 | /** @id MochiKit.DragAndDrop.scrollSpeed */ |
michael@0 | 450 | scrollSpeed: 15, |
michael@0 | 451 | // false, or xy or [x, y] or function (x, y){return [x, y];} |
michael@0 | 452 | |
michael@0 | 453 | /** @id MochiKit.DragAndDrop.snap */ |
michael@0 | 454 | snap: false |
michael@0 | 455 | }, options || {}); |
michael@0 | 456 | |
michael@0 | 457 | var d = MochiKit.DOM; |
michael@0 | 458 | this.element = d.getElement(element); |
michael@0 | 459 | |
michael@0 | 460 | if (options.handle && (typeof(options.handle) == 'string')) { |
michael@0 | 461 | this.handle = d.getFirstElementByTagAndClassName(null, |
michael@0 | 462 | options.handle, this.element); |
michael@0 | 463 | } |
michael@0 | 464 | if (!this.handle) { |
michael@0 | 465 | this.handle = d.getElement(options.handle); |
michael@0 | 466 | } |
michael@0 | 467 | if (!this.handle) { |
michael@0 | 468 | this.handle = this.element; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | if (options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { |
michael@0 | 472 | options.scroll = d.getElement(options.scroll); |
michael@0 | 473 | this._isScrollChild = MochiKit.DOM.isChildNode(this.element, options.scroll); |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | d.makePositioned(this.element); // fix IE |
michael@0 | 477 | |
michael@0 | 478 | this.delta = this.currentDelta(); |
michael@0 | 479 | this.options = options; |
michael@0 | 480 | this.dragging = false; |
michael@0 | 481 | |
michael@0 | 482 | this.eventMouseDown = MochiKit.Signal.connect(this.handle, |
michael@0 | 483 | 'onmousedown', this, this.initDrag); |
michael@0 | 484 | MochiKit.DragAndDrop.Draggables.register(this); |
michael@0 | 485 | }, |
michael@0 | 486 | |
michael@0 | 487 | /** @id MochiKit.DragAndDrop.destroy */ |
michael@0 | 488 | destroy: function () { |
michael@0 | 489 | MochiKit.Signal.disconnect(this.eventMouseDown); |
michael@0 | 490 | MochiKit.DragAndDrop.Draggables.unregister(this); |
michael@0 | 491 | }, |
michael@0 | 492 | |
michael@0 | 493 | /** @id MochiKit.DragAndDrop.currentDelta */ |
michael@0 | 494 | currentDelta: function () { |
michael@0 | 495 | var s = MochiKit.Style.getStyle; |
michael@0 | 496 | return [ |
michael@0 | 497 | parseInt(s(this.element, 'left') || '0'), |
michael@0 | 498 | parseInt(s(this.element, 'top') || '0')]; |
michael@0 | 499 | }, |
michael@0 | 500 | |
michael@0 | 501 | /** @id MochiKit.DragAndDrop.initDrag */ |
michael@0 | 502 | initDrag: function (event) { |
michael@0 | 503 | if (!event.mouse().button.left) { |
michael@0 | 504 | return; |
michael@0 | 505 | } |
michael@0 | 506 | // abort on form elements, fixes a Firefox issue |
michael@0 | 507 | var src = event.target(); |
michael@0 | 508 | var tagName = (src.tagName || '').toUpperCase(); |
michael@0 | 509 | if (tagName === 'INPUT' || tagName === 'SELECT' || |
michael@0 | 510 | tagName === 'OPTION' || tagName === 'BUTTON' || |
michael@0 | 511 | tagName === 'TEXTAREA') { |
michael@0 | 512 | return; |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | if (this._revert) { |
michael@0 | 516 | this._revert.cancel(); |
michael@0 | 517 | this._revert = null; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | var pointer = event.mouse(); |
michael@0 | 521 | var pos = MochiKit.Position.cumulativeOffset(this.element); |
michael@0 | 522 | this.offset = [pointer.page.x - pos.x, pointer.page.y - pos.y]; |
michael@0 | 523 | |
michael@0 | 524 | MochiKit.DragAndDrop.Draggables.activate(this); |
michael@0 | 525 | event.stop(); |
michael@0 | 526 | }, |
michael@0 | 527 | |
michael@0 | 528 | /** @id MochiKit.DragAndDrop.startDrag */ |
michael@0 | 529 | startDrag: function (event) { |
michael@0 | 530 | this.dragging = true; |
michael@0 | 531 | if (this.options.selectclass) { |
michael@0 | 532 | MochiKit.DOM.addElementClass(this.element, |
michael@0 | 533 | this.options.selectclass); |
michael@0 | 534 | } |
michael@0 | 535 | if (this.options.zindex) { |
michael@0 | 536 | this.originalZ = parseInt(MochiKit.Style.getStyle(this.element, |
michael@0 | 537 | 'z-index') || '0'); |
michael@0 | 538 | this.element.style.zIndex = this.options.zindex; |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | if (this.options.ghosting) { |
michael@0 | 542 | this._clone = this.element.cloneNode(true); |
michael@0 | 543 | this.ghostPosition = MochiKit.Position.absolutize(this.element); |
michael@0 | 544 | this.element.parentNode.insertBefore(this._clone, this.element); |
michael@0 | 545 | } |
michael@0 | 546 | |
michael@0 | 547 | if (this.options.scroll) { |
michael@0 | 548 | if (this.options.scroll == window) { |
michael@0 | 549 | var where = this._getWindowScroll(this.options.scroll); |
michael@0 | 550 | this.originalScrollLeft = where.left; |
michael@0 | 551 | this.originalScrollTop = where.top; |
michael@0 | 552 | } else { |
michael@0 | 553 | this.originalScrollLeft = this.options.scroll.scrollLeft; |
michael@0 | 554 | this.originalScrollTop = this.options.scroll.scrollTop; |
michael@0 | 555 | } |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | MochiKit.DragAndDrop.Droppables.prepare(this.element); |
michael@0 | 559 | MochiKit.DragAndDrop.Draggables.notify('start', this, event); |
michael@0 | 560 | if (this.options.starteffect) { |
michael@0 | 561 | this.options.starteffect(this.element); |
michael@0 | 562 | } |
michael@0 | 563 | }, |
michael@0 | 564 | |
michael@0 | 565 | /** @id MochiKit.DragAndDrop.updateDrag */ |
michael@0 | 566 | updateDrag: function (event, pointer) { |
michael@0 | 567 | if (!this.dragging) { |
michael@0 | 568 | this.startDrag(event); |
michael@0 | 569 | } |
michael@0 | 570 | MochiKit.Position.prepare(); |
michael@0 | 571 | MochiKit.DragAndDrop.Droppables.show(pointer, this.element); |
michael@0 | 572 | MochiKit.DragAndDrop.Draggables.notify('drag', this, event); |
michael@0 | 573 | this.draw(pointer); |
michael@0 | 574 | this.options.onchange(this); |
michael@0 | 575 | |
michael@0 | 576 | if (this.options.scroll) { |
michael@0 | 577 | this.stopScrolling(); |
michael@0 | 578 | var p, q; |
michael@0 | 579 | if (this.options.scroll == window) { |
michael@0 | 580 | var s = this._getWindowScroll(this.options.scroll); |
michael@0 | 581 | p = new MochiKit.Style.Coordinates(s.left, s.top); |
michael@0 | 582 | q = new MochiKit.Style.Coordinates(s.left + s.width, |
michael@0 | 583 | s.top + s.height); |
michael@0 | 584 | } else { |
michael@0 | 585 | p = MochiKit.Position.page(this.options.scroll); |
michael@0 | 586 | p.x += this.options.scroll.scrollLeft; |
michael@0 | 587 | p.y += this.options.scroll.scrollTop; |
michael@0 | 588 | p.x += (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0); |
michael@0 | 589 | p.y += (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0); |
michael@0 | 590 | q = new MochiKit.Style.Coordinates(p.x + this.options.scroll.offsetWidth, |
michael@0 | 591 | p.y + this.options.scroll.offsetHeight); |
michael@0 | 592 | } |
michael@0 | 593 | var speed = [0, 0]; |
michael@0 | 594 | if (pointer.page.x > (q.x - this.options.scrollSensitivity)) { |
michael@0 | 595 | speed[0] = pointer.page.x - (q.x - this.options.scrollSensitivity); |
michael@0 | 596 | } else if (pointer.page.x < (p.x + this.options.scrollSensitivity)) { |
michael@0 | 597 | speed[0] = pointer.page.x - (p.x + this.options.scrollSensitivity); |
michael@0 | 598 | } |
michael@0 | 599 | if (pointer.page.y > (q.y - this.options.scrollSensitivity)) { |
michael@0 | 600 | speed[1] = pointer.page.y - (q.y - this.options.scrollSensitivity); |
michael@0 | 601 | } else if (pointer.page.y < (p.y + this.options.scrollSensitivity)) { |
michael@0 | 602 | speed[1] = pointer.page.y - (p.y + this.options.scrollSensitivity); |
michael@0 | 603 | } |
michael@0 | 604 | this.startScrolling(speed); |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | // fix AppleWebKit rendering |
michael@0 | 608 | if (/AppleWebKit'/.test(navigator.appVersion)) { |
michael@0 | 609 | window.scrollBy(0, 0); |
michael@0 | 610 | } |
michael@0 | 611 | event.stop(); |
michael@0 | 612 | }, |
michael@0 | 613 | |
michael@0 | 614 | /** @id MochiKit.DragAndDrop.finishDrag */ |
michael@0 | 615 | finishDrag: function (event, success) { |
michael@0 | 616 | var dr = MochiKit.DragAndDrop; |
michael@0 | 617 | this.dragging = false; |
michael@0 | 618 | if (this.options.selectclass) { |
michael@0 | 619 | MochiKit.DOM.removeElementClass(this.element, |
michael@0 | 620 | this.options.selectclass); |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | if (this.options.ghosting) { |
michael@0 | 624 | // XXX: from a user point of view, it would be better to remove |
michael@0 | 625 | // the node only *after* the MochiKit.Visual.Move end when used |
michael@0 | 626 | // with revert. |
michael@0 | 627 | MochiKit.Position.relativize(this.element, this.ghostPosition); |
michael@0 | 628 | MochiKit.DOM.removeElement(this._clone); |
michael@0 | 629 | this._clone = null; |
michael@0 | 630 | } |
michael@0 | 631 | |
michael@0 | 632 | if (success) { |
michael@0 | 633 | dr.Droppables.fire(event, this.element); |
michael@0 | 634 | } |
michael@0 | 635 | dr.Draggables.notify('end', this, event); |
michael@0 | 636 | |
michael@0 | 637 | var revert = this.options.revert; |
michael@0 | 638 | if (revert && typeof(revert) == 'function') { |
michael@0 | 639 | revert = revert(this.element); |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | var d = this.currentDelta(); |
michael@0 | 643 | if (revert && this.options.reverteffect) { |
michael@0 | 644 | this._revert = this.options.reverteffect(this.element, |
michael@0 | 645 | d[1] - this.delta[1], d[0] - this.delta[0]); |
michael@0 | 646 | } else { |
michael@0 | 647 | this.delta = d; |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | if (this.options.zindex) { |
michael@0 | 651 | this.element.style.zIndex = this.originalZ; |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | if (this.options.endeffect) { |
michael@0 | 655 | this.options.endeffect(this.element); |
michael@0 | 656 | } |
michael@0 | 657 | |
michael@0 | 658 | dr.Draggables.deactivate(); |
michael@0 | 659 | dr.Droppables.reset(this.element); |
michael@0 | 660 | }, |
michael@0 | 661 | |
michael@0 | 662 | /** @id MochiKit.DragAndDrop.keyPress */ |
michael@0 | 663 | keyPress: function (event) { |
michael@0 | 664 | if (event.key().string != "KEY_ESCAPE") { |
michael@0 | 665 | return; |
michael@0 | 666 | } |
michael@0 | 667 | this.finishDrag(event, false); |
michael@0 | 668 | event.stop(); |
michael@0 | 669 | }, |
michael@0 | 670 | |
michael@0 | 671 | /** @id MochiKit.DragAndDrop.endDrag */ |
michael@0 | 672 | endDrag: function (event) { |
michael@0 | 673 | if (!this.dragging) { |
michael@0 | 674 | return; |
michael@0 | 675 | } |
michael@0 | 676 | this.stopScrolling(); |
michael@0 | 677 | this.finishDrag(event, true); |
michael@0 | 678 | event.stop(); |
michael@0 | 679 | }, |
michael@0 | 680 | |
michael@0 | 681 | /** @id MochiKit.DragAndDrop.draw */ |
michael@0 | 682 | draw: function (point) { |
michael@0 | 683 | var pos = MochiKit.Position.cumulativeOffset(this.element); |
michael@0 | 684 | var d = this.currentDelta(); |
michael@0 | 685 | pos.x -= d[0]; |
michael@0 | 686 | pos.y -= d[1]; |
michael@0 | 687 | |
michael@0 | 688 | if (this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { |
michael@0 | 689 | pos.x -= this.options.scroll.scrollLeft - this.originalScrollLeft; |
michael@0 | 690 | pos.y -= this.options.scroll.scrollTop - this.originalScrollTop; |
michael@0 | 691 | } |
michael@0 | 692 | |
michael@0 | 693 | var p = [point.page.x - pos.x - this.offset[0], |
michael@0 | 694 | point.page.y - pos.y - this.offset[1]]; |
michael@0 | 695 | |
michael@0 | 696 | if (this.options.snap) { |
michael@0 | 697 | if (typeof(this.options.snap) == 'function') { |
michael@0 | 698 | p = this.options.snap(p[0], p[1]); |
michael@0 | 699 | } else { |
michael@0 | 700 | if (this.options.snap instanceof Array) { |
michael@0 | 701 | var i = -1; |
michael@0 | 702 | p = MochiKit.Base.map(MochiKit.Base.bind(function (v) { |
michael@0 | 703 | i += 1; |
michael@0 | 704 | return Math.round(v/this.options.snap[i]) * |
michael@0 | 705 | this.options.snap[i]; |
michael@0 | 706 | }, this), p); |
michael@0 | 707 | } else { |
michael@0 | 708 | p = MochiKit.Base.map(MochiKit.Base.bind(function (v) { |
michael@0 | 709 | return Math.round(v/this.options.snap) * |
michael@0 | 710 | this.options.snap; |
michael@0 | 711 | }, this), p); |
michael@0 | 712 | } |
michael@0 | 713 | } |
michael@0 | 714 | } |
michael@0 | 715 | var style = this.element.style; |
michael@0 | 716 | if ((!this.options.constraint) || |
michael@0 | 717 | (this.options.constraint == 'horizontal')) { |
michael@0 | 718 | style.left = p[0] + 'px'; |
michael@0 | 719 | } |
michael@0 | 720 | if ((!this.options.constraint) || |
michael@0 | 721 | (this.options.constraint == 'vertical')) { |
michael@0 | 722 | style.top = p[1] + 'px'; |
michael@0 | 723 | } |
michael@0 | 724 | if (style.visibility == 'hidden') { |
michael@0 | 725 | style.visibility = ''; // fix gecko rendering |
michael@0 | 726 | } |
michael@0 | 727 | }, |
michael@0 | 728 | |
michael@0 | 729 | /** @id MochiKit.DragAndDrop.stopScrolling */ |
michael@0 | 730 | stopScrolling: function () { |
michael@0 | 731 | if (this.scrollInterval) { |
michael@0 | 732 | clearInterval(this.scrollInterval); |
michael@0 | 733 | this.scrollInterval = null; |
michael@0 | 734 | MochiKit.DragAndDrop.Draggables._lastScrollPointer = null; |
michael@0 | 735 | } |
michael@0 | 736 | }, |
michael@0 | 737 | |
michael@0 | 738 | /** @id MochiKit.DragAndDrop.startScrolling */ |
michael@0 | 739 | startScrolling: function (speed) { |
michael@0 | 740 | if (!speed[0] && !speed[1]) { |
michael@0 | 741 | return; |
michael@0 | 742 | } |
michael@0 | 743 | this.scrollSpeed = [speed[0] * this.options.scrollSpeed, |
michael@0 | 744 | speed[1] * this.options.scrollSpeed]; |
michael@0 | 745 | this.lastScrolled = new Date(); |
michael@0 | 746 | this.scrollInterval = setInterval(MochiKit.Base.bind(this.scroll, this), 10); |
michael@0 | 747 | }, |
michael@0 | 748 | |
michael@0 | 749 | /** @id MochiKit.DragAndDrop.scroll */ |
michael@0 | 750 | scroll: function () { |
michael@0 | 751 | var current = new Date(); |
michael@0 | 752 | var delta = current - this.lastScrolled; |
michael@0 | 753 | this.lastScrolled = current; |
michael@0 | 754 | |
michael@0 | 755 | if (this.options.scroll == window) { |
michael@0 | 756 | var s = this._getWindowScroll(this.options.scroll); |
michael@0 | 757 | if (this.scrollSpeed[0] || this.scrollSpeed[1]) { |
michael@0 | 758 | var dm = delta / 1000; |
michael@0 | 759 | this.options.scroll.scrollTo(s.left + dm * this.scrollSpeed[0], |
michael@0 | 760 | s.top + dm * this.scrollSpeed[1]); |
michael@0 | 761 | } |
michael@0 | 762 | } else { |
michael@0 | 763 | this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; |
michael@0 | 764 | this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; |
michael@0 | 765 | } |
michael@0 | 766 | |
michael@0 | 767 | var d = MochiKit.DragAndDrop; |
michael@0 | 768 | |
michael@0 | 769 | MochiKit.Position.prepare(); |
michael@0 | 770 | d.Droppables.show(d.Draggables._lastPointer, this.element); |
michael@0 | 771 | d.Draggables.notify('drag', this); |
michael@0 | 772 | if (this._isScrollChild) { |
michael@0 | 773 | d.Draggables._lastScrollPointer = d.Draggables._lastScrollPointer || d.Draggables._lastPointer; |
michael@0 | 774 | d.Draggables._lastScrollPointer.x += this.scrollSpeed[0] * delta / 1000; |
michael@0 | 775 | d.Draggables._lastScrollPointer.y += this.scrollSpeed[1] * delta / 1000; |
michael@0 | 776 | if (d.Draggables._lastScrollPointer.x < 0) { |
michael@0 | 777 | d.Draggables._lastScrollPointer.x = 0; |
michael@0 | 778 | } |
michael@0 | 779 | if (d.Draggables._lastScrollPointer.y < 0) { |
michael@0 | 780 | d.Draggables._lastScrollPointer.y = 0; |
michael@0 | 781 | } |
michael@0 | 782 | this.draw(d.Draggables._lastScrollPointer); |
michael@0 | 783 | } |
michael@0 | 784 | |
michael@0 | 785 | this.options.onchange(this); |
michael@0 | 786 | }, |
michael@0 | 787 | |
michael@0 | 788 | _getWindowScroll: function (win) { |
michael@0 | 789 | var vp, w, h; |
michael@0 | 790 | MochiKit.DOM.withWindow(win, function () { |
michael@0 | 791 | vp = MochiKit.Style.getViewportPosition(win.document); |
michael@0 | 792 | }); |
michael@0 | 793 | if (win.innerWidth) { |
michael@0 | 794 | w = win.innerWidth; |
michael@0 | 795 | h = win.innerHeight; |
michael@0 | 796 | } else if (win.document.documentElement && win.document.documentElement.clientWidth) { |
michael@0 | 797 | w = win.document.documentElement.clientWidth; |
michael@0 | 798 | h = win.document.documentElement.clientHeight; |
michael@0 | 799 | } else { |
michael@0 | 800 | w = win.document.body.offsetWidth; |
michael@0 | 801 | h = win.document.body.offsetHeight; |
michael@0 | 802 | } |
michael@0 | 803 | return {top: vp.x, left: vp.y, width: w, height: h}; |
michael@0 | 804 | }, |
michael@0 | 805 | |
michael@0 | 806 | /** @id MochiKit.DragAndDrop.repr */ |
michael@0 | 807 | repr: function () { |
michael@0 | 808 | return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]"; |
michael@0 | 809 | } |
michael@0 | 810 | }; |
michael@0 | 811 | |
michael@0 | 812 | MochiKit.DragAndDrop.__new__ = function () { |
michael@0 | 813 | MochiKit.Base.nameFunctions(this); |
michael@0 | 814 | |
michael@0 | 815 | this.EXPORT_TAGS = { |
michael@0 | 816 | ":common": this.EXPORT, |
michael@0 | 817 | ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 818 | }; |
michael@0 | 819 | }; |
michael@0 | 820 | |
michael@0 | 821 | MochiKit.DragAndDrop.__new__(); |
michael@0 | 822 | |
michael@0 | 823 | MochiKit.Base._exportSymbols(this, MochiKit.DragAndDrop); |
michael@0 | 824 |