DragAndDrop.js 24 KB

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