diff options
Diffstat (limited to 'js/controllers')
-rw-r--r-- | js/controllers/clipboard-controller.js | 571 |
1 files changed, 571 insertions, 0 deletions
diff --git a/js/controllers/clipboard-controller.js b/js/controllers/clipboard-controller.js new file mode 100644 index 00000000..e7b772d4 --- /dev/null +++ b/js/controllers/clipboard-controller.js | |||
@@ -0,0 +1,571 @@ | |||
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.currentView === "code") return; | ||
80 | |||
81 | this.copy(clipboardEvent); | ||
82 | |||
83 | clipboardEvent.preventDefault(); | ||
84 | } | ||
85 | }, | ||
86 | |||
87 | handleCut:{ | ||
88 | value:function(clipboardEvent){ | ||
89 | if(this.application.ninja.currentDocument.currentView === "code") return; | ||
90 | |||
91 | this.cut(clipboardEvent); | ||
92 | |||
93 | clipboardEvent.preventDefault(); | ||
94 | } | ||
95 | }, | ||
96 | |||
97 | handlePaste:{ | ||
98 | value:function(clipboardEvent){ | ||
99 | if(this.application.ninja.currentDocument.currentView === "code") return;//for design view only | ||
100 | |||
101 | //TODO: return if stage is not focussed | ||
102 | |||
103 | //identify all types of clipboard data | ||
104 | |||
105 | var clipboardData = clipboardEvent.clipboardData, | ||
106 | ninjaData = clipboardData.getData("ninja"), | ||
107 | htmlData = clipboardData.getData("text/html"), | ||
108 | textData = clipboardData.getData("text/plain"), | ||
109 | imageData = clipboardData.getData("image/png"),//TODO: other img types, tiff, jpeg..... | ||
110 | svgData = clipboardData.getData("image/svg"); | ||
111 | |||
112 | this.pasteCounter++; | ||
113 | |||
114 | if(ninjaData){ | ||
115 | if(this.copiedObjects.copy){this.pasteFromCopy();} | ||
116 | else if(this.copiedObjects.cut){this.pasteFromCut();} | ||
117 | }else{ | ||
118 | try{ | ||
119 | this.pasteFromExternalSource(htmlData, textData); | ||
120 | }catch(e){ | ||
121 | console.log(""+e.stack); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | clipboardEvent.preventDefault(); | ||
126 | } | ||
127 | }, | ||
128 | |||
129 | /* | ||
130 | parameters: | ||
131 | */ | ||
132 | copy:{ | ||
133 | value: function(clipboardEvent){ | ||
134 | var j=0, htmlToClipboard = "", ninjaClipboardObj = {}, textToClipboard = ""; | ||
135 | this.copiedObjects = {}; this.pasteCounter = 0; | ||
136 | this.copiedObjects["copy"] = []; | ||
137 | |||
138 | if(clipboardEvent){ | ||
139 | for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage | ||
140 | this.copiedObjects.copy.push(this.application.ninja.selectedElements[j]); | ||
141 | |||
142 | if(this.application.ninja.selectedElements[j].tagName === "CANVAS"){ | ||
143 | if(!ninjaClipboardObj.canvas){ | ||
144 | ninjaClipboardObj.canvas = true; | ||
145 | } | ||
146 | }else{ | ||
147 | htmlToClipboard = htmlToClipboard + this.serializeHTMLElement(this.application.ninja.selectedElements[j]); | ||
148 | if(!ninjaClipboardObj.plainHtml){ | ||
149 | ninjaClipboardObj.plainHtml = true; | ||
150 | } | ||
151 | textToClipboard = textToClipboard + this.getText(this.application.ninja.selectedElements[j]) + " "; | ||
152 | } | ||
153 | |||
154 | } | ||
155 | //set clipboard data | ||
156 | clipboardEvent.clipboardData.setData('ninja', ''+ JSON.stringify(ninjaClipboardObj)); | ||
157 | clipboardEvent.clipboardData.setData('text/html', '<HTML><BODY>' + htmlToClipboard + '</BODY></HTML>'); | ||
158 | clipboardEvent.clipboardData.setData('text/plain', textToClipboard); | ||
159 | } | ||
160 | else{ | ||
161 | //TODO: custom copy/paste, ex: css, animation, materials | ||
162 | } | ||
163 | } | ||
164 | }, | ||
165 | |||
166 | pasteFromCopy:{//todo: change to appropriate name | ||
167 | value:function(){ | ||
168 | var i=0, j=0, | ||
169 | pastedElements = [],//array of te pastes clones - for selection | ||
170 | node = null, | ||
171 | styles = null, | ||
172 | copiedElement = null; | ||
173 | |||
174 | //TODO: cleanse HTML | ||
175 | |||
176 | //clear previous selections | ||
177 | this.application.ninja.selectedElements.length = 0; | ||
178 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); | ||
179 | |||
180 | |||
181 | for(j=0; j< this.copiedObjects.copy.length; j++){ | ||
182 | copiedElement = this.copiedObjects.copy[j]; | ||
183 | styles = null; | ||
184 | |||
185 | if (copiedElement.tagName === "CANVAS"){ | ||
186 | //clone copied canvas | ||
187 | var canvas = this.cloneCanvas(copiedElement); | ||
188 | NJevent("elementAdded", canvas); | ||
189 | pastedElements.push(canvas); | ||
190 | } | ||
191 | else { | ||
192 | node = copiedElement.cloneNode(true); | ||
193 | |||
194 | if(node.removeAttribute) {node.removeAttribute("style");}//remove the computed styles attribute which is placed only for pasting to external applications | ||
195 | |||
196 | styles = {}; | ||
197 | styles.top = this.application.ninja.elementMediator.getProperty(copiedElement, "top", parseInt); | ||
198 | styles.left = this.application.ninja.elementMediator.getProperty(copiedElement, "left", parseInt); | ||
199 | |||
200 | this.pastePositioned(node, styles); | ||
201 | pastedElements.push(node); | ||
202 | } | ||
203 | |||
204 | } | ||
205 | |||
206 | this.application.ninja.selectionController.selectElements(pastedElements); | ||
207 | |||
208 | this.application.ninja.currentDocument.model.needsSave = true; | ||
209 | } | ||
210 | }, | ||
211 | |||
212 | pasteFromCut:{ | ||
213 | value:function(){ | ||
214 | var i=0, j=0, | ||
215 | clipboardHelper=this.createClipboardHelper(), | ||
216 | node = null, canvas = null, | ||
217 | styles=null, | ||
218 | pastedElements = [];//array of te pastes clones - for selection | ||
219 | |||
220 | for(j=0; j< this.copiedObjects.cut.length; j++){ | ||
221 | clipboardHelper.innerHTML = this.copiedObjects.cut[j].outerhtml; | ||
222 | |||
223 | if (clipboardHelper.lastChild.tagName === "CANVAS"){ | ||
224 | //paste canvas | ||
225 | canvas = this.generateNewCanvas(this.copiedObjects.cut[j].outerhtml, this.copiedObjects.cut[j].styles, this.copiedObjects.cut[j].className, this.copiedObjects.cut[j].worldJson); | ||
226 | NJevent("elementAdded", canvas); | ||
227 | pastedElements.push(canvas); | ||
228 | } | ||
229 | else if((clipboardHelper.lastChild.nodeType === 3) || (clipboardHelper.lastChild.tagName === "A")){//TextNode | ||
230 | |||
231 | |||