diff options
Diffstat (limited to 'js/controllers')
-rw-r--r-- | js/controllers/clipboard-controller.js | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/js/controllers/clipboard-controller.js b/js/controllers/clipboard-controller.js new file mode 100644 index 00000000..3f15ac1d --- /dev/null +++ b/js/controllers/clipboard-controller.js | |||
@@ -0,0 +1,419 @@ | |||
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 | copiedObjects:{ | ||
38 | value: [] | ||
39 | }, | ||
40 | |||
41 | _copyFlag:{ | ||
42 | value:false | ||
43 | }, | ||
44 | |||
45 | copyFlag:{ | ||
46 | get:function(){return this._copyFlag;}, | ||
47 | set:function(value){this._copyFlag = value;} | ||
48 | }, | ||
49 | |||
50 | _newCopyFlag:{ | ||
51 | value:true | ||
52 | }, | ||
53 | |||
54 | newCopyFlag:{ | ||
55 | get:function(){return this._newCopyFlag;}, | ||
56 | set:function(value){this._newCopyFlag = value;} | ||
57 | }, | ||
58 | |||
59 | handleExecuteCopy:{ | ||
60 | value: function(){document.execCommand('copy',false,null);} | ||
61 | }, | ||
62 | |||
63 | handleExecuteCut:{ | ||
64 | value: function(){document.execCommand('cut',false,null);} | ||
65 | }, | ||
66 | |||
67 | handleExecutePaste:{ | ||
68 | value: function(){document.execCommand('paste',false,null);} | ||
69 | }, | ||
70 | |||
71 | handleCopy:{ | ||
72 | value:function(clipboardEvent, test){ | ||
73 | if(this.application.ninja.documentController.activeDocument.currentView === "code") return; | ||
74 | |||
75 | this.copy(clipboardEvent); | ||
76 | |||
77 | clipboardEvent.preventDefault(); | ||
78 | } | ||
79 | }, | ||
80 | |||
81 | handlePaste:{ | ||
82 | value:function(clipboardEvent){ | ||
83 | if(this.application.ninja.documentController.activeDocument.currentView === "code") return;//for design view only | ||
84 | |||
85 | //TODO: return if stage is not focussed | ||
86 | |||
87 | //identify all types of clipboard data | ||
88 | |||
89 | var clipboardData = clipboardEvent.clipboardData, | ||
90 | ninjaData = clipboardData.getData("ninja"), | ||
91 | htmlData = clipboardData.getData("text/html"), | ||
92 | textData = clipboardData.getData("text/plain"), | ||
93 | imageData = clipboardData.getData("image/png"),//TODO: other img types, tiff, jpeg..... | ||
94 | svgData = clipboardData.getData("image/svg"); | ||
95 | |||
96 | if(ninjaData){ | ||
97 | this.pasteFromNinja(); | ||
98 | }else if(imageData){ | ||
99 | this.pasteImage(imageData); | ||
100 | }else{ | ||
101 | this.pasteItems(htmlData, textData); | ||
102 | } | ||
103 | |||
104 | |||
105 | clipboardEvent.preventDefault(); | ||
106 | } | ||
107 | }, | ||
108 | |||
109 | /* | ||
110 | parameters: | ||
111 | */ | ||
112 | copy:{ | ||
113 | value: function(clipboardEvent){ | ||
114 | var j=0, htmlToClipboard = "", ninjaClipboardObj = {}, textToClipboard = ""; | ||
115 | this.copiedObjects.length = 0; | ||
116 | |||
117 | if(clipboardEvent){ | ||
118 | for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage | ||
119 | this.copiedObjects.push(this.application.ninja.selectedElements[j]); | ||
120 | |||
121 | if(this.application.ninja.selectedElements[j].tagName === "CANVAS"){ | ||
122 | if(!ninjaClipboardObj.canvas){ | ||
123 | ninjaClipboardObj.canvas = true; | ||
124 | } | ||
125 | }else{ | ||
126 | htmlToClipboard = htmlToClipboard + this.serialize(this.application.ninja.selectedElements[j]); | ||
127 | if(!ninjaClipboardObj.plainHtml){ | ||
128 | ninjaClipboardObj.plainHtml = true; | ||
129 | } | ||
130 | textToClipboard = textToClipboard + this.getText(this.application.ninja.selectedElements[j]) + " "; | ||
131 | } | ||
132 | |||
133 | } | ||
134 | //set clipboard data | ||
135 | clipboardEvent.clipboardData.setData('ninja', ''+ JSON.stringify(ninjaClipboardObj)); | ||
136 | clipboardEvent.clipboardData.setData('text/html', '<HTML><BODY>' + htmlToClipboard + '</BODY></HTML>'); | ||
137 | clipboardEvent.clipboardData.setData('text/plain', textToClipboard); | ||
138 | } | ||
139 | else{ | ||
140 | //TODO: custom copy/paste, ex: css, animation, materials | ||
141 | } | ||
142 | } | ||
143 | }, | ||
144 | |||
145 | pasteFromNinja:{//todo: change to appropriate name | ||
146 | value:function(){ | ||
147 | var i=0, j=0, | ||
148 | pastedElements = [],//array of te pastes clones - for selection | ||
149 | node = null, | ||
150 | styles = null, | ||
151 | copiedElement = null; | ||
152 | |||
153 | //TODO: cleanse HTML | ||
154 | |||
155 | //clear previous selections | ||
156 | this.application.ninja.selectedElements.length = 0; | ||
157 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); | ||
158 | |||
159 | |||
160 | for(j=0; j< this.copiedObjects.length; j++){ | ||
161 | copiedElement = this.copiedObjects[j]; | ||
162 | styles = null; | ||
163 | |||
164 | if (copiedElement.tagName === "CANVAS"){ | ||
165 | //clone copied canvas | ||
166 | var canvas = document.application.njUtils.make("canvas", copiedElement.className, this.application.ninja.currentDocument); | ||
167 | canvas.width = copiedElement.width; | ||
168 | canvas.height = copiedElement.height; | ||
169 | //end - clone copied canvas | ||
170 | |||
171 | if (!canvas.getAttribute( "data-RDGE-id" )) canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() ); | ||
172 | document.application.njUtils.createModelWithShape(canvas); | ||
173 | |||
174 | styles = canvas.elementModel.data || {}; | ||
175 | styles.top = "" + (this.application.ninja.elementMediator.getProperty(copiedElement, "top", parseInt) - 50) + "px"; | ||
176 | styles.left = "" + (this.application.ninja.elementMediator.getProperty(copiedElement, "left", parseInt) - 50) + "px"; | ||
177 | |||
178 | this.application.ninja.elementMediator.addElements(canvas, styles, false); | ||
179 | |||
180 | var world, worldData = copiedElement.elementModel.shapeModel.GLWorld.exportJSON(); | ||
181 | if(worldData) | ||
182 | { | ||
183 | var jObj; | ||
184 | var index = worldData.indexOf( ';' ); | ||
185 | if ((worldData[0] === 'v') && (index < 24)) | ||
186 | { | ||
187 | // JSON format. separate the version info from the JSON info | ||
188 | var jStr = worldData.substr( index+1 ); | ||
189 | jObj = JSON.parse( jStr ); | ||
190 | |||
191 | world = new World(canvas, jObj.webGL); | ||
192 | canvas.elementModel.shapeModel.GLWorld = world; | ||
193 | canvas.elementModel.shapeModel.useWebGl = jObj.webGL; | ||
194 | world.importJSON(jObj); | ||
195 | this.application.ninja.currentDocument.buildShapeModel( canvas.elementModel, world ); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | NJevent("elementAdded", canvas); | ||
200 | |||
201 | pastedElements.push(canvas); | ||
202 | } | ||
203 | else { | ||
204 | node = copiedElement.cloneNode(true); | ||
205 | |||
206 | if(node.removeAttribute) {node.removeAttribute("style");}//remove the computed styles attribute which is placed only for pasting to external applications | ||
207 | |||
208 | styles = {}; | ||
209 | styles.top = "" + (this.application.ninja.elementMediator.getProperty(copiedElement, "top", parseInt) - 50) + "px"; | ||
210 | styles.left = "" + (this.application.ninja.elementMediator.getProperty(copiedElement, "left", parseInt) - 50) + "px"; | ||
211 | |||
212 | this.pastePositioned(node, styles); | ||
213 | pastedElements.push(node); | ||
214 | } | ||
215 | |||
216 | } | ||
217 | |||
218 | |||
219 | this.application.ninja.selectionController.selectElements(pastedElements);//select pasted elements - not working! | ||
220 | |||
221 | |||
222 | this.application.ninja.documentController.activeDocument.needsSave = true; | ||
223 | } | ||
224 | }, | ||
225 | |||
226 | //paste from external applicaitons | ||
227 | pasteItems:{//todo: change to pasteNinja, pasteHTML, etc | ||
228 | value: function(htmlData, textData){ | ||