Ext.define('Ext.ux.ToolbarDroppable', {
constructor: function(config) {
Ext.apply(this, config);
},
init: function(toolbar) {
this.toolbar = toolbar;
this.toolbar.on({
scope : this,
render: this.createDropTarget
});
},
createDropTarget: function() {
this.dropTarget = Ext.create('Ext.dd.DropTarget', this.toolbar.getEl(), {
notifyOver: Ext.Function.bind(this.notifyOver, this),
notifyDrop: Ext.Function.bind(this.notifyDrop, this)
});
},
addDDGroup: function(ddGroup) {
this.dropTarget.addToGroup(ddGroup);
},
calculateEntryIndex: function(e) {
var entryIndex = 0,
toolbar = this.toolbar,
items = toolbar.items.items,
count = items.length,
xHover = e.getXY()[0],
index = 0,
el, xTotal, width, midpoint;
for (; index < count; index++) {
el = items[index].getEl();
xTotal = el.getXY()[0];
width = el.getWidth();
midpoint = xTotal + width / 2;
if (xHover < midpoint) {
entryIndex = index;
break;
} else {
entryIndex = index + 1;
}
}
return entryIndex;
},
canDrop: function(data) {
return true;
},
notifyOver: function(dragSource, event, data) {
return this.canDrop.apply(this, arguments) ? this.dropTarget.dropAllowed : this.dropTarget.dropNotAllowed;
},
notifyDrop: function(dragSource, event, data) {
var canAdd = this.canDrop(dragSource, event, data),
tbar = this.toolbar;
if (canAdd) {
var entryIndex = this.calculateEntryIndex(event);
tbar.insert(entryIndex, this.createItem(data));
tbar.doLayout();
this.afterLayout();
}
return canAdd;
},
createItem: function(data) {
Ext.Error.raise("The createItem method must be implemented in the ToolbarDroppable plugin");
},
afterLayout: Ext.emptyFn
});