From f3dc624fa464a79fd8e8cec1ddd16ed2109bda23 Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Fri, 10 Feb 2012 16:05:27 -0800 Subject: Revert "Merge from /joseeight/ninja-internal/tree/FileIO" This reverts commit c48eeb01bd726895dc577d8b843b2a75883eee07. Signed-off-by: Ananya Sen --- js/io/document/document-controller.js | 455 +++++++++++++++++++++++++++++++++ js/io/document/html-document.js | 84 +++--- js/io/ui/cloudpopup.reel/cloudpopup.js | 3 - 3 files changed, 493 insertions(+), 49 deletions(-) create mode 100755 js/io/document/document-controller.js (limited to 'js/io') diff --git a/js/io/document/document-controller.js b/js/io/document/document-controller.js new file mode 100755 index 00000000..6dc7e670 --- /dev/null +++ b/js/io/document/document-controller.js @@ -0,0 +1,455 @@ +/* +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. +
*/ + +/** +@module js/document/documentManager +@requires montage/core/core +@requires montage/ui/component +@requires js/document/html-document +@requires js/document/text-document +*/ + +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + Uuid = require("montage/core/uuid").Uuid, + HTMLDocument = require("js/io/document/html-document").HTMLDocument, + TextDocument = require("js/io/document/text-document").TextDocument; + +var DocumentController = exports.DocumentController = Montage.create(Component, { + hasTemplate: { + value: false + }, + + _documents: { + value: [] + }, + + _activeDocument: { value: null }, + _iframeCounter: { value: 1, enumerable: false }, + _iframeHolder: { value: null, enumerable: false }, + _textHolder: { value: null, enumerable: false }, + _codeMirrorCounter: {value: 1, enumerable: false}, + + tmpSourceForTesting: { + value: "function CodeMirror(place, givenOptions) {" + + "// Determine effective options based on given values and defaults." + + "var options = {}, defaults = CodeMirror.defaults; }" + }, + + activeDocument: { + get: function() { + return this._activeDocument; + }, + set: function(doc) { + if(this._activeDocument) this._activeDocument.isActive = false; + + if(this._documents.indexOf(doc) === -1) this._documents.push(doc); + + this._activeDocument = doc; + this._activeDocument.isActive = true; + + if(!!this._activeDocument.editor){ + this._activeDocument.editor.focus(); + } + + } + }, + + deserializedFromTemplate: { + value: function() { + this.eventManager.addEventListener("appLoaded", this, false); + this.eventManager.addEventListener("executeFileOpen", this, false); + this.eventManager.addEventListener("executeNewFile", this, false); + this.eventManager.addEventListener("executeSave", this, false); + + this.eventManager.addEventListener("recordStyleChanged", this, false); + + // Temporary testing opening a new file after Ninja has loaded + this.eventManager.addEventListener("executeNewProject", this, false); + } + }, + + handleAppLoaded: { + value: function() { + //this.openDocument({"type": "html"}); + } + }, + + handleExecuteNewProject: { + value: function() { + this.openDocument({"type": "html"}); + } + }, + + handleExecuteFileOpen: { + value: function(event) { + var pickerSettings = event._event.settings || {}; + pickerSettings.callback = this.openFileWithURI.bind(this); + pickerSettings.pickerMode = "read"; + pickerSettings.inFileMode = true; + this.application.ninja.filePickerController.showFilePicker(pickerSettings); + } + }, + + handleExecuteNewFile: { + value: function(event) { + var newFileSettings = event._event.settings || {}; + newFileSettings.callback = this.createNewFile.bind(this); + this.application.ninja.newFileController.showNewFileDialog(newFileSettings); + } + }, + + handleExecuteSave: { + value: function(event) { + this.activeDocument.save(); + } + }, + + createNewFile:{ + value:function(newFileObj){ + //console.log(newFileObj);//contains the template uri and the new file uri + if(!newFileObj) return; + this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this)); + + if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view + + } else { + //open design view + } + } + }, + + /** + * Public method + * doc contains: + * type : file type, like js, css, etc + * name : file name + * source : file content + * uri : file uri + */ + openNewFileCallback:{ + value:function(doc){ + var response = doc || null;//default just for testing + if(!!response && response.success && !!response.uri){ + this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); + } + } + }, + + openFileWithURI: { + value: function(uriArrayObj) { + var uri = "", fileContent = "", response=null, filename="", fileType="js"; + if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){ + uri = uriArrayObj.uri[0]; + } + //console.log("URI is: ", uri); + if(!!uri){ + this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); + } + } + }, + + /** + * Public method + */ + openFileCallback:{ + value:function(response){ + //Return Object Description +// Object.status (Always presents for handling) +// 204: File exists (Success) +// 404: File does not exists (Failure) +// 500: Unknown (Probably cloud API not running) +// +// (Below only present if succesfull 204) +// +// Object.content +// Object.extension +// Object.name +// Object.uri +// Object.creationDate +// Object.modifiedDate +// Object.readOnly +// Object.size + + var fileDetails = {}; + if(!!response && (response.status === 204)){ + /** + * fileDetails format: + * {"type": "js", "name": "test", "source": fileContent, "uri": uri} + */ + fileDetails.type = response.extension; + fileDetails.source = response.content; + fileDetails.name = response.name; + fileDetails.uri = response.uri; + + this.openDocument(fileDetails); + }else if(!!response && (response.status === 404)){ + alert("Unable to open file.\n [Error: File does not exist]"); + }else if(!!response && (response.status === 500)){ + alert("Unable to open file.\n Check if Ninja Local Cloud is running."); + }else{ + alert("Unable to open file."); + } + + } + }, + + openProjectWithURI: { + value: function(uri) { + console.log("URI is: ", uri); + + } + }, + + /** Open a Document **/ + openDocument: { + value: function(doc) { + var newDoc; + + if(!doc) return false; + + // try { + if (doc.type === 'html' || doc.type === 'htm') { + newDoc = Montage.create(HTMLDocument); + newDoc.initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument); + } else { + newDoc = Montage.create(TextDocument, { + "source": { value: doc.source } + }); + var docUuid = Uuid.generate(); + var textArea = this.application.ninja.stage.stageView.createTextAreaElement(docUuid); + newDoc.initialize(doc, docUuid, textArea, textArea.parentNode); + + // Tmp this will be filled with the real content + newDoc.textArea.value = doc.source; //this.tmpSourceForTesting; + + this.textDocumentOpened(newDoc); + + } + + // } catch (err) { + // console.log("Could not open Document ", err); + // } + } + }, + + textDocumentOpened: { + value: function(doc) { + + + + this.application.ninja.stage.stageView.createTextView(doc); + + /* + DocumentManager._hideCurrentDocument(); + stageManagerModule.stageManager._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe + DocumentManager.activeDocument = doc; + + var type; + + switch(doc.documentType) { + case "css" : + type = "css"; + break; + case "js" : + type = "javascript"; + break; + } + + DocumentManager._codeEditor.editor = CodeMirror.fromTextArea(doc.textArea, { + lineNumbers: true, + mode: type, + onCursorActivity: function() { + DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.hline, null); + DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.editor.getCursor().line, "activeline"); + } + }); + DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(0, "activeline"); + */ + + } + }, + + closeDocument: { + value: function(id) { + if(this.activeDocument.dirtyFlag === true){ + //if file dirty then alert user to save + } + + var doc = this._findDocumentByUUID(id); + this._removeDocumentView(doc.container); + + var closeDocumentIndex = this._findIndexByUUID(id); + this._documents.splice(this._findIndexByUUID(id), 1); + + if(this.activeDocument.uuid === id && this._documents.length > 0) { + var nextDocumentIndex = -1 ; + if((this._documents.length > 0) && (closeDocumentIndex === 0)){ + nextDocumentIndex = 1; + }else if((this._documents.length > 0) && (closeDocumentIndex > 0)){ + nextDocumentIndex = closeDocumentIndex - 1; + } + this.application.ninja.stage.stageView.switchDocument(this._documents[nextDocumentIndex]); + }else{ + //if there are no documents to switch to then just show the iframeContainer + document.getElementById("iframeContainer").style.display="block"; + } + } + }, + + // Document has been loaded into the Iframe. Dispatch the event. + // Event Detail: Contains the current ActiveDocument + _onOpenDocument: { + value: function(doc){ + //var data = DocumentManager.activeDocument; + //DocumentManager._hideCurrentDocument(); + + //stageManagerModule.stageManager.toggleCanvas(); + + DocumentController.activeDocument = doc; + + NJevent("onOpenDocument", doc); +// appDelegateModule.MyAppDelegate.onSetActiveDocument(); + + } + }, + + + _onOpenTextDocument: { + value: function(doc) { + this._hideCurrentDocument(); + this.application.ninja.stage._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe + this.activeDocument = doc; + + var type; + + switch(doc.documentType) { + case "css" : + type = "css"; + break; + case "js" : + type = "javascript"; + break; + } + + DocumentController._codeEditor.editor = CodeMirror.fromTextArea(doc.textArea, { + lineNumbers: true, + mode: type, + onCursorActivity: function() { + DocumentController._codeEditor.editor.setLineClass(DocumentController._codeEditor.hline, null); + DocumentController._codeEditor.hline = DocumentController._codeEditor.editor.setLineClass(DocumentController._codeEditor.editor.getCursor().line, "activeline"); + } + }); + DocumentController._codeEditor.hline = DocumentController._codeEditor.editor.setLineClass(0, "activeline"); + + } + }, + + /** + * VIEW Related Methods + */ + // PUBLIC + ShowActiveDocument: { + value: function() { + this.activeDocument.iframe.style.opacity = 1.0; + } + }, + + // PRIVATE + _findDocumentByUUID: { + value: function(uuid) { + var len = this._documents.length; + for(var i = 0; i < len; i++) { + if(this._documents[i].uuid === uuid) return this._documents[i]; + } + + return false; + } + }, + + _findIndexByUUID: { + value: function(uuid) { + var len = this._documents.length; + for(var i = 0; i < len; i++) { + if(this._documents[i].uuid === uuid) return i; + } + + return false; + } + }, + + _hideCurrentDocument: { + value: function() { + if(this.activeDocument) { + if(this.activeDocument.currentView === "design"){ + this.application.ninja.stage.saveScroll(); + this.activeDocument.container.parentNode.style["display"] = "none"; + this.application.ninja.stage.hideCanvas(true); + this.application.ninja.stage.stageView.hideRulers(); + } + + this.activeDocument.container.style["display"] = "none"; + } + } + }, + + _showCurrentDocument: { + value: function() { + if(this.activeDocument) { + this.activeDocument.container.style["display"] = "block"; + if(this.activeDocument.currentView === "design"){ + this.activeDocument.container.parentNode.style["display"] = "block"; + this.application.ninja.stage.restoreScroll(); + this.application.ninja.stage.hideCanvas(false); + this.application.ninja.stage.stageView.showRulers(); + }else{ + //hide the iframe when switching to code view + document.getElementById("iframeContainer").style.display="none"; + } + } + } + }, + + _removeDocumentView: { + value: function(node) { + node.parentNode.removeChild(node); + } + }, + + reloadDocumentContent: { + value: function() { + this.activeDocument._window.location.reload(); + } + }, + + /** + * Creates a new iFrame element using a new unique ID for it. Returns the iframe ID. + */ + _createIframeElement: { + value: function() { + var e = document.createElement("iframe"); + e.id = this._createIframeID(); + e.style.border = "none"; + e.style.opacity = 0; + e.height = 1000; + e.width = 2000; + e.src = ""; + + if(!this._iframeHolder) this._iframeHolder = document.getElementById("iframeContainer"); + + this._iframeHolder.appendChild(e); + + return e; + } + }, + + + _createIframeID: { + value: function() { + return "userDocument_" + (this._iframeCounter++); + } + } +}); diff --git a/js/io/document/html-document.js b/js/io/document/html-document.js index fbb34a1d..d51cd279 100755 --- a/js/io/document/html-document.js +++ b/js/io/document/html-document.js @@ -11,7 +11,7 @@ var Montage = require("montage/core/core").Montage, var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.BaseDocument, { // PRIVATE MEMBERS _selectionExclude: { value: null, enumerable: false }, - _htmlTemplateUrl: { value: "user-document-templates/montage-application-cloud/index.html", enumerable: false}, + _cloudTemplateUri: { value: "user-document-templates/montage-application-cloud/index.html", enumerable: false}, _iframe: { value: null, enumerable: false }, _server: { value: null, enumerable: false }, _selectionModel: { value: [], enumerable: false }, @@ -24,7 +24,7 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base _styles: { value: null, enumerable: false }, _stylesheets: { value: null, enumerable: false }, _stageStyleSheetId : { value: 'nj-stage-stylesheet', enumerable: false }, - _userDocument: { value: null, enumerable: false }, + _initialUserDocument: { value: null, enumerable: false }, _htmlSource: {value: "", enumerable: false}, _glData: {value: null, enumerable: false }, @@ -218,27 +218,30 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base //****************************************// // PUBLIC METHODS - - - //////////////////////////////////////////////////////////////////// - // - initialize: { - value: function(file, uuid, iframe, callback) { - // - this._userDocument = file; - // - this.init(file.name, file.uri, file.extension, iframe, uuid, callback); - // - this.iframe = iframe; - this.selectionExclude = ["HTML", "BODY", "Viewport", "UserContent", "stageBG"]; - this.currentView = "design"; - // - this.iframe.src = this._htmlTemplateUrl; - this.iframe.addEventListener("load", this, true); + initialize: { + value: function(doc, uuid, iframe, callback) { + // Shell mode is not used anymore + //if(!window.IsInShellMode()) { + if(!doc.name){doc.name = "index-cloud"}; + if(!doc.uri){doc.uri = this._cloudTemplateUri}; + this.init(doc.name, doc.uri, doc.type, iframe, uuid, callback); + /* + } else { + var tmpurl = doc.uri.split('\\'); + var fileUrl = doc.server.url + "/" + tmpurl[tmpurl.length -1] + "?fileio=true&template=/user-document-templates/montage-application/index.html"; + this.init(name, fileUrl, doc.type, iframe, uuid, callback); + this.server = doc.server; + this._initialUserDocument = doc; + } + */ + this.iframe = iframe; + this.selectionExclude = ["HTML", "BODY", "Viewport", "UserContent", "stageBG"]; + this.currentView = "design"; + + this._loadDocument(this.uri); + } }, - //////////////////////////////////////////////////////////////////// - collectGLData: { value: function( elt, dataArray ) @@ -342,8 +345,7 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base } }, - /* -// Private + // Private _loadDocument: { value: function(uri) { // Load the document into the Iframe @@ -351,32 +353,23 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base this.iframe.addEventListener("load", this, true); } }, -*/ - - - - //////////////////////////////////////////////////////////////////// - // + handleEvent: { value: function(event){ - // - this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent"); + this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent"); this.stageBG = this.iframe.contentWindow.document.getElementById("stageBG"); this.stageBG.onclick = null; this._document = this.iframe.contentWindow.document; this._window = this.iframe.contentWindow; - // - if(!this.documentRoot.Ninja) this.documentRoot.Ninja = {}; - // - - this.documentRoot.innerHTML = this._userDocument.content.body; - this.iframe.contentWindow.document.getElementById("userHead").innerHTML = this._userDocument.content.head; - - - - - - + if(!this.documentRoot.Ninja) + { + this.documentRoot.Ninja = {}; + } + + if(this._initialUserDocument) { + // Now load the user content + this.documentRoot.innerHTML = this._initialUserDocument.body; + this.iframe.contentWindow.document.getElementById("userHead").innerHTML = this._initialUserDocument.head; this.cssLoadInterval = setInterval(function() { if(this._document.styleSheets.length > 1) { @@ -390,8 +383,7 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base // TODO - Not sure where this goes this._userComponentSet = {}; - - + } else { this._styles = this._document.styleSheets[this._document.styleSheets.length - 1]; this._stylesheets = this._document.styleSheets; // Entire stlyesheets array @@ -437,7 +429,7 @@ var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.Base } this.callback(this); - + } } }, diff --git a/js/io/ui/cloudpopup.reel/cloudpopup.js b/js/io/ui/cloudpopup.reel/cloudpopup.js index e5bad98e..1344661b 100755 --- a/js/io/ui/cloudpopup.reel/cloudpopup.js +++ b/js/io/ui/cloudpopup.reel/cloudpopup.js @@ -51,9 +51,6 @@ exports.CloudPopup = Montage.create(Component, { this._os = 'windows'; } else if (navigator.appVersion.indexOf("Mac")!=-1) { this._os = 'mac'; - } else { - //Alternate message for no OS detected (probably Linux) - this.element.getElementsByTagName('section')[0].style.display = 'none'; } } }, -- cgit v1.2.3