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 #12 from adobe/open-file-from-tree
Browse files Browse the repository at this point in the history
Open file from tree - looks good!
  • Loading branch information
peterflynn committed Dec 16, 2011
2 parents ea0847e + c38918f commit f12f2a1
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 31 deletions.
36 changes: 36 additions & 0 deletions src/CommandManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*/

var CommandManager = {};

CommandManager._commands = {};

/**
* Registers a global command.
*
* @param {string} id The ID of the command.
* @param {function} command The function to call when the command is executed. Any arguments passed to
* execute() (after the id) are passed as arguments to the function.
*/
CommandManager.register = function(id, command) {
if (CommandManager._commands[id]) {
throw new Error("Attempting to register an already-registered command: " + id);
}
CommandManager._commands[id] = command;
}

/**
* Runs a global command. Additional arguments are passed to the command.
*
* @param {string} id The ID of the command to run.
*/
CommandManager.execute = function(id) {
var command = CommandManager._commands[id];
if (command) {
command.apply(null, Array.prototype.slice.call(arguments, 1));
}
else {
console.log("Attempted to call unregistered command: " + id);
}
}
10 changes: 10 additions & 0 deletions src/Commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*/

/**
* List of constants for global command IDs.
*/
var Commands = {
FILE_OPEN: "file.open"
};
3 changes: 3 additions & 0 deletions src/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,8 @@ ProjectManager._renderTree = function(treeDataProvider) {
// file because jsTree insists on loading one itself)

strings : { loading : "Loading ...", new_node : "New node" } // TODO: localization
})
.bind("select_node.jstree", function(event, data) {
CommandManager.execute(Commands.FILE_OPEN, data.rslt.obj.data("entry").fullPath);
});
};
119 changes: 90 additions & 29 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*/

// TODO: break out the definition of brackets into a separate module from the application controller logic

// Define core brackets namespace
brackets = window.brackets || {};

brackets.inBrowser = !brackets.hasOwnProperty("fs");

/**
* General purpose modal error dialog.
*
* @param {string} title The title of the error dialog. Can contain HTML markup.
* @param {string} message The message to display in the error dialog. Can contain HTML markup.
*/
brackets.showErrorDialog = function(title, message) {
var dlg = $("#error-dialog");

// Set title and message
$("#error-dialog-title").html(title);
$("#error-dialog-message").html(message);

// Click handler for OK button
dlg.delegate("#error-dialog-ok", "click", function(e) {
dlg.modal(true).hide();
});

// Run the dialog
dlg.modal(
{ backdrop: "static"
, show: true
}
);
}

$(document).ready(function() {

/**
* General purpose modal error dialog.
*
* @param {string} title The title of the error dialog. Can contain HTML markup.
* @param {string} message The message to display in the error dialog. Can contain HTML markup.
*/
brackets.showErrorDialog = function(title, message) {
var dlg = $("#error-dialog");

// Set title and message
$("#error-dialog-title").html(title);
$("#error-dialog-message").html(message);

// Click handler for OK button
dlg.delegate("#error-dialog-ok", "click", function(e) {
dlg.modal(true).hide();
});

// Run the dialog
dlg.modal(
{ backdrop: "static"
, show: true
}
);
}

var myCodeMirror = CodeMirror($('#editor').get(0), {
value: 'var myResponse="Yes, it will be!"\n'
});
var editor = CodeMirror($('#editor').get(0));

// Load a default project into the tree
if (brackets.inBrowser) {
Expand All @@ -56,6 +55,10 @@ $(document).ready(function() {
ProjectManager.openProject();
});

// Implements the "Open File" menu
$("#menu-file-open").click(function() {
CommandManager.execute(Commands.FILE_OPEN);
});

// Implements the 'Run Tests' menu to bring up the Jasmine unit test window
var testWindow = null;
Expand Down Expand Up @@ -117,4 +120,62 @@ $(document).ready(function() {
});
});*/

// Utility functions
function doOpen(fullPath) {
if (fullPath) {
var reader = new NativeFileSystem.FileReader();

// TODO: we should implement something like NativeFileSystem.resolveNativeFileSystemURL() (similar
// to what's in the standard file API) to get a FileEntry, rather than manually constructing it
var fileEntry = new NativeFileSystem.FileEntry(fullPath);

// TODO: it's weird to have to construct a FileEntry just to get a File.
fileEntry.file(function(file) {
reader.onload = function(event) {
// TODO: have a real controller object for the editor
editor.setValue(event.target.result);
editor.clearHistory();

// In the main toolbar, show the project-relative path (if the file is inside the current project)
// or the full absolute path (if it's not in the project).
var projectRootPath = ProjectManager.getProjectRoot().fullPath;
if (projectRootPath.length > 0 && projectRootPath.charAt(projectRootPath.length - 1) != "/") {
projectRootPath += "/";
}
if (fullPath.indexOf(projectRootPath) == 0) {
fullPath = fullPath.slice(projectRootPath.length);
if (fullPath.charAt(0) == '/') {
fullPath = fullPath.slice(1);
}
}
$("#main-toolbar .title").text(fullPath);
};
reader.onerror = function(event) {
// TODO: display meaningful error
}

reader.readAsText(file, "utf8");
},
function (error) {
// TODO: display meaningful error
});
}
}

// Register global commands
CommandManager.register(Commands.FILE_OPEN, function(fullPath) {
if (!fullPath) {
// Prompt the user with a dialog
NativeFileSystem.showOpenDialog(false, false, "Open File", ProjectManager.getProjectRoot().fullPath,
["htm", "html", "js", "css"], function(files) {
if (files.length > 0) {
doOpen(files[0]);
}
});
}
else {
doOpen(fullPath);
}
});

});
6 changes: 4 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
<script src="thirdparty/jstree_pre1.0_fix_1/jquery.jstree.js"></script>

<script src="NativeFileSystem.js"></script>
<script src="brackets.js"></script>
<script src="CommandManager.js"></script>
<script src="Commands.js"></script>
<script src="ProjectManager.js"></script>
<script src="brackets.js"></script>
<script src="strings.js"></script>
</head>
<body>
Expand Down Expand Up @@ -73,7 +75,7 @@
<input class="settings" type="button" value="Settings"/>
-->
</div>
<div class="title">will-brackets-be-awesome.js</div>
<div class="title">Untitled</div>
</div>
<div id="editor"></div>
</div>
Expand Down

0 comments on commit f12f2a1

Please sign in to comment.