forked from yui/yui3-gallery
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gallery-undoableaction.js
230 lines (191 loc) · 4.98 KB
/
gallery-undoableaction.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Provides UndoableAction class
*
* @module gallery-undo
*/
(function(){
/**
* Create a UndoableAction
*
* @class UndoableAction
* @extends Base
* @param config {Object} Configuration object
* @constructor
*/
function UndoableAction( config ){
UndoableAction.superclass.constructor.apply( this, arguments );
}
var Lang = Y.Lang,
UAName = "UndoableAction",
LABEL = "label",
BEFOREUNDO = "beforeUndo",
UNDOFINISHED = "undoFinished",
BEFOREREDO = "beforeRedo",
REDOFINISHED = "redoFinished";
Y.mix( UndoableAction, {
/**
* The identity of UndoableAction.
*
* @property UndoableAction.NAME
* @type String
* @static
*/
NAME : UAName,
/**
* Static property used to define the default attribute configuration of UndoableAction.
*
* @property UndoableAction.ATTRS
* @type Object
* @protected
* @static
*/
ATTRS : {
/**
* The label of action
*
* @attribute label
* @type String
* @default ""
*/
label: {
value: "",
validator: Lang.isString
},
/**
* Boolean, indicates if action must be processed asynchronously.
* If true, <code>undo</code> method must fire <code>undoFinished</code> event.
* Respectively, <code>redo</code> method must fire <code>redoFinished</code> event
*
* @attribute asyncProcessing
* @type Boolean
* @default false
*/
asyncProcessing : {
value: false,
validator: Lang.isBoolean
}
}
});
Y.extend( UndoableAction, Y.Base, {
/**
* Container for child actions of this action
*
* @property _childActions
* @protected
* @type Array
*/
_childActions : [],
/**
* Publishes events
*
* @method initializer
* @protected
*/
initializer : function( cfg ) {
this._initEvents();
},
/**
* Destructor lifecycle implementation for UndoableAction class.
*
* @method destructor
* @protected
*/
destructor : function() {
},
/**
* Publishes UndoableAction's events
*
* @method _initEvents
* @protected
*/
_initEvents : function(){
/**
* Signals the beginning of action undo.
*
* @event beforeUndo
* @param event {Event.Facade} An Event Facade object
*/
this.publish( BEFOREUNDO );
/**
* Signals the end of action undo.
*
* @event undoFinished
* @param event {Event.Facade} An Event Facade object
*/
this.publish( UNDOFINISHED );
/**
* Signals the beginning of action redo.
*
* @event beforeRedo
* @param event {Event.Facade} An Event Facade object
*/
this.publish( BEFOREREDO );
/**
* Signals the end of action redo.
*
* @event redoFinished
* @param event {Event.Facade} An Event Facade object
*/
this.publish( REDOFINISHED );
},
/**
* The default implemetation undoes all child actions in reverse order.
*
* @method undo
*/
undo : function(){
var childActions, action, i;
this.fire( BEFOREUNDO );
childActions = this._childActions;
for( i = childActions.length - 1; i > 0; i-- ){
action = childActions[i];
action.undo();
}
this.fire( UNDOFINISHED );
},
/**
* The default implemetation redoes all child actions.
*
* @method redo
*/
redo : function(){
var childActions, action, i, length;
this.fire( BEFOREREDO );
childActions = this._childActions;
length = childActions.length;
for( i = 0; i < length; i++ ){
action = childActions[i];
action.redo();
}
this.fire( REDOFINISHED );
},
/**
* Depending on the application, an UndoableAction may merge with another action. If merge was successfull, merge must return true; otherwise returns false.
* The default implemetation returns false.
*
* @method merge
* @param {Y.UndoableAction} newAction The action to merge with
* @return {Boolean} false
*/
merge : function( newAction ){
return false;
},
/**
* UndoManager invokes <code>cancel</code> method of action before removing it from the list.<br>
* The default implemetation does nothing.
*
* @method cancel
*/
cancel : function(){
},
/**
* Overrides <code>toString()</code> method.<br>
* The default implementation returns the value of <code>label</code> property.
*
*/
toString : function(){
return this.get( LABEL );
}
});
Y.UndoableAction = UndoableAction;
}());