3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.ux.ToolbarDroppable
18 * Plugin which allows items to be dropped onto a toolbar and be turned into new Toolbar items.
19 * To use the plugin, you just need to provide a createItem implementation that takes the drop
20 * data as an argument and returns an object that can be placed onto the toolbar. Example:
22 * Ext.create('Ext.ux.ToolbarDroppable', {
23 * createItem: function(data) {
24 * return Ext.create('Ext.Button', {text: data.text});
28 * The afterLayout function can also be overridden, and is called after a new item has been
29 * created and inserted into the Toolbar. Use this for any logic that needs to be run after
30 * the item has been created.
32 Ext.define('Ext.ux.ToolbarDroppable', {
38 constructor: function(config) {
39 Ext.apply(this, config);
43 * Initializes the plugin and saves a reference to the toolbar
44 * @param {Ext.toolbar.Toolbar} toolbar The toolbar instance
46 init: function(toolbar) {
49 * @type Ext.toolbar.Toolbar
50 * The toolbar instance that this plugin is tied to
52 this.toolbar = toolbar;
56 render: this.createDropTarget
61 * Creates a drop target on the toolbar
63 createDropTarget: function() {
65 * @property dropTarget
66 * @type Ext.dd.DropTarget
67 * The drop target attached to the toolbar instance
69 this.dropTarget = Ext.create('Ext.dd.DropTarget', this.toolbar.getEl(), {
70 notifyOver: Ext.Function.bind(this.notifyOver, this),
71 notifyDrop: Ext.Function.bind(this.notifyDrop, this)
76 * Adds the given DD Group to the drop target
77 * @param {String} ddGroup The DD Group
79 addDDGroup: function(ddGroup) {
80 this.dropTarget.addToGroup(ddGroup);
84 * Calculates the location on the toolbar to create the new sorter button based on the XY of the
86 * @param {Ext.EventObject} e The event object
87 * @return {Number} The index at which to insert the new button
89 calculateEntryIndex: function(e) {
91 toolbar = this.toolbar,
92 items = toolbar.items.items,
94 xTotal = toolbar.getEl().getXY()[0],
95 xHover = e.getXY()[0] - xTotal;
97 for (var index = 0; index < count; index++) {
98 var item = items[index],
99 width = item.getEl().getWidth(),
100 midpoint = xTotal + width / 2;
104 if (xHover < midpoint) {
109 entryIndex = index + 1;
117 * Returns true if the drop is allowed on the drop target. This function can be overridden
118 * and defaults to simply return true
119 * @param {Object} data Arbitrary data from the drag source
120 * @return {Boolean} True if the drop is allowed
122 canDrop: function(data) {
127 * Custom notifyOver method which will be used in the plugin's internal DropTarget
128 * @return {String} The CSS class to add
130 notifyOver: function(dragSource, event, data) {
131 return this.canDrop.apply(this, arguments) ? this.dropTarget.dropAllowed : this.dropTarget.dropNotAllowed;
135 * Called when the drop has been made. Creates the new toolbar item, places it at the correct location
136 * and calls the afterLayout callback.
138 notifyDrop: function(dragSource, event, data) {
139 var canAdd = this.canDrop(dragSource, event, data),
143 var entryIndex = this.calculateEntryIndex(event);
145 tbar.insert(entryIndex, this.createItem(data));
155 * Creates the new toolbar item based on drop data. This method must be implemented by the plugin instance
156 * @param {Object} data Arbitrary data from the drop
157 * @return {Mixed} An item that can be added to a toolbar
159 createItem: function(data) {
161 Ext.Error.raise("The createItem method must be implemented in the ToolbarDroppable plugin");
166 * Called after a new button has been created and added to the toolbar. Add any required cleanup logic here
168 afterLayout: Ext.emptyFn