diff options
Diffstat (limited to 'js/controllers/undo-controller.js')
-rw-r--r-- | js/controllers/undo-controller.js | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/js/controllers/undo-controller.js b/js/controllers/undo-controller.js new file mode 100644 index 00000000..926803d3 --- /dev/null +++ b/js/controllers/undo-controller.js | |||
@@ -0,0 +1,206 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Montage = require("montage/core/core").Montage, | ||
8 | Component = require("montage/ui/component").Component; | ||
9 | |||
10 | exports.Command = Montage.create( Montage, { | ||
11 | |||
12 | description: { value: "" }, | ||
13 | |||
14 | receiver: { value: "" }, | ||
15 | |||
16 | execute: { value: function() {} }, | ||
17 | |||
18 | unexecute: { value: function() {} } | ||
19 | }); | ||
20 | |||
21 | exports.GroupCommand = Montage.create( Montage, { | ||
22 | _commands: { value: [] }, | ||
23 | |||
24 | init: { | ||
25 | value: function() { | ||
26 | this._commands = []; | ||
27 | } | ||
28 | }, | ||
29 | |||
30 | addCommand: { | ||
31 | value: function(c) { | ||
32 | if(c) { | ||
33 | this._commands.push(c); | ||
34 | } | ||
35 | } | ||
36 | }, | ||
37 | |||
38 | description: { value: "" }, | ||
39 | |||
40 | execute: { | ||
41 | value: function() { | ||
42 | var items = []; | ||
43 | for (var i = 0, len = this._commands.length; i < len; i++) { | ||
44 | items.push(this._commands[i].execute()); | ||
45 | } | ||
46 | return items; | ||
47 | } | ||
48 | }, | ||
49 | |||
50 | unexecute: { | ||
51 | value: function() { | ||
52 | var items = []; | ||
53 | for (var i = 0, len = this._commands.length; i < len; i++) { | ||
54 | items.push(this._commands[i].unexecute()); | ||
55 | } | ||
56 | return items; | ||
57 | } | ||
58 | } | ||
59 | }); | ||
60 | |||
61 | exports.UndoController = Montage.create( Component, { | ||
62 | /** | ||
63 | * The maximum number of undo items allowed in the undo history. | ||
64 | */ | ||
65 | _MAX_UNDO_STATES: { | ||
66 | value:1000, | ||
67 | writable:false, | ||
68 | enumerable:false | ||
69 | }, | ||
70 | |||
71 | /** | ||
72 | * Undo Queue | ||
73 | */ | ||
74 | _undoQueue: { value: [] }, | ||
75 | |||
76 | undoQueue: { | ||
77 | get: function() { | ||
78 | return this._undoQueue; | ||
79 | } | ||
80 | }, | ||
81 | |||
82 | /** | ||
83 | * Redo Queue | ||
84 | */ | ||
85 | _redoQueue: { value: [], enumerable: false }, | ||
86 | |||
87 | redoQueue: { | ||
88 | get: function() { | ||
89 | return this._redoQueue; | ||
90 | } | ||
91 | }, | ||
92 | |||
93 | canUndo: { | ||
94 | dependencies: ["undoQueue.count()"], | ||
95 | get: function() { | ||
96 | return !!this.undoQueue.length; | ||
97 | } | ||
98 | }, | ||
99 | |||
100 | canRedo: { | ||
101 | dependencies: ["redoQueue.count()"], | ||
102 | get: function() { | ||
103 | return !!this.redoQueue.length; | ||
104 | } | ||
105 | }, | ||
106 | |||
107 | |||
108 | deserializedFromTemplate: { | ||
109 | value: function(){ | ||
110 | this.eventManager.addEventListener( "openDocument", this, false); | ||
111 | this.eventManager.addEventListener( "sendToUndo", this, false); | ||
112 | this.eventManager.addEventListener( "executeUndo", this, false); | ||
113 | this.eventManager.addEventListener( "executeRedo", this, false); | ||
114 | } | ||
115 | }, | ||
116 | |||
117 | handleOpenDocument: { | ||
118 | value: function(undoQueue, redoQueue) { | ||
119 | // this._undoQueue = undoQueue; | ||
120 | // this._redoQueue = redoQueue; | ||
121 | } | ||
122 | }, | ||
123 | |||
124 | handleSendToUndo: { | ||
125 | value: function(event) { | ||
126 | this.undoQueue.push(event.detail); | ||
127 | this._clearRedo(); | ||
128 | } | ||
129 | }, | ||
130 | |||
131 | handleExecuteUndo: { | ||
132 | value: function() { | ||
133 | this.undo(); | ||
134 | } | ||
135 | }, | ||
136 | |||
137 | handleExecuteRedo: { | ||
138 | value: function() { | ||
139 | this.redo(); | ||
140 | } | ||
141 | }, | ||
142 | |||
143 | undo: { | ||
144 | value: function(levels) { | ||
145 | try{ | ||
146 | var command, info; | ||
147 | |||
148 | /* Add Levels support later | ||
149 | for (var i=0; i < levels; i++) { | ||
150 | do the undo | ||
151 | } | ||
152 | */ | ||
153 | if(this.undoQueue.length !== 0) { | ||
154 | command = this.undoQueue.pop(); | ||
155 | info = command.unexecute(); | ||
156 | NJevent( "undo", info ); | ||
157 | this.redoQueue.push(command); | ||
158 | } | ||
159 | |||
160 | } catch (err) { | ||
161 | console.log("Cannot Undo -- ", err); | ||
162 | } | ||
163 | } | ||
164 | }, | ||
165 | |||
166 | redo: { | ||
167 | value: function(levels) { | ||
168 | try{ | ||
169 | var command, info; | ||
170 | |||
171 | /* Add Levels support later | ||
172 | for (var i=0; i < levels; i++) { | ||
173 | do the redo | ||
174 | } | ||
175 | */ | ||
176 | if(this.redoQueue.length === 0) { | ||
177 | return; | ||
178 | } | ||
179 | |||
180 | command = this.redoQueue.pop(); | ||
181 | info = command.execute(); | ||
182 | NJevent( "redo", info ); | ||
183 | this.undoQueue.push(command); | ||
184 | |||
185 | |||
186 | } catch (err) { | ||
187 | console.log("Cannot Redo -- ", err); | ||
188 | } | ||
189 | } | ||
190 | }, | ||
191 | // | ||
192 | // insertCommand: { | ||
193 | // value: function(command) { | ||
194 | // debugger; | ||
195 | // this._undoQueue.push(command); | ||
196 | // this._clearRedo(); | ||
197 | // } | ||
198 | // }, | ||
199 | |||
200 | _clearRedo: { | ||
201 | value: function() { | ||
202 | this.redoQueue.splice(0, this.redoQueue.length); | ||
203 | //this.redoQueue = []; | ||
204 | } | ||
205 | } | ||
206 | }); \ No newline at end of file | ||