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