/*
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
// Don't do anything if an input or other control is focused
if(document.activeElement.nodeName !== "BODY") {
return;
}
this.copy(clipboardEvent);
clipboardEvent.preventDefault();
}
},
handleCut:{
value:function(clipboardEvent){
if(this.application.ninja.currentDocument.currentView === "code") return;
// Don't do anything if an input or other control is focused
if(document.activeElement.nodeName !== "BODY") {
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
// Don't do anything if an input or other control is focused
if(document.activeElement.nodeName !== "BODY") {
return;
}
//TODO: return if stage is not focussed
this.pasteCounter++;
if(ninjaData){
if(this.copiedObjects.copy){
try{
this.pasteFromCopy();
}catch(e){
console.log(""+e.stack);
}
}
else if(this.copiedObjects.cut){
try{
this.pasteFromCut();
}catch(e){
console.log(""+e.stack);
}
}
}
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, true/*notify*/);
pastedElements.push(node);
}
}
this.application.ninja.selectionController.selectElements(pastedElements);
this.application.ninja.currentDocument.model.needsSave = true;
}
},
pasteFromCut:{
value:function(){
var i=0, j=0,
node = null, canvas = null,
styles=null,
pastedElements = [];//array of te pastes clones - for selection
for(j=0; j< this.copiedObjects.cut.length; j++){
node = this.deserializeHtmlString(this.copiedObjects.cut[j].outerhtml)[0];
if (node.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);
node = null;
}
else if((node.nodeType === 3) || (node.tagName === "A")){//TextNode
node = null;
}
else {
this.pastePositioned(node, this.copiedObjects.cut[j].styles, true/*notify*/, false/*fromCopy*/);
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,
pastedElements = null,
node = null, nodeList = null,
styles = null,
divWrapper = null,
spanWrapper = null,
metaEl = null,
self = this;
if(htmlData){
//TODO: cleanse HTML
htmlData.replace(/[