BoxReorderer.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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-BoxReorderer'>/**
  19. </span> * Base class from Ext.ux.TabReorderer.
  20. */
  21. Ext.define('Ext.ux.BoxReorderer', {
  22. mixins: {
  23. observable: 'Ext.util.Observable'
  24. },
  25. <span id='Ext-ux-BoxReorderer-cfg-itemSelector'> /**
  26. </span> * @cfg {String} itemSelector
  27. * A {@link Ext.DomQuery DomQuery} selector which identifies the encapsulating elements of child
  28. * Components which participate in reordering.
  29. */
  30. itemSelector: '.x-box-item',
  31. <span id='Ext-ux-BoxReorderer-cfg-animate'> /**
  32. </span> * @cfg {Mixed} animate
  33. * If truthy, child reordering is animated so that moved boxes slide smoothly into position.
  34. * If this option is numeric, it is used as the animation duration in milliseconds.
  35. */
  36. animate: 100,
  37. constructor: function() {
  38. this.addEvents(
  39. <span id='Ext-ux-BoxReorderer-event-StartDrag'> /**
  40. </span> * @event StartDrag
  41. * Fires when dragging of a child Component begins.
  42. * @param {Ext.ux.BoxReorderer} this
  43. * @param {Ext.container.Container} container The owning Container
  44. * @param {Ext.Component} dragCmp The Component being dragged
  45. * @param {Number} idx The start index of the Component being dragged.
  46. */
  47. 'StartDrag',
  48. <span id='Ext-ux-BoxReorderer-event-Drag'> /**
  49. </span> * @event Drag
  50. * Fires during dragging of a child Component.
  51. * @param {Ext.ux.BoxReorderer} this
  52. * @param {Ext.container.Container} container The owning Container
  53. * @param {Ext.Component} dragCmp The Component being dragged
  54. * @param {Number} startIdx The index position from which the Component was initially dragged.
  55. * @param {Number} idx The current closest index to which the Component would drop.
  56. */
  57. 'Drag',
  58. <span id='Ext-ux-BoxReorderer-event-ChangeIndex'> /**
  59. </span> * @event ChangeIndex
  60. * Fires when dragging of a child Component causes its drop index to change.
  61. * @param {Ext.ux.BoxReorderer} this
  62. * @param {Ext.container.Container} container The owning Container
  63. * @param {Ext.Component} dragCmp The Component being dragged
  64. * @param {Number} startIdx The index position from which the Component was initially dragged.
  65. * @param {Number} idx The current closest index to which the Component would drop.
  66. */
  67. 'ChangeIndex',
  68. <span id='Ext-ux-BoxReorderer-event-Drop'> /**
  69. </span> * @event Drop
  70. * Fires when a child Component is dropped at a new index position.
  71. * @param {Ext.ux.BoxReorderer} this
  72. * @param {Ext.container.Container} container The owning Container
  73. * @param {Ext.Component} dragCmp The Component being dropped
  74. * @param {Number} startIdx The index position from which the Component was initially dragged.
  75. * @param {Number} idx The index at which the Component is being dropped.
  76. */
  77. 'Drop'
  78. );
  79. this.mixins.observable.constructor.apply(this, arguments);
  80. },
  81. init: function(container) {
  82. var me = this;
  83. me.container = container;
  84. // Set our animatePolicy to animate the start position (ie x for HBox, y for VBox)
  85. me.animatePolicy = {};
  86. me.animatePolicy[container.getLayout().names.x] = true;
  87. // Initialize the DD on first layout, when the innerCt has been created.
  88. me.container.on({
  89. scope: me,
  90. boxready: me.afterFirstLayout,
  91. destroy: me.onContainerDestroy
  92. });
  93. },
  94. <span id='Ext-ux-BoxReorderer-method-onContainerDestroy'> /**
  95. </span> * @private Clear up on Container destroy
  96. */
  97. onContainerDestroy: function() {
  98. if (this.dd) {
  99. this.dd.unreg();
  100. }
  101. },
  102. afterFirstLayout: function() {
  103. var me = this,
  104. layout = me.container.getLayout(),
  105. names = layout.names,
  106. dd;
  107. // Create a DD instance. Poke the handlers in.
  108. // TODO: Ext5's DD classes should apply config to themselves.
  109. // TODO: Ext5's DD classes should not use init internally because it collides with use as a plugin
  110. // TODO: Ext5's DD classes should be Observable.
  111. // TODO: When all the above are trus, this plugin should extend the DD class.
  112. dd = me.dd = Ext.create('Ext.dd.DD', layout.innerCt, me.container.id + '-reorderer');
  113. Ext.apply(dd, {
  114. animate: me.animate,
  115. reorderer: me,
  116. container: me.container,
  117. getDragCmp: this.getDragCmp,
  118. clickValidator: Ext.Function.createInterceptor(dd.clickValidator, me.clickValidator, me, false),
  119. onMouseDown: me.onMouseDown,
  120. startDrag: me.startDrag,
  121. onDrag: me.onDrag,
  122. endDrag: me.endDrag,
  123. getNewIndex: me.getNewIndex,
  124. doSwap: me.doSwap,
  125. findReorderable: me.findReorderable
  126. });
  127. // Decide which dimension we are measuring, and which measurement metric defines
  128. // the *start* of the box depending upon orientation.
  129. dd.dim = names.width;
  130. dd.startAttr = names.left;
  131. dd.endAttr = names.right;
  132. },
  133. getDragCmp: function(e) {
  134. return this.container.getChildByElement(e.getTarget(this.itemSelector, 10));
  135. },
  136. // check if the clicked component is reorderable
  137. clickValidator: function(e) {
  138. var cmp = this.getDragCmp(e);
  139. // If cmp is null, this expression MUST be coerced to boolean so that createInterceptor is able to test it against false
  140. return !!(cmp &amp;&amp; cmp.reorderable !== false);
  141. },
  142. onMouseDown: function(e) {
  143. var me = this,
  144. container = me.container,
  145. containerBox,
  146. cmpEl,
  147. cmpBox;
  148. // Ascertain which child Component is being mousedowned
  149. me.dragCmp = me.getDragCmp(e);
  150. if (me.dragCmp) {
  151. cmpEl = me.dragCmp.getEl();
  152. me.startIndex = me.curIndex = container.items.indexOf(me.dragCmp);
  153. // Start position of dragged Component
  154. cmpBox = cmpEl.getPageBox();
  155. // Last tracked start position
  156. me.lastPos = cmpBox[this.startAttr];
  157. // Calculate constraints depending upon orientation
  158. // Calculate offset from mouse to dragEl position
  159. containerBox = container.el.getPageBox();
  160. if (me.dim === 'width') {
  161. me.minX = containerBox.left;
  162. me.maxX = containerBox.right - cmpBox.width;
  163. me.minY = me.maxY = cmpBox.top;
  164. me.deltaX = e.getPageX() - cmpBox.left;
  165. } else {
  166. me.minY = containerBox.top;
  167. me.maxY = containerBox.bottom - cmpBox.height;
  168. me.minX = me.maxX = cmpBox.left;
  169. me.deltaY = e.getPageY() - cmpBox.top;
  170. }
  171. me.constrainY = me.constrainX = true;
  172. }
  173. },
  174. startDrag: function() {
  175. var me = this,
  176. dragCmp = me.dragCmp;
  177. if (dragCmp) {
  178. // For the entire duration of dragging the *Element*, defeat any positioning and animation of the dragged *Component*
  179. dragCmp.setPosition = Ext.emptyFn;
  180. dragCmp.animate = false;
  181. // Animate the BoxLayout just for the duration of the drag operation.
  182. if (me.animate) {
  183. me.container.getLayout().animatePolicy = me.reorderer.animatePolicy;
  184. }
  185. // We drag the Component element
  186. me.dragElId = dragCmp.getEl().id;
  187. me.reorderer.fireEvent('StartDrag', me, me.container, dragCmp, me.curIndex);
  188. // Suspend events, and set the disabled flag so that the mousedown and mouseup events
  189. // that are going to take place do not cause any other UI interaction.
  190. dragCmp.suspendEvents();
  191. dragCmp.disabled = true;
  192. dragCmp.el.setStyle('zIndex', 100);
  193. } else {
  194. me.dragElId = null;
  195. }
  196. },
  197. <span id='Ext-ux-BoxReorderer-method-findReorderable'> /**
  198. </span> * @private
  199. * Find next or previous reorderable component index.
  200. * @param {Number} newIndex The initial drop index.
  201. * @return {Number} The index of the reorderable component.
  202. */
  203. findReorderable: function(newIndex) {
  204. var me = this,
  205. items = me.container.items,
  206. newItem;
  207. if (items.getAt(newIndex).reorderable === false) {
  208. newItem = items.getAt(newIndex);
  209. if (newIndex &gt; me.startIndex) {
  210. while(newItem &amp;&amp; newItem.reorderable === false) {
  211. newIndex++;
  212. newItem = items.getAt(newIndex);
  213. }
  214. } else {
  215. while(newItem &amp;&amp; newItem.reorderable === false) {
  216. newIndex--;
  217. newItem = items.getAt(newIndex);
  218. }
  219. }
  220. }
  221. newIndex = Math.min(Math.max(newIndex, 0), items.getCount() - 1);
  222. if (items.getAt(newIndex).reorderable === false) {
  223. return -1;
  224. }
  225. return newIndex;
  226. },
  227. <span id='Ext-ux-BoxReorderer-method-doSwap'> /**
  228. </span> * @private
  229. * Swap 2 components.
  230. * @param {Number} newIndex The initial drop index.
  231. */
  232. doSwap: function(newIndex) {
  233. var me = this,
  234. items = me.container.items,
  235. container = me.container,
  236. wasRoot = me.container._isLayoutRoot,
  237. orig, dest, tmpIndex, temp;
  238. newIndex = me.findReorderable(newIndex);
  239. if (newIndex === -1) {
  240. return;
  241. }
  242. me.reorderer.fireEvent('ChangeIndex', me, container, me.dragCmp, me.startIndex, newIndex);
  243. orig = items.getAt(me.curIndex);
  244. dest = items.getAt(newIndex);
  245. items.remove(orig);
  246. tmpIndex = Math.min(Math.max(newIndex, 0), items.getCount() - 1);
  247. items.insert(tmpIndex, orig);
  248. items.remove(dest);
  249. items.insert(me.curIndex, dest);
  250. // Make the Box Container the topmost layout participant during the layout.
  251. container._isLayoutRoot = true;
  252. container.updateLayout();
  253. container._isLayoutRoot = wasRoot;
  254. me.curIndex = newIndex;
  255. },
  256. onDrag: function(e) {
  257. var me = this,
  258. newIndex;
  259. newIndex = me.getNewIndex(e.getPoint());
  260. if ((newIndex !== undefined)) {
  261. me.reorderer.fireEvent('Drag', me, me.container, me.dragCmp, me.startIndex, me.curIndex);
  262. me.doSwap(newIndex);
  263. }
  264. },
  265. endDrag: function(e) {
  266. if (e) {
  267. e.stopEvent();
  268. }
  269. var me = this,
  270. layout = me.container.getLayout(),
  271. temp;
  272. if (me.dragCmp) {
  273. delete me.dragElId;
  274. // Reinstate the Component's positioning method after mouseup, and allow the layout system to animate it.
  275. delete me.dragCmp.setPosition;
  276. me.dragCmp.animate = true;
  277. // Ensure the lastBox is correct for the animation system to restore to when it creates the &quot;from&quot; animation frame
  278. me.dragCmp.lastBox[layout.names.x] = me.dragCmp.getPosition(true)[layout.names.widthIndex];
  279. // Make the Box Container the topmost layout participant during the layout.
  280. me.container._isLayoutRoot = true;
  281. me.container.updateLayout();
  282. me.container._isLayoutRoot = undefined;
  283. // Attempt to hook into the afteranimate event of the drag Component to call the cleanup
  284. temp = Ext.fx.Manager.getFxQueue(me.dragCmp.el.id)[0];
  285. if (temp) {
  286. temp.on({
  287. afteranimate: me.reorderer.afterBoxReflow,
  288. scope: me
  289. });
  290. }
  291. // If not animated, clean up after the mouseup has happened so that we don't click the thing being dragged
  292. else {
  293. Ext.Function.defer(me.reorderer.afterBoxReflow, 1, me);
  294. }
  295. if (me.animate) {
  296. delete layout.animatePolicy;
  297. }
  298. me.reorderer.fireEvent('drop', me, me.container, me.dragCmp, me.startIndex, me.curIndex);
  299. }
  300. },
  301. <span id='Ext-ux-BoxReorderer-method-afterBoxReflow'> /**
  302. </span> * @private
  303. * Called after the boxes have been reflowed after the drop.
  304. * Re-enabled the dragged Component.
  305. */
  306. afterBoxReflow: function() {
  307. var me = this;
  308. me.dragCmp.el.setStyle('zIndex', '');
  309. me.dragCmp.disabled = false;
  310. me.dragCmp.resumeEvents();
  311. },
  312. <span id='Ext-ux-BoxReorderer-method-getNewIndex'> /**
  313. </span> * @private
  314. * Calculate drop index based upon the dragEl's position.
  315. */
  316. getNewIndex: function(pointerPos) {
  317. var me = this,
  318. dragEl = me.getDragEl(),
  319. dragBox = Ext.fly(dragEl).getPageBox(),
  320. targetEl,
  321. targetBox,
  322. targetMidpoint,
  323. i = 0,
  324. it = me.container.items.items,
  325. ln = it.length,
  326. lastPos = me.lastPos;
  327. me.lastPos = dragBox[me.startAttr];
  328. for (; i &lt; ln; i++) {
  329. targetEl = it[i].getEl();
  330. // Only look for a drop point if this found item is an item according to our selector
  331. if (targetEl.is(me.reorderer.itemSelector)) {
  332. targetBox = targetEl.getPageBox();
  333. targetMidpoint = targetBox[me.startAttr] + (targetBox[me.dim] &gt;&gt; 1);
  334. if (i &lt; me.curIndex) {
  335. if ((dragBox[me.startAttr] &lt; lastPos) &amp;&amp; (dragBox[me.startAttr] &lt; (targetMidpoint - 5))) {
  336. return i;
  337. }
  338. } else if (i &gt; me.curIndex) {
  339. if ((dragBox[me.startAttr] &gt; lastPos) &amp;&amp; (dragBox[me.endAttr] &gt; (targetMidpoint + 5))) {
  340. return i;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. });
  347. </pre>
  348. </body>
  349. </html>