diff options
Diffstat (limited to 'js/controllers')
-rw-r--r-- | js/controllers/clipboard-controller.js | 698 | ||||
-rwxr-xr-x | js/controllers/styles-controller.js | 66 |
2 files changed, 751 insertions, 13 deletions
diff --git a/js/controllers/clipboard-controller.js b/js/controllers/clipboard-controller.js new file mode 100644 index 00000000..bc78922c --- /dev/null +++ b/js/controllers/clipboard-controller.js | |||
@@ -0,0 +1,698 @@ | |||
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 | //////////////////////////////////////////////////////////////////////// | ||
8 | // | ||
9 | var Montage = require("montage/core/core").Montage, | ||
10 | Component = require("montage/ui/component").Component, | ||
11 | NJUtils = require("js/lib/NJUtils").NJUtils, | ||
12 | World = require("js/lib/drawing/world").World; | ||
13 | |||
14 | var ClipboardController = exports.ClipboardController = Montage.create(Component, { | ||
15 | hasTemplate: { | ||
16 | value: false | ||
17 | }, | ||
18 | |||
19 | deserializedFromTemplate: { | ||
20 | value: function() { | ||
21 | document.body.addEventListener("copy", this, false); | ||
22 | document.body.addEventListener("cut", this, false); | ||
23 | document.body.addEventListener("paste", this, false); | ||
24 | |||
25 | //ninja menu events | ||
26 | this.eventManager.addEventListener("executeCut", this, false); | ||
27 | this.eventManager.addEventListener("executeCopy", this, false); | ||
28 | this.eventManager.addEventListener("executePaste", this, false); | ||
29 | |||
30 | } | ||
31 | }, | ||
32 | |||
33 | clipboardOperationsAgent:{//appropriate agent instant required for execution of cut/copy/paste | ||
34 | value: null | ||
35 | }, | ||
36 | |||
37 | //count how many times pasted | ||
38 | //used to move multiple pastes of same copy | ||
39 | pasteCounter:{ | ||
40 | value: 0 | ||
41 | }, | ||
42 | |||
43 | copiedObjects:{ | ||
44 | value: {} | ||
45 | }, | ||
46 | |||
47 | _copyFlag:{ | ||
48 | value:false | ||
49 | }, | ||
50 | |||
51 | copyFlag:{ | ||
52 | get:function(){return this._copyFlag;}, | ||
53 | set:function(value){this._copyFlag = value;} | ||
54 | }, | ||
55 | |||
56 | _newCopyFlag:{ | ||
57 | value:true | ||
58 | }, | ||
59 | |||
60 | newCopyFlag:{ | ||
61 | get:function(){return this._newCopyFlag;}, | ||
62 | set:function(value){this._newCopyFlag = value;} | ||
63 | }, | ||
64 | |||
65 | handleExecuteCopy:{ | ||
66 | value: function(){document.execCommand('copy',false,null);} | ||
67 | }, | ||
68 | |||
69 | handleExecuteCut:{ | ||
70 | value: function(){document.execCommand('cut',false,null);} | ||
71 | }, | ||
72 | |||
73 | handleExecutePaste:{ | ||
74 | value: function(){document.execCommand('paste',false,null);} | ||
75 | }, | ||
76 | |||
77 | handleCopy:{ | ||
78 | value:function(clipboardEvent){ | ||
79 | if(!this.application.ninja.currentDocument | ||
80 | || (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){ | ||
81 | |||
82 | return; | ||
83 | }//for design view only | ||
84 | |||
85 | // Don't do anything if an input or other control is focused | ||
86 | if(document.activeElement.nodeName !== "BODY") { | ||
87 | return; | ||
88 | } | ||
89 | |||
90 | this.copy(clipboardEvent); | ||
91 | |||
92 | clipboardEvent.preventDefault(); | ||
93 | } | ||
94 | }, | ||
95 | |||
96 | handleCut:{ | ||
97 | value:function(clipboardEvent){ | ||
98 | if(this.application.ninja.currentDocument.currentView === "code") return; | ||
99 | |||
100 | // Don't do anything if an input or other control is focused | ||
101 | if(document.activeElement.nodeName !== "BODY") { | ||
102 | return; | ||
103 | } | ||
104 | |||
105 | this.cut(clipboardEvent); | ||
106 | |||
107 | clipboardEvent.preventDefault(); | ||
108 | } | ||
109 | }, | ||
110 | |||
111 | handlePaste:{ | ||
112 | value:function(clipboardEvent){ | ||
113 | var clipboardData = clipboardEvent.clipboardData, | ||
114 | ninjaData = clipboardData.getData("ninja"), | ||
115 | htmlData = clipboardData.getData("text/html"), | ||
116 | textData = clipboardData.getData("text/plain"), | ||
117 | i=0, | ||
118 | imageMime, imageData, imageElement; | ||
119 | |||
120 | if(!this.application.ninja.currentDocument | ||
121 | || (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){ | ||
122 | |||
123 | return; | ||
124 | }//for design view only | ||
125 | |||
126 | // Don't do anything if an input or other control is focused | ||
127 | if(document.activeElement.nodeName !== "BODY") { | ||
128 | return; | ||
129 | } | ||
130 | |||
131 | //TODO: return if stage is not focussed | ||
132 | |||
133 | this.pasteCounter++; | ||
134 | |||
135 | if(ninjaData){ | ||
136 | if(this.copiedObjects.copy){this.pasteFromCopy();} | ||
137 | else if(this.copiedObjects.cut){this.pasteFromCut();} | ||
138 | } | ||
139 | else{ | ||
140 | |||
141 | //handle image blobs | ||
142 | if(clipboardData.items && (clipboardData.items.length > 0)){ | ||
143 | for(i=0; i < clipboardData.items.length; i++ ){ | ||
144 | if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png" | ||
145 | imageMime = clipboardData.items[i].type; | ||
146 | imageData = clipboardData.items[i].getAsFile(); | ||
147 | try{ | ||
148 | imageElement = this.generateImageElement(imageData); | ||
149 | }catch(e){ | ||
150 | console.log(""+e.stack); | ||
151 | } | ||
152 | this.application.ninja.selectionController.selectElements(imageElement); | ||
153 | this.application.ninja.currentDocument.model.needsSave = true; | ||
154 | |||
155 | } | ||
156 | } | ||
157 | } | ||
158 | |||
159 | try{ | ||
160 | this.pasteFromExternalSource(htmlData, textData); | ||
161 | }catch(e){ | ||
162 | console.log(""+e.stack); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | clipboardEvent.preventDefault(); | ||
167 | } | ||
168 | }, | ||
169 | |||
170 | /* | ||
171 | parameters: | ||
172 | */ | ||
173 | copy:{ | ||
174 | value: function(clipboardEvent){ | ||
175 | var j=0, htmlToClipboard = "", ninjaClipboardObj = {}, textToClipboard = ""; | ||
176 | this.copiedObjects = {}; this.pasteCounter = 0; | ||
177 | this.copiedObjects["copy"] = []; | ||
178 | |||
179 | if(clipboardEvent){ | ||
180 | for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage | ||
181 | this.copiedObjects.copy.push(this.application.ninja.selectedElements[j]); | ||
182 | |||
183 | if(this.application.ninja.selectedElements[j].tagName === "CANVAS"){ | ||
184 | if(!ninjaClipboardObj.canvas){ | ||
185 | ninjaClipboardObj.canvas = true; | ||
186 | } | ||
187 | }else{ | ||
188 | htmlToClipboard = htmlToClipboard + this.serializeHTMLElement(this.application.ninja.selectedElements[j]); | ||
189 | if(!ninjaClipboardObj.plainHtml){ | ||
190 | ninjaClipboardObj.plainHtml = true; | ||
191 | } | ||
192 | textToClipboard = textToClipboard + this.getText(this.application.ninja.selectedElements[j]) + " "; | ||
193 | } | ||
194 | |||
195 | } | ||
196 | //set clipboard data | ||
197 | clipboardEvent.clipboardData.setData('ninja', ''+ JSON.stringify(ninjaClipboardObj)); | ||
198 | clipboardEvent.clipboardData.setData('text/html', '<HTML><BODY>' + htmlToClipboard + '</BODY></HTML>'); | ||
199 | clipboardEvent.clipboardData.setData('text/plain', textToClipboard); | ||
200 | } | ||
201 | else{ | ||
202 | //TODO: custom copy/paste, ex: css, animation, materials | ||
203 | } | ||
204 | } | ||
205 | }, | ||
206 | |||
207 | pasteFromCopy:{//todo: change to appropriate name | ||
208 | value:function(){ | ||
209 | var i=0, j=0, | ||
210 | pastedElements = [],//array of te pastes clones - for selection | ||
211 | node = null, | ||
212 | styles = null, | ||
213 | copiedElement = null; | ||
214 | |||
215 | //TODO: cleanse HTML | ||
216 | |||
217 | for(j=0; j< this.copiedObjects.copy.length; j++){ | ||
218 | copiedElement = this.copiedObjects.copy[j]; | ||
219 | styles = null; | ||
220 | |||
221 | if (copiedElement.tagName === "CANVAS"){ | ||
222 | //clone copied canvas | ||
223 | var canvas = this.cloneCanvas(copiedElement); | ||
224 | NJevent("elementAdded", canvas); | ||
225 | pastedElements.push(canvas); | ||
226 | } | ||
227 | else { | ||
228 | node = copiedElement.cloneNode(true); | ||
229 | |||