aboutsummaryrefslogtreecommitdiff
path: root/js/clipboard/external-apps-clipboard-agent.js
diff options
context:
space:
mode:
authorKruti Shah2012-06-22 10:00:53 -0700
committerKruti Shah2012-06-22 10:00:53 -0700
commit4bf8e4cdc179c3b388fc06f26008808aa4b77eb0 (patch)
tree743ea0d3a3af9cae65d5af531a6dba1951b03b33 /js/clipboard/external-apps-clipboard-agent.js
parentcb9fbfeba90b5b76d3f3f2b9303129ca4651e5be (diff)
parent020ad879a627c4234a8732f05e175ad69cedf180 (diff)
downloadninja-4bf8e4cdc179c3b388fc06f26008808aa4b77eb0.tar.gz
Merge branch 'refs/heads/TimelineUberJD' into Timeline-local-kruti
Diffstat (limited to 'js/clipboard/external-apps-clipboard-agent.js')
-rw-r--r--js/clipboard/external-apps-clipboard-agent.js220
1 files changed, 220 insertions, 0 deletions
diff --git a/js/clipboard/external-apps-clipboard-agent.js b/js/clipboard/external-apps-clipboard-agent.js
new file mode 100644
index 00000000..7e55cf08
--- /dev/null
+++ b/js/clipboard/external-apps-clipboard-agent.js
@@ -0,0 +1,220 @@
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//
9
10var Montage = require("montage/core/core").Montage,
11 Component = require("montage/ui/component").Component,
12 ClipboardUtil = require("js/clipboard/util").ClipboardUtil;
13
14var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.create(Component, {
15
16 paste:{
17 value: function(clipboardEvent){
18 var clipboardData = clipboardEvent.clipboardData,
19 htmlData = clipboardData.getData("text/html"),
20 textData = clipboardData.getData("text/plain"),
21 i=0,
22 imageMime, imageData, imageElement;
23
24 //handle image blobs
25 if(clipboardData.items && (clipboardData.items.length > 0)){
26 for(i=0; i < clipboardData.items.length; i++ ){
27 if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png"
28 imageMime = clipboardData.items[i].type;
29 imageData = clipboardData.items[i].getAsFile();
30 try{
31 imageElement = this.pasteImageBinary(imageData);
32 }catch(e){
33 console.log(""+e.stack);
34 }
35 this.application.ninja.selectionController.selectElements(imageElement);
36 this.application.ninja.currentDocument.model.needsSave = true;
37
38 }
39 }
40 }
41
42 try{
43 if(!!htmlData || !!textData){
44 this.pasteHtml(htmlData, textData);
45 }
46 }catch(e){
47 console.log(""+e.stack);
48 }
49
50 }
51 },
52
53 //todo: this will be moved to a seperate api
54 pasteImageBinary:{
55 value: function(imageBlob){
56 var element, self = this,
57 fileType = imageBlob.type;
58
59 element = this.application.ninja.ioMediator.createFileFromBinary(imageBlob, {"addFileToStage" : self.addImageElement.bind(self)});
60
61 return element;
62
63 }
64 },
65
66 addImageElement:{
67 value: function(status){
68 var save = status.save,
69 fileName = status.filename,
70 url = status.url,
71 fileType = status.fileType,
72 element, rules, self = this;
73
74 if (save && save.success && save.status === 201) {
75 //
76 if (fileType.indexOf('svg') !== -1) {
77 element = document.application.njUtils.make('embed', null, this.application.ninja.currentDocument);//TODO: Verify this is proper
78 element.type = 'image/svg+xml';
79 element.src = url+'/'+fileName;
80 } else {
81 element = document.application.njUtils.make('image', null, this.application.ninja.currentDocument);
82 element.src = url+'/'+fileName;
83 }
84 //Adding element once it is loaded
85 element.onload = function () {
86 element.onload = null;
87 self.application.ninja.elementMediator.addElements(element, rules, true);
88 };
89 //Setting rules of element
90 rules = {
91 'position': 'absolute',
92 'top' : '100px',
93 'left' : '100px'
94 };
95 //
96 self.application.ninja.elementMediator.addElements(element, rules, false);
97 } else {
98 //TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT
99 }
100
101 return element;
102 }
103 },
104
105 //paste from external applicaitons
106 pasteHtml:{//todo: change to pasteNinja, pasteHTML, etc
107 value: function(htmlData, textData){
108 var i=0, j=0,
109 pasteDataObject=null,
110 pastedElements = [],
111 node = null, nodeList = null,
112 styles = null,
113 divWrapper = null,
114 spanWrapper = null,
115 metaEl = null,
116 self = this;
117
118 if(htmlData){
119
120 //TODO: cleanse HTML
121
122 htmlData.replace(/[<script]/g," ");
123
124 this.application.ninja.selectedElements.length = 0;
125 NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} );
126
127 try{
128 nodeList = ClipboardUtil.deserializeHtmlString(htmlData);//this removes html and body tags
129 }
130 catch(e){
131 console.log(""+e.stack);
132 }
133
134 for(i=0; i < nodeList.length; i++){
135 if(nodeList[i].tagName === "META") {
136 nodeList[i] = null;
137 }
138 else if (nodeList[i].tagName === "CANVAS"){
139 //can't paste external canvas for lack of all metadata
140 nodeList[i] = null;
141 }
142 else if((nodeList[i].nodeType === 3) || (nodeList[i].tagName === "A")){
143 node = nodeList[i].cloneNode(true);
144
145 divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument);
146 spanWrapper = document.application.njUtils.make("span", null, this.application.ninja.currentDocument);
147 spanWrapper.appendChild(node);
148 divWrapper.appendChild(spanWrapper);
149 styles = {"position":"absolute", "top":"100px", "left":"100px"};
150
151 this.pastePositioned(divWrapper, styles);
152
153 nodeList[i] = null;
154 pastedElements.push(divWrapper);
155
156 }else if(nodeList[i].tagName === "SPAN"){
157 node = nodeList[i].cloneNode(true);
158
159 divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument);
160 divWrapper.appendChild(node);
161 styles = {"position":"absolute", "top":"100px", "left":"100px"};
162
163 this.pastePositioned(divWrapper, styles);
164
165 nodeList[i] = null;
166 pastedElements.push(divWrapper);
167 }
168 else {
169 node = nodeList[i].cloneNode(true);
170
171 //get class string while copying .... generate styles from class
172 styles = {"position":"absolute", "top":"100px", "left":"100px"};
173
174 this.pastePositioned(node, styles);
175
176 nodeList[i] = null;
177 pastedElements.push(node);
178 }
179
180 }
181
182 nodeList = null;
183
184
185 }else if(textData){
186 node = ClipboardUtil.deserializeHtmlString("<div><span>"+ textData +"</span></div>")[0];
187 styles = {"position":"absolute", "top":"100px", "left":"100px"};
188 this.pastePositioned(node, styles);
189 }
190
191 NJevent("elementAdded", pastedElements);
192 this.application.ninja.currentDocument.model.needsSave = true;
193
194 }
195 },
196
197 pastePositioned:{
198 value: function(element, styles, fromCopy){// for now can wok for both in-place and centered paste
199 var modObject = [], x,y, newX, newY, counter;
200
201 if((typeof fromCopy === "undefined") || (fromCopy && fromCopy === true)){
202 counter = this.pasteCounter;
203 }else{
204 counter = this.pasteCounter - 1;
205 }
206
207 x = styles ? ("" + styles.left + "px") : "100px";
208 y = styles ? ("" + styles.top + "px") : "100px";
209 newX = styles ? ("" + (styles.left + (25 * counter)) + "px") : "100px";
210 newY = styles ? ("" + (styles.top + (25 * counter)) + "px") : "100px";
211
212 if(!styles || (styles && !styles.position)){
213 this.application.ninja.elementMediator.addElements(element, null, false);
214 }else if(styles && (styles.position === "absolute")){