/*
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,
UndoManager = require("montage/core/undo-manager").UndoManager,
AppData = require("js/data/appdata").AppData;
var matrix = require("js/lib/math/matrix");
var NjUtils = require("js/lib/NJUtils").NJUtils;
exports.Ninja = Montage.create(Component, {
ninjaVersion: {
value: null
},
_workspaceMode: {
value: null
},
workspaceMode: {
get: function() {
return this.workspaceMode;
},
set: function(val) {
if(this._workspaceMode !== val ) {
if(this._workspaceMode !== null) {
document.body.classList.remove("ws-" + this._workspaceMode);
}
document.body.classList.add("ws-" + val);
this._workspaceMode = val;
}
}
},
toolsData: { value: null },
appData: { value: AppData },
currentDocument: {
value: null
},
_isResizing: {
value: null
},
_resizedHeight : {
value: 0
},
_height: {
value: null
},
height: {
get: function() {
if(this._height === null) {
var storedData = this.application.localStorage.getItem("timelinePanel");
if(storedData && storedData.value) {
this._height = storedData.value;
}
}
return this._height;
},
set: function(val) {
if(this._height != val) {
this._height = val;
this.application.localStorage.setItem("timelinePanel", {"version": this.version, "value": val});
this.needsDraw = true;
}
}
},
_resizedWidth : {
value: 0
},
_width: {
value: null
},
width: {
get: function() {
if(this._width === null) {
var storedData = this.application.localStorage.getItem("rightPanelsContainer");
if(storedData && storedData.value) {
this._width = storedData.value;
}
}
return this._width;
},
set: function(val) {
if(this._width != val) {
this._width = val;
this.application.localStorage.setItem("rightPanelsContainer", {"version": this.version, "value": val});
this.needsDraw = true;
}
}
},
handleResizeStart: {
value:function(e) {
this.isResizing = true;
this.height = parseInt(this.timeline.element.offsetHeight);
this.width = parseInt(this.rightPanelContainer.offsetWidth);
this.rightPanelContainer.classList.add("disableTransition");
this.timeline.element.classList.add("disableTransition");
this.needsDraw = true;
}
},
handleResizeMove: {
value:function(e) {
this._resizedHeight = e._event.dY;
this._resizedWidth = e._event.dX;
this.stage.resizeCanvases = true;
this.needsDraw = true;
}
},
handleResizeEnd: {
value: function(e) {
this.stage.resizeCanvases = true;
this._resizedHeight = 0;
this._resizedWidth = 0;
this.isResizing = false;
this.needsDraw = true;
this.rightPanelContainer.classList.remove("disableTransition");
this.timeline.element.classList.remove("disableTransition");
this.height = this.timeline.element.offsetHeight;
this.width = this.rightPanelContainer.offsetWidth;
}
},
handleResizeReset: {
value: function(e) {
this.width = 253;
this.height = 140;
this._resizedHeight = 0;
this._resizedWidth = 0;
this.timelineSplitter.collapsed = false;
this.panelSplitter.collapsed = false;
this.stage.resizeCanvases = true;
this.needsDraw = true;
}
},
selectedElements: {
value: []
},
currentSelectedContainer: {
value: null
},
templateDidLoad: {
value: function() {
this.ninjaVersion = window.ninjaVersion.ninja.version;
this.undoManager = document.application.undoManager = UndoManager.create();
document.application.njUtils = NjUtils;
}
},
prepareForDraw: {
value: function() {
this.workspaceMode = "default";
console.log("Loading Ninja --> ", this.ninjaVersion);
this.application.ninja = this;
this.toolsData.selectedTool = this.toolsData.defaultToolsData[this.application.ninja.toolsData.selectionToolIndex];
this.toolsData.defaultSubToolsData = this.toolsData.defaultToolsData[this.application.ninja.toolsData.shapeToolIndex].subtools;
this.toolsData.selectedSubTool = this.toolsData.defaultToolsData[this.application.ninja.toolsData.shapeToolIndex].subtools[1];
this.toolsData.selectedToolInstance = this.toolsList[this.toolsData.selectedTool.action];
this.setupGlobalHelpers();
window.addEventListener("resize", this, false);
this.eventManager.addEventListener( "selectTool", this, false);
this.eventManager.addEventListener( "selectSubTool", this, false);
this.eventManager.addEventListener( "onOpenDocument", this, false);
this.addPropertyChangeListener("appModel.livePreview", this.executeLivePreview, false);
this.addPropertyChangeListener("appModel.chromePreview", this.executeChromePreview, false);
this.addPropertyChangeListener("appModel.debug", this.toggleDebug, false);
NJevent("appLoading");
}
},
////////////////////////////////////////////////////////////////////
//TODO: Expand method to allow other browsers for preview
executeChromePreview: {
value: function () {
this.application.ninja.documentController.activeDocument.model.browserPreview('chrome');
}
},
////////////////////////////////////////////////////////////////////
handleResize: {
value: function() {
this.stage.resizeCanvases = true;
}
},
draw: {
value: function() {
if(this.isResizing) {
this.timelineSplitter.collapsed = this.height - this._resizedHeight < 46;
this.panelSplitter.collapsed = this.width - this._resizedWidth < 30;
}
this.rightPanelContainer.style.width = (this.width - this._resizedWidth) + "px";
this.timeline.element.style.height = (this.height - this._resizedHeight) + "px";
}
},
_didDraw: {
value: false
},
didDraw: {
value: function() {
if(!this._didDraw) {
if (!this.application.ninja.coreIoApi.ioServiceDetected) {
var check = this.application.ninja.coreIoApi.cloudAvailable();
}
NJevent("appLoaded");
this._didDraw = true;
}
}
},
handleSelectTool: {
value: function(event) {
this.toolsData.defaultToolsData[this.toolsData.defaultToolsData.indexOf(this.toolsData.selectedTool)].selected = false;
if(this.toolsData.selectedTool.container) {
this.toolsList[this.toolsData.selectedSubTool.action]._configure(false);
} else {
this.toolsList[this.toolsData.selectedTool.action]._configure(false);
}
this.toolsData.selectedTool = event.detail;
this.toolsData.defaultToolsData[this.toolsData.defaultToolsData.indexOf(this.toolsData.selectedTool)].selected = true;
if(this.toolsData.selectedTool.container) {
this.toolsData.selectedToolInstance = this.toolsList[this.toolsData.selectedSubTool.action];
} else {
this.toolsData.selectedToolInstance = this.toolsList[this.toolsData.selectedTool.action];
}
this.stage.SelectTool(this.toolsData.selectedTool.cursor);
this.toolsData.selectedToolInstance._configure(true);
}
},
handleSelectSubTool: {
value: function(event) {
this.toolsData.defaultSubToolsData[this.toolsData.defaultSubToolsData.indexOf(this.toolsData.selectedSubTool)].selected = false;
this.toolsList[this.toolsData.selectedSubTool.action]._configure(false);
this.toolsData.selectedSubTool = event.detail;
this.toolsData.defaultSubToolsData[this.toolsData.defaultSubToolsData.indexOf(this.toolsData.selectedSubTool)].selected = true;
this.toolsData.selectedToolInstance = this.toolsList[this.toolsData.selectedSubTool.action];
this.toolsList[this.toolsData.selectedSubTool.action]._configure(true);
}
},
handleOnOpenDocument: {
value: function(event) {
this.currentDocument = event.detail;
if(this.currentDocument.documentRoot) {
this.application.ninja.currentSelectedContainer = this.currentDocument.documentRoot;
} else {
alert("The current document has not loaded yet");
return;
}
this.appModel.show3dGrid = this.currentDocument.draw3DGrid;
NJevent("openDocument");
}
},
executeLivePreview: {
value: function() {
var background, overflow, transitionStopRule;
this.stage.hideCanvas(this.appModel.livePreview);
// TODO: Remove marker for old template: NINJA-STAGE-REWORK
if(this.appModel.livePreview) {
// background = "#000000";
// overflow = "hidden";
transitionStopRule = "nj-css-garbage-selector";
} else {
// background = "#808080";
// overflow = "visible";
transitionStopRule = "*"
}
// TODO: Remove marker for old template: NINJA-STAGE-REWORK
// this.currentDocument.documentRoot.elementModel.controller.setProperty(this.currentDocument.documentRoot, "body-background", background);
// this.currentDocument.documentRoot.elementModel.controller.setProperty(this.currentDocument.documentRoot, "overflow", overflow);
// this.currentDocument.documentRoot.elementModel.controller.changeSelector(this.currentDocument.documentRoot, "transitionStopRule", transitionStopRule);
this.application.ninja.stylesController._stageStylesheet.rules[0].selectorText = transitionStopRule;
this._toggleWebGlAnimation(this.appModel.livePreview);
}
},
// Turn on WebGL animation during preview
_toggleWebGlAnimation: {
value: function(inLivePreview) {
var glCanvases = this.currentDocument.model.views.design.iframe.contentWindow.document.querySelectorAll('[data-RDGE-id]'),
glShapeModel;
if(glCanvases) {
for(var i = 0, len = glCanvases.length; i