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