aboutsummaryrefslogtreecommitdiff
path: root/js/mediators
diff options
context:
space:
mode:
authorValerio Virgillito2012-04-17 11:08:29 -0700
committerValerio Virgillito2012-04-17 11:08:29 -0700
commit2292a35f545a161c0d19b26a8640b9421afce792 (patch)
treec9a64c50dd4c42cdbd221e35548418fe883c2d13 /js/mediators
parent4b900ea5cd6bb77eb30cec8c03b9ec9fa662c1e9 (diff)
parent7ed5215332123d16f819a7a26c651af520a68669 (diff)
downloadninja-2292a35f545a161c0d19b26a8640b9421afce792.tar.gz
Merge pull request #163 from mencio/undo-manager
Undo manager - Fixing a couple undo bugs and architecture work to support undo/redo
Diffstat (limited to 'js/mediators')
-rwxr-xr-xjs/mediators/element-mediator.js225
1 files changed, 59 insertions, 166 deletions
diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js
index a05ca631..919aaec1 100755
--- a/js/mediators/element-mediator.js
+++ b/js/mediators/element-mediator.js
@@ -138,70 +138,25 @@ exports.ElementMediator = Montage.create(Component, {
138 }, 138 },
139 139
140 /** 140 /**
141 Set a property change command for an element or array of elements 141 Set a property change command for an element or array of elements
142 @param els: Array of elements. Can contain 1 or more elements 142 @param element: Element
143 @param p: Property to set 143 @param attribute: Attribute to set
144 @param value: Value to be set. This is an array of values corresponding to the array of elements 144 @param value: Value to be set.
145 @param eventType: Change/Changing. Will be passed to the dispatched event 145 @param currentValue: current value
146 @param source: String for the source object making the call 146 @param source: String for the source object making the call
147 @param currentValue *OPTIONAL*: current value array. If not found the current value is calculated 147 */
148 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline
149 */
150 setAttribute: { 148 setAttribute: {
151 value: function(el, att, value, eventType, source, currentValue) { 149 value: function(element, attribute, value, currentValue, source) {
152 150 element.elementModel.controller["setAttribute"](element, attribute, value);
153 if(eventType === "Changing") {
154 this._setAttribute(el, att, value, eventType, source);
155 } else {
156 // Calculate currentValue if not found for each element
157 if(currentValue === null) {
158 currentValue = el.getAttribute(att);
159 }
160
161 var command = Montage.create(Command, {
162 _el: { value: el },
163 _att: { value: att },
164 _value: { value: value },
165 _previous: { value: currentValue },
166 _eventType: { value: eventType},
167 _source: { value: "undo-redo"},
168 description: { value: "Set Attribute"},
169 receiver: { value: this},
170
171 execute: {
172 value: function(senderObject) {
173 if(senderObject) this._source = senderObject;
174 this.receiver._setAttribute(this._el, this._att, this._value, this._eventType, this._source);
175 this._source = "undo-redo";
176 return "";
177 }
178 },
179
180 unexecute: {
181 value: function() {
182 this.receiver._setAttribute(this._el, this._att, this._previous, this._eventType, this._source);
183 return "";
184 }
185 }
186 });
187
188 NJevent("sendToUndo", command);
189 command.execute(source);
190 }
191
192 }
193 },
194 151
195 _setAttribute: { 152 // Add to the undo
196 value: function(el, att, value, eventType, source) { 153 var undoLabel = "Attribute change";
197 el.elementModel.controller["setAttribute"](el, att, value); 154 document.application.undoManager.add(undoLabel, this.setAttribute, this, element, attribute, currentValue, value, source);
198 155
199 NJevent("attribute" + eventType, {type : "setAttribute", source: source, data: {"els": el, "prop": att, "value": value}, redraw: null}); 156 NJevent("attributeChange");
200 } 157 }
201 }, 158 },
202 159
203
204
205 /** 160 /**
206 Set a property change command for an element or array of elements 161 Set a property change command for an element or array of elements
207 @param els: Array of elements. Can contain 1 or more elements 162 @param els: Array of elements. Can contain 1 or more elements
@@ -213,7 +168,7 @@ exports.ElementMediator = Montage.create(Component, {
213 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline 168 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline
214 */ 169 */
215 setProperty: { 170 setProperty: {
216 value: function(els, p, value, eventType, source, currentValue, stageRedraw) { 171 value: function(els, p, value, eventType, source, currentValue) {
217 if(eventType === "Changing") { 172 if(eventType === "Changing") {
218 this._setProperty(els, p, value, eventType, source); 173 this._setProperty(els, p, value, eventType, source);
219 } else { 174 } else {
@@ -272,130 +227,68 @@ exports.ElementMediator = Montage.create(Component, {
272 }, 227 },
273 228
274 /** 229 /**
275 Set a property change command for an element or array of elements 230 Sets a property object for an element or array of elements. The same properties object gets applied to all the elements
276 @param els: Array of elements. Can contain 1 or more elements 231 @param elements: Array of elements objects: element, properties and previousProperties
277 @param props: Property/ies object containing both the value and property
278 @param eventType: Change/Changing. Will be passed to the dispatched event 232 @param eventType: Change/Changing. Will be passed to the dispatched event
279 @param source: String for the source object making the call 233 @param source: String for the source object making the call
280 @param currentProps *OPTIONAL*: current properties objects array. If not found it will be calculated
281 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline
282 */ 234 */
283 setProperties: { 235 setProperties: {
284 value: function(els, props, eventType, source, currentProps, stageRedraw) { 236 value: function(elements, eventType, source) {
285 if(eventType === "Changing") { 237
286 this._setProperties(els, props, eventType, source); 238 elements.forEach(function(elementObject) {
287 } else { 239 elementObject.element.elementModel.controller["setProperties"](elementObject.element, elementObject.properties);
288 var command = Montage.create(Command, { 240 });
289 _els: { value: els }, 241
290 _props: { value: props }, 242 if(eventType !== "Changing") {
291 _previous: { value: currentProps }, 243 var undoLabel = "Properties change";
292 _eventType: { value: eventType}, 244 elements.forEach(function(elementObject) {
293 _source: { value: "undo-redo"}, 245 var swap = elementObject.properties;
294 description: { value: "Set Properties"}, 246 elementObject.properties = elementObject.previousProperties;
295 receiver: { value: this}, 247 elementObject.previousProperties = swap;
296
297 execute: {
298 value: function(senderObject) {
299 if(senderObject) this._source = senderObject;
300 this.receiver._setProperties(this._els, this._props, this._eventType, this._source);
301 this._source = "undo-redo";
302 return "";
303 }
304 },
305
306 unexecute: {
307 value: function() {
308 this.receiver._setProperties(this._els, this._previous, this._eventType, this._source);
309 return "";
310 }
311 }
312 }); 248 });
313 249 document.application.undoManager.add(undoLabel, this.setProperties, this, elements, eventType, source);
314 NJevent("sendToUndo", command);
315 command.execute(source);
316 } 250 }
317 }
318 },
319 251
320 _setProperties: { 252 // Map the elements for the event data
321 value: function(els, props, eventType, source) { 253 // TODO: Clean this up
322 var propsArray; 254 var els = elements.map(function(element) {
255 return element.element;
256 });
323 257
324 for(var i=0, item; item = els[i]; i++) { 258 // Dispatch the element change/changing event.
325 item.elementModel.controller["setProperties"](item, props, i); 259 NJevent("element" + eventType, {type : "setProperties", source: source, data: {"els": els, "prop": elements[0].properties, "value": elements}, redraw: null});
326 }
327
328 NJevent("element" + eventType, {type : "setProperties", source: source, data: {"els": els, "prop": props, "value": props}, redraw: null});
329 } 260 }
330 }, 261 },
331 262
332 /**
333 Set a property change command for an element or array of elements
334 @param els: Array of elements. Can contain 1 or more elements
335 @param props: Property/ies object containing both the value and property
336 @param eventType: Change/Changing. Will be passed to the dispatched event
337 @param source: String for the source object making the call
338 @param currentProps *OPTIONAL*: current properties objects array. If not found it will be calculated
339 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline
340 */
341 set3DProperties: { 263 set3DProperties: {
342 value: function(els, props, eventType, source, currentProps, stageRedraw) { 264 value: function(elements, eventType, source) {
343 if(eventType === "Changing") {
344 this._set3DProperties(els, props, eventType, source);
345 } else {
346 // Calculate currentProps if not found for each element
347 if(!currentProps) {
348 var that = this;
349 currentProps = els.map(function(item) {
350 return that.get3DProperties(item);
351 });
352 }
353
354 var command = Montage.create(Command, {
355 _els: { value: els },
356 _props: { value: props },
357 _previous: { value: currentProps },
358 _eventType: { value: eventType},
359 _source: { value: "undo-redo"},
360 description: { value: "Set 3D Properties"},
361 receiver: { value: this},
362
363 execute: {
364 value: function(senderObject) {
365 if(senderObject) this._source = senderObject;
366 this.receiver._set3DProperties(this._els, this._props, this._eventType, this._source);
367 this._source = "undo-redo";
368</