-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
141 lines (114 loc) · 2.79 KB
/
index.js
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
var has = Object.hasOwnProperty
module.exports = function(model){
model.undoCommand = model.undoCommand || defaultSetter;
model.redoCommand = model.redoCommand || defaultSetter;
model.on('change', function(instance,attr,val,prev){
if (isClass){
handleChange(model,instance,attr,val,prev);
} else {
handleChange(model,model,instance,attr,val);
}
})
model.on('save', function(instance){
if (isClass){
handleSave(instance);
} else {
handleSave(model);
}
})
var isClass = ('function' == typeof model);
var target = (isClass ? model.prototype : model);
target.undo = function(){
withCommands(this, function(){
this._cmds.undo();
});
return this;
}
target.redo = function(){
withCommands(this, function(){
this._cmds.redo();
});
return this;
}
target.undoAll = function(){
withCommands(this, function(){
this._cmds.undoAll();
});
return this;
}
target.redoAll = function(){
withCommands(this, function(){
this._cmds.redoAll();
});
return this;
}
return model;
}
// private
function handleChange(model,instance,attr,val,prev){
if (!instance._undoing){
var cmd = {
undo: function(){
model.undoCommand(instance,attr,prev);
if ('undefined' == typeof prev && has.call(instance,'dirty')){
delete instance.dirty[attr];
}
if (instance.emit) instance.emit('undo',attr,prev);
},
redo: function(){
model.redoCommand(instance,attr,val);
if (instance.emit) instance.emit('redo',attr,val);
}
};
(instance._cmds = instance._cmds || new CmdStack).push(cmd);
}
}
function handleSave(instance){
if (instance._cmds) instance._cmds.reset();
}
function withCommands(instance,fn){
if (!instance._cmds) return;
instance._undoing = true;
fn.call(instance);
instance._undoing = false;
}
function defaultSetter(instance,attr,val){
return instance[attr](val);
}
function CmdStack(){
this.reset();
}
CmdStack.prototype.reset = function(){
this.resetDone();
this.resetUndone();
}
CmdStack.prototype.resetUndone = function(){
this.undone = [];
}
CmdStack.prototype.resetDone = function(){
this.done = [];
}
CmdStack.prototype.push = function(cmd){
this.done.push(cmd);
this.resetUndone();
}
CmdStack.prototype.undoAll = function(){
while (this.done.length) this.undo();
}
CmdStack.prototype.redoAll = function(){
while (this.undone.length) this.redo();
}
CmdStack.prototype.undo = function(){
var last = this.done.pop();
if (last) {
last.undo();
this.undone.push( last );
}
}
CmdStack.prototype.redo = function(){
var last = this.undone.pop();
if (last) {
last.redo();
this.done.push( last );
}
}