aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/element-mediator.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/mediators/element-mediator.js')
-rwxr-xr-xjs/mediators/element-mediator.js439
1 files changed, 128 insertions, 311 deletions
diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js
index d62fa1f8..919aaec1 100755
--- a/js/mediators/element-mediator.js
+++ b/js/mediators/element-mediator.js
@@ -5,146 +5,97 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
5</copyright> */ 5</copyright> */
6 6
7var Montage = require("montage/core/core").Montage, 7var Montage = require("montage/core/core").Montage,
8 NJComponent = require("js/lib/nj-base").NJComponent; 8 Component = require("montage/ui/component").Component;
9 9
10var ElementController = require("js/controllers/elements/element-controller").ElementController, 10var ElementController = require("js/controllers/elements/element-controller").ElementController,
11 Command = require("js/controllers/undo-controller").Command, 11 Command = require("js/controllers/undo-controller").Command,
12 GroupCommand = require("js/controllers/undo-controller").GroupCommand,
13 NJUtils = require("js/lib/NJUtils").NJUtils; 12 NJUtils = require("js/lib/NJUtils").NJUtils;
14 13
15exports.ElementMediator = Montage.create(NJComponent, { 14exports.ElementMediator = Montage.create(Component, {
16 15
17 deleteDelegate: {
18 value: null
19 },
20 16
21 deserializedFromTemplate: { 17 addDelegate: {
22 value: function () { 18 enumerable: false,
23 this.eventManager.addEventListener("elementAdding", this, false); 19 value: null
24 this.eventManager.addEventListener("deleting", this, false);
25 }
26 }, 20 },
27 21
28 // TODO use the specific controller to be able to subclass the functionality 22 deleteDelegate: {
29 handleElementAdding: { 23 enumerable: false,
30 value: function(event) { 24 value: null
31 this.addElement(event.detail.el, event.detail.data);
32 }
33 }, 25 },
34 26
35 handleDeleting: { 27 addElements: {
36 value: function(event) { 28 value: function(elements, rules, notify) {
37 if(this.deleteDelegate && (typeof this.deleteDelegate.handleDelete === 'function')) { 29 if(Array.isArray(elements)) {
38 this.deleteDelegate.handleDelete(); 30 elements.forEach(function(element) {
39 } else { 31 ElementController.addElement(element, rules);
40 // Add the Undo/Redo 32 if(element.elementModel && element.elementModel.props3D) {
41 var els = [], 33 element.elementModel.props3D.init(element, false);
42 len = this.application.ninja.selectedElements.length;
43
44 if(len) {
45 for(var i = 0; i<len; i++) {
46 els.push(this.application.ninja.selectedElements[i]);
47 }
48
49 for(i=0; i<len; i++) {
50 this._removeElement(els[i]._element);
51 } 34 }
52 35 });
53 NJevent( "deleteSelection", els ); 36 } else {
37 ElementController.addElement(elements, rules);
38 if(elements.elementModel && elements.elementModel.props3D) {
39 elements.elementModel.props3D.init(elements, false);
54 } 40 }
55 } 41 }
56 }
57 },
58
59 addElement: {
60 value: function(el, rules, noEvent) {
61 var command = Montage.create(Command, {
62 _el: { value: el },
63 _rules: { value: rules },
64 _noEvent: { value: noEvent },
65 42
66 description: { value: "Adding Element"}, 43 if(this.addDelegate && typeof (this.addDelegate['onAddElements']) === "function") {
44 this.addDelegate['onAddElements'].call(this.addDelegate, elements);
45 }
67 46
68 receiver: { value: this}, 47 var undoLabel = "add element";
69 48
70 execute: { 49 document.application.undoManager.add(undoLabel, this.removeElements, this, elements, notify);
71 value: function() {
72 this.receiver._addElement(this._el, this._rules, this._noEvent);
73 return this._el;
74 }
75 },
76 50
77 unexecute: { 51 this.application.ninja.documentController.activeDocument.needsSave = true;
78 value: function() {
79 this.receiver._removeElement(this._el, this._rules, this._noEvent);
80 return this._el;
81 }
82 }
83 });
84
85 NJevent("sendToUndo", command);
86 command.execute();
87 }
88 },
89 52
90 _addElement: { 53 if(notify || notify === undefined) {
91 value: function(el, rules, noEvent) { 54 NJevent("elementAdded", elements);
92 ElementController.addElement(el, rules);
93 var p3d = this.get3DProperties(el);
94 if(p3d) {
95 el.elementModel.controller["set3DProperties"](el, [p3d], 0, true);
96 }
97 if(!noEvent) {
98 this.application.ninja.documentController.activeDocument.needsSave = true;
99 NJevent("elementAdded", el);
100 } 55 }
101 } 56 }
102 }, 57 },
103 58
104 deleteElements: { 59 removeElements: {
105 value: function(items) { 60 value: function(elements, notify /* Used for the add undo */) {
106 // Add the Undo/Redo
107 var len, el;
108 61
109 len = items.length; 62 if(this.deleteDelegate && (typeof this.deleteDelegate.handleDelete === 'function')) {
63 return this.deleteDelegate.handleDelete();
64 // this.handleDelete.call(deleteDelegate);
65 }
110 66
111 if(len) { 67 if(Array.isArray(elements)) {
68 elements = Array.prototype.slice.call(elements, 0);
69 elements.forEach(function(element) {
70 ElementController.removeElement(element);
71 });
72 } else {
73 ElementController.removeElement(elements);
74 }
112 75
113 for(var i = len - 1; i >= 0; i--) { 76 var undoLabel = "add element";
114 el = items[i]._element || items[i];
115 this._removeElement(el);
116 }
117 77
118 NJevent( "deleteSelection", items ); 78 document.application.undoManager.add(undoLabel, this.addElements, this, elements, null, notify);
119 }
120 }
121 },
122 79
123 _removeElement: {
124 value: function(el, rules) {
125 ElementController.removeElement(el, rules);
126 this.application.ninja.documentController.activeDocument.needsSave = true; 80 this.application.ninja.documentController.activeDocument.needsSave = true;
127 NJevent("elementDeleted", el); 81
82 NJevent("elementsRemoved", elements);
128 } 83 }
129 }, 84 },
130 85
131 replaceElement: { 86 replaceElement: {
132 value: function(el, el2) { 87 value: function(newChild, oldChild, notify) {
133 el2.elementModel = el.elementModel;
134 this.application.ninja.currentDocument.documentRoot.replaceChild(el2, el);
135 }
136 },
137 88
138 getNJProperty: { 89 this.application.ninja.currentDocument.documentRoot.replaceChild(newChild, oldChild);
139 value: function(el, p) { 90
140 if(el.elementModel) { 91 var undoLabel = "replace element";
141 if(el.elementModel.hasOwnProperty(p)) { 92
142 return el.elementModel[p]; 93 document.application.undoManager.add(undoLabel, this.replaceElement, this, oldChild, newChild);
143 } else { 94
144 console.log("Element Model does not have ", p); 95 this.application.ninja.documentController.activeDocument.needsSave = true;
145 } 96
146 } else { 97 if(notify || notify === undefined) {
147 console.log("Element has no Model -- Create one"); 98 NJevent("elementReplaced", {type : "replaceElement", data: {"newChild": newChild, "oldChild": oldChild}});
148 } 99 }
149 } 100 }
150 }, 101 },
@@ -187,74 +138,25 @@ exports.ElementMediator = Montage.create(NJComponent, {
187 }, 138 },
188 139
189 /** 140 /**
190 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
191 @param els: Array of elements. Can contain 1 or more elements 142 @param element: Element
192 @param p: Property to set 143 @param attribute: Attribute to set
193 @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.
194 @param eventType: Change/Changing. Will be passed to the dispatched event 145 @param currentValue: current value
195 @param source: String for the source object making the call 146 @param source: String for the source object making the call
196 @param currentValue *OPTIONAL*: current value array. If not found the current value is calculated 147 */
197 @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline
198 */