DataMove

class webix.DataMove()

Datamove mixin

References

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

Referenced by

mixins
Touch().
components
DataCollection().
views
dataview(), list().

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
webix.DataMove={
    //creates a copy of the item
    copy:function(sid,tindex,tobj, details){
        details = details || {};
        var new_id = details.newId || sid;
        tobj = tobj||this;

        var data = this.getItem(sid);
        webix.assert(data,"Incorrect ID in DataMove::copy");

        //make data conversion between objects
        if (tobj)
            data = tobj._externalData(data);

        //adds new element same as original
        return tobj.data.add(tobj._externalData(data,new_id),tindex,(details.parent || 0));
    },
    _next_move_index:function(nid, next, source){
        if (next && nid){
            var new_index = this.getIndexById(nid);
            return new_index+(source == this && source.getIndexById(next)<new_index?0:1);
        }
    },
    //move item to the new position
    move:function(sid,tindex,tobj, details){
        details = details || {};
        var new_id = details.newId || sid;

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

        //can process an arrya - it allows to use it from onDrag
        if (webix.isArray(sid)){
            //block separate repaint operations
            if (sid.length > 3) //heuristic value, duplicated below
                this.$blockRender = tobj.$blockRender = true;

            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);
            }

            this.$blockRender = tobj.$blockRender = false;
            if (sid.length > 3){
                //repaint whole component
                this.refresh();
                if (tobj != this)
                    tobj.refresh();
            }
            return;
        }

        var nid = sid; //id after moving

        var data = this.getItem(sid);
        webix.assert(data,"Incorrect ID in DataMove::move");

        if (!tobj || tobj == this){
            if (tindex < 0) tindex = this.data.order.length - 1;
            this.data.move(this.getIndexById(sid),tindex);    //move inside the same object
            this.data.callEvent("onDataMove", [sid, tindex, null, this.data.order[tindex+1]]);
        } else {
            //copy to the new object
            nid = tobj.data.add(tobj._externalData(data,new_id),tindex, (details.parent || 0));
            this.data.remove(sid);//delete in old object
        }
        return nid;    //return ID of item after moving
    },
    //move item on one position up
    moveUp:function(id,step){
        return this.move(id,this.getIndexById(id)-(step||1));
    },
    //move item on one position down
    moveDown:function(id,step){
        return this.moveUp(id, (step||1)*-1);
    },
    //move item to the first position
    moveTop:function(id){
        return this.move(id,0);
    },
    //move item to the last position
    moveBottom:function(id){
        return this.move(id,this.data.count()-1);
    },
    /*
        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 newdata = webix.extend({},data);
        newdata.id = (!id || this.data.pull[id])?webix.uid():id;


        newdata.$template=null;

        if (this._settings.externalData)
            newdata = this._settings.externalData.call(this, newdata, id, data);
        return newdata;
    }
};