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.

192 lines
6.9 KiB

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