From 8db9e73ca68c006769af3997034959f6b7008add Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Thu, 21 Jun 2012 11:02:55 -0700 Subject: - added io api to generate file from binary - refactoring and cleanup Signed-off-by: Ananya Sen --- js/clipboard/external-apps-clipboard-agent.js | 220 ++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 js/clipboard/external-apps-clipboard-agent.js (limited to 'js/clipboard/external-apps-clipboard-agent.js') 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 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +//////////////////////////////////////////////////////////////////////// +// + +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + ClipboardUtil = require("js/clipboard/util").ClipboardUtil; + +var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.create(Component, { + + paste:{ + value: function(clipboardEvent){ + var clipboardData = clipboardEvent.clipboardData, + htmlData = clipboardData.getData("text/html"), + textData = clipboardData.getData("text/plain"), + i=0, + imageMime, imageData, imageElement; + + //handle image blobs + if(clipboardData.items && (clipboardData.items.length > 0)){ + for(i=0; i < clipboardData.items.length; i++ ){ + if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png" + imageMime = clipboardData.items[i].type; + imageData = clipboardData.items[i].getAsFile(); + try{ + imageElement = this.pasteImageBinary(imageData); + }catch(e){ + console.log(""+e.stack); + } + this.application.ninja.selectionController.selectElements(imageElement); + this.application.ninja.currentDocument.model.needsSave = true; + + } + } + } + + try{ + if(!!htmlData || !!textData){ + this.pasteHtml(htmlData, textData); + } + }catch(e){ + console.log(""+e.stack); + } + + } + }, + + //todo: this will be moved to a seperate api + pasteImageBinary:{ + value: function(imageBlob){ + var element, self = this, + fileType = imageBlob.type; + + element = this.application.ninja.ioMediator.createFileFromBinary(imageBlob, {"addFileToStage" : self.addImageElement.bind(self)}); + + return element; + + } + }, + + addImageElement:{ + value: function(status){ + var save = status.save, + fileName = status.filename, + url = status.url, + fileType = status.fileType, + element, rules, self = this; + + if (save && save.success && save.status === 201) { + // + if (fileType.indexOf('svg') !== -1) { + element = document.application.njUtils.make('embed', null, this.application.ninja.currentDocument);//TODO: Verify this is proper + element.type = 'image/svg+xml'; + element.src = url+'/'+fileName; + } else { + element = document.application.njUtils.make('image', null, this.application.ninja.currentDocument); + element.src = url+'/'+fileName; + } + //Adding element once it is loaded + element.onload = function () { + element.onload = null; + self.application.ninja.elementMediator.addElements(element, rules, true); + }; + //Setting rules of element + rules = { + 'position': 'absolute', + 'top' : '100px', + 'left' : '100px' + }; + // + self.application.ninja.elementMediator.addElements(element, rules, false); + } else { + //TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT + } + + return element; + } + }, + + //paste from external applicaitons + pasteHtml:{//todo: change to pasteNinja, pasteHTML, etc + value: function(htmlData, textData){ + var i=0, j=0, + pasteDataObject=null, + pastedElements = [], + node = null, nodeList = null, + styles = null, + divWrapper = null, + spanWrapper = null, + metaEl = null, + self = this; + + if(htmlData){ + + //TODO: cleanse HTML + + htmlData.replace(/["+ textData +"")[0]; + styles = {"position":"absolute", "top":"100px", "left":"100px"}; + this.pastePositioned(node, styles); + } + + NJevent("elementAdded", pastedElements); + this.application.ninja.currentDocument.model.needsSave = true; + + } + }, + + pastePositioned:{ + value: function(element, styles, fromCopy){// for now can wok for both in-place and centered paste + var modObject = [], x,y, newX, newY, counter; + + if((typeof fromCopy === "undefined") || (fromCopy && fromCopy === true)){ + counter = this.pasteCounter; + }else{ + counter = this.pasteCounter - 1; + } + + x = styles ? ("" + styles.left + "px") : "100px"; + y = styles ? ("" + styles.top + "px") : "100px"; + newX = styles ? ("" + (styles.left + (25 * counter)) + "px") : "100px"; + newY = styles ? ("" + (styles.top + (25 * counter)) + "px") : "100px"; + + if(!styles || (styles && !styles.position)){ + this.application.ninja.elementMediator.addElements(element, null, false); + }else if(styles && (styles.position === "absolute")){ + this.application.ninja.elementMediator.addElements(element, {"top" : newY, "left" : newX}, false);//displace + } + } + } + +}); \ No newline at end of file -- cgit v1.2.3