Undo

class webix.Undo()

Undo mixin

References

mixins
PowerArray().
helpers
copy(), extend().

Referenced by

mixins
EditAbility().

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
webix.Undo= {
    $init:function(){
        this._undoHistory = webix.extend([],webix.PowerArray,true);
        this._undoCursor = -1;
    },
    undo_setter: function(value){
        if(value){
            this._init_undo();
            this._init_undo = function(){};
        }
        return value;
    },
    _init_undo: function(){
        var view = this;

        // drag-n-drop
        this.attachEvent("onBeforeDrop", function(context){
            if(context.from == context.to){
                var item = view._draggedItem = webix.copy(this.getItem(context.start));
                if(this.data.branch){
                    item.$index = this.getBranchIndex(item.id);
                }
                else
                    item.$index = this.getIndexById(item.id);
            }
        });
        this.data.attachEvent("onDataMove", function( sid ){
            if(view._draggedItem && view._draggedItem.id == sid){
                var data = view._draggedItem;
                view._draggedItem = null;
                view._addToHistory(sid, data, "move");
            }
        });

        // add, remove
        this.data.attachEvent("onBeforeDelete", function(id){
            if(this.getItem(id)){
                var item = view._deletedItem = webix.copy(this.getItem(id));
                if(this.branch){
                    item.$index = this.getBranchIndex(id);
                    if(this.branch[id])
                        item.$branch = webix.copy(this.serialize(id));
                }
                else
                    item.$index = this.getIndexById(id);
            }
        });
        this.data.attachEvent("onDataUpdate", function(id, data, old){
            view._addToHistory(id+"", old, "update");
        });
        this.data.attachEvent("onStoreUpdated", function(id, item, mode){
            var data = null;
            if(id){
                if(mode == "add"){
                    data = webix.copy(item);
                }
                else if( mode == "delete") {
                    data = view._deletedItem;
                }

                if(data)
                    view._addToHistory(id, data, mode);
            }
        });

        // id change
        this.data.attachEvent("onIdChange", function(oldId,newId){
            if(typeof oldId == "object")
                oldId = oldId.row;
            for(var i =0; i < view._undoHistory.length; i++){
                if(view._undoHistory[i].id == oldId){
                    view._undoHistory[i].id = newId;
                }
            }
        });
    },
    _addToHistory: function(id, data, action){
        if(!this._skipHistory && this._settings.undo){
            this._undoHistory.push({id: id, action: action, data: data});
            if(this._undoHistory.length==20)
                this._undoHistory.splice(0,1);
            if(!this._skipCursorInc)
                this._undoCursor = this._undoHistory.length - 1;
        }
    },
    ignoreUndo: function(func, master){
         this._skipHistory = true;
         func.call(master||this);
         this._skipHistory = false;
    },
    removeUndo: function(id){
        for( var i = this._undoHistory.length-1; i >=0; i--){
            if(this._undoHistory[i].id == id){
                if(this._undoHistory[i].action == "id"){
                    id = this._undoHistory[i].data;
                }
                this._undoHistory.removeAt(i);
            }
        }
        this._undoCursor = this._undoHistory.length - 1;
    },
    undo: function(id){
        if(id){
            this.ignoreUndo(function(){
                var data, i;
                for( i = this._undoHistory.length-1; !data && i >=0; i--){
                    if(this._undoHistory[i].id == id)
                        data = this._undoHistory[i];
                }

                if(data){
                    /*if(data.action == "id")
                        id = data.data;*/
                    this._undoAction(data);
                    this._undoHistory.removeAt(i+1);
                    this._undoCursor = this._undoHistory.length - 1;
                }
            });
        }
        else{
            var data = this._undoHistory[this._undoCursor];
            if(data){
                this.ignoreUndo(function(){
                    this._undoAction(data);
                    this._undoHistory.removeAt(this._undoCursor);
                });
                this._undoCursor--;
                /*if(data.action == "id")
                    this.undo();*/
            }
        }
    },
    _undoAction: function(obj){
        if(obj.action == "delete"){
            var branch = null,
                parentId = obj.data.$parent;

            if(obj.data.$branch){
                branch = {
                    parent: obj.id,
                    data: webix.copy(obj.data.$branch)
                };
                delete obj.data.$branch;
                if(parentId && !this.data.branch[parentId])
                    parentId = 0;
            }

            this.add(obj.data, obj.data.$index, parentId);
            if(branch){
                this.parse(branch);
            }
        }
        else if(obj.action == "add"){
            this.remove(obj.id);
        }
        else if(obj.action == "update"){
            this.updateItem(obj.id, obj.data);
        }
        else if(obj.action == "move"){
            if(obj.data.$parent){
                if(this.getItem(obj.data.$parent))
                    this.move(obj.id, obj.data.$index, null, {parent: obj.data.$parent});
            }
            else
                this.move(obj.id, obj.data.$index);
        }
        /*else if(obj.action == "id"){
            this.data.changeId(obj.id, obj.data);
        }*/
    }
};

/*
    Behavior:DataLoader - load data in the component

    @export
        load
        parse
*/