Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from adobe/initial-fileio-work
Browse files Browse the repository at this point in the history
Checking in initial fileIO.js implementation
  • Loading branch information
Narciso Jaramillo committed Dec 13, 2011
2 parents f56ed50 + 293c2c2 commit 25d55b3
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 9 deletions.
169 changes: 169 additions & 0 deletions src/NativeFileSystem.js
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
};



65 changes: 61 additions & 4 deletions src/brackets.js
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
}
}

});
9 changes: 5 additions & 4 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<script src="thirdparty/jquery-1.7.min.js"></script>
<script src="widgets/bootstrap-dropdown.js"></script>

<script src="NativeFileSystem.js"></script>
<script src="brackets.js"></script>
</head>
<body>
Expand All @@ -32,10 +33,10 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle">File</a>
<ul class="dropdown-menu">
<li><a href="#">New</a></li>
<li><a href="#">Open</a></li>
<li><a href="#">Save</a></li>
<li><a href="#">Quit</a></li>
<li><a href="#" id="menu-file-new">New</a></li>
<li><a href="#" id="menu-file-open">Open</a></li>
<li><a href="#" id="menu-file-save">Save</a></li>
<li><a href="#" id="menu-file-quit">Quit</a></li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<!-- include spec files here... -->
<script type="text/javascript" src="spec/SampleTest.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="../src/thirdparty/CodeMirror2/lib/CodeMirror.js"></script>
<script type="text/javascript" src="../src/thirdparty/CodeMirror2/mode/javascript/javascript.js"></script>
Expand Down

0 comments on commit 25d55b3

Please sign in to comment.