DragPane.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. define("dojox/layout/DragPane", ["dojo/_base/declare", "dijit/_Widget", "dojo/_base/html", "dojo/dom-style"],
  2. function(declare, Widget, htmlUtil, domStyle){
  3. /*===== var Widget = dijit._Widget; =====*/
  4. return declare("dojox.layout.DragPane", Widget, {
  5. // summary: Makes a pane's content dragable by/within it's surface
  6. //
  7. // description:
  8. // A small widget which takes a node with overflow:auto and
  9. // allows dragging to position the content. Useful with images,
  10. // or for just adding "something" to a overflow-able div.
  11. //
  12. // invert: Boolean
  13. // Naturally, the behavior is to invert the axis of the drag.
  14. // Setting invert:false will make the pane drag in the same
  15. // direction as the mouse.
  16. invert: true,
  17. postCreate: function(){
  18. this.connect(this.domNode, "onmousedown", "_down");
  19. this.connect(this.domNode, "onmouseleave", "_up");
  20. this.connect(this.domNode, "onmouseup", "_up");
  21. },
  22. _down: function(e){
  23. // summary: mousedown handler, start the dragging
  24. var t = this.domNode;
  25. e.preventDefault();
  26. domStyle.set(t, "cursor", "move");
  27. this._x = e.pageX;
  28. this._y = e.pageY;
  29. if ((this._x < t.offsetLeft + t.clientWidth) &&
  30. (this._y < t.offsetTop + t.clientHeight)) {
  31. htmlUtil.setSelectable(t,false);
  32. this._mover = this.connect(t, "onmousemove", "_move");
  33. }
  34. },
  35. _up: function(e){
  36. // summary: mouseup handler, stop the dragging
  37. htmlUtil.setSelectable(this.domNode,true);
  38. domStyle.set(this.domNode, "cursor", "pointer");
  39. this._mover && this.disconnect(this._mover);
  40. delete this._mover;
  41. },
  42. _move: function(e){
  43. // summary: mousemove listener, offset the scroll amount by the delta
  44. // since our last call.
  45. var mod = this.invert ? 1 : -1;
  46. this.domNode.scrollTop += (this._y - e.pageY) * mod;
  47. this.domNode.scrollLeft += (this._x - e.pageX) * mod;
  48. this._x = e.pageX;
  49. this._y = e.pageY;
  50. }
  51. });
  52. });