This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from adobe/initial-fileio-work
Checking in initial fileIO.js implementation
- Loading branch information
Showing
4 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters