diff options
Diffstat (limited to 'js/mediators/element-mediator.js')
-rw-r--r-- | js/mediators/element-mediator.js | 677 |
1 files changed, 677 insertions, 0 deletions
diff --git a/js/mediators/element-mediator.js b/js/mediators/element-mediator.js new file mode 100644 index 00000000..91b09475 --- /dev/null +++ b/js/mediators/element-mediator.js | |||
@@ -0,0 +1,677 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Montage = require("montage/core/core").Montage, | ||
8 | NJComponent = require("js/lib/nj-base").NJComponent; | ||
9 | |||
10 | var ElementController = require("js/controllers/elements/element-controller").ElementController, | ||
11 | Command = require("js/controllers/undo-controller").Command, | ||
12 | GroupCommand = require("js/controllers/undo-controller").GroupCommand, | ||
13 | NJUtils = require("js/lib/NJUtils").NJUtils; | ||
14 | |||
15 | exports.ElementMediator = Montage.create(NJComponent, { | ||
16 | |||
17 | deleteDelegate: { | ||
18 | value: null | ||
19 | }, | ||
20 | |||
21 | deserializedFromTemplate: { | ||
22 | value: function () { | ||
23 | this.eventManager.addEventListener("elementAdding", this, false); | ||
24 | this.eventManager.addEventListener("deleting", this, false); | ||
25 | } | ||
26 | }, | ||
27 | |||
28 | // TODO use the specific controller to be able to subclass the functionality | ||
29 | handleElementAdding: { | ||
30 | value: function(event) { | ||
31 | /* | ||
32 | var cmd = ElementControllerCommands.addElementCommand(event.detail.el, event.detail.data); | ||
33 | NJevent("sendToUndo", cmd); | ||
34 | cmd.execute(); | ||
35 | */ | ||
36 | this.addElement(event.detail.el, event.detail.data); | ||
37 | |||
38 | } | ||
39 | }, | ||
40 | |||
41 | handleDeleting: { | ||
42 | value: function(event) { | ||
43 | if(this.deleteDelegate && (typeof this.deleteDelegate.handleDelete === 'function')) { | ||
44 | this.deleteDelegate.handleDelete(); | ||
45 | } else { | ||
46 | // Add the Undo/Redo | ||
47 | var els = []; | ||
48 | |||
49 | if(this.application.ninja.selectedElements.length > 0) { | ||
50 | for(var i=0, item; item = this.application.ninja.selectedElements[i]; i++) { | ||
51 | ElementController.removeElement(item._element); | ||
52 | els.push(item._element); | ||
53 | } | ||
54 | |||
55 | NJevent( "deleteSelection", els ); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | }, | ||
60 | |||
61 | addElement: { | ||
62 | value: function(el, rules, noEvent) { | ||
63 | var command = Montage.create(Command, { | ||
64 | _el: { value: el }, | ||
65 | _rules: { value: rules }, | ||
66 | _noEvent: { value: noEvent }, | ||
67 | |||
68 | description: { value: "Adding Element"}, | ||
69 | |||
70 | receiver: { value: this}, | ||
71 | |||
72 | execute: { | ||
73 | value: function() { | ||
74 | this.receiver._addElement(this._el, this._rules, this._noEvent); | ||
75 | return this._el; | ||
76 | } | ||
77 | }, | ||
78 | |||
79 | unexecute: { | ||
80 | value: function() { | ||
81 | this.receiver._removeElement(this._el, this._rules, this._noEvent); | ||
82 | return this._el; | ||
83 | } | ||
84 | } | ||
85 | }); | ||
86 | |||
87 | NJevent("sendToUndo", command); | ||
88 | command.execute(); | ||
89 | } | ||
90 | }, | ||
91 | |||
92 | _addElement: { | ||
93 | value: function(el, rules, noEvent) { | ||
94 | ElementController.addElement(el, rules); | ||
95 | if(!noEvent) NJevent("elementAdded", el); | ||
96 | } | ||
97 | }, | ||
98 | |||
99 | _removeElement: { | ||
100 | value: function(el, rules) { | ||
101 | ElementController.removeElement(el, rules); | ||
102 | NJevent("elementDeleted", el); | ||
103 | } | ||
104 | }, | ||
105 | |||
106 | getNJProperty: { | ||
107 | value: function(el, p) { | ||
108 | if(el.elementModel) { | ||
109 | if(el.elementModel.hasOwnProperty(p)) { | ||
110 | return el.elementModel[p]; | ||
111 | } else { | ||
112 | console.log("Element Model does not have ", p); | ||
113 | } | ||
114 | } else { | ||
115 | console.log("Element has no Model -- Create one"); | ||
116 | } | ||
117 | } | ||
118 | }, | ||
119 | |||
120 | getProperty: { | ||
121 | value: function(el, prop, valueMutator) { | ||
122 | if(!el.elementModel) { | ||
123 | console.log("Element has no Model -> One should have been created"); | ||
124 | NJUtils.makeElementModel(el, "Div", "block"); | ||
125 | } | ||
126 | |||
127 | if(valueMutator && typeof valueMutator === "function") { | ||
128 | return valueMutator(el.elementModel.controller["getProperty"](el, prop)); | ||
129 | } else { | ||
130 | return el.elementModel.controller["getProperty"](el, prop, valueMutator); | ||
131 | } | ||
132 | } | ||
133 | }, | ||
134 | |||
135 | getShapeProperty: { | ||
136 | value: function(el, prop) { | ||
137 | if(!el.elementModel) { | ||
138 | console.log("Element has no Model -> One should have been created"); | ||
139 | NJUtils.makeElementModel(el, "Canvas", "block", true); | ||
140 | } | ||
141 | |||
142 | return el.elementModel.controller["getShapeProperty"](el, prop); | ||
143 | } | ||
144 | }, | ||
145 | |||
146 | setShapeProperty: { | ||
147 | value: function(el, prop, value) { | ||
148 | if(!el.elementModel) { | ||
149 | console.log("Element has no Model -> One should have been created"); | ||
150 | NJUtils.makeElementModel(el, "Canvas", "block", true); | ||
151 | } | ||
152 | |||
153 | return el.elementModel.controller["setShapeProperty"](el, prop, value); | ||
154 | } | ||
155 | }, | ||
156 | |||
157 | /** | ||
158 | Set a property change command for an element or array of elements | ||
159 | @param els: Array of elements. Can contain 1 or more elements | ||
160 | @param p: Property to set | ||
161 | @param value: Value to be set. This is an array of values corresponding to the array of elements | ||
162 | @param eventType: Change/Changing. Will be passed to the dispatched event | ||
163 | @param source: String for the source object making the call | ||
164 | @param currentValue *OPTIONAL*: current value array. If not found the current value is calculated | ||
165 | @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline | ||
166 | */ | ||
167 | setAttribute: { | ||
168 | value: function(el, att, value, eventType, source, currentValue) { | ||
169 | |||
170 | if(eventType === "Changing") { | ||
171 | this._setAttribute(el, att, value, eventType, source); | ||
172 | } else { | ||
173 | // Calculate currentValue if not found for each element | ||
174 | if(currentValue === null) { | ||
175 | console.log("Here"); | ||
176 | var item = el._element || el; | ||
177 | currentValue = item.getAttribute(att); | ||
178 | } | ||
179 | |||
180 | var command = Montage.create(Command, { | ||
181 | _el: { value: el }, | ||
182 | _att: { value: att }, | ||
183 | _value: { value: value }, | ||
184 | _previous: { value: currentValue }, | ||
185 | _eventType: { value: eventType}, | ||
186 | _source: { value: "undo-redo"}, | ||
187 | description: { value: "Set Attribute"}, | ||
188 | receiver: { value: this}, | ||
189 | |||
190 | execute: { | ||
191 | value: function(senderObject) { | ||
192 | if(senderObject) this._source = senderObject; | ||
193 | this.receiver._setAttribute(this._el, this._att, this._value, this._eventType, this._source); | ||
194 | this._source = "undo-redo"; | ||
195 | return ""; | ||
196 | } | ||
197 | }, | ||
198 | |||
199 | unexecute: { | ||
200 | value: function() { | ||
201 | this.receiver._setAttribute(this._el, this._att, this._previous, this._eventType, this._source); | ||
202 | return ""; | ||
203 | } | ||
204 | } | ||
205 | }); | ||
206 | |||
207 | NJevent("sendToUndo", command); | ||
208 | command.execute(source); | ||
209 | } | ||
210 | |||
211 | } | ||
212 | }, | ||
213 | |||
214 | _setAttribute: { | ||
215 | value: function(el, att, value, eventType, source) { | ||
216 | var item = el._element || el; | ||
217 | |||
218 | item.elementModel.controller["setAttribute"](item, att, value); | ||
219 | |||
220 | NJevent("attribute" + eventType, {type : "setAttribute", source: source, data: {"els": el, "prop": att, "value": value}, redraw: null}); | ||
221 | } | ||
222 | }, | ||
223 | |||
224 | |||
225 | |||
226 | /** | ||
227 | Set a property change command for an element or array of elements | ||
228 | @param els: Array of elements. Can contain 1 or more elements | ||
229 | @param p: Property to set | ||
230 | @param value: Value to be set. This is an array of values corresponding to the array of elements | ||
231 | @param eventType: Change/Changing. Will be passed to the dispatched event | ||
232 | @param source: String for the source object making the call | ||
233 | @param currentValue *OPTIONAL*: current value array. If not found the current value is calculated | ||
234 | @param stageRedraw: *OPTIONAL*: True. If set to false the stage will not redraw the selection/outline | ||