DragSelector.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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-ux-DataView-DragSelector'>/**
  19. </span> * @class Ext.ux.DataView.DragSelector
  20. * @extends Object
  21. * @author Ed Spencer
  22. *
  23. */
  24. Ext.define('Ext.ux.DataView.DragSelector', {
  25. requires: ['Ext.dd.DragTracker', 'Ext.util.Region'],
  26. <span id='Ext-ux-DataView-DragSelector-method-init'> /**
  27. </span> * Initializes the plugin by setting up the drag tracker
  28. */
  29. init: function(dataview) {
  30. <span id='Ext-ux-DataView-DragSelector-property-dataview'> /**
  31. </span> * @property dataview
  32. * @type Ext.view.View
  33. * The DataView bound to this instance
  34. */
  35. this.dataview = dataview;
  36. dataview.mon(dataview, {
  37. beforecontainerclick: this.cancelClick,
  38. scope: this,
  39. render: {
  40. fn: this.onRender,
  41. scope: this,
  42. single: true
  43. }
  44. });
  45. },
  46. <span id='Ext-ux-DataView-DragSelector-method-onRender'> /**
  47. </span> * @private
  48. * Called when the attached DataView is rendered. This sets up the DragTracker instance that will be used
  49. * to created a dragged selection area
  50. */
  51. onRender: function() {
  52. <span id='Ext-ux-DataView-DragSelector-property-tracker'> /**
  53. </span> * @property tracker
  54. * @type Ext.dd.DragTracker
  55. * The DragTracker attached to this instance. Note that the 4 on* functions are called in the scope of the
  56. * DragTracker ('this' refers to the DragTracker inside those functions), so we pass a reference to the
  57. * DragSelector so that we can call this class's functions.
  58. */
  59. this.tracker = Ext.create('Ext.dd.DragTracker', {
  60. dataview: this.dataview,
  61. el: this.dataview.el,
  62. dragSelector: this,
  63. onBeforeStart: this.onBeforeStart,
  64. onStart: this.onStart,
  65. onDrag : this.onDrag,
  66. onEnd : this.onEnd
  67. });
  68. <span id='Ext-ux-DataView-DragSelector-property-dragRegion'> /**
  69. </span> * @property dragRegion
  70. * @type Ext.util.Region
  71. * Represents the region currently dragged out by the user. This is used to figure out which dataview nodes are
  72. * in the selected area and to set the size of the Proxy element used to highlight the current drag area
  73. */
  74. this.dragRegion = Ext.create('Ext.util.Region');
  75. },
  76. <span id='Ext-ux-DataView-DragSelector-method-onBeforeStart'> /**
  77. </span> * @private
  78. * Listener attached to the DragTracker's onBeforeStart event. Returns false if the drag didn't start within the
  79. * DataView's el
  80. */
  81. onBeforeStart: function(e) {
  82. return e.target == this.dataview.getEl().dom;
  83. },
  84. <span id='Ext-ux-DataView-DragSelector-method-onStart'> /**
  85. </span> * @private
  86. * Listener attached to the DragTracker's onStart event. Cancel's the DataView's containerclick event from firing
  87. * and sets the start co-ordinates of the Proxy element. Clears any existing DataView selection
  88. * @param {Ext.EventObject} e The click event
  89. */
  90. onStart: function(e) {
  91. var dragSelector = this.dragSelector,
  92. dataview = this.dataview;
  93. // Flag which controls whether the cancelClick method vetoes the processing of the DataView's containerclick event.
  94. // On IE (where else), this needs to remain set for a millisecond after mouseup because even though the mouse has
  95. // moved, the mouseup will still trigger a click event.
  96. this.dragging = true;
  97. //here we reset and show the selection proxy element and cache the regions each item in the dataview take up
  98. dragSelector.fillRegions();
  99. dragSelector.getProxy().show();
  100. dataview.getSelectionModel().deselectAll();
  101. },
  102. <span id='Ext-ux-DataView-DragSelector-method-cancelClick'> /**
  103. </span> * @private
  104. * Reusable handler that's used to cancel the container click event when dragging on the dataview. See onStart for
  105. * details
  106. */
  107. cancelClick: function() {
  108. return !this.tracker.dragging;
  109. },
  110. <span id='Ext-ux-DataView-DragSelector-method-onDrag'> /**
  111. </span> * @private
  112. * Listener attached to the DragTracker's onDrag event. Figures out how large the drag selection area should be and
  113. * updates the proxy element's size to match. Then iterates over all of the rendered items and marks them selected
  114. * if the drag region touches them
  115. * @param {Ext.EventObject} e The drag event
  116. */
  117. onDrag: function(e) {
  118. var dragSelector = this.dragSelector,
  119. selModel = dragSelector.dataview.getSelectionModel(),
  120. dragRegion = dragSelector.dragRegion,
  121. bodyRegion = dragSelector.bodyRegion,
  122. proxy = dragSelector.getProxy(),
  123. regions = dragSelector.regions,
  124. length = regions.length,
  125. startXY = this.startXY,
  126. currentXY = this.getXY(),
  127. minX = Math.min(startXY[0], currentXY[0]),
  128. minY = Math.min(startXY[1], currentXY[1]),
  129. width = Math.abs(startXY[0] - currentXY[0]),
  130. height = Math.abs(startXY[1] - currentXY[1]),
  131. region, selected, i;
  132. Ext.apply(dragRegion, {
  133. top: minY,
  134. left: minX,
  135. right: minX + width,
  136. bottom: minY + height
  137. });
  138. dragRegion.constrainTo(bodyRegion);
  139. proxy.setRegion(dragRegion);
  140. for (i = 0; i &lt; length; i++) {
  141. region = regions[i];
  142. selected = dragRegion.intersect(region);
  143. if (selected) {
  144. selModel.select(i, true);
  145. } else {
  146. selModel.deselect(i);
  147. }
  148. }
  149. },
  150. <span id='Ext-ux-DataView-DragSelector-method-onEnd'> /**
  151. </span> * @private
  152. * Listener attached to the DragTracker's onEnd event. This is a delayed function which executes 1
  153. * millisecond after it has been called. This is because the dragging flag must remain active to cancel
  154. * the containerclick event which the mouseup event will trigger.
  155. * @param {Ext.EventObject} e The event object
  156. */
  157. onEnd: Ext.Function.createDelayed(function(e) {
  158. var dataview = this.dataview,
  159. selModel = dataview.getSelectionModel(),
  160. dragSelector = this.dragSelector;
  161. this.dragging = false;
  162. dragSelector.getProxy().hide();
  163. }, 1),
  164. <span id='Ext-ux-DataView-DragSelector-method-getProxy'> /**
  165. </span> * @private
  166. * Creates a Proxy element that will be used to highlight the drag selection region
  167. * @return {Ext.Element} The Proxy element
  168. */
  169. getProxy: function() {
  170. if (!this.proxy) {
  171. this.proxy = this.dataview.getEl().createChild({
  172. tag: 'div',
  173. cls: 'x-view-selector'
  174. });
  175. }
  176. return this.proxy;
  177. },
  178. <span id='Ext-ux-DataView-DragSelector-method-fillRegions'> /**
  179. </span> * @private
  180. * Gets the region taken up by each rendered node in the DataView. We use these regions to figure out which nodes
  181. * to select based on the selector region the user has dragged out
  182. */
  183. fillRegions: function() {
  184. var dataview = this.dataview,
  185. regions = this.regions = [];
  186. dataview.all.each(function(node) {
  187. regions.push(node.getRegion());
  188. });
  189. this.bodyRegion = dataview.getEl().getRegion();
  190. }
  191. });</pre>
  192. </body>
  193. </html>