/*
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(/[