From ad0ee69be3512325ede94738f23597086a141a3e Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Fri, 10 Feb 2012 01:55:30 -0800 Subject: file open and file new integrated again Signed-off-by: Ananya Sen --- .../file-input-field.reel/file-input-field.js | 3 +- .../ui/FilePicker/file-picker-controller.js | 50 ++++++++----------- .../pickerNavigator.reel/pickerNavigator.js | 37 +++++++------- js/data/menu-data.js | 2 +- js/io/document/document-controller.js | 57 ++++++++++++++++++---- js/io/templates/descriptor.json | 34 ++++++------- .../new-file-options-navigator.js | 8 +-- .../new-file-workflow-controller.js | 2 +- 8 files changed, 108 insertions(+), 85 deletions(-) (limited to 'js') diff --git a/js/components/ui/FilePicker/file-input-field.reel/file-input-field.js b/js/components/ui/FilePicker/file-input-field.reel/file-input-field.js index 8710de63..235be8ad 100755 --- a/js/components/ui/FilePicker/file-input-field.reel/file-input-field.js +++ b/js/components/ui/FilePicker/file-input-field.reel/file-input-field.js @@ -70,8 +70,7 @@ var FileInputField = exports.FileInputField = Montage.create(Component, { settings.inFileMode = true; settings.pickerName = this.pickerName || "fileSelector"; } - settings.callback = this.filePickerCallback; - settings.callbackScope = this; + settings.callback = this.filePickerCallback.bind(this); openFilePicker.settings = settings; this.eventManager.dispatchEvent(openFilePicker); } diff --git a/js/components/ui/FilePicker/file-picker-controller.js b/js/components/ui/FilePicker/file-picker-controller.js index e71d1460..0ff20cc8 100755 --- a/js/components/ui/FilePicker/file-picker-controller.js +++ b/js/components/ui/FilePicker/file-picker-controller.js @@ -38,30 +38,6 @@ var FilePickerController = exports.FilePickerController = Montage.create(require value: "filePicker" }, - /** - * this will be stored in the local storage and in the cloud may be, for the cloud one. - */ - _lastOpenedFolderURI:{ - writable:true, - enumerable:true, - value:{ - lastFolderUri_local:null, - lastFolderUri_cloud:null - } - }, - - /** - * this will be stored in the local storage and in the cloud may be, for the cloud one. - */ - _lastSavedFolderURI:{ - writable:true, - enumerable:true, - value:{ - lastSavedFolderUri_local:null, - lastSavedFolderUri_cloud:null - } - }, - /** *this function is used to create an instance of a file picker * @@ -87,7 +63,6 @@ var FilePickerController = exports.FilePickerController = Montage.create(require var callback, callbackScope, pickerMode, currentFilter, allFileFilters, inFileMode, allowNewFileCreation, allowMultipleSelections, pickerName; if(!!settings){ if(typeof settings.callback !== "undefined"){callback = settings.callback;} - if(typeof settings.callbackScope !== "undefined"){callbackScope = settings.callbackScope;} if(typeof settings.pickerMode !== "undefined"){pickerMode = settings.pickerMode;} if(typeof settings.currentFilter !== "undefined"){currentFilter = settings.currentFilter;} if(typeof settings.allFileFilters !== "undefined"){allFileFilters = settings.allFileFilters;} @@ -97,6 +72,12 @@ var FilePickerController = exports.FilePickerController = Montage.create(require if(typeof settings.pickerName !== "undefined"){this.filePickerPopupType = settings.pickerName;} } + if(settings.pickerName === "saveAsDirectoryPicker"){//need to set the picker mode in a better way + pickerMode = "write"; + }else{ + pickerMode = "read"; + } + var aModel = filePickerModelModule.FilePickerModel.create(); var topLevelDirectories = null; @@ -131,13 +112,20 @@ var FilePickerController = exports.FilePickerController = Montage.create(require } //populate the last opened folder first, if none then populate default root - var sessionStorage = window.sessionStorage; var storedUri = null; - - if(pickerMode === "write"){ - storedUri = sessionStorage.getItem("lastSavedFolderURI"); - }else{ - storedUri = sessionStorage.getItem("lastOpenedFolderURI"); + var sessionStorage = window.sessionStorage; + try{ + if(pickerMode === "write"){ + storedUri = sessionStorage.getItem("lastSavedFolderURI"); + }else if(inFileMode === true){ + storedUri = sessionStorage.getItem("lastOpenedFolderURI_fileSelection"); + }else if(inFileMode === false){ + storedUri = sessionStorage.getItem("lastOpenedFolderURI_folderSelection"); + } + }catch(e){ + if(e.code == 22){ + sessionStorage.clear(); + } } if(!!storedUri){ diff --git a/js/components/ui/FilePicker/pickerNavigator.reel/pickerNavigator.js b/js/components/ui/FilePicker/pickerNavigator.reel/pickerNavigator.js index d7a19c14..ff9c7073 100644 --- a/js/components/ui/FilePicker/pickerNavigator.reel/pickerNavigator.js +++ b/js/components/ui/FilePicker/pickerNavigator.reel/pickerNavigator.js @@ -7,7 +7,6 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot var Montage = require("montage/core/core").Montage, Component = require("montage/ui/component").Component, iconsListModule = require("js/components/ui/icon-list-basic/iconsList.reel"), - filePickerControllerModule = require("js/components/ui/FilePicker/file-picker-controller"), treeModule = require("js/components/ui/tree-basic/tree.reel"); var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { @@ -335,7 +334,7 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { temp = temp.substring(0, temp.lastIndexOf("/")); //populate dropdown irrespective of validity -// if(!!filePickerControllerModule.FilePickerController._directoryContentCache[temp]){//check if it is a valid location +// if(!!this.application.ninja.filePickerController._directoryContentCache[temp]){//check if it is a valid location // arr.push(temp); // }else{ // break; @@ -358,7 +357,7 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { value:function(element, uri){ if(!!element){ var tree = treeModule.Tree.create(); - tree.treeViewDataObject = filePickerControllerModule.FilePickerController.prepareContentList(uri, this.pickerModel); + tree.treeViewDataObject = this.application.ninja.filePickerController.prepareContentList(uri, this.pickerModel); //console.log("renderTree() for "+ uri); //console.log(tree.treeViewDataObject); tree.element = element; @@ -374,10 +373,10 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { var status = true; var iconViewContainer = this.element.querySelector(".iconViewContainer"); if((typeof fromCache === 'undefined') || (fromCache === true)){ - this.newContent = filePickerControllerModule.FilePickerController.prepareContentList(uri, this.pickerModel); + this.newContent = this.application.ninja.filePickerController.prepareContentList(uri, this.pickerModel); } else{ - this.newContent = filePickerControllerModule.FilePickerController.prepareContentList(uri, this.pickerModel, false); + this.newContent = this.application.ninja.filePickerController.prepareContentList(uri, this.pickerModel, false); } if(!!this.newContent && this.newContent.length > 0){ //clear selection @@ -423,10 +422,10 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { if(!!treeViewContainer){ var data = []; if((typeof fromCache === 'undefined') || (fromCache === true)){ - data = filePickerControllerModule.FilePickerController.prepareContentList(uri, this.pickerModel); + data = this.application.ninja.filePickerController.prepareContentList(uri, this.pickerModel); } else{ - data = filePickerControllerModule.FilePickerController.prepareContentList(uri, this.pickerModel, false); + data = this.application.ninja.filePickerController.prepareContentList(uri, this.pickerModel, false); } if(data.length > 0){ @@ -475,7 +474,7 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { enumerable: false, writable:false, value:function(currentUri){ - var data = filePickerControllerModule.FilePickerController._directoryContentCache[currentUri]; + var data = this.application.ninja.filePickerController._directoryContentCache[currentUri]; var metadata = ""; if(!!data){ if(data.name !== ""){ @@ -610,8 +609,8 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { this.currentSelectedNode = null; } //enable OK button if the selection is valid as per the picker mode - if((this.pickerModel.inFileMode && (filePickerControllerModule.FilePickerController._directoryContentCache[uri].type === "file")) - || (!this.pickerModel.inFileMode && (filePickerControllerModule.FilePickerController._directoryContentCache[uri].type === "directory"))){ + if((this.pickerModel.inFileMode && (this.application.ninja.filePickerController._directoryContentCache[uri].type === "file")) + || (!this.pickerModel.inFileMode && (this.application.ninja.filePickerController._directoryContentCache[uri].type === "directory"))){ this.okButton.removeAttribute("disabled"); //put into selectedItems..currently single selection is supported @@ -784,9 +783,9 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { value: function(evt){ console.log("$$$ File Picker : selected "+ this.selectedItems.toString()); var success = true; - if(!!this.pickerModel.callback && !!this.pickerModel.callbackScope && (this.selectedItems.length > 0)){//call the callback if it is available + if(!!this.pickerModel.callback && (this.selectedItems.length > 0)){//call the callback if it is available try{ - this.pickerModel.callback.call(this.pickerModel.callbackScope, {"uri":this.selectedItems}); + this.pickerModel.callback({"uri":this.selectedItems}); }catch(e){ success = false; console.log("[Error] Failed to open "+ this.selectedItems.toString()); @@ -803,12 +802,12 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { var dataStore = window.sessionStorage; try { if(this.pickerModel.pickerMode === "write"){ - filePickerControllerModule.FilePickerController._lastSavedFolderURI.lastSavedFolderUri_local = this.currentURI; dataStore.setItem('lastSavedFolderURI',escape(""+this.currentURI)); } - else{ - filePickerControllerModule.FilePickerController._lastOpenedFolderURI.lastFolderUri_local = this.currentURI; - dataStore.setItem('lastOpenedFolderURI',escape(""+this.currentURI)); + else if(this.pickerModel.inFileMode === true){ + dataStore.setItem('lastOpenedFolderURI_fileSelection',escape(""+this.currentURI)); + }else if(this.pickerModel.inFileMode === false){ + dataStore.setItem('lastOpenedFolderURI_folderSelection',escape(""+this.currentURI)); } } catch(e){ @@ -962,9 +961,9 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { handlePickerNavRefreshTreeSegment:{ value: function(evt){ -// if(filePickerControllerModule.FilePickerController.checkIfStale(evt.uri)){ +// if(this.application.ninja.filePickerController.checkIfStale(evt.uri)){ // //update tree segment if was stale -// evt.treeSegment.treeViewDataObject = filePickerControllerModule.FilePickerController.prepareContentList(evt.uri, this.pickerModel, true, false); +// evt.treeSegment.treeViewDataObject = this.application.ninja.filePickerController.prepareContentList(evt.uri, this.pickerModel, true, false); // } } }, @@ -1007,7 +1006,7 @@ var PickerNavigator = exports.PickerNavigator = Montage.create(Component, { value:function(){ //clear memory - TODO:check for more memory leaks this.pickerModel = null; - filePickerControllerModule.FilePickerController._directoryContentCache = {}; + this.application.ninja.filePickerController._directoryContentCache = {}; //remove listeners this.element.removeEventListener("openFolder", this, false);//add icon double click event listener to reload iconList with new set of data this.element.removeEventListener("selectedItem", this, false);//for single selection only diff --git a/js/data/menu-data.js b/js/data/menu-data.js index 07337fa0..683be2dc 100755 --- a/js/data/menu-data.js +++ b/js/data/menu-data.js @@ -25,7 +25,7 @@ exports.MenuData = Montage.create( Montage, { "action": "executeNewFile" }, { - "displayText" : "Open...", + "displayText" : "Open File", "hasSubMenu" : false, "enabled": true, "action": "executeFileOpen" diff --git a/js/io/document/document-controller.js b/js/io/document/document-controller.js index e36181bf..6f67b57c 100755 --- a/js/io/document/document-controller.js +++ b/js/io/document/document-controller.js @@ -78,8 +78,9 @@ var DocumentController = exports.DocumentController = Montage.create(Component, handleExecuteFileOpen: { value: function(event) { var pickerSettings = event._event.settings || {}; - pickerSettings.callback = this.openFileWithURI; - pickerSettings.callbackScope = this; + pickerSettings.callback = this.openFileWithURI.bind(this); + pickerSettings.pickerMode = "read"; + pickerSettings.inFileMode = true; this.application.ninja.filePickerController.showFilePicker(pickerSettings); } }, @@ -87,8 +88,7 @@ var DocumentController = exports.DocumentController = Montage.create(Component, handleExecuteNewFile: { value: function(event) { var newFileSettings = event._event.settings || {}; - newFileSettings.callback = this.createNewFile; - newFileSettings.callbackScope = this; + newFileSettings.callback = this.createNewFile.bind(this); this.application.ninja.newFileController.showNewFileDialog(newFileSettings); } }, @@ -123,9 +123,9 @@ var DocumentController = exports.DocumentController = Montage.create(Component, */ openNewFileCallback:{ value:function(doc){ - var response = doc || {"uri":"/Users/xhdq84/Documents/test.js", "success":true};//default just for testing + var response = doc || null;//default just for testing if(!!response && response.success && !!response.uri){ - this.application.ninja.ioMediator.fileOpen({"uri":response.uri}, this.openFileCallback.bind(this)); + this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); } } }, @@ -137,8 +137,9 @@ var DocumentController = exports.DocumentController = Montage.create(Component, uri = uriArrayObj.uri[0]; } //console.log("URI is: ", uri); - - this.application.ninja.ioMediator.fileOpen({"uri":uri}, this.openFileCallback.bind(this)); + if(!!uri){ + this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); + } } }, @@ -151,8 +152,44 @@ var DocumentController = exports.DocumentController = Montage.create(Component, * uri : file uri */ openFileCallback:{ - value:function(doc){ - this.openDocument(doc); + 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."); + } + } }, diff --git a/js/io/templates/descriptor.json b/js/io/templates/descriptor.json index 20870934..acc03979 100644 --- a/js/io/templates/descriptor.json +++ b/js/io/templates/descriptor.json @@ -6,60 +6,60 @@ "name":"Blank File", "uri":"/", "type":"directory", - "children":["/js/io/templates/files/html.txt", "/js/io/templates/files/files/js.txt", "/js/io/templates/files/css.txt", "/js/io/templates/files/json.txt", "/js/io/templates/files/php.txt", "/js/io/templates/files/pl.txt", "/js/io/templates/files/py.txt", "/js/io/templates/files/rb.txt"] + "children":["js/io/templates/files/html.txt", "js/io/templates/files/js.txt", "js/io/templates/files/css.txt", "js/io/templates/files/json.txt", "js/io/templates/files/php.txt", "js/io/templates/files/pl.txt", "js/io/templates/files/py.txt", "js/io/templates/files/rb.txt"] }, - "/js/io/templates/files/html.txt":{ + "js/io/templates/files/html.txt":{ "name":"HTML", - "uri":"/js/io/templates/files/html.txt", + "uri":"js/io/templates/files/html.txt", "type":"file", "fileExtension":".html", "children":["defaultTemplate"] }, - "/js/io/templates/files/files/js.txt":{ + "js/io/templates/files/js.txt":{ "name":"JavaScript", - "uri":"/js/io/templates/files/files/js.txt", + "uri":"js/io/templates/files/js.txt", "type":"file", "fileExtension":".js", "children":["defaultTemplate"] }, - "/js/io/templates/files/css.txt":{ + "js/io/templates/files/css.txt":{ "name":"Cascading Style Sheets", - "uri":"/js/io/templates/files/css.txt", + "uri":"js/io/templates/files/css.txt", "type":"file", "fileExtension":".css", "children":["defaultTemplate"] }, - "/js/io/templates/files/json.txt":{ + "js/io/templates/files/json.txt":{ "name":"JSON", - "uri":"/js/io/templates/files/json.txt", + "uri":"js/io/templates/files/json.txt", "type":"file", "fileExtension":".json", "children":["defaultTemplate"] }, - "/js/io/templates/files/php.txt":{ + "js/io/templates/files/php.txt":{ "name":"PHP", - "uri":"/js/io/templates/files/php.txt", + "uri":"js/io/templates/files/php.txt", "type":"file", "fileExtension":".php", "children":["defaultTemplate"] }, - "/js/io/templates/files/pl.txt":{ + "js/io/templates/files/pl.txt":{ "name":"Perl", - "uri":"/js/io/templates/files/pl.txt", + "uri":"js/io/templates/files/pl.txt", "type":"file", "fileExtension":".pl", "children":["defaultTemplate"] }, - "/js/io/templates/files/py.txt":{ + "js/io/templates/files/py.txt":{ "name":"Python", - "uri":"/js/io/templates/files/py.txt", + "uri":"js/io/templates/files/py.txt", "type":"file", "fileExtension":".py", "children":["defaultTemplate"] }, - "/js/io/templates/files/rb.txt":{ + "js/io/templates/files/rb.txt":{ "name":"Ruby", - "uri":"/js/io/templates/files/rb.txt", + "uri":"js/io/templates/files/rb.txt", "type":"file", "fileExtension":".rb", "children":["defaultTemplate"] diff --git a/js/io/ui/new-file-dialog/new-file-options-navigator.reel/new-file-options-navigator.js b/js/io/ui/new-file-dialog/new-file-options-navigator.reel/new-file-options-navigator.js index bcb9d123..f514b81a 100644 --- a/js/io/ui/new-file-dialog/new-file-options-navigator.reel/new-file-options-navigator.js +++ b/js/io/ui/new-file-dialog/new-file-options-navigator.reel/new-file-options-navigator.js @@ -230,10 +230,10 @@ var NewFileOptionsNavigator = exports.NewFileOptionsNavigator = Montage.create(C ){ this.error.innerHTML=""; //console.log("$$$ new file selections: \n" + selectionlog); - if(!!this.newFileModel.callback && !!this.newFileModel.callbackScope){//inform document-controller if save successful - this.newFileModel.callback.call(this.newFileModel.callbackScope, {"fileTemplateUri":selectedProjectTypeID, - "newFilePath":newFilePath, - "fileExtension":fileExtension});//document-controller api + if(!!this.newFileModel.callback){//inform document-controller if save successful + this.newFileModel.callback({"fileTemplateUri":selectedProjectTypeID, + "newFilePath":newFilePath, + "fileExtension":fileExtension});//document-controller api }else{ //send selection event var newFileSelectionEvent = document.createEvent("Events"); diff --git a/js/io/ui/new-file-dialog/new-file-workflow-controller.js b/js/io/ui/new-file-dialog/new-file-workflow-controller.js index 6cf2a2ae..7b7f4572 100755 --- a/js/io/ui/new-file-dialog/new-file-workflow-controller.js +++ b/js/io/ui/new-file-dialog/new-file-workflow-controller.js @@ -44,7 +44,7 @@ var NewFileWorkflowController = exports.NewFileWorkflowController = Montage.cre this.model.projectTypeData = this.loadDescriptor("js/io/templates/descriptor.json"); //get default project type - this.model.defaultProjectType = "/js/io/templates/files/html.txt"; + this.model.defaultProjectType = "js/io/templates/files/html.txt"; this.model.callback = data.callback || null; this.model.callbackScope = data.callbackScope || null; -- cgit v1.2.3