From 3a754133dbc138390503341fd2e9beba3e43aa4b Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 27 Jan 2012 12:05:17 -0800 Subject: Merged old FileIO --- .../ui/FilePicker/file-picker-controller.js | 106 +++++++++++---------- 1 file changed, 58 insertions(+), 48 deletions(-) mode change 100644 => 100755 js/components/ui/FilePicker/file-picker-controller.js (limited to 'js/components/ui/FilePicker/file-picker-controller.js') diff --git a/js/components/ui/FilePicker/file-picker-controller.js b/js/components/ui/FilePicker/file-picker-controller.js old mode 100644 new mode 100755 index 526578d1..f14857ac --- a/js/components/ui/FilePicker/file-picker-controller.js +++ b/js/components/ui/FilePicker/file-picker-controller.js @@ -21,38 +21,24 @@ var FilePickerController = exports.FilePickerController = Montage.create(require enumerable:true, value:function(){ var that = this; - this.eventManager.addEventListener("executeFileOpen", function(evt){ - var callback, pickerMode, currentFilter, allFileFilters,inFileMode, allowNewFileCreation, allowMultipleSelections; - - if(!!evt.callback){ - callback = evt.callback; - } - if(!!evt.pickerMode){ - pickerMode = evt.pickerMode; - } - if(!!evt.currentFilter){ - currentFilter = evt.currentFilter; - } - if(!!evt.inFileMode){ - inFileMode = evt.inFileMode; - } - if(!!evt.allFileFilters){ - allFileFilters = evt.allFileFilters; + this.eventManager.addEventListener("openFilePicker", function(evt){ + var settings; + if(typeof evt._event.settings !== "undefined"){ + settings = evt._event.settings; } - if(!!evt.allowNewFileCreation){ - allowNewFileCreation = evt.allowNewFileCreation; - } - if(!!evt.allowMultipleSelections){ - allowMultipleSelections = evt.allowMultipleSelections; - } - - that.showFilePicker(callback, pickerMode, currentFilter, allFileFilters,inFileMode, allowNewFileCreation, allowMultipleSelections); - + that.showFilePicker(settings); }, false); + } }, + filePickerPopupType:{ + writable: true, + enumerable: false, + value: "filePicker" + }, + /** * this will be stored in the local storage and in the cloud may be, for the cloud one. */ @@ -81,14 +67,16 @@ var FilePickerController = exports.FilePickerController = Montage.create(require *this function is used to create an instance of a file picker * * parameters: - * callback: the call back function which will be used to send the selected URIs back - * pickerMode: ["read", "write"] : specifies if the file picker is opened to read a file/folder or to save a file - * currentFilter: if a current filter needs to be applied [ex: .psd] - * allFileFilters: list of filters that user can use to filter the view - * inFileMode: true => allow file selection , false => allow directory selection - * allowNewFileCreation: flag to specify whether or not it should return URI(s) to item(s) that do not exist. i.e. a user can type a filename to a new file that doesn't yet exist in the file system. - * allowMultipleSelections: allowMultipleSelections - *rootDirectories: invoker of this function can mention a subset of the allowed root directories to show in the file picker + * settings is an object containing : + * callback [optional]: the call back function which will be used to send the selected URIs back. If undefined then an event is fired with the selected uri + * callbackScope : required if callback is set + * pickerMode [optional]: ["read", "write"] : specifies if the file picker is opened to read a file/folder or to save a file + * currentFilter [optional]: if a current filter needs to be applied [ex: .psd] + * allFileFilters [optional]: list of filters that user can use to filter the view + * inFileMode [optional]: true => allow file selection , false => allow directory selection + * allowNewFileCreation [optional]: flag to specify whether or not it should return URI(s) to item(s) that do not exist. i.e. a user can type a filename to a new file that doesn't yet exist in the file system. + * allowMultipleSelections [optional]: allowMultipleSelections + * pickerName: name for montage custom popup * * return: none */ @@ -96,7 +84,19 @@ var FilePickerController = exports.FilePickerController = Montage.create(require showFilePicker:{ writable:false, enumerable:true, - value:function(callback, pickerMode, currentFilter, allFileFilters,inFileMode, allowNewFileCreation, allowMultipleSelections){ + value:function(settings){ + 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;} + if(typeof settings.inFileMode !== "undefined"){inFileMode = settings.inFileMode;} + if(typeof settings.allowNewFileCreation !== "undefined"){allowNewFileCreation = settings.allowNewFileCreation;} + if(typeof settings.allowMultipleSelections !== "undefined"){allowMultipleSelections = settings.allowMultipleSelections;} + if(typeof settings.pickerName !== "undefined"){this.filePickerPopupType = settings.pickerName;} + } var aModel = filePickerModelModule.FilePickerModel.create(); @@ -114,8 +114,17 @@ var FilePickerController = exports.FilePickerController = Montage.create(require aModel.fatalError = " ** Unable to get files [Error: "+ errorCause +"]"; } - aModel.currentFilter = currentFilter; - aModel.inFileMode = inFileMode; + //dummy data - TODO:remove after testing + //aModel.currentFilter = "*.html, *.png"; + //aModel.currentFilter = "*.jpg"; + aModel.currentFilter = "*.*"; + aModel.inFileMode = true; + aModel.fileFilters = [".html, .htm", ".jpg, .jpeg, .png, .gif", ".js, .json", ".css", ".txt, .rtf", ".doc, .docx", ".pdf", ".avi, .mov, .mpeg, .ogg, .webm", "*.*"]; + //-end - dummy data + + if(!!currentFilter){aModel.currentFilter = currentFilter;} + if(typeof inFileMode !== "undefined"){aModel.inFileMode = inFileMode;} + aModel.topLevelDirectories = topLevelDirectories; if(!!topLevelDirectories && !!topLevelDirectories[0]){ @@ -136,17 +145,12 @@ var FilePickerController = exports.FilePickerController = Montage.create(require aModel.currentRoot = unescape(storedUri); } - aModel.fileFilters = allFileFilters; - aModel.callback = callback; - aModel.pickerMode = pickerMode; + if(!!allFileFilters){aModel.fileFilters = allFileFilters;} + if(!!callback){aModel.callback = callback;} + if(!!callbackScope){aModel.callbackScope = callbackScope;} + if(typeof pickerMode !== "undefined"){aModel.pickerMode = pickerMode;} + - //dummy data - TODO:remove after testing - //aModel.currentFilter = "*.html, *.png"; - //aModel.currentFilter = "*.jpg"; - aModel.currentFilter = "*.*"; - aModel.inFileMode = true; - aModel.fileFilters = [".html, .htm", ".jpg, .jpeg, .png, .gif", ".js, .json", ".css", ".txt, .rtf", ".doc, .docx", ".pdf", ".avi, .mov, .mpeg, .ogg, .webm", "*.*"]; - //-end - dummy data //logic: get file content data onDemand from the REST api for the default or last opened root. Cache the data in page [in local cache ? dirty fs? ]. Filter on client side to reduce network calls. this.openFilePickerAsModal(callback, aModel); @@ -172,6 +176,12 @@ var FilePickerController = exports.FilePickerController = Montage.create(require var pickerNavChoices = Montage.create(pickerNavigatorReel); var initUri = aModel.currentRoot; + + //remove extra / at the end + if((initUri.length > 1) && (initUri.charAt(initUri.length - 1) === "/")){ + initUri = initUri.substring(0, (initUri.length - 1)); + } + pickerNavChoices.mainContentData = this.prepareContentList(initUri, aModel); pickerNavChoices.pickerModel = aModel; pickerNavChoices.element = pickerNavContent; @@ -182,8 +192,8 @@ var FilePickerController = exports.FilePickerController = Montage.create(require var popup = Popup.create(); popup.content = pickerNavChoices; popup.modal = true; + popup.type = this.filePickerPopupType;//should be set always to not default to the single custom popup layer popup.show(); - pickerNavChoices.popup = popup;//handle to be used for hiding the popup } }, -- cgit v1.2.3 From fcd9d2f2fd63c11160fcabdc8a554a1b2a81cc47 Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Mon, 30 Jan 2012 14:41:56 -0800 Subject: calling coreioapi.js directly since filesystem.js will be deleted Signed-off-by: Ananya Sen --- js/components/ui/FilePicker/file-picker-controller.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'js/components/ui/FilePicker/file-picker-controller.js') diff --git a/js/components/ui/FilePicker/file-picker-controller.js b/js/components/ui/FilePicker/file-picker-controller.js index f14857ac..635da3a2 100755 --- a/js/components/ui/FilePicker/file-picker-controller.js +++ b/js/components/ui/FilePicker/file-picker-controller.js @@ -8,7 +8,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot var Montage = require("montage/core/core").Montage, pickerNavigatorReel = require("js/components/ui/FilePicker/pickerNavigator.reel").PickerNavigator, filePickerModelModule = require("js/components/ui/FilePicker/file-picker-model"), - fileSystem = require("js/io/system/filesystem").FileSystem, + fileSystem = require("js/io/system/coreioapi").CoreIoApi, Popup = require("montage/ui/popup/popup.reel").Popup; //singleton with functions to create a new file picker instance and utilities to format or filter the model data @@ -101,7 +101,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require var aModel = filePickerModelModule.FilePickerModel.create(); var topLevelDirectories = null; - var driveData = fileSystem.shellApiHandler.getDirectoryContents({uri:"", recursive:false, returnType:"all"}); + var driveData = fileSystem.getDirectoryContents({uri:"", recursive:false, returnType:"all"}); if(driveData.success){ topLevelDirectories = (JSON.parse(driveData.content)).children; }else{ @@ -244,7 +244,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require || !this._directoryContentCache[folderUri].children){ //get data using IO api try{ - var iodata = fileSystem.shellApiHandler.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); + var iodata = fileSystem.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ folderContent = JSON.parse(iodata.content); @@ -456,7 +456,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require //check for directory staleness.... if stale query filesystem if((new Date()).getTime() > (this._directoryContentCache[folderUri].queriedTimeStamp + this.cacheStaleTime)){ try{ - var ifModifiedResponse = fileSystem.shellApiHandler.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp); + var ifModifiedResponse = fileSystem.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp); //console.log("ifModifiedResponse"); //console.log(ifModifiedResponse); }catch(e){ @@ -468,7 +468,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require wasStale = true; //uri has changed. so update cache try{ - var iodata = fileSystem.shellApiHandler.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); + var iodata = fileSystem.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ folderContent = JSON.parse(iodata.content); -- cgit v1.2.3 From 553fce7721cacfd13b6013fdcdd0243c90083b5e Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Wed, 1 Feb 2012 11:59:11 -0800 Subject: fixed reference to coreioapi cleaning up opening code view tabs Signed-off-by: Ananya Sen --- js/components/ui/FilePicker/file-picker-controller.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'js/components/ui/FilePicker/file-picker-controller.js') diff --git a/js/components/ui/FilePicker/file-picker-controller.js b/js/components/ui/FilePicker/file-picker-controller.js index 635da3a2..e71d1460 100755 --- a/js/components/ui/FilePicker/file-picker-controller.js +++ b/js/components/ui/FilePicker/file-picker-controller.js @@ -8,7 +8,6 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot var Montage = require("montage/core/core").Montage, pickerNavigatorReel = require("js/components/ui/FilePicker/pickerNavigator.reel").PickerNavigator, filePickerModelModule = require("js/components/ui/FilePicker/file-picker-model"), - fileSystem = require("js/io/system/coreioapi").CoreIoApi, Popup = require("montage/ui/popup/popup.reel").Popup; //singleton with functions to create a new file picker instance and utilities to format or filter the model data @@ -101,7 +100,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require var aModel = filePickerModelModule.FilePickerModel.create(); var topLevelDirectories = null; - var driveData = fileSystem.getDirectoryContents({uri:"", recursive:false, returnType:"all"}); + var driveData = this.application.ninja.coreIoApi.getDirectoryContents({uri:"", recursive:false, returnType:"all"}); if(driveData.success){ topLevelDirectories = (JSON.parse(driveData.content)).children; }else{ @@ -244,7 +243,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require || !this._directoryContentCache[folderUri].children){ //get data using IO api try{ - var iodata = fileSystem.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); + var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ folderContent = JSON.parse(iodata.content); @@ -456,7 +455,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require //check for directory staleness.... if stale query filesystem if((new Date()).getTime() > (this._directoryContentCache[folderUri].queriedTimeStamp + this.cacheStaleTime)){ try{ - var ifModifiedResponse = fileSystem.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp); + var ifModifiedResponse = this.application.ninja.coreIoApi.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp); //console.log("ifModifiedResponse"); //console.log(ifModifiedResponse); }catch(e){ @@ -468,7 +467,7 @@ var FilePickerController = exports.FilePickerController = Montage.create(require wasStale = true; //uri has changed. so update cache try{ - var iodata = fileSystem.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); + var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ folderContent = JSON.parse(iodata.content); -- cgit v1.2.3 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 --- .../ui/FilePicker/file-picker-controller.js | 50 ++++++++-------------- 1 file changed, 19 insertions(+), 31 deletions(-) (limited to 'js/components/ui/FilePicker/file-picker-controller.js') 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){ -- cgit v1.2.3 From a9c369c2e5d8f6fc8d936f7e1e6b84f693226ddf Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Thu, 16 Feb 2012 13:11:48 -0800 Subject: moved file picker in file io Signed-off-by: Ananya Sen --- .../ui/FilePicker/file-picker-controller.js | 494 --------------------- 1 file changed, 494 deletions(-) delete mode 100755 js/components/ui/FilePicker/file-picker-controller.js (limited to 'js/components/ui/FilePicker/file-picker-controller.js') diff --git a/js/components/ui/FilePicker/file-picker-controller.js b/js/components/ui/FilePicker/file-picker-controller.js deleted file mode 100755 index 0ff20cc8..00000000 --- a/js/components/ui/FilePicker/file-picker-controller.js +++ /dev/null @@ -1,494 +0,0 @@ -/* -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, - pickerNavigatorReel = require("js/components/ui/FilePicker/pickerNavigator.reel").PickerNavigator, - filePickerModelModule = require("js/components/ui/FilePicker/file-picker-model"), - Popup = require("montage/ui/popup/popup.reel").Popup; - -//singleton with functions to create a new file picker instance and utilities to format or filter the model data -var FilePickerController = exports.FilePickerController = Montage.create(require("montage/ui/component").Component, { - /** - * Register a listener for file open event - */ - deserializedFromTemplate:{ - writable:false, - enumerable:true, - value:function(){ - var that = this; - - this.eventManager.addEventListener("openFilePicker", function(evt){ - var settings; - if(typeof evt._event.settings !== "undefined"){ - settings = evt._event.settings; - } - that.showFilePicker(settings); - }, false); - - } - }, - - filePickerPopupType:{ - writable: true, - enumerable: false, - value: "filePicker" - }, - - /** - *this function is used to create an instance of a file picker - * - * parameters: - * settings is an object containing : - * callback [optional]: the call back function which will be used to send the selected URIs back. If undefined then an event is fired with the selected uri - * callbackScope : required if callback is set - * pickerMode [optional]: ["read", "write"] : specifies if the file picker is opened to read a file/folder or to save a file - * currentFilter [optional]: if a current filter needs to be applied [ex: .psd] - * allFileFilters [optional]: list of filters that user can use to filter the view - * inFileMode [optional]: true => allow file selection , false => allow directory selection - * allowNewFileCreation [optional]: flag to specify whether or not it should return URI(s) to item(s) that do not exist. i.e. a user can type a filename to a new file that doesn't yet exist in the file system. - * allowMultipleSelections [optional]: allowMultipleSelections - * pickerName: name for montage custom popup - * - * return: none - */ - - showFilePicker:{ - writable:false, - enumerable:true, - value:function(settings){ - var callback, callbackScope, pickerMode, currentFilter, allFileFilters, inFileMode, allowNewFileCreation, allowMultipleSelections, pickerName; - if(!!settings){ - if(typeof settings.callback !== "undefined"){callback = settings.callback;} - if(typeof settings.pickerMode !== "undefined"){pickerMode = settings.pickerMode;} - if(typeof settings.currentFilter !== "undefined"){currentFilter = settings.currentFilter;} - if(typeof settings.allFileFilters !== "undefined"){allFileFilters = settings.allFileFilters;} - if(typeof settings.inFileMode !== "undefined"){inFileMode = settings.inFileMode;} - if(typeof settings.allowNewFileCreation !== "undefined"){allowNewFileCreation = settings.allowNewFileCreation;} - if(typeof settings.allowMultipleSelections !== "undefined"){allowMultipleSelections = settings.allowMultipleSelections;} - 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; - var driveData = this.application.ninja.coreIoApi.getDirectoryContents({uri:"", recursive:false, returnType:"all"}); - if(driveData.success){ - topLevelDirectories = (JSON.parse(driveData.content)).children; - }else{ - var errorCause = ""; - if(driveData.status === null){ - errorCause = "Service Unavailable" - }else{ - errorCause = driveData.status; - } - aModel.fatalError = " ** Unable to get files [Error: "+ errorCause +"]"; - } - - //dummy data - TODO:remove after testing - //aModel.currentFilter = "*.html, *.png"; - //aModel.currentFilter = "*.jpg"; - aModel.currentFilter = "*.*"; - aModel.inFileMode = true; - aModel.fileFilters = [".html, .htm", ".jpg, .jpeg, .png, .gif", ".js, .json", ".css", ".txt, .rtf", ".doc, .docx", ".pdf", ".avi, .mov, .mpeg, .ogg, .webm", "*.*"]; - //-end - dummy data - - if(!!currentFilter){aModel.currentFilter = currentFilter;} - if(typeof inFileMode !== "undefined"){aModel.inFileMode = inFileMode;} - - aModel.topLevelDirectories = topLevelDirectories; - - if(!!topLevelDirectories && !!topLevelDirectories[0]){ - aModel.currentRoot = topLevelDirectories[0].uri; - } - - //populate the last opened folder first, if none then populate default root - var storedUri = null; - 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){ - aModel.currentRoot = unescape(storedUri); - } - - if(!!allFileFilters){aModel.fileFilters = allFileFilters;} - if(!!callback){aModel.callback = callback;} - if(!!callbackScope){aModel.callbackScope = callbackScope;} - if(typeof pickerMode !== "undefined"){aModel.pickerMode = pickerMode;} - - - - //logic: get file content data onDemand from the REST api for the default or last opened root. Cache the data in page [in local cache ? dirty fs? ]. Filter on client side to reduce network calls. - this.openFilePickerAsModal(callback, aModel); - - - //to open this on another modal dialog, make it a popup instead above the modal dialog container layer - - } - }, - - openFilePickerAsModal:{ - writable:false, - enumerable:true, - value:function(callback, aModel){ - //render modal dialog - var pickerNavContent = document.createElement("div"); - pickerNavContent.id = "filePicker"; - - pickerNavContent.style.color = "#fff"; - - //hack (elements needs to be on DOM to be drawn) - document.getElementById('modalContainer').appendChild(pickerNavContent); - - var pickerNavChoices = Montage.create(pickerNavigatorReel); - var initUri = aModel.currentRoot; - - //remove extra / at the end - if((initUri.length > 1) && (initUri.charAt(initUri.length - 1) === "/")){ - initUri = initUri.substring(0, (initUri.length - 1)); - } - - pickerNavChoices.mainContentData = this.prepareContentList(initUri, aModel); - pickerNavChoices.pickerModel = aModel; - pickerNavChoices.element = pickerNavContent; - - //hack - remove after rendering and add in modal dialog - document.getElementById('modalContainer').removeChild(pickerNavContent); - - var popup = Popup.create(); - popup.content = pickerNavChoices; - popup.modal = true; - popup.type = this.filePickerPopupType;//should be set always to not default to the single custom popup layer - popup.show(); - pickerNavChoices.popup = popup;//handle to be used for hiding the popup - } - }, - openFilePickerAsPopup:{ - writable:false, - enumerable:true, - value:function(){} - }, - - expandDirectory:{ - writable:false, - enumerable:true, - value: function(root, currentFilter, inFileMode){ - //populate children in dom - } - }, - - refreshDirectoryCache:{ - writable:false, - enumerable:true, - value:function(directoryUri){ - if(directoryContentCache[directoryUri] !== null){ - directoryContentCache[directoryUri] = null; //invalidate the cached content - //fetch fresh content - } - } - }, - - /** - * queries the cache to build contents array. If not found queries the file system - * - * parameters: - * folderUri - * aModel: model instance per picker instance, containing - */ - - prepareContentList:{ - writable: false, - enumerable:true, - value:function(folderUri, aModel, fromCache, checkStaleness){ - var contentList = [], - childrenArray = []; - - var folderContent = null; - // query filesystem and populate cache - if(((typeof fromCache !== "undefined") && (fromCache === false)) - || !this._directoryContentCache[folderUri] - || !this._directoryContentCache[folderUri].children){ - //get data using IO api - try{ - var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); - //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); - if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ - folderContent = JSON.parse(iodata.content); - } - }catch(e){ - console.error("Error to IO uri: "+folderUri+"\n"+e.message); - } - - if(!!folderContent){ - //contentList = folderContent.children;//need to apply filters and mode - this.cacheContentForRandomAccess(folderUri, folderContent); - } - } - //now from cache - apply filters and mode - if((!!this._directoryContentCache[folderUri]) - && (this._directoryContentCache[folderUri].type === "directory") - && (typeof this._directoryContentCache[folderUri].children !== "undefined") - && (this._directoryContentCache[folderUri].children !== null)){ - - //console.log("$$$ this._directoryContentCache"); - //console.log(this._directoryContentCache); - - //check for directory staleness.... if stale query filesystem - if((typeof checkStaleness === "undefined") || (checkStaleness === true)){ - this.checkIfStale(folderUri); - } - - childrenArray = this._directoryContentCache[folderUri].children; - - //prepare content array for folder uri - childrenArray.forEach(function(item){ - if(this._directoryContentCache[item]){ - //apply mode and filtering here - if(aModel.inFileMode){// if in file selection mode, do filtering - if((this._directoryContentCache[item].type === "directory") || !aModel.currentFilter){//no filetering - contentList.push(this._directoryContentCache[item]); - } - else if(aModel.currentFilter){ - if(this.applyFilter(this._directoryContentCache[item].name, aModel.currentFilter)){ - contentList.push(this._directoryContentCache[item]); - } - } - }else{// if in folder selection mode - if(this._directoryContentCache[item].type === "directory"){ - contentList.push(this._directoryContentCache[item]); - } - } - } - }, this); - } - else if((typeof this._directoryContentCache[folderUri] !== 'undefined') && (this._directoryContentCache[folderUri].type === "file")){//if the uri is for a file - - //check for directory staleness.... if stale query filesystem - if((typeof checkStaleness === "undefined") || (checkStaleness === true)){ - this.checkIfStale(folderUri); - } - - contentList.push(this._directoryContentCache[folderUri]); - } - //end - from cache - - return contentList; - } - }, - - /** - * populates/updates cache for a uri - */ - cacheContentForRandomAccess:{ - writable:false, - enumerable:true, - value: function(directoryUri, directoryContents){ - - var that = this; - //assumption: directoryContents will have only its direct files and subfolders - //uri is the unique identifier - - - //check if the directoryUri exists in cache - //if not add uri content object, prepare children's uri array,then add/update objects for children - if(!this._directoryContentCache[directoryUri]){//uri not in cache... so add it - //add uri content object - this._directoryContentCache[directoryUri] = {"type":directoryContents.type,"name":directoryContents.name,"uri":directoryUri}; - if(!!directoryContents.size){ - this._directoryContentCache[directoryUri].size = directoryContents.size; - } - if(!!directoryContents.creationDate){ - this._directoryContentCache[directoryUri].creationDate = directoryContents.creationDate; - } - if(!!directoryContents.modifiedDate){ - this._directoryContentCache[directoryUri].modifiedDate = directoryContents.modifiedDate; - } - - //store the current queried time for refreshing cache logic - this._directoryContentCache[directoryUri].queriedTimeStamp = (new Date()).getTime(); - - if(!!directoryContents.children && directoryContents.children.length > 0){ - this._directoryContentCache[directoryUri].children = []; - - //add the uri to this._directoryContentCache[directoryUri].children, and add the child's description objects - directoryContents.children.forEach(function(obj){ - //add uri to parent's children list - that._directoryContentCache[directoryUri].children.push(obj.uri); - //add the child object - that._directoryContentCache[obj.uri] = obj; - - //store the current queried time for refreshing cache logic - that._directoryContentCache[obj.uri].queriedTimeStamp = (new Date()).getTime(); - - } ,this); - } - }else{//uri in cache... so update it AND its children - this._directoryContentCache[directoryUri].type = directoryContents.type; - this._directoryContentCache[directoryUri].name = directoryContents.name; - if(!!directoryContents.size){ - this._directoryContentCache[directoryUri].size = directoryContents.size; - } - if(!!directoryContents.creationDate){ - this._directoryContentCache[directoryUri].creationDate = directoryContents.creationDate; - } - if(!!directoryContents.modifiedDate){ - this._directoryContentCache[directoryUri].modifiedDate = directoryContents.modifiedDate; - } - - //store the current queried time for refreshing cache logic - this._directoryContentCache[directoryUri].queriedTimeStamp = (new Date()).getTime(); - - if(!!directoryContents.children && directoryContents.children.length > 0){ - - // logic to clear off objects from cache if they no longer exist in the filesystem - //better logic - use isUpdatedFlag for a folder .. then compare modified date - - //hack for now - clear up the old children and add new ones - var tempArr = this._directoryContentCache[directoryUri].children; - if( !!tempArr && Array.isArray(tempArr) && tempArr.length>0){ - tempArr.forEach(function(uriString){ - if(!!that._directoryContentCache[uriString]){ - delete that._directoryContentCache[uriString]; - } - }); - } - - this._directoryContentCache[directoryUri].children = []; - - //add the uri to this._directoryContentCache[directoryUri].children, and add the child's description objects - directoryContents.children.forEach(function(obj){ - //add uri to parent's children list - that._directoryContentCache[directoryUri].children.push(obj.uri); - //add the child object - that._directoryContentCache[obj.uri] = obj; - //store the current queried time for refreshing cache logic - that._directoryContentCache[obj.uri].queriedTimeStamp = (new Date()).getTime(); - } ,this); - }else{ - this._directoryContentCache[directoryUri].children = []; - } - } - - //console.log("$$$ "+directoryUri+" modifiedDate = "+this._directoryContentCache[directoryUri].modifiedDate); - //console.log("$$$ "+directoryUri+" queriedTimeStamp = "+this._directoryContentCache[directoryUri].queriedTimeStamp); - } - }, - - applyFilter:{ - writable: false, - enumerable:true, - value:function(fileName , filters){ - - if(filters.indexOf("*.*") !== -1){return true;} - - //console.log(""+fileName); - var filtersArr = filters.split(","); - var passed = false; - for(var i=0; i< filtersArr.length; i++){ - filtersArr[i] = filtersArr[i].trim(); - //console.log(filtersArr[i]); - var fileType = filtersArr[i].substring(filtersArr[i].indexOf(".") ); - //console.log(""+fileType); - - //ignore uppercase - fileName = fileName.toLowerCase(); - fileType = fileType.toLowerCase(); - - if(fileName.indexOf(fileType, fileName.length - fileType.length) !== -1){//ends with file type - passed = true; - break; - } - } - return passed; - } - }, - - /** - * Stale Time (ms) for each resource - * Logic: the last queried time for a resource is compared to stale time. If stale, then file system is queried - */ - cacheStaleTime:{ - writable: false, - enumerable: false, - value: 5000 - }, - - checkIfStale: { - writable: false, - enumerable: true, - value: function(folderUri){ - var wasStale = false; - var folderContent = null; - //check for directory staleness.... if stale query filesystem - if((new Date()).getTime() > (this._directoryContentCache[folderUri].queriedTimeStamp + this.cacheStaleTime)){ - try{ - var ifModifiedResponse = this.application.ninja.coreIoApi.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp); - //console.log("ifModifiedResponse"); - //console.log(ifModifiedResponse); - }catch(e){ - console.error("Error to IO uri with isDirectoryModified: "+folderUri+"\n"+e.message); - } - if(ifModifiedResponse && ifModifiedResponse.status === 304){ - //do nothing since the uri has not changed - }else if(ifModifiedResponse && (ifModifiedResponse.status === 200)){ - wasStale = true; - //uri has changed. so update cache - try{ - var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"}); - //console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content); - if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){ - folderContent = JSON.parse(iodata.content); - } - }catch(e){ - console.error("Error to IO uri: "+folderUri+"\n"+e.message); - } - - if(!!folderContent){ - this.cacheContentForRandomAccess(folderUri, folderContent); - } - } - } - - return wasStale; - } - }, - - /** - * This will store the directory content per session - * check session storage for this - */ - _directoryContentCache:{ - writable:true, - enumerable:false, - value:{} - }, - - clearCache:{ - writable:false, - enumerable: true, - value: function(){ - this._directoryContentCache = {}; - } - } -}); \ No newline at end of file -- cgit v1.2.3