diff --git a/src/NativeFileSystem.js b/src/NativeFileSystem.js new file mode 100644 index 00000000000..1efedc348e4 --- /dev/null +++ b/src/NativeFileSystem.js @@ -0,0 +1,169 @@ +window.NativeFileSystem = { + + + /** showOpenDialog + * + * @param {bool} allowMultipleSelection + * @param {bool} chooseDirectories + * @param {string} title + * @param {string} initialPath + * @param {string[]} fileTypes + * @param {function} resultCallback + * @constructor + */ + showOpenDialog: function ( allowMultipleSelection, + chooseDirectories, + title, + initialPath, + fileTypes, + resultCallback ) { + + if( !resultCallback ) + return null; + + var files = brackets.file.showOpenDialog( allowMultipleSelection, + chooseDirectories, + title, + initialPath, + fileTypes, + resultCallback ); + }, + + + /** requestNativeFileSystem + * + * @param {string} path + * @param {function} successCallback + * @param {function} errorCallback + */ + requestNativeFileSystem: function( path, successCallback, errorCallback ){ + + // TODO: assumes path is a directory right now. Need to error check + // TODO: don't actually need to get the listing here, but should verify the directory exists + var entryList = brackets.file.getDirectoryListing(path); + if (entryList) { + var files = JSON.parse(entryList); + var root = new DirectoryEntry( path ); + return root; + } + else { + return null; + } + } +}; + + +/** class: Entry + * + * @param {string} name + * @param {string} isFile + * @constructor + */ +Entry = function( fullPath, isDirectory) { + this.isDirectory = isDirectory; + this.isFile = !isDirectory; + // IMPLEMENT LATER void getMetadata (MetadataCallback successCallback, optional ErrorCallback errorCallback); + this.fullPath = fullPath; + + // Extract name from fullPath + this.name = null; // default if extraction fails + if( fullPath ){ + var pathParts = fullPath.split( "/" ); + if( pathParts.length > 0 ) + this.name = pathParts.pop(); + } + + // IMPLEMENT LATERvar filesystem; + // IMPLEMENT LATER void moveTo (DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATER void copyTo (DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATER DOMString toURL (optional DOMString mimeType); + // IMPLEMENT LATER void remove (VoidCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATER void getParent (EntryCallback successCallback, optional ErrorCallback errorCallback); +}; + + + +/** class: FileEntry + * + * @param {string} name + * @constructor + * @extends {Entry} + */ +FileEntry = function( name ) { + Entry.call(this, name, false); + + // TODO: make FileEntry actually inherit from Entry by modifying prototype. I don't know how to do this yet. + + // IMPLEMENT LATER void createWriter (FileWriterCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATER void file (FileCallback successCallback, optional ErrorCallback errorCallback); +}; + + +/** class: DirectoryEntry + * + * @param {string} name + * @constructor + * @extends {Entry} + */ +DirectoryEntry = function( name ) { + Entry.call(this, name, true); + + // TODO: make DirectoryEntry actually inherit from Entry by modifying prototype. I don't know how to do this yet. + + // IMPLEMENT LATERvoid getFile (DOMString path, optional Flags options, optional EntryCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATERvoid getDirectory (DOMString path, optional Flags options, optional EntryCallback successCallback, optional ErrorCallback errorCallback); + // IMPLEMENT LATERvoid removeRecursively (VoidCallback successCallback, optional ErrorCallback errorCallback); +}; + + +DirectoryEntry.prototype.createReader = function() { + var dirReader = new DirectoryReader(); + dirReader._directory = this; + + return dirReader; +}; + + +/** class: DirectoryReader + */ +DirectoryReader = function() { + +}; + + +/** readEntires + * + * @param {function} successCallback + * @param {function} errorCallback + * @returns {Entry[]} + */ +DirectoryReader.prototype.readEntries = function( successCallback, errorCallback ){ + var rootPath = this._directory.fullPath; + var jsonList = brackets.file.getDirectoryListing( rootPath ); + var nameList = JSON.parse(jsonList); + + // Create entries for each name + var entries = []; + nameList.forEach(function(item){ + // Ignore names starting with "." + if (item.indexOf(".") != 0) { + var itemFullPath = rootPath + "/" + item; + + if( brackets.file.isDirectory( itemFullPath ) ) { + entries.push( new DirectoryEntry( itemFullPath ) ); + } + else { + entries.push( new FileEntry( itemFullPath ) ); + } + } + }); + + + + successCallback( entries ); + + // TODO: error handling +}; + + + diff --git a/src/brackets.js b/src/brackets.js index 5057bd40324..ce60f62e3a5 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -1,9 +1,66 @@ /* TODO: copyright notice, etc. */ $(document).ready(function() { - var myCodeMirror = CodeMirror($('#editor').get(0), { - value: 'var myResponse="Yes, it will be!"\n' - }); - // TODO: Write Brackets. + var myCodeMirror = CodeMirror($('#editor').get(0), { + value: 'var myResponse="Yes, it will be!"\n' + }); + + // Set the "inBrowser" flag + var inBrowser = !window.hasOwnProperty("brackets"); + + + // Temporary button to test file directory traversa; + $("#menu-file-open").click(function(){ + if (!inBrowser) { + window.NativeFileSystem.showOpenDialog(false, true, "Choose a folder", null, null, showOpenDialogCallback); + } + }); + + function showOpenDialogCallback( files ) { + var folderName = files instanceof Array ? files[0] : files; + + if (folderName != "") { + var rootEntry = window.NativeFileSystem.requestNativeFileSystem( folderName, null, null ); // TODO: add callbacks + + var nestingLevel = 0; + + if( rootEntry && rootEntry.isDirectory ) + readDirectory( rootEntry ); + } + + + // Test directory traversal + function readDirectory( entry ){ + var reader = entry.createReader(); + reader.readEntries( dirReaderSuccessCB, dirReaderErrorCB); + } + + function dirReaderSuccessCB( entries ){ + var tabs = ""; + for( i = 0; i < nestingLevel; i++ ){ + tabs += " "; + } + + for ( var entryI in entries ){ + var entry = entries[entryI]; + if( entry.isFile ){ + // create leaf tree node using entry.name + console.log( tabs+ entry.name ); + } + else if ( entry.isDirectory ){ + // create branch tree node using entry.name + console.log( tabs + entry.name ); + + nestingLevel++; + readDirectory( entry ) + } + } + } + + function dirReaderErrorCB() { + // handle error + } + } + }); diff --git a/src/index.html b/src/index.html index 3b321a49572..32e8a3be90e 100644 --- a/src/index.html +++ b/src/index.html @@ -18,6 +18,7 @@ + @@ -32,10 +33,10 @@ diff --git a/test/SpecRunner.html b/test/SpecRunner.html index e3e36ca28f6..88751e6932b 100644 --- a/test/SpecRunner.html +++ b/test/SpecRunner.html @@ -11,7 +11,7 @@ - +