DropZone.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * This class provides a container DD instance that allows dropping on multiple child target nodes.
  3. *
  4. * By default, this class requires that child nodes accepting drop are registered with {@link Ext.dd.Registry}.
  5. * However a simpler way to allow a DropZone to manage any number of target elements is to configure the
  6. * DropZone with an implementation of {@link #getTargetFromEvent} which interrogates the passed
  7. * mouse event to see if it has taken place within an element, or class of elements. This is easily done
  8. * by using the event's {@link Ext.EventObject#getTarget getTarget} method to identify a node based on a
  9. * {@link Ext.DomQuery} selector.
  10. *
  11. * Once the DropZone has detected through calling getTargetFromEvent, that the mouse is over
  12. * a drop target, that target is passed as the first parameter to {@link #onNodeEnter}, {@link #onNodeOver},
  13. * {@link #onNodeOut}, {@link #onNodeDrop}. You may configure the instance of DropZone with implementations
  14. * of these methods to provide application-specific behaviour for these events to update both
  15. * application state, and UI state.
  16. *
  17. * For example to make a GridPanel a cooperating target with the example illustrated in
  18. * {@link Ext.dd.DragZone DragZone}, the following technique might be used:
  19. *
  20. * myGridPanel.on('render', function() {
  21. * myGridPanel.dropZone = new Ext.dd.DropZone(myGridPanel.getView().scroller, {
  22. *
  23. * // If the mouse is over a grid row, return that node. This is
  24. * // provided as the "target" parameter in all "onNodeXXXX" node event handling functions
  25. * getTargetFromEvent: function(e) {
  26. * return e.getTarget(myGridPanel.getView().rowSelector);
  27. * },
  28. *
  29. * // On entry into a target node, highlight that node.
  30. * onNodeEnter : function(target, dd, e, data){
  31. * Ext.fly(target).addCls('my-row-highlight-class');
  32. * },
  33. *
  34. * // On exit from a target node, unhighlight that node.
  35. * onNodeOut : function(target, dd, e, data){
  36. * Ext.fly(target).removeCls('my-row-highlight-class');
  37. * },
  38. *
  39. * // While over a target node, return the default drop allowed class which
  40. * // places a "tick" icon into the drag proxy.
  41. * onNodeOver : function(target, dd, e, data){
  42. * return Ext.dd.DropZone.prototype.dropAllowed;
  43. * },
  44. *
  45. * // On node drop we can interrogate the target to find the underlying
  46. * // application object that is the real target of the dragged data.
  47. * // In this case, it is a Record in the GridPanel's Store.
  48. * // We can use the data set up by the DragZone's getDragData method to read
  49. * // any data we decided to attach in the DragZone's getDragData method.
  50. * onNodeDrop : function(target, dd, e, data){
  51. * var rowIndex = myGridPanel.getView().findRowIndex(target);
  52. * var r = myGridPanel.getStore().getAt(rowIndex);
  53. * Ext.Msg.alert('Drop gesture', 'Dropped Record id ' + data.draggedRecord.id +
  54. * ' on Record id ' + r.id);
  55. * return true;
  56. * }
  57. * });
  58. * }
  59. *
  60. * See the {@link Ext.dd.DragZone DragZone} documentation for details about building a DragZone which
  61. * cooperates with this DropZone.
  62. */
  63. Ext.define('Ext.dd.DropZone', {
  64. extend: 'Ext.dd.DropTarget',
  65. requires: ['Ext.dd.Registry'],
  66. /**
  67. * Returns a custom data object associated with the DOM node that is the target of the event. By default
  68. * this looks up the event target in the {@link Ext.dd.Registry}, although you can override this method to
  69. * provide your own custom lookup.
  70. * @param {Event} e The event
  71. * @return {Object} data The custom data
  72. */
  73. getTargetFromEvent : function(e){
  74. return Ext.dd.Registry.getTargetFromEvent(e);
  75. },
  76. /**
  77. * Called when the DropZone determines that a {@link Ext.dd.DragSource} has entered a drop node
  78. * that has either been registered or detected by a configured implementation of {@link #getTargetFromEvent}.
  79. * This method has no default implementation and should be overridden to provide
  80. * node-specific processing if necessary.
  81. * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from
  82. * {@link #getTargetFromEvent} for this node)
  83. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  84. * @param {Event} e The event
  85. * @param {Object} data An object containing arbitrary data supplied by the drag source
  86. */
  87. onNodeEnter : function(n, dd, e, data){
  88. },
  89. /**
  90. * Called while the DropZone determines that a {@link Ext.dd.DragSource} is over a drop node
  91. * that has either been registered or detected by a configured implementation of {@link #getTargetFromEvent}.
  92. * The default implementation returns this.dropAllowed, so it should be
  93. * overridden to provide the proper feedback.
  94. * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from
  95. * {@link #getTargetFromEvent} for this node)
  96. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  97. * @param {Event} e The event
  98. * @param {Object} data An object containing arbitrary data supplied by the drag source
  99. * @return {String} status The CSS class that communicates the drop status back to the source so that the
  100. * underlying {@link Ext.dd.StatusProxy} can be updated
  101. * @template
  102. */
  103. onNodeOver : function(n, dd, e, data){
  104. return this.dropAllowed;
  105. },
  106. /**
  107. * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dragged out of
  108. * the drop node without dropping. This method has no default implementation and should be overridden to provide
  109. * node-specific processing if necessary.
  110. * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from
  111. * {@link #getTargetFromEvent} for this node)
  112. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  113. * @param {Event} e The event
  114. * @param {Object} data An object containing arbitrary data supplied by the drag source
  115. * @template
  116. */
  117. onNodeOut : function(n, dd, e, data){
  118. },
  119. /**
  120. * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dropped onto
  121. * the drop node. The default implementation returns false, so it should be overridden to provide the
  122. * appropriate processing of the drop event and return true so that the drag source's repair action does not run.
  123. * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from
  124. * {@link #getTargetFromEvent} for this node)
  125. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  126. * @param {Event} e The event
  127. * @param {Object} data An object containing arbitrary data supplied by the drag source
  128. * @return {Boolean} True if the drop was valid, else false
  129. * @template
  130. */
  131. onNodeDrop : function(n, dd, e, data){
  132. return false;
  133. },
  134. /**
  135. * Called while the DropZone determines that a {@link Ext.dd.DragSource} is being dragged over it,
  136. * but not over any of its registered drop nodes. The default implementation returns this.dropNotAllowed, so
  137. * it should be overridden to provide the proper feedback if necessary.
  138. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  139. * @param {Event} e The event
  140. * @param {Object} data An object containing arbitrary data supplied by the drag source
  141. * @return {String} status The CSS class that communicates the drop status back to the source so that the
  142. * underlying {@link Ext.dd.StatusProxy} can be updated
  143. * @template
  144. */
  145. onContainerOver : function(dd, e, data){
  146. return this.dropNotAllowed;
  147. },
  148. /**
  149. * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dropped on it,
  150. * but not on any of its registered drop nodes. The default implementation returns false, so it should be
  151. * overridden to provide the appropriate processing of the drop event if you need the drop zone itself to
  152. * be able to accept drops. It should return true when valid so that the drag source's repair action does not run.
  153. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  154. * @param {Event} e The event
  155. * @param {Object} data An object containing arbitrary data supplied by the drag source
  156. * @return {Boolean} True if the drop was valid, else false
  157. * @template
  158. */
  159. onContainerDrop : function(dd, e, data){
  160. return false;
  161. },
  162. /**
  163. * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the source is now over
  164. * the zone. The default implementation returns this.dropNotAllowed and expects that only registered drop
  165. * nodes can process drag drop operations, so if you need the drop zone itself to be able to process drops
  166. * you should override this method and provide a custom implementation.
  167. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  168. * @param {Event} e The event
  169. * @param {Object} data An object containing arbitrary data supplied by the drag source
  170. * @return {String} status The CSS class that communicates the drop status back to the source so that the
  171. * underlying {@link Ext.dd.StatusProxy} can be updated
  172. * @template
  173. */
  174. notifyEnter : function(dd, e, data){
  175. return this.dropNotAllowed;
  176. },
  177. /**
  178. * The function a {@link Ext.dd.DragSource} calls continuously while it is being dragged over the drop zone.
  179. * This method will be called on every mouse movement while the drag source is over the drop zone.
  180. * It will call {@link #onNodeOver} while the drag source is over a registered node, and will also automatically
  181. * delegate to the appropriate node-specific methods as necessary when the drag source enters and exits
  182. * registered nodes ({@link #onNodeEnter}, {@link #onNodeOut}). If the drag source is not currently over a
  183. * registered node, it will call {@link #onContainerOver}.
  184. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  185. * @param {Event} e The event
  186. * @param {Object} data An object containing arbitrary data supplied by the drag source
  187. * @return {String} status The CSS class that communicates the drop status back to the source so that the
  188. * underlying {@link Ext.dd.StatusProxy} can be updated
  189. * @template
  190. */
  191. notifyOver : function(dd, e, data){
  192. var n = this.getTargetFromEvent(e);
  193. if(!n) { // not over valid drop target
  194. if(this.lastOverNode){
  195. this.onNodeOut(this.lastOverNode, dd, e, data);
  196. this.lastOverNode = null;
  197. }
  198. return this.onContainerOver(dd, e, data);
  199. }
  200. if(this.lastOverNode != n){
  201. if(this.lastOverNode){
  202. this.onNodeOut(this.lastOverNode, dd, e, data);
  203. }
  204. this.onNodeEnter(n, dd, e, data);
  205. this.lastOverNode = n;
  206. }
  207. return this.onNodeOver(n, dd, e, data);
  208. },
  209. /**
  210. * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the source has been dragged
  211. * out of the zone without dropping. If the drag source is currently over a registered node, the notification
  212. * will be delegated to {@link #onNodeOut} for node-specific handling, otherwise it will be ignored.
  213. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop target
  214. * @param {Event} e The event
  215. * @param {Object} data An object containing arbitrary data supplied by the drag zone
  216. * @template
  217. */
  218. notifyOut : function(dd, e, data){
  219. if(this.lastOverNode){
  220. this.onNodeOut(this.lastOverNode, dd, e, data);
  221. this.lastOverNode = null;
  222. }
  223. },
  224. /**
  225. * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the dragged item has
  226. * been dropped on it. The drag zone will look up the target node based on the event passed in, and if there
  227. * is a node registered for that event, it will delegate to {@link #onNodeDrop} for node-specific handling,
  228. * otherwise it will call {@link #onContainerDrop}.
  229. * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone
  230. * @param {Event} e The event
  231. * @param {Object} data An object containing arbitrary data supplied by the drag source
  232. * @return {Boolean} False if the drop was invalid.
  233. * @template
  234. */
  235. notifyDrop : function(dd, e, data){
  236. if(this.lastOverNode){
  237. this.onNodeOut(this.lastOverNode, dd, e, data);
  238. this.lastOverNode = null;
  239. }
  240. var n = this.getTargetFromEvent(e);
  241. return n ?
  242. this.onNodeDrop(n, dd, e, data) :
  243. this.onContainerDrop(dd, e, data);
  244. },
  245. // private
  246. triggerCacheRefresh : function() {
  247. Ext.dd.DDM.refreshCache(this.groups);
  248. }
  249. });