DragSelector.js 7.2 KB

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