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