From 48d4dd0f0570f4ac3556f228846ed0fd98a674e5 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Mon, 9 Apr 2012 16:47:56 -0700 Subject: setProperties to the montage undo/redo Signed-off-by: Valerio Virgillito --- js/controllers/elements/element-controller.js | 16 +++++-- js/mediators/element-mediator.js | 62 +++++++-------------------- js/tools/SelectionTool.js | 57 ++++++------------------ 3 files changed, 42 insertions(+), 93 deletions(-) (limited to 'js') diff --git a/js/controllers/elements/element-controller.js b/js/controllers/elements/element-controller.js index fda3a3c5..adac1420 100755 --- a/js/controllers/elements/element-controller.js +++ b/js/controllers/elements/element-controller.js @@ -47,9 +47,19 @@ exports.ElementController = Montage.create(Component, { }, setProperties: { - value: function(el, props, index) { - for(var p in props) { - this.application.ninja.stylesController.setElementStyle(el, p, props[p][index]); + value: function(element, properties) { + /* Array of properties is not supported yet + if(Array.isArray(properties)) { + + elements.forEach(function(property) { + this.application.ninja.stylesController.setElementStyle(element, p, props[p][index]); + }); + } else { + } + */ + + for(var property in properties) { + this.application.ninja.stylesController.setElementStyle(element, property, properties[property]); } } }, diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js index a05ca631..738e65fc 100755 --- a/js/mediators/element-mediator.js +++ b/js/mediators/element-mediator.js @@ -272,60 +272,28 @@ exports.ElementMediator = Montage.create(Component, { }, /** - Set a property change command for an element or array of elements + Sets a property object for an element or array of elements. The same properties object gets applied to all the elements @param els: Array of elements. Can contain 1 or more elements - @param props: Property/ies object containing both the value and property + @param properties: Properties object containing both the value and property + @param currentProperties: current properties object for undo/redo. Must be an valid object or null @param eventType: Change/Changing. Will be passed to the dispatched event @param source: String for the source object making the call - @param currentProps *OPTIONAL*: current properties objects array. If not found it will be calculated - @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline */ setProperties: { - value: function(els, props, eventType, source, currentProps, stageRedraw) { - if(eventType === "Changing") { - this._setProperties(els, props, eventType, source); - } else { - var command = Montage.create(Command, { - _els: { value: els }, - _props: { value: props }, - _previous: { value: currentProps }, - _eventType: { value: eventType}, - _source: { value: "undo-redo"}, - description: { value: "Set Properties"}, - receiver: { value: this}, - - execute: { - value: function(senderObject) { - if(senderObject) this._source = senderObject; - this.receiver._setProperties(this._els, this._props, this._eventType, this._source); - this._source = "undo-redo"; - return ""; - } - }, - - unexecute: { - value: function() { - this.receiver._setProperties(this._els, this._previous, this._eventType, this._source); - return ""; - } - } - }); - - NJevent("sendToUndo", command); - command.execute(source); - } - } - }, - - _setProperties: { - value: function(els, props, eventType, source) { - var propsArray; - - for(var i=0, item; item = els[i]; i++) { - item.elementModel.controller["setProperties"](item, props, i); + value: function(elements, properties, currentProperties, eventType, source) { + // Assume elements is an array of elements always + elements.forEach(function(element) { + element.elementModel.controller["setProperties"](element, properties); + }); + + // Add to undo only when a change is requested + if(eventType !== "Changing") { + var undoLabel = "Properties change"; + document.application.undoManager.add(undoLabel, this.setProperties, this, elements, currentProperties, properties, eventType, source); } - NJevent("element" + eventType, {type : "setProperties", source: source, data: {"els": els, "prop": props, "value": props}, redraw: null}); + // Dispatch the element change/changing event. + NJevent("element" + eventType, {type : "setProperties", source: source, data: {"els": elements, "prop": properties, "value": properties}, redraw: null}); } }, diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js index 94cc6b83..6dec781b 100755 --- a/js/tools/SelectionTool.js +++ b/js/tools/SelectionTool.js @@ -372,46 +372,21 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { } } } - if(addToUndoStack) - { - if(!this._use3DMode) - { + + if(addToUndoStack) { + if(!this._use3DMode) { // if we have a delta, that means the transform handles were used and // we should update the width and height too. Otherwise, just update left and top. - if(this._delta) - { - ElementsMediator.setProperties(this.application.ninja.selectedElements, - { "left": newLeft, "top": newTop, "width": newWidth, "height": newHeight }, - "Change", - "selectionTool", - { "left" : previousLeft, "top" : previousTop, "width": previousWidth, "height": previousHeight} - ); - } - else - { - ElementsMediator.setProperties(this.application.ninja.selectedElements, - { "left": newLeft, "top": newTop }, - "Change", - "selectionTool", - { "left" : previousLeft, "top" : previousTop } - ); + if(this._delta) { + ElementsMediator.setProperties(this.application.ninja.selectedElements, {"left": newLeft, "top": newTop, "width": newWidth, "height": newHeight }, + { "left" : previousLeft, "top" : previousTop, "width": previousWidth, "height": previousHeight}, "Change", "selectionTool"); + } else { + ElementsMediator.setProperties(this.application.ninja.selectedElements, {"left": newLeft, "top": newTop }, {"left": previousLeft, "top": previousTop}, "Change", "selectionTool"); } - } - else - { + } else { // TODO - We don't support transform handles in 3d space for now - ElementsMediator.setProperties(this.application.ninja.selectedElements, - { "width": newWidth, "height": newHeight }, - "Change", - "selectionTool", - { "width": previousWidth, "height": previousHeight} - ); - ElementsMediator.set3DProperties(this.application.ninja.selectedElements, - newStyles, - "Change", - "translateTool", - previousStyles - ); + ElementsMediator.setProperties(this.application.ninja.selectedElements, {"width": newWidth, "height": newHeight }, {"width": previousWidth, "height": previousHeight}, "Change", "selectionTool"); + ElementsMediator.set3DProperties(this.application.ninja.selectedElements, newStyles, "Change", "translateTool", previousStyles); } } // Save previous value for undo/redo @@ -471,13 +446,9 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { } } - if(newLeft.length) - { - ElementsMediator.setProperties(this.application.ninja.selectedElements, - { "left": newLeft, "top": newTop }, "Changing", "SelectionTool" ); - } - else - { + if(newLeft.length) { + ElementsMediator.setProperties(this.application.ninja.selectedElements, {"left": newLeft, "top": newTop }, null, "Changing", "SelectionTool" ); + } else { NJevent("elementChanging", {type : "Changing", redraw: false}); } } -- cgit v1.2.3