-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.js-shell.js
296 lines (247 loc) · 9.26 KB
/
jquery.js-shell.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* jsShell - Interactive command console.
*
* @author Mark Bowker
*/
(function ($)
{
// Config
// TODO: Create a proper method of managing plugin settings.
var iCommandHeight; // Height of a single line. Multi-line entries will be multiples of this.
var iHistoryTop; // The effective height of the command history (will be used as top offset for new entries)
var command_outputs;
var strCommandPrefix;
// Some common elements we will be using throughout
var $cmdInput,$cmdHistory,$cmdCursor,$cmdForm;
/**
* Main plugin definition.
*
* Based on the parameters given, calls the required method.
*/
$.fn.jsShell = function ()
{
// Until multiple actions are supported, we just call init for now.
return methods.init.apply(this, arguments);
};
/**
* Public methods. Called from the main plugin definition.
*/
var methods =
{
// Setup the console
init: function(options)
{
var $this = this;
if ($this.length == 0)
{
log('no element given');
return $this;
}
if (typeof(options.data) != 'object')
{
log('no command outputs given');
return $this;
}
// Setup the settings
command_outputs = options.data;
strCommandPrefix = typeof(options.cmd_prefix) == "string" ? options.cmd_prefix : '';
// Create the console HTML
jsConsole.createConsoleShell($this);
// Common DOM elements we will be using throughout
$cmdInputContainer = $('div#command_input_container');
$cmdInput = $('input#command_entry');
$cmdHistory = $('div#command_history');
$cmdCursor = $('div#command_cursor');
$cmdForm = $('form#command_entry_form');
// Required styling
$cmdHistory.css('overflow', 'hidden');
$cmdHistory.css('position', 'relative');
$cmdInput.css({'width':'100%', 'border-width':'0'});
if (!$cmdHistory.height()) $cmdHistory.height('250'); // TODO: Use new method for default settings.
// For some reason, browsers keep their own styling on input boxes unless specified directly on the element
$cmdInput.css({'font-family':$this.css('font-family'), 'font-size':$this.css('font-size')});
// If there is a prefix, the width of the input box will need reducing.
// Also, the margins of the history entries will need altering
if (strCommandPrefix != '')
{
var iPrefixWidth = $('span#command_entry_prefix').width();
$cmdInput.css('width', $cmdInput.width() - iPrefixWidth);
$("<style type='text/css'>div.history_text{margin-left:"+iPrefixWidth+"px} div.history_text_command{margin-left:0}</style>").appendTo("head");
}
// Calculate height and offset for new history entries
var iBrowserHack = $.browser.mozilla ? 3 : 0; // Firefox seems to be 'lower down' and is cutting off the bottom of text
iBrowserHack = $.browser.webkit ? -1 : iBrowserHack;
iCommandHeight = $cmdInputContainer.height(); // TODO: Ability to get custom value from paramters.
iHistoryTop = $cmdHistory.height() - iCommandHeight - iBrowserHack;
// Capture command entries
$cmdForm.bind('submit.jsShell', jsConsole.processCommand);
// Create welcome message entries
if (typeof(command_outputs.welcome.text) == 'object')
{
for (var iLoop = 0; iLoop < command_outputs.welcome.text.length; iLoop++)
{
jsConsole.createCommandHistoryEntry(command_outputs.welcome.text[iLoop]);
}
}
// Reset the command entry (as some browsers can save the field value)
jsConsole.resetCommandEntry();
return $this;
}
};
/**
* All functions related to processing and outputting commands to the console shell.
*/
var jsConsole =
{
resetCommandEntry: function()
{
$cmdInput.val('');
$cmdInput.focus();
$cmdCursor.addClass('cursor_focus');
},
processCommand: function()
{
// Get the entered command
var strCommand = $cmdInput.val();
// Create the output history entry of the command (hidden)
var strNewID = jsConsole.createCommandHistoryEntry(strCommandPrefix+strCommand, 'history_text_command');
// Get the output of the command
var arrOutput = jsConsole.getCommandOutput(strCommand);
for (var iLoop = 0; iLoop < arrOutput.length; iLoop++)
{
jsConsole.createCommandHistoryEntry(arrOutput[iLoop]);
}
// Clear and re-focus the command entry
jsConsole.resetCommandEntry();
},
getCommandOutput: function(strCommand)
{
var arrOutput = new Array();
// The command will always be the first word
var arrCommandParts = strCommand.split(' ');
strCommand = arrCommandParts.shift();
var strArguments = arrCommandParts.join(' ');
// TODO: There's is a lot of duplicate code below. May have to move some into a new function.
if (typeof command_outputs[strCommand] == "object")
{
if (command_outputs[strCommand].type == 'link')
{
window.open(command_outputs[strCommand].href, '_blank');
window.focus();
arrOutput.push(command_outputs[strCommand].text);
}
else if (command_outputs[strCommand].type == 'text')
{
if (typeof command_outputs[strCommand].text == 'string')
{
arrOutput.push(command_outputs[strCommand].text);
}
else
{
// Assume the output is in an array
for (var iLoop = 0; iLoop < command_outputs[strCommand].text.length; iLoop++)
{
arrOutput.push(command_outputs[strCommand].text[iLoop]);
}
}
}
else if (command_outputs[strCommand].type == 'function')
{
if (typeof(command_outputs[strCommand].callback) == "function")
{
var arrFnOutput = command_outputs[strCommand].callback(strArguments);
if (typeof(arrFnOutput) == "string")
{
arrOutput.push(arrFnOutput);
}
else if (typeof(arrFnOutput) == "object")
{
// Assume the output is in an array
for (var iLoop = 0; iLoop < arrFnOutput.length; iLoop++)
{
arrOutput.push(arrFnOutput[iLoop]);
}
}
}
}
}
else
{
arrOutput.push('command not found. type \'help\' for available commands');
}
return arrOutput;
},
moveCommandHistory: function(iTop, strIgnore)
{
$('div.history_text').each(function()
{
if (this.id == strIgnore) {
return;
}
var currentOffset = $(this).offset();
$(this).offset({ top: (currentOffset.top + iTop) });
});
},
createCommandHistoryEntry: function(strEntry, strAdditionalClass)
{
var strClass = 'history_text';
if (typeof strAdditionalClass == "string") {
strClass += ' '+strAdditionalClass;
}
// Generate the ID of the new entry
var strNewID = 'history_entry_' + new Date().getTime();
// Parse the entry for display
strEntry = strEntry.replace(/ /g, " ");
// Create the entry DIV and append to this history
iNewTop = iHistoryTop - jsConsole.getCommandHistoryHeight();
strNewHistoryEntry = '<div id="'+strNewID+'" style="display:block; visibility:hidden; position:relative; top:'+iNewTop+'px;" class="'+strClass+'">'+strEntry+'</div>';
$cmdHistory.append(strNewHistoryEntry);
// Move existing history up
jsConsole.moveCommandHistory(-iCommandHeight, strNewID);
// Show the new command now that the rest of the history has been moved
$('div#'+strNewID).css({'visibility':'visible'});
// Ensure we're scrolled to the bottom of the history
$cmdHistory.attr({ scrollTop: $cmdHistory.attr("scrollHeight") });
return strNewID;
},
calculateCommandHeight: function(strEntry)
{
var iLineBreakCount = strEntry.match(/<br \/>/g);
return iLineBreakCount.length + 1; // +1 as we will always have the first line
},
/**
* Needed to determine the the total height of the command history.
* Even though most of it is hidden, we still need this height to determine
* the top value of the new entries.
*/
getCommandHistoryHeight: function()
{
var iHistoryHeight = 0;
$('div.history_text').each(function()
{
iHistoryHeight += $(this).height();
});
return iHistoryHeight;
},
/**
* Creates the actual HTML for the shell.
*/
createConsoleShell: function($container)
{
var $history = $('<div id="command_history_container"><div id="command_history"></div></div>');
var strInputEntry = '<div><span id="command_entry_prefix">'+strCommandPrefix+'</span><input type="text" id="command_entry" autocomplete="off" /></div>';
var $inputForm = $('<div id="command_input_container"><form id="command_entry_form" action="#">'+strInputEntry+'</form></div>');
$container.append($history).append($inputForm);
}
};
/**
* Simple wrapper function for console.log()
*/
function log(msg)
{
if (window.console && window.console.log)
{
window.console.log("jsShell ::", msg);
}
};
})(jQuery);