FND / jquery

jQuery plugins

This URL has Read+Write access

jquery / jquery.cli.js
100644 125 lines (114 sloc) 2.794 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
jQuery.CLI v0.2.2
pluggable command-line interface
 
Author: FND (http://fnd.lewcid.org)
License: BSD (http://www.opensource.org/licenses/bsd-license.php)
Source: http://github.com/FND/jquery
 
To Do:
* documentation
* output buffer
* command history
* support for auto-completion
* Quake-like dropdown console
* Ubiquity-like API for commands
* tests
*/
 
(function($) {
 
var cmds, styles, keys;
 
$.CLI = function(commands, options) {
cmds = $.extend({}, $.CLI.defaults.commands, commands);
styles = $.extend({}, $.CLI.defaults.styles, options ? options.styles : null);
keys = $.extend({}, $.CLI.defaults.keys, options ? options.keys : null);
$(document).keypress(function(e) {
// suppress custom keyboard events for input fields
if($.inArray(e.target.nodeName.toUpperCase(), ["INPUT", "TEXTAREA"]) != -1) {
return true;
} else if(e.which == keys.trigger) {
$.CLI.init();
}
});
};
 
$.CLI.init = function() { // TODO: rename to toggle?
var container = $("#CLI");
if(container.length) {
container.find("input").focus();
} else {
container = $("<div id='CLI' />").
css(styles.shared).css(styles.container).
appendTo(document.body);
$("<input type='text' />").
css(styles.shared).css(styles.input).
keypress(function(e) {
var key = e.keyCode || e.which; // XXX: keyCode required for ESC!?
switch(key) {
case keys.confirm:
dispatch(this.value);
/*jsl:fallthru*/
case keys.confirm:
case keys.abort:
$(this).parent().remove();
break;
default:
break;
}
}).
appendTo(container).focus();
}
};
 
// invoke command
var dispatch = function(cmd) {
var params = String.prototype.readBracketedList ? cmd.readBracketedList() : cmd.split(" "); //# readBracketedList is TiddlyWiki-specific
cmd = cmds[params.shift()];
if(cmd) {
cmd(params);
} else {
bell();
}
};
 
// visual bell -- XXX: bloat? optional?
var bell = function() {
$("<div />").
css(styles.cloak).css({
width: $(window).width(),
height: $(window).height()
}).
appendTo(document.body).
animate({ opacity: 0.1 }, {
duration: 50,
complete: function() { $(this).remove(); }
});
};
 
// default options
$.CLI.defaults = {
commands: {
print: function(params, place) { $(document.body).prepend(params.join(" ")); }
},
keys: {
trigger: 58, // colon (charCode) -- TODO: rename
confirm: 13, // ENTER (keyCode) -- XXX: TiddlyWiki also uses keycode 10!?
abort: 27 // ESC (keyCode)
},
styles: {
shared: {
width: "100%",
color: "#0F0",
backgroundColor: "#000"
},
container: {
position: "absolute",
top: "0",
left: "0",
padding: "5px 0"
},
input: {
border: "none"
},
cloak: {
position: "absolute",
top: 0,
left: 0,
backgroundColor: "#000"
}
}
};
 
})(jQuery);