From a11ef2eed7049835c8bdfa50a2b893632c46eaa0 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Wed, 4 Apr 2012 11:11:58 -0700 Subject: Squashed commit of Preparing for the montage undo-manager: Architecture changes Reworked the add and remove elements into 1 function which can take 1 or more elements. Removed the _element from the selection array Many other changes related to those 2 changes Undo/Redo shortcuts are now using montage undo/redo manager. Signed-off-by: Valerio Virgillito --- js/mediators/drag-drop-mediator.js | 8 +- js/mediators/element-mediator.js | 218 +++++++++++-------------------------- js/mediators/keyboard-mediator.js | 37 ++----- js/mediators/mouse-mediator.js | 4 +- 4 files changed, 82 insertions(+), 185 deletions(-) (limited to 'js/mediators') diff --git a/js/mediators/drag-drop-mediator.js b/js/mediators/drag-drop-mediator.js index 8663b06e..9d1b69a9 100755 --- a/js/mediators/drag-drop-mediator.js +++ b/js/mediators/drag-drop-mediator.js @@ -111,6 +111,7 @@ exports.DragDropMediator = Montage.create(Component, { fileName = tempName; } if (save && save.success && save.status === 201) { + var self = this; // if (e.currentTarget.fileType.indexOf('svg') !== -1) { element = NJUtils.makeNJElement('embed', 'SVG', 'block');//TODO: Verify this is proper @@ -121,9 +122,10 @@ exports.DragDropMediator = Montage.create(Component, { element.src = url+'/'+fileName; } //TODO: Remove temp fix for elements to redraw on drop + element.onload = function () { - NJevent("elementAdding", {el: element, data: rules}); - } + self.application.ninja.elementMediator.addElements(element, rules); + }; // rules = { 'position': 'absolute', @@ -133,7 +135,7 @@ exports.DragDropMediator = Montage.create(Component, { '-webkit-transform' : 'perspective(1400) matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)' }; // - NJevent("elementAdding", {el: element, data: rules}); + self.application.ninja.elementMediator.addElements(element, rules); } else { //TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT } diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js index d62fa1f8..09f2aeed 100755 --- a/js/mediators/element-mediator.js +++ b/js/mediators/element-mediator.js @@ -5,147 +5,82 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot */ var Montage = require("montage/core/core").Montage, - NJComponent = require("js/lib/nj-base").NJComponent; + Component = require("montage/ui/component").Component; var ElementController = require("js/controllers/elements/element-controller").ElementController, Command = require("js/controllers/undo-controller").Command, - GroupCommand = require("js/controllers/undo-controller").GroupCommand, NJUtils = require("js/lib/NJUtils").NJUtils; -exports.ElementMediator = Montage.create(NJComponent, { +exports.ElementMediator = Montage.create(Component, { - deleteDelegate: { - value: null - }, - deserializedFromTemplate: { - value: function () { - this.eventManager.addEventListener("elementAdding", this, false); - this.eventManager.addEventListener("deleting", this, false); - } + addDelegate: { + enumerable: false, + value: null }, - // TODO use the specific controller to be able to subclass the functionality - handleElementAdding: { - value: function(event) { - this.addElement(event.detail.el, event.detail.data); - } + deleteDelegate: { + enumerable: false, + value: null }, - handleDeleting: { - value: function(event) { - if(this.deleteDelegate && (typeof this.deleteDelegate.handleDelete === 'function')) { - this.deleteDelegate.handleDelete(); + addElements: { + value: function(elements, rules, notify) { + if(Array.isArray(elements)) { + elements.forEach(function(element) { + element = element; + ElementController.addElement(element, rules); + }); } else { - // Add the Undo/Redo - var els = [], - len = this.application.ninja.selectedElements.length; + ElementController.addElement(elements, rules); - if(len) { - for(var i = 0; i= 0; i--) { - el = items[i]._element || items[i]; - this._removeElement(el); - } + var undoLabel = "add element"; - NJevent( "deleteSelection", items ); - } - } - }, + document.application.undoManager.add(undoLabel, this.addElements, this, elements, null, notify); - _removeElement: { - value: function(el, rules) { - ElementController.removeElement(el, rules); this.application.ninja.documentController.activeDocument.needsSave = true; - NJevent("elementDeleted", el); - } - }, - - replaceElement: { - value: function(el, el2) { - el2.elementModel = el.elementModel; - this.application.ninja.currentDocument.documentRoot.replaceChild(el2, el); - } - }, - getNJProperty: { - value: function(el, p) { - if(el.elementModel) { - if(el.elementModel.hasOwnProperty(p)) { - return el.elementModel[p]; - } else { - console.log("Element Model does not have ", p); - } - } else { - console.log("Element has no Model -- Create one"); - } + NJevent("elementsRemoved", elements); } }, @@ -204,9 +139,7 @@ exports.ElementMediator = Montage.create(NJComponent, { } else { // Calculate currentValue if not found for each element if(currentValue === null) { - console.log("Here"); - var item = el._element || el; - currentValue = item.getAttribute(att); + currentValue = el.getAttribute(att); } var command = Montage.create(Command, { @@ -245,9 +178,7 @@ exports.ElementMediator = Montage.create(NJComponent, { _setAttribute: { value: function(el, att, value, eventType, source) { - var item = el._element || el; - - item.elementModel.controller["setAttribute"](item, att, value); + el.elementModel.controller["setAttribute"](el, att, value); NJevent("attribute" + eventType, {type : "setAttribute", source: source, data: {"els": el, "prop": att, "value": value}, redraw: null}); } @@ -274,7 +205,7 @@ exports.ElementMediator = Montage.create(NJComponent, { if(!currentValue) { var that = this; currentValue = els.map(function(item) { - return that.getProperty((item._element || item), p); + return that.getProperty((item), p); }); } @@ -317,8 +248,7 @@ exports.ElementMediator = Montage.create(NJComponent, { var el; for(var i=0, item; item = els[i]; i++) { - el = item._element || item; - el.elementModel.controller["setProperty"](el, p, value[i]); + item.elementModel.controller["setProperty"](item, p, value[i]); } NJevent("element" + eventType, {type : "setProperty", source: source, data: {"els": els, "prop": p, "value": value}, redraw: null}); @@ -373,11 +303,10 @@ exports.ElementMediator = Montage.create(NJComponent, { _setProperties: { value: function(els, props, eventType, source) { - var el, propsArray; + var propsArray; for(var i=0, item; item = els[i]; i++) { - el = item._element || item; - el.elementModel.controller["setProperties"](el, props, i); + item.elementModel.controller["setProperties"](item, props, i); } NJevent("element" + eventType, {type : "setProperties", source: source, data: {"els": els, "prop": props, "value": props}, redraw: null}); @@ -440,16 +369,14 @@ exports.ElementMediator = Montage.create(NJComponent, { _set3DProperties: { value: function(els, props, eventType, source) { - var el, - update3DModel = false; + var update3DModel = false; - if(eventType === "Change") - { + if(eventType === "Change") { update3DModel = true; } + for(var i=0, item; item = els[i]; i++) { - el = item._element || item; - el.elementModel.controller["set3DProperties"](el, props, i, update3DModel); + item.elementModel.controller["set3DProperties"](item, props, i, update3DModel); } NJevent("element" + eventType, {type : "set3DProperties", source: source, data: {"els": els, "prop": "matrix", "value": props}, redraw: null}); @@ -488,7 +415,7 @@ exports.ElementMediator = Montage.create(NJComponent, { if(!currentValue) { var that = this; currentValue = els.map(function(item) { - return that.getColor(item._element, isFill); + return that.getColor(item, isFill); }); } @@ -528,19 +455,14 @@ exports.ElementMediator = Montage.create(NJComponent, { _setColor: { value: function(els, value, isFill, eventType, source) { - var el; - for(var i=0, item; item = els[i]; i++) { - el = item._element || item; - el.elementModel.controller["setColor"](el, value, isFill); + item.elementModel.controller["setColor"](item, value, isFill); } NJevent("element" + eventType, {type : "setColor", source: source, data: {"els": els, "prop": "color", "value": value, "isFill": isFill}, redraw: null}); } }, - - getStroke: { value: function(el) { if(!el.elementModel) { @@ -570,7 +492,7 @@ exports.ElementMediator = Montage.create(NJComponent, { if(!currentValue) { var that = this; currentValue = els.map(function(item) { - return that.getStroke(item._element); + return that.getStroke(item); }); } @@ -609,11 +531,8 @@ exports.ElementMediator = Montage.create(NJComponent, { _setStroke: { value: function(els, value, eventType, source) { - var el; - for(var i=0, item; item = els[i]; i++) { - el = item._element || item; - el.elementModel.controller["setStroke"](el, value); + item.elementModel.controller["setStroke"](item, value); } NJevent("element" + eventType, {type : "setStroke", source: source, data: {"els": els, "prop": "stroke", "value": value}, redraw: null}); @@ -663,8 +582,7 @@ exports.ElementMediator = Montage.create(NJComponent, { }, getPerspectiveMode: { - value: function(el) - { + value: function(el) { return this.getProperty(el, "-webkit-transform-style"); } }, @@ -674,12 +592,9 @@ exports.ElementMediator = Montage.create(NJComponent, { var dist = el.elementModel.controller["getPerspectiveDist"](el); el.elementModel.controller["set3DProperties"](el, [{mat:mat, dist:dist}], 0, !isChanging); - if(isChanging) - { + if(isChanging) { NJevent("elementChanging", {type : "setMatrix", source: null, data: {"els": [el], "prop": "matrix", "value": mat}, redraw: null}); - } - else - { + } else { NJevent("elementChange", {type : "setMatrix", source: null, data: {"els": [el], "prop": "matrix", "value": mat}, redraw: null}); } } @@ -688,14 +603,7 @@ exports.ElementMediator = Montage.create(NJComponent, { has3D: { value: function(el) { var str = this.getProperty(el, "-webkit-transform"); - if (str && str.length) - { - return true; - } - else - { - return false; - } + return str && str.length; } } diff --git a/js/mediators/keyboard-mediator.js b/js/mediators/keyboard-mediator.js index fc0d0f6a..82596693 100755 --- a/js/mediators/keyboard-mediator.js +++ b/js/mediators/keyboard-mediator.js @@ -99,12 +99,11 @@ exports.KeyboardMediator = Montage.create(Component, { evt.preventDefault(); } - // Check DELETE OR BACKSPACE + // DELETE or BACKSPACE event handler - Removes the current selected elements from the DOM if((evt.keyCode == Keyboard.BACKSPACE) || (evt.keyCode == Keyboard.DELETE)) { evt.stopImmediatePropagation(); evt.preventDefault(); - NJevent("deleting"); - return; + return this.application.ninja.elementMediator.removeElements(document.application.ninja.selectedElements); } @@ -197,9 +196,7 @@ exports.KeyboardMediator = Montage.create(Component, { } if(evt.keyCode === Keyboard.ESCAPE){//ESC key - //console.log("ESC key pressed"); if(this.application.ninja.toolsData) this.application.ninja.toolsData.selectedToolInstance.HandleEscape(evt); - //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); } @@ -214,37 +211,27 @@ exports.KeyboardMediator = Montage.create(Component, { // Check if cmd+z/ctrl+z for Undo (Windows/Mac) if ((evt.keyCode == Keyboard.Z) && (evt.ctrlKey || evt.metaKey) && !evt.shiftKey) { - NJevent("executeUndo"); - //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); - return; - } - - // Check if cmd+s/ctrl+s for Save (Windows/Mac) - if ((evt.keyCode == Keyboard.S) && (evt.ctrlKey || evt.metaKey) && !evt.shiftKey) { - try{ - NJevent("executeSave"); - } - catch(e){ - console.warn("Unable to save"); - console.log(e.stack); - } - evt.preventDefault(); + document.application.undoManager.undo(); return; } // Check if cmd+shift+z for Redo (Mac) if ((evt.keyCode == Keyboard.Z) && evt.metaKey && evt.shiftKey) { - NJevent("executeRedo"); - //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); + document.application.undoManager.redo(); return; } - // Check if ctrl+y for Redo (Windows) + // Check if ctrl+y for Redo (Windows) if ((evt.keyCode == Keyboard.Y) && evt.ctrlKey) { - NJevent("executeRedo"); - //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); + document.application.undoManager.redo(); return; } + + // Check if cmd+s/ctrl+s for Save (Windows/Mac) + if ((evt.keyCode == Keyboard.S) && (evt.ctrlKey || evt.metaKey) && !evt.shiftKey) { + NJevent("executeSave"); + evt.preventDefault(); + } } }, diff --git a/js/mediators/mouse-mediator.js b/js/mediators/mouse-mediator.js index cef6c6c4..4d1fb62a 100755 --- a/js/mediators/mouse-mediator.js +++ b/js/mediators/mouse-mediator.js @@ -5,9 +5,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot */ var Montage = require("montage/core/core").Montage, - NJComponent = require("js/lib/nj-base").NJComponent; + Component = require("montage/ui/component").Component; -exports.MouseMediator = Montage.create( NJComponent, { +exports.MouseMediator = Montage.create(Component, { hasTemplate: { value: false }, -- cgit v1.2.3