3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
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:
15 * new Ext.ux.ToolbarReorderer({
16 * defaultReorderable: true
20 * {text: 'Button 1', reorderable: false},
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.
29 Ext.ux.ToolbarReorderer = Ext.extend(Ext.ux.Reorderer, {
31 * Initializes the plugin, decorates the toolbar with additional functionality
33 init: function(toolbar) {
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
39 this.buttonXCache = {};
43 add : function(toolbar, item) {
44 this.createIfReorderable(item);
48 //super sets a reference to the toolbar in this.target
49 Ext.ux.ToolbarReorderer.superclass.init.apply(this, arguments);
53 * Sets up the given Toolbar item as a draggable
54 * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
56 createItemDD: function(button) {
57 if (button.dd != undefined) {
61 var el = button.getEl(),
66 button.dd = new Ext.dd.DD(el, undefined, {
70 //if a button has a menu, it is disabled while dragging with this function
71 var menuDisabler = function() {
75 Ext.apply(button.dd, {
76 b4StartDrag: function() {
77 this.startPosition = el.getXY();
79 //bump up the z index of the button being dragged but keep a reference to the original
80 this.startZIndex = el.getStyle('zIndex');
81 el.setStyle('zIndex', 10000);
83 button.suspendEvents();
85 button.menu.on('beforeshow', menuDisabler, me);
89 startDrag: function() {
90 this.constrainTo(tbar.getEl());
91 this.setYConstraint(0, 0, 0);
95 //calculate the button's index within the toolbar and its current midpoint
96 var buttonX = el.getXY()[0],
97 deltaX = buttonX - this.startPosition[0],
98 items = tbar.items.items,
99 oldIndex = items.indexOf(button),
102 //find which item in the toolbar the midpoint is currently over
103 for (var index = 0; index < items.length; index++) {
104 var item = items[index];
106 if (item.reorderable && item.id != button.id) {
107 //find the midpoint of the button
108 var box = item.getEl().getBox(),
109 midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
110 movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
111 movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
113 if (movedLeft || movedRight) {
114 me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
122 * After the drag has been completed, make sure the button being dragged makes it back to
123 * the correct location and resets its z index
125 endDrag: function() {
126 //we need to update the cache here for cases where the button was dragged but its
127 //position in the toolbar did not change
128 me.updateButtonXCache();
130 el.moveTo(me.buttonXCache[button.id], el.getY(), {
131 duration: me.animationDuration,
133 callback: function() {
134 button.resumeEvents();
136 button.menu.un('beforeshow', menuDisabler, me);
139 tbar.fireEvent('reordered', button, tbar);
143 el.setStyle('zIndex', this.startZIndex);
148 onMovedLeft: function(item, newIndex, oldIndex) {
149 var tbar = this.target,
150 items = tbar.items.items;
152 if (newIndex != undefined && newIndex != oldIndex) {
153 //move the button currently under drag to its new location
154 tbar.remove(item, false);
155 tbar.insert(newIndex, item);
157 //set the correct x location of each item in the toolbar
158 this.updateButtonXCache();
159 for (var index = 0; index < items.length; index++) {
160 var obj = items[index],
161 newX = this.buttonXCache[obj.id];
164 item.dd.startPosition[0] = newX;
166 var el = obj.getEl();
168 el.moveTo(newX, el.getY(), {duration: this.animationDuration});
174 onMovedRight: function(item, newIndex, oldIndex) {
175 this.onMovedLeft.apply(this, arguments);
180 * Updates the internal cache of button X locations.
182 updateButtonXCache: function() {
183 var tbar = this.target,
185 totalX = tbar.getEl().getBox(true).x;
187 items.each(function(item) {
188 this.buttonXCache[item.id] = totalX;
190 totalX += item.getEl().getWidth();