DragTracker.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.dd.DragTracker = function(config) {
  7. Ext.apply(this, config);
  8. this.addEvents('mousedown', 'mouseup', 'mousemove', 'dragstart', 'dragend',
  9. 'drag');
  10. this.dragRegion = new Ext.lib.Region(0, 0, 0, 0);
  11. if (this.el) {
  12. this.initEl(this.el);
  13. }
  14. }
  15. Ext.extend(Ext.dd.DragTracker, Ext.util.Observable, {
  16. active : false,
  17. tolerance : 5,
  18. autoStart : false,
  19. initEl : function(el) {
  20. this.el = Ext.get(el);
  21. el.on('mousedown', this.onMouseDown, this, this.delegate ? {
  22. delegate : this.delegate
  23. } : undefined);
  24. },
  25. destroy : function() {
  26. this.el.un('mousedown', this.onMouseDown, this);
  27. },
  28. onMouseDown : function(e, target) {
  29. if (this.fireEvent('mousedown', this, e) !== false
  30. && this.onBeforeStart(e) !== false) {
  31. this.startXY = this.lastXY = e.getXY();
  32. this.dragTarget = this.delegate ? target : this.el.dom;
  33. e.preventDefault();
  34. var doc = Ext.getDoc();
  35. doc.on('mouseup', this.onMouseUp, this);
  36. doc.on('mousemove', this.onMouseMove, this);
  37. doc.on('selectstart', this.stopSelect, this);
  38. if (this.autoStart) {
  39. this.timer = this.triggerStart
  40. .defer( this.autoStart === true
  41. ? 1000
  42. : this.autoStart, this);
  43. }
  44. }
  45. },
  46. onMouseMove : function(e, target) {
  47. e.preventDefault();
  48. var xy = e.getXY(), s = this.startXY;
  49. this.lastXY = xy;
  50. if (!this.active) {
  51. if (Math.abs(s[0] - xy[0]) > this.tolerance
  52. || Math.abs(s[1] - xy[1]) > this.tolerance) {
  53. this.triggerStart();
  54. } else {
  55. return;
  56. }
  57. }
  58. this.fireEvent('mousemove', this, e);
  59. this.onDrag(e);
  60. this.fireEvent('drag', this, e);
  61. },
  62. onMouseUp : function(e) {
  63. var doc = Ext.getDoc();
  64. doc.un('mousemove', this.onMouseMove, this);
  65. doc.un('mouseup', this.onMouseUp, this);
  66. doc.un('selectstart', this.stopSelect, this);
  67. e.preventDefault();
  68. this.clearStart();
  69. this.active = false;
  70. delete this.elRegion;
  71. this.fireEvent('mouseup', this, e);
  72. this.onEnd(e);
  73. this.fireEvent('dragend', this, e);
  74. },
  75. triggerStart : function(isTimer) {
  76. this.clearStart();
  77. this.active = true;
  78. this.onStart(this.startXY);
  79. this.fireEvent('dragstart', this, this.startXY);
  80. },
  81. clearStart : function() {
  82. if (this.timer) {
  83. clearTimeout(this.timer);
  84. delete this.timer;
  85. }
  86. },
  87. stopSelect : function(e) {
  88. e.stopEvent();
  89. return false;
  90. },
  91. onBeforeStart : function(e) {
  92. },
  93. onStart : function(xy) {
  94. },
  95. onDrag : function(e) {
  96. },
  97. onEnd : function(e) {
  98. },
  99. getDragTarget : function() {
  100. return this.dragTarget;
  101. },
  102. getDragCt : function() {
  103. return this.el;
  104. },
  105. getXY : function(constrain) {
  106. return constrain ? this.constrainModes[constrain].call(this,
  107. this.lastXY) : this.lastXY;
  108. },
  109. getOffset : function(constrain) {
  110. var xy = this.getXY(constrain);
  111. var s = this.startXY;
  112. return [s[0] - xy[0], s[1] - xy[1]];
  113. },
  114. constrainModes : {
  115. 'point' : function(xy) {
  116. if (!this.elRegion) {
  117. this.elRegion = this.getDragCt().getRegion();
  118. }
  119. var dr = this.dragRegion;
  120. dr.left = xy[0];
  121. dr.top = xy[1];
  122. dr.right = xy[0];
  123. dr.bottom = xy[1];
  124. dr.constrainTo(this.elRegion);
  125. return [dr.left, dr.top];
  126. }
  127. }
  128. });