diff options
Diffstat (limited to 'js/controllers/clipboard-controller.js')
-rw-r--r-- | js/controllers/clipboard-controller.js | 706 |
1 files changed, 706 insertions, 0 deletions
diff --git a/js/controllers/clipboard-controller.js b/js/controllers/clipboard-controller.js new file mode 100644 index 00000000..afa9cbdb --- /dev/null +++ b/js/controllers/clipboard-controller.js | |||
@@ -0,0 +1,706 @@ | |||
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){ | ||
137 | try{ | ||
138 | this.pasteFromCopy(); | ||
139 | }catch(e){ | ||
140 | console.log(""+e.stack); | ||
141 | } | ||
142 | } | ||
143 | else if(this.copiedObjects.cut){ | ||
144 | try{ | ||
145 | this.pasteFromCut(); | ||
146 | }catch(e){ | ||
147 | console.log(""+e.stack); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | else{ | ||
152 | |||
153 | //handle image blobs | ||
154 | if(clipboardData.items && (clipboardData.items.length > 0)){ | ||
155 | for(i=0; i < clipboardData.items.length; i++ ){ | ||
156 | if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png" | ||
157 | imageMime = clipboardData.items[i].type; | ||
158 | imageData = clipboardData.items[i].getAsFile(); | ||
159 | try{ | ||
160 | imageElement = this.generateImageElement(imageData); | ||
161 | }catch(e){ | ||
162 | console.log(""+e.stack); | ||
163 | } | ||
164 | this.application.ninja.selectionController.selectElements(imageElement); | ||
165 | this.application.ninja.currentDocument.model.needsSave = true; | ||
166 | |||
167 | } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | try{ | ||
172 | this.pasteFromExternalSource(htmlData, textData); | ||
173 | }catch(e){ | ||
174 | console.log(""+e.stack); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | clipboardEvent.preventDefault(); | ||
179 | } | ||
180 | }, | ||
181 | |||
182 | /* | ||
183 | parameters: | ||
184 | */ | ||
185 | copy:{ | ||
186 | value: function(clipboardEvent){ | ||
187 | var j=0, htmlToClipboard = "", ninjaClipboardObj = {}, textToClipboard = ""; | ||
188 | this.copiedObjects = {}; this.pasteCounter = 0; | ||
189 | this.copiedObjects["copy"] = []; | ||
190 | |||
191 | if(clipboardEvent){ | ||
192 | for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage | ||
193 | this.copiedObjects.copy.push(this.application.ninja.selectedElements[j]); | ||
194 | |||
195 | if(this.application.ninja.selectedElements[j].tagName === "CANVAS"){ | ||
196 | if(!ninjaClipboardObj.canvas){ | ||
197 | ninjaClipboardObj.canvas = true; | ||
198 | } | ||
199 | }else{ | ||
200 | htmlToClipboard = htmlToClipboard + this.serializeHTMLElement(this.application.ninja.selectedElements[j]); | ||
201 | if(!ninjaClipboardObj.plainHtml){ | ||
202 | ninjaClipboardObj.plainHtml = true; | ||
203 | } | ||
204 | textToClipboard = textToClipboard + this.getText(this.application.ninja.selectedElements[j]) + " "; | ||
205 | } | ||
206 | |||
207 | } | ||
208 | //set clipboard data | ||
209 | clipboardEvent.clipboardData.setData('ninja', ''+ JSON.stringify(ninjaClipboardObj)); | ||
210 | clipboardEvent.clipboardData.setData('text/html', '<HTML><BODY>' + htmlToClipboard + '</BODY></HTML>'); | ||
211 | clipboardEvent.clipboardData.setData('text/plain', textToClipboard); | ||
212 | } | ||
213 | else{ | ||
214 | //TODO: custom copy/paste, ex: css, animation, materials | ||
215 | } | ||
216 | } | ||
217 | }, | ||
218 | |||
219 | pasteFromCopy:{//todo: change to appropriate name | ||
220 | value:function(){ | ||
221 | var i=0, j=0, | ||
222 | pastedElements = [],//array of te pastes clones - for selection | ||
223 | node = null, | ||
224 | styles = null, | ||
225 | copiedElement = null; | ||
226 | |||
227 | //TODO: cleanse HTML | ||
228 | |||
229 | for(j=0; j< this.copiedObjects.copy.length; j++){ | ||
230 | copiedElement = this.copiedObjects.copy[j]; | ||
231 | styles = null; | ||