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.js222
1 files changed, 72 insertions, 150 deletions
diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js
index a2440fb4..05e5093b 100755
--- a/js/mediators/element-mediator.js
+++ b/js/mediators/element-mediator.js
@@ -5,151 +5,100 @@ 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) {
31 element = element;
32 ElementController.addElement(element, rules);
33 });
39 } else { 34 } else {
40 // Add the Undo/Redo 35 ElementController.addElement(elements, rules);
41 var els = [],
42 len = this.application.ninja.selectedElements.length;
43 36
44 if(len) { 37 // TODO - Check with webgl branch - Props seem to be already there.
45 for(var i = 0; i<len; i++) { 38 var prop3d = this.get3DProperties(elements);
46 els.push(this.application.ninja.selectedElements[i]); 39 if(prop3d) {
40 elements.elementModel.controller["set3DProperties"](elements, [prop3d], 0, true);
47 } 41 }
48
49 for(i=0; i<len; i++) {
50 this._removeElement(els[i]._element);
51 } 42 }
52 43
53 NJevent( "deleteSelection", els ); 44 if(this.addDelegate && typeof (this.addDelegate['onAddElements']) === "function") {
45 this.addDelegate['onAddElements'].call(this.addDelegate, elements);
54 } 46 }
55 }
56 }
57 },
58 47
59 addElement: { 48 var undoLabel = "add element";
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 49
66 description: { value: "Adding Element"}, 50 document.application.undoManager.add(undoLabel, this.removeElements, this, elements, notify);
67 51
68 receiver: { value: this}, 52 this.application.ninja.documentController.activeDocument.needsSave = true;
69 53
70 execute: { 54 if(notify || notify === undefined) {
71 value: function() { 55 NJevent("elementAdded", elements);
72 this.receiver._addElement(this._el, this._rules, this._noEvent);
73 return this._el;
74 } 56 }
57 }
75 }, 58 },
76 59
77 unexecute: { 60 removeElements: {
78 value: function() { 61 value: function(elements, notify /* Used for the add undo */) {
79 this.receiver._removeElement(this._el, this._rules, this._noEvent);
80 return this._el;
81 }
82 }
83 });
84 62
85 NJevent("sendToUndo", command); 63 if(this.deleteDelegate && (typeof this.deleteDelegate.handleDelete === 'function')) {
86 command.execute(); 64 return this.deleteDelegate.handleDelete();
65 // this.handleDelete.call(deleteDelegate);
87 } 66 }
88 },
89 67
90 _addElement: { 68 if(Array.isArray(elements)) {
91 value: function(el, rules, noEvent) { 69 elements = Array.prototype.slice.call(elements, 0);
92 ElementController.addElement(el, rules); 70 elements.forEach(function(element) {
93// var p3d = this.get3DProperties(el); 71 ElementController.removeElement(element);
94// if(p3d) { 72 });
95// el.elementModel.controller["set3DProperties"](el, [p3d], 0, true); 73 } else {
96// } 74 ElementController.removeElement(elements);
97 if(el.elementModel && el.elementModel.props3D) {
98 el.elementModel.props3D.init(el, false);
99 } 75 }
100 if(!noEvent) { 76
77 var undoLabel = "add element";
78
79 document.application.undoManager.add(undoLabel, this.addElements, this, elements, null, notify);
80
101 this.application.ninja.documentController.activeDocument.needsSave = true; 81 this.application.ninja.documentController.activeDocument.needsSave = true;
102 NJevent("elementAdded", el); 82
83 NJevent("elementsRemoved", elements);
103 } 84 }
104 }
105 }, 85 },
106 86
107 deleteElements: { 87 replaceElement: {
108 value: function(items) { 88 value: function(newChild, oldChild, notify) {
109 // Add the Undo/Redo
110 var len, el;
111 89
112 len = items.length; 90 this.application.ninja.currentDocument.documentRoot.replaceChild(newChild, oldChild);
113 91
114 if(len) { 92 var undoLabel = "replace element";
115 93
116 for(var i = len - 1; i >= 0; i--) { 94 document.application.undoManager.add(undoLabel, this.replaceElement, this, oldChild, newChild);
117 el = items[i]._element || items[i];
118 this._removeElement(el);
119 }
120 95
121 NJevent( "deleteSelection", items );
122 }
123 }
124 },
125
126 _removeElement: {
127 value: function(el, rules) {
128 ElementController.removeElement(el, rules);
129 this.application.ninja.documentController.activeDocument.needsSave = true; 96 this.application.ninja.documentController.activeDocument.needsSave = true;
130 NJevent("elementDeleted", el);
131 }
132 },
133 97
134 replaceElement: { 98 if(notify || notify === undefined) {
135 value: function(el, el2) { 99 NJevent("elementReplaced", {type : "replaceElement", data: {"newChild": newChild, "oldChild": oldChild}});
136 el2.elementModel = el.elementModel;
137 this.application.ninja.currentDocument.documentRoot.replaceChild(el2, el);
138 } 100 }
139 },
140
141 getNJProperty: {
142 value: function(el, p) {
143 if(el.elementModel) {
144 if(el.elementModel.hasOwnProperty(p)) {
145 return el.elementModel[p];
146 } else {
147 console.log("Element Model does not have ", p);
148 } 101 }
149 } else {
150 console.log("Element has no Model -- Create one");
151 }
152 }
153 }, 102 },
154 103
155 getProperty: { 104 getProperty: {
@@ -207,9 +156,7 @@ exports.ElementMediator = Montage.create(NJComponent, {
207 } else { 156 } else {
208 // Calculate currentValue if not found for each element 157 // Calculate currentValue if not found for each element
209 if(currentValue === null) { 158 if(currentValue === null) {
210 console.log("Here"); 159 currentValue = el.getAttribute(att);
211 var item = el._element || el;
212 currentValue = item.getAttribute(att);
213 } 160 }
214 161
215 var command = Montage.create(Command, { 162 var command = Montage.create(Command, {
@@ -248,9 +195,7 @@ exports.ElementMediator = Montage.create(NJComponent, {
248 195
249 _setAttribute: { 196 _setAttribute: {