forked from ondras/my-mind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
216 lines (200 loc) · 6.43 KB
/
action.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
MM.Action = function() {}
MM.Action.prototype.perform = function() {}
MM.Action.prototype.undo = function() {}
MM.Action.Multi = function(actions) {
this._actions = actions;
}
MM.Action.Multi.prototype = Object.create(MM.Action.prototype);
MM.Action.Multi.prototype.perform = function() {
this._actions.forEach(function(action) {
action.perform();
});
}
MM.Action.Multi.prototype.undo = function() {
this._actions.slice().reverse().forEach(function(action) {
action.undo();
});
}
MM.Action.InsertNewItem = function(parent, index) {
this._parent = parent;
this._index = index;
this._item = new MM.Item();
}
MM.Action.InsertNewItem.prototype = Object.create(MM.Action.prototype);
MM.Action.InsertNewItem.prototype.perform = function() {
this._parent.expand(); /* FIXME remember? */
this._item = this._parent.insertChild(this._item, this._index);
MM.App.select(this._item);
}
MM.Action.InsertNewItem.prototype.undo = function() {
this._parent.removeChild(this._item);
MM.App.select(this._parent);
}
MM.Action.AppendItem = function(parent, item) {
this._parent = parent;
this._item = item;
}
MM.Action.AppendItem.prototype = Object.create(MM.Action.prototype);
MM.Action.AppendItem.prototype.perform = function() {
this._parent.insertChild(this._item);
MM.App.select(this._item);
}
MM.Action.AppendItem.prototype.undo = function() {
this._parent.removeChild(this._item);
MM.App.select(this._parent);
}
MM.Action.RemoveItem = function(item) {
this._item = item;
this._parent = item.getParent();
this._index = this._parent.getChildren().indexOf(this._item);
}
MM.Action.RemoveItem.prototype = Object.create(MM.Action.prototype);
MM.Action.RemoveItem.prototype.perform = function() {
this._parent.removeChild(this._item);
MM.App.select(this._parent);
}
MM.Action.RemoveItem.prototype.undo = function() {
this._parent.insertChild(this._item, this._index);
MM.App.select(this._item);
}
MM.Action.MoveItem = function(item, newParent, newIndex, newSide) {
this._item = item;
this._newParent = newParent;
this._newIndex = (arguments.length < 3 ? null : newIndex);
this._newSide = newSide || "";
this._oldParent = item.getParent();
this._oldIndex = this._oldParent.getChildren().indexOf(item);
this._oldSide = item.getSide();
}
MM.Action.MoveItem.prototype = Object.create(MM.Action.prototype);
MM.Action.MoveItem.prototype.perform = function() {
this._item.setSide(this._newSide);
if (this._newIndex === null) {
this._newParent.insertChild(this._item);
} else {
this._newParent.insertChild(this._item, this._newIndex);
}
MM.App.select(this._item);
}
MM.Action.MoveItem.prototype.undo = function() {
this._item.setSide(this._oldSide);
this._oldParent.insertChild(this._item, this._oldIndex);
MM.App.select(this._newParent);
}
MM.Action.Swap = function(item, diff) {
this._item = item;
this._parent = item.getParent();
var children = this._parent.getChildren();
var sibling = this._parent.getLayout().pickSibling(this._item, diff);
this._sourceIndex = children.indexOf(this._item);
this._targetIndex = children.indexOf(sibling);
}
MM.Action.Swap.prototype = Object.create(MM.Action.prototype);
MM.Action.Swap.prototype.perform = function() {
this._parent.insertChild(this._item, this._targetIndex);
}
MM.Action.Swap.prototype.undo = function() {
this._parent.insertChild(this._item, this._sourceIndex);
}
MM.Action.SetLayout = function(item, layout) {
this._item = item;
this._layout = layout;
this._oldLayout = item.getOwnLayout();
}
MM.Action.SetLayout.prototype = Object.create(MM.Action.prototype);
MM.Action.SetLayout.prototype.perform = function() {
this._item.setLayout(this._layout);
}
MM.Action.SetLayout.prototype.undo = function() {
this._item.setLayout(this._oldLayout);
}
MM.Action.SetShape = function(item, shape) {
this._item = item;
this._shape = shape;
this._oldShape = item.getOwnShape();
}
MM.Action.SetShape.prototype = Object.create(MM.Action.prototype);
MM.Action.SetShape.prototype.perform = function() {
this._item.setShape(this._shape);
}
MM.Action.SetShape.prototype.undo = function() {
this._item.setShape(this._oldShape);
}
MM.Action.SetColor = function(item, color) {
this._item = item;
this._color = color;
this._oldColor = item.getOwnColor();
}
MM.Action.SetColor.prototype = Object.create(MM.Action.prototype);
MM.Action.SetColor.prototype.perform = function() {
this._item.setColor(this._color);
}
MM.Action.SetColor.prototype.undo = function() {
this._item.setColor(this._oldColor);
}
MM.Action.SetText = function(item, text) {
this._item = item;
this._text = text;
this._oldText = item.getText();
this._oldValue = item.getValue(); /* adjusting text can also modify value! */
}
MM.Action.SetText.prototype = Object.create(MM.Action.prototype);
MM.Action.SetText.prototype.perform = function() {
this._item.setText(this._text);
var numText = Number(this._text);
if (numText == this._text) { this._item.setValue(numText); }
}
MM.Action.SetText.prototype.undo = function() {
this._item.setText(this._oldText);
this._item.setValue(this._oldValue);
}
MM.Action.SetValue = function(item, value) {
this._item = item;
this._value = value;
this._oldValue = item.getValue();
}
MM.Action.SetValue.prototype = Object.create(MM.Action.prototype);
MM.Action.SetValue.prototype.perform = function() {
this._item.setValue(this._value);
}
MM.Action.SetValue.prototype.undo = function() {
this._item.setValue(this._oldValue);
}
MM.Action.SetStatus = function(item, status) {
this._item = item;
this._status = status;
this._oldStatus = item.getStatus();
}
MM.Action.SetStatus.prototype = Object.create(MM.Action.prototype);
MM.Action.SetStatus.prototype.perform = function() {
this._item.setStatus(this._status);
}
MM.Action.SetStatus.prototype.undo = function() {
this._item.setStatus(this._oldStatus);
}
MM.Action.SetIcon = function(item, icon) {
this._item = item;
this._icon = icon;
this._oldIcon = item.getIcon();
}
MM.Action.SetIcon.prototype = Object.create(MM.Action.prototype);
MM.Action.SetIcon.prototype.perform = function() {
this._item.setIcon(this._icon);
}
MM.Action.SetIcon.prototype.undo = function() {
this._item.setIcon(this._oldIcon);
}
MM.Action.SetSide = function(item, side) {
this._item = item;
this._side = side;
this._oldSide = item.getSide();
}
MM.Action.SetSide.prototype = Object.create(MM.Action.prototype);
MM.Action.SetSide.prototype.perform = function() {
this._item.setSide(this._side);
this._item.getMap().update();
}
MM.Action.SetSide.prototype.undo = function() {
this._item.setSide(this._oldSide);
this._item.getMap().update();
}