DropZone.html 15 KB

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