/*
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,
NJUtils = require("js/lib/NJUtils").NJUtils,
World = require("js/lib/drawing/world").World;
var ClipboardController = exports.ClipboardController = Montage.create(Component, {
hasTemplate: {
value: false
},
deserializedFromTemplate: {
value: function() {
document.body.addEventListener("copy", this, false);
document.body.addEventListener("cut", this, false);
document.body.addEventListener("paste", this, false);
//ninja menu events
this.eventManager.addEventListener("executeCut", this, false);
this.eventManager.addEventListener("executeCopy", this, false);
this.eventManager.addEventListener("executePaste", this, false);
}
},
clipboardOperationsAgent:{//appropriate agent instant required for execution of cut/copy/paste
value: null
},
//count how many times pasted
//used to move multiple pastes of same copy
pasteCounter:{
value: 0
},
copiedObjects:{
value: {}
},
_copyFlag:{
value:false
},
copyFlag:{
get:function(){return this._copyFlag;},
set:function(value){this._copyFlag = value;}
},
_newCopyFlag:{
value:true
},
newCopyFlag:{
get:function(){return this._newCopyFlag;},
set:function(value){this._newCopyFlag = value;}
},
handleExecuteCopy:{
value: function(){document.execCommand('copy',false,null);}
},
handleExecuteCut:{
value: function(){document.execCommand('cut',false,null);}
},
handleExecutePaste:{
value: function(){document.execCommand('paste',false,null);}
},
handleCopy:{
value:function(clipboardEvent){
if(this.application.ninja.currentDocument.currentView === "code") return;
this.copy(clipboardEvent);
clipboardEvent.preventDefault();
}
},
handleCut:{
value:function(clipboardEvent){
if(this.application.ninja.currentDocument.currentView === "code") return;
this.cut(clipboardEvent);
clipboardEvent.preventDefault();
}
},
handlePaste:{
value:function(clipboardEvent){
if(this.application.ninja.currentDocument.currentView === "code") return;//for design view only
//TODO: return if stage is not focussed
//identify all types of clipboard data
var clipboardData = clipboardEvent.clipboardData,
ninjaData = clipboardData.getData("ninja"),
htmlData = clipboardData.getData("text/html"),
textData = clipboardData.getData("text/plain"),
imageData = clipboardData.getData("image/png"),//TODO: other img types, tiff, jpeg.....
svgData = clipboardData.getData("image/svg");
this.pasteCounter++;
if(ninjaData){
if(this.copiedObjects.copy){this.pasteFromCopy();}
else if(this.copiedObjects.cut){this.pasteFromCut();}
}else{
try{
this.pasteFromExternalSource(htmlData, textData);
}catch(e){
console.log(""+e.stack);
}
}
clipboardEvent.preventDefault();
}
},
/*
parameters:
*/
copy:{
value: function(clipboardEvent){
var j=0, htmlToClipboard = "", ninjaClipboardObj = {}, textToClipboard = "";
this.copiedObjects = {}; this.pasteCounter = 0;
this.copiedObjects["copy"] = [];
if(clipboardEvent){
for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage
this.copiedObjects.copy.push(this.application.ninja.selectedElements[j]);
if(this.application.ninja.selectedElements[j].tagName === "CANVAS"){
if(!ninjaClipboardObj.canvas){
ninjaClipboardObj.canvas = true;
}
}else{
htmlToClipboard = htmlToClipboard + this.serializeHTMLElement(this.application.ninja.selectedElements[j]);
if(!ninjaClipboardObj.plainHtml){
ninjaClipboardObj.plainHtml = true;
}
textToClipboard = textToClipboard + this.getText(this.application.ninja.selectedElements[j]) + " ";
}
}
//set clipboard data
clipboardEvent.clipboardData.setData('ninja', ''+ JSON.stringify(ninjaClipboardObj));
clipboardEvent.clipboardData.setData('text/html', '
' + htmlToClipboard + '');
clipboardEvent.clipboardData.setData('text/plain', textToClipboard);
}
else{
//TODO: custom copy/paste, ex: css, animation, materials
}
}
},
pasteFromCopy:{//todo: change to appropriate name
value:function(){
var i=0, j=0,
pastedElements = [],//array of te pastes clones - for selection
node = null,
styles = null,
copiedElement = null;
//TODO: cleanse HTML
//clear previous selections
this.application.ninja.selectedElements.length = 0;
NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} );
for(j=0; j< this.copiedObjects.copy.length; j++){
copiedElement = this.copiedObjects.copy[j];
styles = null;
if (copiedElement.tagName === "CANVAS"){
//clone copied canvas
var canvas = this.cloneCanvas(copiedElement);
NJevent("elementAdded", canvas);
pastedElements.push(canvas);
}
else {
node = copiedElement.cloneNode(true);
if(node.removeAttribute) {node.removeAttribute("style");}//remove the computed styles attribute which is placed only for pasting to external applications
styles = {};
styles.top = this.application.ninja.elementMediator.getProperty(copiedElement, "top", parseInt);
styles.left = this.application.ninja.elementMediator.getProperty(copiedElement, "left", parseInt);
this.pastePositioned(node, styles);
pastedElements.push(node);
}
}
this.application.ninja.selectionController.selectElements(pastedElements);
this.application.ninja.currentDocument.model.needsSave = true;
}
},
pasteFromCut:{
value:function(){
var i=0, j=0,
clipboardHelper=this.createClipboardHelper(),
node = null, canvas = null,
styles=null,
pastedElements = [];//array of te pastes clones - for selection
for(j=0; j< this.copiedObjects.cut.length; j++){
clipboardHelper.innerHTML = this.copiedObjects.cut[j].outerhtml;
if (clipboardHelper.lastChild.tagName === "CANVAS"){
//paste canvas
canvas = this.generateNewCanvas(this.copiedObjects.cut[j].outerhtml, this.copiedObjects.cut[j].styles, this.copiedObjects.cut[j].className, this.copiedObjects.cut[j].worldJson);
NJevent("elementAdded", canvas);
pastedElements.push(canvas);
}
else if((clipboardHelper.lastChild.nodeType === 3) || (clipboardHelper.lastChild.tagName === "A")){//TextNode
}
else {
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
this.pastePositioned(node, this.copiedObjects.cut[j].styles);
pastedElements.push(node);
}
}
this.application.ninja.selectionController.selectElements(pastedElements);
this.application.ninja.currentDocument.model.needsSave = true;
}
},
//paste from external applicaitons
pasteFromExternalSource:{//todo: change to pasteNinja, pasteHTML, etc
value: function(htmlData, textData){
var i=0, j=0,
pasteDataObject=null,
clipboardHelper=this.createClipboardHelper(),
pastedElements = null,
node = null,
styles = null,
divWrapper = null,
spanWrapper = null,
metaEl = null;
if(htmlData){
//TODO: cleanse HTML
//TODO: remove all script tags for security
htmlData.replace(/[