/hippo/src/main/webapp/ext/src/dd/DragTracker.js

http://hdbc.googlecode.com/ · JavaScript · 217 lines · 133 code · 30 blank · 54 comment · 14 complexity · 7d57cbf479fb97f1826830c425fb2d18 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.dd.DragTracker
  9. * @extends Ext.util.Observable
  10. */
  11. Ext.dd.DragTracker = function(config){
  12. Ext.apply(this, config);
  13. this.addEvents(
  14. /**
  15. * @event mousedown
  16. * @param {Object} this
  17. * @param {Object} e event object
  18. */
  19. 'mousedown',
  20. /**
  21. * @event mouseup
  22. * @param {Object} this
  23. * @param {Object} e event object
  24. */
  25. 'mouseup',
  26. /**
  27. * @event mousemove
  28. * @param {Object} this
  29. * @param {Object} e event object
  30. */
  31. 'mousemove',
  32. /**
  33. * @event dragstart
  34. * @param {Object} this
  35. * @param {Object} startXY the page coordinates of the event
  36. */
  37. 'dragstart',
  38. /**
  39. * @event dragend
  40. * @param {Object} this
  41. * @param {Object} e event object
  42. */
  43. 'dragend',
  44. /**
  45. * @event drag
  46. * @param {Object} this
  47. * @param {Object} e event object
  48. */
  49. 'drag'
  50. );
  51. this.dragRegion = new Ext.lib.Region(0,0,0,0);
  52. if(this.el){
  53. this.initEl(this.el);
  54. }
  55. }
  56. Ext.extend(Ext.dd.DragTracker, Ext.util.Observable, {
  57. /**
  58. * @cfg {Boolean} active
  59. * Defaults to <tt>false</tt>.
  60. */
  61. active: false,
  62. /**
  63. * @cfg {Number} tolerance
  64. * Defaults to <tt>5</tt>.
  65. */
  66. tolerance: 5,
  67. /**
  68. * @cfg {Boolean/Number} autoStart
  69. * Defaults to <tt>false</tt>. Specify <tt>true</tt> to defer trigger start by 1000 ms.
  70. * Specify a Number for the number of milliseconds to defer trigger start.
  71. */
  72. autoStart: false,
  73. initEl: function(el){
  74. this.el = Ext.get(el);
  75. el.on('mousedown', this.onMouseDown, this,
  76. this.delegate ? {delegate: this.delegate} : undefined);
  77. },
  78. destroy : function(){
  79. this.el.un('mousedown', this.onMouseDown, this);
  80. },
  81. onMouseDown: function(e, target){
  82. if(this.fireEvent('mousedown', this, e) !== false && this.onBeforeStart(e) !== false){
  83. this.startXY = this.lastXY = e.getXY();
  84. this.dragTarget = this.delegate ? target : this.el.dom;
  85. if(this.preventDefault !== false){
  86. e.preventDefault();
  87. }
  88. var doc = Ext.getDoc();
  89. doc.on('mouseup', this.onMouseUp, this);
  90. doc.on('mousemove', this.onMouseMove, this);
  91. doc.on('selectstart', this.stopSelect, this);
  92. if(this.autoStart){
  93. this.timer = this.triggerStart.defer(this.autoStart === true ? 1000 : this.autoStart, this);
  94. }
  95. }
  96. },
  97. onMouseMove: function(e, target){
  98. // HACK: IE hack to see if button was released outside of window. */
  99. if(this.active && Ext.isIE && !e.browserEvent.button){
  100. e.preventDefault();
  101. this.onMouseUp(e);
  102. return;
  103. }
  104. e.preventDefault();
  105. var xy = e.getXY(), s = this.startXY;
  106. this.lastXY = xy;
  107. if(!this.active){
  108. if(Math.abs(s[0]-xy[0]) > this.tolerance || Math.abs(s[1]-xy[1]) > this.tolerance){
  109. this.triggerStart();
  110. }else{
  111. return;
  112. }
  113. }
  114. this.fireEvent('mousemove', this, e);
  115. this.onDrag(e);
  116. this.fireEvent('drag', this, e);
  117. },
  118. onMouseUp: function(e){
  119. var doc = Ext.getDoc();
  120. doc.un('mousemove', this.onMouseMove, this);
  121. doc.un('mouseup', this.onMouseUp, this);
  122. doc.un('selectstart', this.stopSelect, this);
  123. e.preventDefault();
  124. this.clearStart();
  125. var wasActive = this.active;
  126. this.active = false;
  127. delete this.elRegion;
  128. this.fireEvent('mouseup', this, e);
  129. if(wasActive){
  130. this.onEnd(e);
  131. this.fireEvent('dragend', this, e);
  132. }
  133. },
  134. triggerStart: function(isTimer){
  135. this.clearStart();
  136. this.active = true;
  137. this.onStart(this.startXY);
  138. this.fireEvent('dragstart', this, this.startXY);
  139. },
  140. clearStart : function(){
  141. if(this.timer){
  142. clearTimeout(this.timer);
  143. delete this.timer;
  144. }
  145. },
  146. stopSelect : function(e){
  147. e.stopEvent();
  148. return false;
  149. },
  150. onBeforeStart : function(e){
  151. },
  152. onStart : function(xy){
  153. },
  154. onDrag : function(e){
  155. },
  156. onEnd : function(e){
  157. },
  158. getDragTarget : function(){
  159. return this.dragTarget;
  160. },
  161. getDragCt : function(){
  162. return this.el;
  163. },
  164. getXY : function(constrain){
  165. return constrain ?
  166. this.constrainModes[constrain].call(this, this.lastXY) : this.lastXY;
  167. },
  168. getOffset : function(constrain){
  169. var xy = this.getXY(constrain);
  170. var s = this.startXY;
  171. return [s[0]-xy[0], s[1]-xy[1]];
  172. },
  173. constrainModes: {
  174. 'point' : function(xy){
  175. if(!this.elRegion){
  176. this.elRegion = this.getDragCt().getRegion();
  177. }
  178. var dr = this.dragRegion;
  179. dr.left = xy[0];
  180. dr.top = xy[1];
  181. dr.right = xy[0];
  182. dr.bottom = xy[1];
  183. dr.constrainTo(this.elRegion);
  184. return [dr.left, dr.top];
  185. }
  186. }
  187. });