You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.7 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 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
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.ux.ToolbarReorderer
  17. * @extends Ext.ux.Reorderer
  18. * Plugin which can be attached to any Ext.Toolbar instance. Provides ability to reorder toolbar items
  19. * with drag and drop. Example:
  20. * <pre>
  21. * new Ext.Toolbar({
  22. * plugins: [
  23. * new Ext.ux.ToolbarReorderer({
  24. * defaultReorderable: true
  25. * })
  26. * ],
  27. * items: [
  28. * {text: 'Button 1', reorderable: false},
  29. * {text: 'Button 2'},
  30. * {text: 'Button 3'}
  31. * ]
  32. * });
  33. * </pre>
  34. * In the example above, buttons 2 and 3 will be reorderable via drag and drop. An event named 'reordered'
  35. * is added to the Toolbar, and is fired whenever a reorder has been completed.
  36. */
  37. Ext.ux.ToolbarReorderer = Ext.extend(Ext.ux.Reorderer, {
  38. /**
  39. * Initializes the plugin, decorates the toolbar with additional functionality
  40. */
  41. init: function(toolbar) {
  42. /**
  43. * This is used to store the correct x value of each button in the array. We need to use this
  44. * instead of the button's reported x co-ordinate because the buttons are animated when they move -
  45. * if another onDrag is fired while the button is still moving, the comparison x value will be incorrect
  46. */
  47. this.buttonXCache = {};
  48. toolbar.on({
  49. scope: this,
  50. add : function(toolbar, item) {
  51. this.createIfReorderable(item);
  52. }
  53. });
  54. this.movedTask = new Ext.util.DelayedTask(this.finishMove, this);
  55. //super sets a reference to the toolbar in this.target
  56. Ext.ux.ToolbarReorderer.superclass.init.apply(this, arguments);
  57. },
  58. /**
  59. * Sets up the given Toolbar item as a draggable
  60. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  61. */
  62. createItemDD: function(button) {
  63. if (button.dd != undefined) {
  64. return;
  65. }
  66. var el = button.getEl(),
  67. id = el.id,
  68. tbar = this.target,
  69. me = this;
  70. button.dd = new Ext.dd.DD(el, undefined, {
  71. isTarget: false
  72. });
  73. //if a button has a menu, it is disabled while dragging with this function
  74. var menuDisabler = function() {
  75. return false;
  76. };
  77. Ext.apply(button.dd, {
  78. b4StartDrag: function() {
  79. this.startPosition = el.getXY();
  80. //bump up the z index of the button being dragged but keep a reference to the original
  81. this.startZIndex = el.getStyle('zIndex');
  82. el.setStyle('zIndex', 10000);
  83. button.suspendEvents();
  84. if (button.menu) {
  85. button.menu.on('beforeshow', menuDisabler, me);
  86. }
  87. },
  88. startDrag: function() {
  89. this.constrainTo(tbar.getEl());
  90. this.setYConstraint(0, 0, 0);
  91. },
  92. onDrag: function(e) {
  93. //calculate the button's index within the toolbar and its current midpoint
  94. var buttonX = el.getXY()[0],
  95. deltaX = buttonX - this.startPosition[0],
  96. items = tbar.items.items,
  97. oldIndex = items.indexOf(button),
  98. newIndex;
  99. //find which item in the toolbar the midpoint is currently over
  100. for (var index = 0; index < items.length; index++) {
  101. var item = items[index];
  102. if (item.reorderable && item.id != button.id) {
  103. //find the midpoint of the button
  104. var box = item.getEl().getBox(),
  105. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  106. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  107. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  108. if (movedLeft || movedRight) {
  109. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  110. break;
  111. }
  112. }
  113. }
  114. },
  115. /**
  116. * After the drag has been completed, make sure the button being dragged makes it back to
  117. * the correct location and resets its z index
  118. */
  119. endDrag: function() {
  120. //we need to update the cache here for cases where the button was dragged but its
  121. //position in the toolbar did not change
  122. me.updateButtonXCache();
  123. el.moveTo(me.buttonXCache[button.id], el.getY(), {
  124. duration: me.animationDuration,
  125. scope : this,
  126. callback: function() {
  127. me.movedTask.delay(200);
  128. button.resumeEvents();
  129. if (button.menu) {
  130. button.menu.un('beforeshow', menuDisabler, me);
  131. }
  132. tbar.fireEvent('reordered', button, tbar);
  133. }
  134. });
  135. el.setStyle('zIndex', this.startZIndex);
  136. }
  137. });
  138. },
  139. onMovedLeft: function(item, newIndex, oldIndex) {
  140. var tbar = this.target,
  141. items = tbar.items.items;
  142. this.movedTask.cancel();
  143. if (newIndex != undefined && newIndex != oldIndex) {
  144. //move the button currently under drag to its new location
  145. tbar.remove(item, false);
  146. tbar.insert(newIndex, item);
  147. //set the correct x location of each item in the toolbar
  148. this.updateButtonXCache();
  149. for (var index = 0; index < items.length; index++) {
  150. var obj = items[index],
  151. newX = this.buttonXCache[obj.id];
  152. if (item == obj) {
  153. item.dd.startPosition[0] = newX;
  154. } else {
  155. var el = obj.getEl();
  156. el.moveTo(newX, el.getY(), {
  157. duration: this.animationDuration
  158. });
  159. }
  160. }
  161. }
  162. },
  163. onMovedRight: function(item, newIndex, oldIndex) {
  164. this.onMovedLeft.apply(this, arguments);
  165. },
  166. finishMove: function(){
  167. var tbar = this.target;
  168. tbar.items.each(function(btn){
  169. btn.el.dom.style.left = '';
  170. });
  171. tbar.doLayout();
  172. },
  173. /**
  174. * @private
  175. * Updates the internal cache of button X locations.
  176. */
  177. updateButtonXCache: function() {
  178. var tbar = this.target,
  179. items = tbar.items,
  180. totalX = tbar.getEl().getBox(true).x;
  181. items.each(function(item) {
  182. this.buttonXCache[item.id] = totalX;
  183. totalX += item.getEl().getWidth();
  184. }, this);
  185. }
  186. });