diff options
author | Ananya Sen | 2012-06-21 11:02:55 -0700 |
---|---|---|
committer | Ananya Sen | 2012-06-21 11:02:55 -0700 |
commit | 8db9e73ca68c006769af3997034959f6b7008add (patch) | |
tree | c99666706ec0678e523af8826d8297173116bda6 /js/controllers | |
parent | 8a47d6934fb2647ec26104935caf57a5cd73cdf2 (diff) | |
download | ninja-8db9e73ca68c006769af3997034959f6b7008add.tar.gz |
- added io api to generate file from binary
- refactoring and cleanup
Signed-off-by: Ananya Sen <Ananya.Sen@motorola.com>
Diffstat (limited to 'js/controllers')
-rw-r--r-- | js/controllers/clipboard-controller.js | 624 |
1 files changed, 19 insertions, 605 deletions
diff --git a/js/controllers/clipboard-controller.js b/js/controllers/clipboard-controller.js index afa9cbdb..259d916e 100644 --- a/js/controllers/clipboard-controller.js +++ b/js/controllers/clipboard-controller.js | |||
@@ -8,13 +8,10 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
8 | // | 8 | // |
9 | var Montage = require("montage/core/core").Montage, | 9 | var Montage = require("montage/core/core").Montage, |
10 | Component = require("montage/ui/component").Component, | 10 | Component = require("montage/ui/component").Component, |
11 | NJUtils = require("js/lib/NJUtils").NJUtils, | 11 | ElementsClipboardAgent = require("js/clipboard/internal-ops/elements-clipboard-agent").ElementsClipboardAgent, |
12 | World = require("js/lib/drawing/world").World; | 12 | ExternalAppsClipboardAgent= require("js/clipboard/external-apps-clipboard-agent").ExternalAppsClipboardAgent; |
13 | 13 | ||
14 | var ClipboardController = exports.ClipboardController = Montage.create(Component, { | 14 | var ClipboardController = exports.ClipboardController = Montage.create(Component, { |
15 | hasTemplate: { | ||
16 | value: false | ||
17 | }, | ||
18 | 15 | ||
19 | deserializedFromTemplate: { | 16 | deserializedFromTemplate: { |
20 | value: function() { | 17 | value: function() { |
@@ -30,36 +27,12 @@ var ClipboardController = exports.ClipboardController = Montage.create(Component | |||
30 | } | 27 | } |
31 | }, | 28 | }, |
32 | 29 | ||
33 | clipboardOperationsAgent:{//appropriate agent instant required for execution of cut/copy/paste | 30 | clipboardContext:{ |
34 | value: null | 31 | value : "stage" /* cleanup: formulate better context representation */ |
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 | }, | 32 | }, |
59 | 33 | ||
60 | newCopyFlag:{ | 34 | operationsAgent:{//appropriate agent instant required for execution of cut/copy/paste |
61 | get:function(){return this._newCopyFlag;}, | 35 | value: null |
62 | set:function(value){this._newCopyFlag = value;} | ||
63 | }, | 36 | }, |
64 | 37 | ||
65 | handleExecuteCopy:{ | 38 | handleExecuteCopy:{ |
@@ -87,7 +60,9 @@ var ClipboardController = exports.ClipboardController = Montage.create(Component | |||
87 | return; | 60 | return; |
88 | } | 61 | } |
89 | 62 | ||
90 | this.copy(clipboardEvent); | 63 | if(this.clipboardContext === "stage"){ |
64 | ElementsClipboardAgent.copy(clipboardEvent); | ||
65 | } | ||
91 | 66 | ||
92 | clipboardEvent.preventDefault(); | 67 | clipboardEvent.preventDefault(); |
93 | } | 68 | } |
@@ -102,7 +77,9 @@ var ClipboardController = exports.ClipboardController = Montage.create(Component | |||
102 | return; | 77 | return; |
103 | } | 78 | } |
104 | 79 | ||
105 | this.cut(clipboardEvent); | 80 | if(this.clipboardContext === "stage"){ |
81 | ElementsClipboardAgent.cut(clipboardEvent); | ||
82 | } | ||
106 | 83 | ||
107 | clipboardEvent.preventDefault(); | 84 | clipboardEvent.preventDefault(); |
108 | } | 85 | } |
@@ -111,11 +88,7 @@ var ClipboardController = exports.ClipboardController = Montage.create(Component | |||
111 | handlePaste:{ | 88 | handlePaste:{ |
112 | value:function(clipboardEvent){ | 89 | value:function(clipboardEvent){ |
113 | var clipboardData = clipboardEvent.clipboardData, | 90 | var clipboardData = clipboardEvent.clipboardData, |
114 | ninjaData = clipboardData.getData("ninja"), | 91 | ninjaData = clipboardData.getData("ninja"); |
115 | htmlData = clipboardData.getData("text/html"), | ||
116 | textData = clipboardData.getData("text/plain"), | ||
117 | i=0, | ||
118 | imageMime, imageData, imageElement; | ||
119 | 92 | ||
120 | if(!this.application.ninja.currentDocument | 93 | if(!this.application.ninja.currentDocument |
121 | || (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){ | 94 | || (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){ |
@@ -130,577 +103,18 @@ var ClipboardController = exports.ClipboardController = Montage.create(Component | |||
130 | 103 | ||
131 | //TODO: return if stage is not focussed | 104 | //TODO: return if stage is not focussed |
132 | 105 | ||
133 | this.pasteCounter++; | 106 | if(this.clipboardContext === "stage"){ |
134 | 107 | if(ninjaData){ | |
135 | if(ninjaData){ | 108 | ElementsClipboardAgent.pasteInternal(); |
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 | } | 109 | } |
170 | 110 | else{ | |
171 | try{ | 111 | ExternalAppsClipboardAgent.paste(clipboardEvent); |
172 | this.pasteFromExternalSource(htmlData, textData); | ||
173 | }catch(e){ | ||
174 | console.log(""+e.stack); | ||
175 | } | 112 | } |
176 | } | 113 | } |
177 | 114 | ||
178 | clipboardEvent.preventDefault(); | 115 | clipboardEvent.preventDefault(); |
179 | } | 116 | } |
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; | ||
232 | |||
233 | if (copiedElement.tagName === "CANVAS"){ | ||
234 | //clone copied canvas | ||
235 | var canvas = this.cloneCanvas(copiedElement); | ||
236 | pastedElements.push(canvas); | ||
237 | } | ||