2 * @class Ext.ux.ToolbarDroppable
4 * Plugin which allows items to be dropped onto a toolbar and be turned into new Toolbar items.
5 * To use the plugin, you just need to provide a createItem implementation that takes the drop
6 * data as an argument and returns an object that can be placed onto the toolbar. Example:
8 * Ext.create('Ext.ux.ToolbarDroppable', {
9 * createItem: function(data) {
10 * return Ext.create('Ext.Button', {text: data.text});
14 * The afterLayout function can also be overridden, and is called after a new item has been
15 * created and inserted into the Toolbar. Use this for any logic that needs to be run after
16 * the item has been created.
18 Ext.define('Ext.ux.ToolbarDroppable', {
24 constructor: function(config) {
25 Ext.apply(this, config);
29 * Initializes the plugin and saves a reference to the toolbar
30 * @param {Ext.toolbar.Toolbar} toolbar The toolbar instance
32 init: function(toolbar) {
35 * @type Ext.toolbar.Toolbar
36 * The toolbar instance that this plugin is tied to
38 this.toolbar = toolbar;
42 render: this.createDropTarget
47 * Creates a drop target on the toolbar
49 createDropTarget: function() {
51 * @property dropTarget
52 * @type Ext.dd.DropTarget
53 * The drop target attached to the toolbar instance
55 this.dropTarget = Ext.create('Ext.dd.DropTarget', this.toolbar.getEl(), {
56 notifyOver: Ext.Function.bind(this.notifyOver, this),
57 notifyDrop: Ext.Function.bind(this.notifyDrop, this)
62 * Adds the given DD Group to the drop target
63 * @param {String} ddGroup The DD Group
65 addDDGroup: function(ddGroup) {
66 this.dropTarget.addToGroup(ddGroup);
70 * Calculates the location on the toolbar to create the new sorter button based on the XY of the
72 * @param {Ext.EventObject} e The event object
73 * @return {Number} The index at which to insert the new button
75 calculateEntryIndex: function(e) {
77 toolbar = this.toolbar,
78 items = toolbar.items.items,
80 xTotal = toolbar.getEl().getXY()[0],
81 xHover = e.getXY()[0] - xTotal;
83 for (var index = 0; index < count; index++) {
84 var item = items[index],
85 width = item.getEl().getWidth(),
86 midpoint = xTotal + width / 2;
90 if (xHover < midpoint) {
95 entryIndex = index + 1;
103 * Returns true if the drop is allowed on the drop target. This function can be overridden
104 * and defaults to simply return true
105 * @param {Object} data Arbitrary data from the drag source
106 * @return {Boolean} True if the drop is allowed
108 canDrop: function(data) {
113 * Custom notifyOver method which will be used in the plugin's internal DropTarget
114 * @return {String} The CSS class to add
116 notifyOver: function(dragSource, event, data) {
117 return this.canDrop.apply(this, arguments) ? this.dropTarget.dropAllowed : this.dropTarget.dropNotAllowed;
121 * Called when the drop has been made. Creates the new toolbar item, places it at the correct location
122 * and calls the afterLayout callback.
124 notifyDrop: function(dragSource, event, data) {
125 var canAdd = this.canDrop(dragSource, event, data),
129 var entryIndex = this.calculateEntryIndex(event);
131 tbar.insert(entryIndex, this.createItem(data));
141 * Creates the new toolbar item based on drop data. This method must be implemented by the plugin instance
142 * @param {Object} data Arbitrary data from the drop
143 * @return {Mixed} An item that can be added to a toolbar
145 createItem: function(data) {
147 Ext.Error.raise("The createItem method must be implemented in the ToolbarDroppable plugin");
152 * Called after a new button has been created and added to the toolbar. Add any required cleanup logic here
154 afterLayout: Ext.emptyFn