example.js | |
---|---|
/*------------------------------------------------------------------------*
* Copyright 2013 Arne F. Claassen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------*/
(function(root, $, _) {
Josh.Example = (function(root, $, _) { | |
Enable console debugging, when Josh.Debug is set and there is a console object on the document root. | var _console = (Josh.Debug && root.console) ? root.console : {
log: function() {
}
}; |
Setup of Shell | |
build the fake directory structure used to illustrate path commands and completions. | var treeroot = buildTree(); |
Create | var history = Josh.History();
var killring = new Josh.KillRing(); |
Create the | var readline = new Josh.ReadLine({history: history, killring: killring, console: _console }); |
Finally, create the | var shell = Josh.Shell({readline: readline, history: history, console: _console}); |
Create killring command | |
Setup the | var killringItemTemplate = _.template("<div><% _.each(items, function(item, i) { %><div><%- i %> <%- item %></div><% }); %></div>") |
Create a the command | shell.setCommandHandler("killring", { |
We don't implement any completion for the | exec: function(cmd, args, callback) { |
| if(args[0] == "-c") {
killring.clear(); |
The callback of an | callback();
return;
} |
Return the output of feeding all items from the killring into our template. | callback(killringItemTemplate({items: killring.items()}));
}
}); |
Setup PathHandler | |
| var pathhandler = new Josh.PathHandler(shell, {console: _console}); |
where name is the The pathhandler expects to be initialized with the current directory, i.e. a path node. | pathhandler.current = treeroot; |
| pathhandler.getNode = function(path, callback) {
if(!path) {
return callback(pathhandler.current);
}
var parts = _.filter(path.split('/'), function(x) {
return x;
});
var start = ((path || '')[0] == '/') ? treeroot : pathhandler.current;
_console.log('start: ' + start.path + ', parts: ' + JSON.stringify(parts));
return findNode(start, parts, callback);
}; |
| pathhandler.getChildNodes = function(node, callback) {
_console.log("children for " + node.name);
callback(node.childnodes);
}; |
| function findNode(current, parts, callback) {
if(!parts || parts.length == 0) {
return callback(current);
}
if(parts[0] == ".") {
} else if(parts[0] == "..") {
current = current.parent;
} else {
current = _.first(_.filter(current.childnodes, function(node) {
return node.name == parts[0];
}));
}
if(!current) {
return callback();
}
return findNode(current, _.rest(parts), callback);
} |
Setup Document Behavior | |
Activation and display behavior happens at document ready time. | $(document).ready(function() { |
The default name for the div the shell uses as its container is | var $consolePanel = $('#shell-panel'); |
We use jquery-ui's | $consolePanel.resizable({ handles: "s"}); |
activate the shell | shell.activate();
}); |
We attach the various objects we've created here to | Josh.Instance = {
Tree: treeroot,
Shell: shell,
PathHandler: pathhandler,
KillRing: killring
}; |
This code builds our fake directory structure. Since most real applications of | function buildTree() {
var fs = {
bin: {},
boot: {},
dev: {},
etc: {
default: {},
'rc.d': {},
sysconfig: {},
x11: {}
},
home: {
bob: {
video: {
'firefly.m4v': {}
},
videos: {
'Arrested Development': {
's1e1.m4v': {}
},
'Better Off Ted': {
's1e1.m4v': {}
}
}
},
jane: {}
},
lib: {},
'lost+found': {},
misc: {},
mnt: {
cdrom: {},
sysimage: {}
},
net: {},
opt: {},
proc: {},
root: {},
sbin: {},
usr: {
x11: {},
bin: {},
include: {},
lib: {},
local: {},
man: {},
sbin: {},
share: {
doc: {}
},
src: {}
},
var: {
lib: {},
lock: {},
run: {},
log: {
httpd: {
access_log: {},
error_log: {}
},
'boot.log': {},
cron: {},
messages: {}
}
}
};
function build(parent, node) {
parent.childnodes = _.map(_.pairs(node), function(pair) {
var child = {
name: pair[0],
path: parent.path + "/" + pair[0],
parent: parent
};
build(child, pair[1]);
return child;
});
parent.children = _.keys(node);
return parent;
}
var tree = build({name: "", path: ""}, fs);
tree.path = '/';
return tree;
}
})(root, $, _);
})(this, $, _);
|