/*
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
|| (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){
return;
}//for design view only
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){
var clipboardData = clipboardEvent.clipboardData,
ninjaData = clipboardData.getData("ninja"),
htmlData = clipboardData.getData("text/html"),
textData = clipboardData.getData("text/plain"),
i=0,
imageMime, imageData, imageElement;
if(!this.application.ninja.currentDocument
|| (this.application.ninja.currentDocument && this.application.ninja.currentDocument.currentView === "code")){
return;
}//for design view only
//TODO: return if stage is not focussed
this.pasteCounter++;
if(ninjaData){
if(this.copiedObjects.copy){this.pasteFromCopy();}
else if(this.copiedObjects.cut){this.pasteFromCut();}
}
else{
//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.generateImageElement(imageData);
}catch(e){
console.log(""+e.stack);
}
this.application.ninja.selectionController.selectElements(imageElement);
this.application.ninja.currentDocument.model.needsSave = true;
}
}
}
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
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(copiedElement.ownerDocument.defaultView.getComputedStyle(copiedElement).getPropertyValue("position") === "absolute"){
styles = {};
styles.top = this.application.ninja.elementMediator.getProperty(copiedElement, "top", parseInt);
styles.left = this.application.ninja.elementMediator.getProperty(copiedElement, "left", parseInt);
styles.position = "absolute";
}else{
styles = null;
}
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(/[