<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-tree-TreeDropZone-method-constructor'><span id='Ext-tree-TreeDropZone'>/**
</span></span> * @class Ext.tree.TreeDropZone
 * @extends Ext.dd.DropZone
 * @constructor
 * @param {String/HTMLElement/Element} tree The {@link Ext.tree.TreePanel} for which to enable dropping
 * @param {Object} config
 */
if(Ext.dd.DropZone){
    
Ext.tree.TreeDropZone = function(tree, config){
<span id='Ext-tree-TreeDropZone-cfg-allowParentInsert'>    /**
</span>     * @cfg {Boolean} allowParentInsert
     * Allow inserting a dragged node between an expanded parent node and its first child that will become a
     * sibling of the parent when dropped (defaults to false)
     */
    this.allowParentInsert = config.allowParentInsert || false;
<span id='Ext-tree-TreeDropZone-cfg-allowContainerDrop'>    /**
</span>     * @cfg {String} allowContainerDrop
     * True if drops on the tree container (outside of a specific tree node) are allowed (defaults to false)
     */
    this.allowContainerDrop = config.allowContainerDrop || false;
<span id='Ext-tree-TreeDropZone-cfg-appendOnly'>    /**
</span>     * @cfg {String} appendOnly
     * True if the tree should only allow append drops (use for trees which are sorted, defaults to false)
     */
    this.appendOnly = config.appendOnly || false;

    Ext.tree.TreeDropZone.superclass.constructor.call(this, tree.getTreeEl(), config);
<span id='Ext-tree-TreeDropZone-property-tree'>    /**
</span>    * The TreePanel for this drop zone
    * @type Ext.tree.TreePanel
    * @property
    */
    this.tree = tree;
<span id='Ext-tree-TreeDropZone-property-dragOverData'>    /**
</span>    * Arbitrary data that can be associated with this tree and will be included in the event object that gets
    * passed to any nodedragover event handler (defaults to {})
    * @type Ext.tree.TreePanel
    * @property
    */
    this.dragOverData = {};
    // private
    this.lastInsertClass = &quot;x-tree-no-status&quot;;
};

Ext.extend(Ext.tree.TreeDropZone, Ext.dd.DropZone, {
<span id='Ext-tree-TreeDropZone-cfg-ddGroup'>    /**
</span>     * @cfg {String} ddGroup
     * A named drag drop group to which this object belongs.  If a group is specified, then this object will only
     * interact with other drag drop objects in the same group (defaults to 'TreeDD').
     */
    ddGroup : &quot;TreeDD&quot;,

<span id='Ext-tree-TreeDropZone-cfg-expandDelay'>    /**
</span>     * @cfg {String} expandDelay
     * The delay in milliseconds to wait before expanding a target tree node while dragging a droppable node
     * over the target (defaults to 1000)
     */
    expandDelay : 1000,

    // private
    expandNode : function(node){
        if(node.hasChildNodes() &amp;&amp; !node.isExpanded()){
            node.expand(false, null, this.triggerCacheRefresh.createDelegate(this));
        }
    },

    // private
    queueExpand : function(node){
        this.expandProcId = this.expandNode.defer(this.expandDelay, this, [node]);
    },

    // private
    cancelExpand : function(){
        if(this.expandProcId){
            clearTimeout(this.expandProcId);
            this.expandProcId = false;
        }
    },

    // private
    isValidDropPoint : function(n, pt, dd, e, data){
        if(!n || !data){ return false; }
        var targetNode = n.node;
        var dropNode = data.node;
        // default drop rules
        if(!(targetNode &amp;&amp; targetNode.isTarget &amp;&amp; pt)){
            return false;
        }
        if(pt == &quot;append&quot; &amp;&amp; targetNode.allowChildren === false){
            return false;
        }
        if((pt == &quot;above&quot; || pt == &quot;below&quot;) &amp;&amp; (targetNode.parentNode &amp;&amp; targetNode.parentNode.allowChildren === false)){
            return false;
        }
        if(dropNode &amp;&amp; (targetNode == dropNode || dropNode.contains(targetNode))){
            return false;
        }
        // reuse the object
        var overEvent = this.dragOverData;
        overEvent.tree = this.tree;
        overEvent.target = targetNode;
        overEvent.data = data;
        overEvent.point = pt;
        overEvent.source = dd;
        overEvent.rawEvent = e;
        overEvent.dropNode = dropNode;
        overEvent.cancel = false;  
        var result = this.tree.fireEvent(&quot;nodedragover&quot;, overEvent);
        return overEvent.cancel === false &amp;&amp; result !== false;
    },

    // private
    getDropPoint : function(e, n, dd){
        var tn = n.node;
        if(tn.isRoot){
            return tn.allowChildren !== false ? &quot;append&quot; : false; // always append for root
        }
        var dragEl = n.ddel;
        var t = Ext.lib.Dom.getY(dragEl), b = t + dragEl.offsetHeight;
        var y = Ext.lib.Event.getPageY(e);
        var noAppend = tn.allowChildren === false || tn.isLeaf();
        if(this.appendOnly || tn.parentNode.allowChildren === false){
            return noAppend ? false : &quot;append&quot;;
        }
        var noBelow = false;
        if(!this.allowParentInsert){
            noBelow = tn.hasChildNodes() &amp;&amp; tn.isExpanded();
        }
        var q = (b - t) / (noAppend ? 2 : 3);
        if(y &gt;= t &amp;&amp; y &lt; (t + q)){
            return &quot;above&quot;;
        }else if(!noBelow &amp;&amp; (noAppend || y &gt;= b-q &amp;&amp; y &lt;= b)){
            return &quot;below&quot;;
        }else{
            return &quot;append&quot;;
        }
    },

    // private
    onNodeEnter : function(n, dd, e, data){
        this.cancelExpand();
    },
    
    onContainerOver : function(dd, e, data) {
        if (this.allowContainerDrop &amp;&amp; this.isValidDropPoint({ ddel: this.tree.getRootNode().ui.elNode, node: this.tree.getRootNode() }, &quot;append&quot;, dd, e, data)) {
            return this.dropAllowed;
        }
        return this.dropNotAllowed;
    },

    // private
    onNodeOver : function(n, dd, e, data){
        var pt = this.getDropPoint(e, n, dd);
        var node = n.node;
        
        // auto node expand check
        if(!this.expandProcId &amp;&amp; pt == &quot;append&quot; &amp;&amp; node.hasChildNodes() &amp;&amp; !n.node.isExpanded()){
            this.queueExpand(node);
        }else if(pt != &quot;append&quot;){
            this.cancelExpand();
        }
        
        // set the insert point style on the target node
        var returnCls = this.dropNotAllowed;
        if(this.isValidDropPoint(n, pt, dd, e, data)){
           if(pt){
               var el = n.ddel;
               var cls;
               if(pt == &quot;above&quot;){
                   returnCls = n.node.isFirst() ? &quot;x-tree-drop-ok-above&quot; : &quot;x-tree-drop-ok-between&quot;;
                   cls = &quot;x-tree-drag-insert-above&quot;;
               }else if(pt == &quot;below&quot;){
                   returnCls = n.node.isLast() ? &quot;x-tree-drop-ok-below&quot; : &quot;x-tree-drop-ok-between&quot;;
                   cls = &quot;x-tree-drag-insert-below&quot;;
               }else{
                   returnCls = &quot;x-tree-drop-ok-append&quot;;
                   cls = &quot;x-tree-drag-append&quot;;
               }
               if(this.lastInsertClass != cls){
                   Ext.fly(el).replaceClass(this.lastInsertClass, cls);
                   this.lastInsertClass = cls;
               }
           }
       }
       return returnCls;
    },

    // private
    onNodeOut : function(n, dd, e, data){
        this.cancelExpand();
        this.removeDropIndicators(n);
    },

    // private
    onNodeDrop : function(n, dd, e, data){
        var point = this.getDropPoint(e, n, dd);
        var targetNode = n.node;
        targetNode.ui.startDrop();
        if(!this.isValidDropPoint(n, point, dd, e, data)){
            targetNode.ui.endDrop();
            return false;
        }
        // first try to find the drop node
        var dropNode = data.node || (dd.getTreeNode ? dd.getTreeNode(data, targetNode, point, e) : null);
        return this.processDrop(targetNode, data, point, dd, e, dropNode);
    },
    
    onContainerDrop : function(dd, e, data){
        if (this.allowContainerDrop &amp;&amp; this.isValidDropPoint({ ddel: this.tree.getRootNode().ui.elNode, node: this.tree.getRootNode() }, &quot;append&quot;, dd, e, data)) {
            var targetNode = this.tree.getRootNode();       
            targetNode.ui.startDrop();
            var dropNode = data.node || (dd.getTreeNode ? dd.getTreeNode(data, targetNode, 'append', e) : null);
            return this.processDrop(targetNode, data, 'append', dd, e, dropNode);
        }
        return false;
    },
    
    // private
    processDrop: function(target, data, point, dd, e, dropNode){
        var dropEvent = {
            tree : this.tree,
            target: target,
            data: data,
            point: point,
            source: dd,
            rawEvent: e,
            dropNode: dropNode,
            cancel: !dropNode,
            dropStatus: false
        };
        var retval = this.tree.fireEvent(&quot;beforenodedrop&quot;, dropEvent);
        if(retval === false || dropEvent.cancel === true || !dropEvent.dropNode){
            target.ui.endDrop();
            return dropEvent.dropStatus;
        }
    
        target = dropEvent.target;
        if(point == 'append' &amp;&amp; !target.isExpanded()){
            target.expand(false, null, function(){
                this.completeDrop(dropEvent);
            }.createDelegate(this));
        }else{
            this.completeDrop(dropEvent);
        }
        return true;
    },

    // private
    completeDrop : function(de){
        var ns = de.dropNode, p = de.point, t = de.target;
        if(!Ext.isArray(ns)){
            ns = [ns];
        }
        var n;
        for(var i = 0, len = ns.length; i &lt; len; i++){
            n = ns[i];
            if(p == &quot;above&quot;){
                t.parentNode.insertBefore(n, t);
            }else if(p == &quot;below&quot;){
                t.parentNode.insertBefore(n, t.nextSibling);
            }else{
                t.appendChild(n);
            }
        }
        n.ui.focus();
        if(Ext.enableFx &amp;&amp; this.tree.hlDrop){
            n.ui.highlight();
        }
        t.ui.endDrop();
        this.tree.fireEvent(&quot;nodedrop&quot;, de);
    },

    // private
    afterNodeMoved : function(dd, data, e, targetNode, dropNode){
        if(Ext.enableFx &amp;&amp; this.tree.hlDrop){
            dropNode.ui.focus();
            dropNode.ui.highlight();
        }
        this.tree.fireEvent(&quot;nodedrop&quot;, this.tree, targetNode, data, dd, e);
    },

    // private
    getTree : function(){
        return this.tree;
    },

    // private
    removeDropIndicators : function(n){
        if(n &amp;&amp; n.ddel){
            var el = n.ddel;
            Ext.fly(el).removeClass([
                    &quot;x-tree-drag-insert-above&quot;,
                    &quot;x-tree-drag-insert-below&quot;,
                    &quot;x-tree-drag-append&quot;]);
            this.lastInsertClass = &quot;_noclass&quot;;
        }
    },

    // private
    beforeDragDrop : function(target, e, id){
        this.cancelExpand();
        return true;
    },

    // private
    afterRepair : function(data){
        if(data &amp;&amp; Ext.enableFx){
            data.node.ui.highlight();
        }
        this.hideProxy();
    }    
});

}</pre>
</body>
</html>