TreeDataMove

class webix.TreeDataMove()

Treedatamove mixin

References

helpers
assert(), copy(), isArray(), uid().

Referenced by

components
TreeCollection().
views
tree(), treetable().

External references

Official documentation page.

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
webix.TreeDataMove={
    $init:function(){
        webix.assert(this.data, "DataMove :: Component doesn't have DataStore");
    },
    //creates a copy of the item
    copy:function(sid,tindex,tobj,details){
        details = details || {};
        details.copy = true;
        return this.move(sid, tindex, tobj, details);
    },
    _next_move_index:function(nid, next, source){
        if (next && nid){
            var new_index = this.getBranchIndex(nid);
            return new_index+(source == this && source.getBranchIndex(next)<new_index?0:1);
        }
    },
    _check_branch_child:function(parent, child){
        var t = this.data.branch[parent];
        if (t && t.length){
            for (var i=0; i < t.length; i++) {
                if (t[i] == child) return true;
                if (this._check_branch_child(t[i], child)) return true;
            }
        }
        return false;
    },
    //move item to the new position
    move:function(sid,tindex,tobj, details){
        details = details || {};
        tindex = tindex || 0;
        var new_id = details.newId || sid;
        var target_parent = details.parent || 0;

        tobj = tobj||this;
        webix.assert(tobj.data, "moving attempt to component without datastore");
        if (!tobj.data) return;

        if (webix.isArray(sid)){
            for (var i=0; i < sid.length; i++) {
                //increase index for each next item in the set, so order of insertion will be equal to order in the array
                var nid = this.move(sid[i], tindex, tobj, details);
                tindex = tobj._next_move_index(nid, sid[i+1], this);
            }
            return;
        }

        if (this != tobj || details.copy){
            new_id = tobj.data.add(tobj._externalData(this.getItem(sid),new_id), tindex, (target_parent || 0));
            if (this.data.branch[sid] && tobj.getBranchIndex){
                var temp = this.data._scheme_serialize;
                this.data._scheme_serialize = function(obj){
                    var copy = webix.copy(obj);
                    delete copy.$parent; delete copy.$level; delete copy.$child;
                    if (tobj.data.pull[copy.id])
                        copy.id = webix.uid();
                    return copy;
                };
                var copy_data = { data:this.serialize(sid, true), parent:new_id };
                this.data._scheme_serialize = temp;
                tobj.parse(copy_data);
            }
            if (!details.copy)
                this.data.remove(sid);
        } else {
            //move in self
            if (sid == target_parent || this._check_branch_child(sid,target_parent)) return;

            var source = this.getItem(sid);
            var tbranch = this.data.branch[target_parent];
            if (!tbranch)
                tbranch = this.data.branch[target_parent] = [];
            var sbranch = this.data.branch[source.$parent];

            var sindex = webix.PowerArray.find.call(sbranch, sid);
            if (tindex < 0) tindex = Math.max(tbranch.length - 1, 0);
            //in the same branch
            if (sbranch === tbranch && tindex === sindex) return; //same position

            webix.PowerArray.removeAt.call(sbranch, sindex);
            webix.PowerArray.insertAt.call(tbranch, sid, Math.min(tbranch.length, tindex));

            if (!sbranch.length)
                delete this.data.branch[source.$parent];


            if(source.$parent && source.$parent != "0")
                this.getItem(source.$parent).$count--;

            if (target_parent && target_parent != "0"){
                var target = tobj.getItem(target_parent);
                target.$count++;
                this._set_level_rec(source, target.$level+1);
            } else
                this._set_level_rec(source, 1);

            source.$parent = target_parent;
            tobj.data.callEvent("onDataMove", [sid, tindex, target_parent, tbranch[tindex+1]]);
        }

        this.refresh();
        return new_id;    //return ID of item after moving
    },
    _set_level_rec:function(item, value){
        item.$level = value;
        var branch = this.data.branch[item.id];
        if (branch)
            for (var i=0; i<branch.length; i++)
                this._set_level_rec(this.getItem(branch[i]), value+1);
    },
    //reaction on pause during dnd
    _drag_pause:function(id){
        if (id && !id.header) //ignore drag other header
            this.open(id);
    },
    $dropAllow:function(context){
        if (context.from != context.to) return true;
        for (var i=0; i<context.source.length; i++)
            if (context.source ==  context.target || this._check_branch_child(context.source, context.target)) return false;

        return true;
    },
    /*
        this is a stub for future functionality
        currently it just makes a copy of data object, which is enough for current situation
    */
    _externalData:function(data,id){
        var new_data = webix.DataMove._externalData.call(this, data, id);
        delete new_data.open;
        return new_data;
    }
};