Created
April 24, 2012 22:07
-
-
Save wedgybo/2484221 to your computer and use it in GitHub Desktop.
My tree overrides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ext.define('HOD.overrides.TreeDnD', {}, function() { | |
Ext.override(Ext.tree.ViewDropZone, { | |
onContainerOver : function(dd, e, data) { | |
var defaultCls = this.dropNotAllowed; | |
if (this.allowContainerDrops) | |
defaultCls = this.dropAllowed; | |
return e.getTarget('.' + this.indicatorCls) ? this.currentCls : defaultCls; | |
}, | |
getPosition: function(e, node) { | |
var view = this.view, | |
record = view.getRecord(node), | |
y = e.getPageY(), | |
noAppend = record.isLeaf(), | |
noBelow = false, | |
region = Ext.fly(node).getRegion(), | |
fragment; | |
// If we are dragging on top of the root node of the tree, we always want to append. | |
if (record.isRoot()) { | |
return 'append'; | |
} | |
// Return 'append' if the node we are dragging on top of is not a leaf else return false. | |
if (this.appendOnly) { | |
return noAppend ? false : 'append'; | |
} | |
if (!this.allowParentInsert) { | |
noBelow = record.hasChildNodes() && record.isExpanded(); | |
} | |
fragment = (region.bottom - region.top) / (noAppend ? 2 : 3); | |
if (y >= region.top && y < (region.top + fragment)) { | |
return 'before'; | |
} | |
else if (!noBelow && (noAppend || (y >= (region.bottom - fragment) && y <= region.bottom))) { | |
return 'after'; | |
} | |
else if (this.allowContainerDrops && (y >= region.bottom)) { | |
return 'after'; | |
} | |
else { | |
return 'append'; | |
} | |
}, | |
onNodeDrop: function(node, dragZone, e, data) { | |
var me = this, | |
dropped = false, | |
// Create a closure to perform the operation which the event handler may use. | |
// Users may now return <code>false</code> from the beforedrop handler, and perform any kind | |
// of asynchronous processing such as an Ext.Msg.confirm, or an Ajax request, | |
// and complete the drop gesture at some point in the future by calling this function. | |
processDrop = function () { | |
me.invalidateDrop(); | |
me.handleNodeDrop(data, me.overRecord, me.currentPosition); | |
dropped = true; | |
me.fireViewEvent('drop', node, data, me.overRecord, me.currentPosition); | |
}, | |
performOperation = false; | |
if (me.valid) { | |
fakeDrop = false; | |
performOperation = me.fireViewEvent('beforedrop', node, data, me.overRecord, me.currentPosition, processDrop); | |
//console.log(performOperation, fakeDrop); | |
if (fakeDrop) { | |
//console.log('I should be acting like I have performed the drop, but not adding anything to the tree'); | |
dropped = true; | |
me.fireViewEvent('drop', node, data, me.overRecord, me.currentPosition); | |
} else if (performOperation !== false) { | |
// If the processDrop function was called in the event handler, do not do it again. | |
if (!dropped) { | |
processDrop(); | |
} | |
} | |
} | |
return performOperation; | |
} | |
}); | |
Ext.override(Ext.view.DropZone, { | |
// The mouse is past the end of all nodes (or there are no nodes) | |
onContainerOver : function(dd, e, data) { | |
var me = this, | |
view = me.view, | |
count = view.store.getCount(); | |
// There are records, so position after the last one | |
if (count) { | |
me.positionIndicator(view.getNode(count - 1), data, e); | |
} | |
// No records, position the indicator at the top | |
else { | |
delete me.overRecord; | |
delete me.currentPosition; | |
me.getIndicator().setWidth(Ext.fly(view.el).getWidth()).showAt(0, 0); | |
me.valid = true; | |
} | |
return me.dropAllowed; | |
}, | |
getTargetFromEvent : function(e) { | |
var node = e.getTarget(this.view.getItemSelector()), | |
mouseY, nodeList, testNode, i, len, box; | |
// Not over a row node: The content may be narrower than the View's encapsulating element, so return the closest. | |
// If we fall through because the mouse is below the nodes (or there are no nodes), we'll get an onContainerOver call. | |
if (!node) { | |
mouseY = e.getPageY(); | |
for (i = 0, nodeList = this.view.getNodes(), len = nodeList.length; i < len; i++) { | |
testNode = nodeList[i]; | |
box = Ext.fly(testNode).getBox(); | |
if (mouseY <= box.bottom) { | |
return testNode; | |
} | |
} | |
if (this.allowContainerDrops) { | |
return nodeList[nodeList.length - 1]; | |
} | |
} | |
return node; | |
} | |
}); | |
Ext.override(Ext.view.DragZone, { | |
getDragText: function() { | |
if (this.dragField) { | |
var fieldValue = this.dragData.records[0].get(this.dragField); | |
return Ext.String.format(this.dragText, fieldValue); | |
} else { | |
var count = this.dragData.records.length; | |
return Ext.String.format(this.dragText, count, count == 1 ? '' : 's'); | |
} | |
} | |
}); | |
Ext.override(Ext.grid.plugin.DragDrop, { | |
onViewRender : function(view) { | |
var me = this; | |
if (me.enableDrag) { | |
me.dragZone = Ext.create('Ext.view.DragZone', { | |
view: view, | |
ddGroup: me.dragGroup || me.ddGroup, | |
dragText: me.dragText, | |
dragField: me.dragField | |
}); | |
} | |
if (me.enableDrop) { | |
me.dropZone = Ext.create('Ext.grid.ViewDropZone', { | |
view: view, | |
ddGroup: me.dropGroup || me.ddGroup | |
}); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment