(function($) {
var screen, input, spinner_id, resultPrompt, prompt_id;
var spinner = "
";
// Options:
// * prompt: html string to use as prompt
// * resultPrompt: html string before each result
// * loop: function to handle eval and logging of its result,
// default is $.repl.loop
// * spinner: location of spinner image
// * startMessage: function which returns string to display when starting repl
$.fn.repl = function(options) {
options = $.extend({
prompt: '>> ',
resultPrompt: '=> ',
loop: $.repl.loop,
spinner: 'spinner.gif',
startMessage: function() {
return '# New repl.js session using jQuery '+$().jquery;
}
}, options);
input = $(this);
input.addClass('repl_input');
resultPrompt = options.resultPrompt;
var input_id = this.selector.replace(/^#/, '');
spinner_id = input_id + '_spinner';
spinner = spinner.replace('%s', spinner_id);
spinner = spinner.replace('%i', options.spinner);
prompt_id = input_id + '_prompt';
if (!$('#'+prompt_id).length) {
input.before('');
}
var screen_id = input_id + '_screen';
$('#'+prompt_id).html(options.prompt).
before("");
screen = $('#'+screen_id);
if (options.startMessage) { $.repl.log(options.startMessage()); }
input.focus();
input.parent('form').submit(function() {
var line = input.val();
$.repl.log(options.prompt + line + spinner);
options.loop(line);
input.val("").focus();
return false;
});
if ($.hotkeys) {
input.bind('keydown', 'ctrl+l', function() { screen.html(''); });
}
return this;
};
$.repl = {
version: '0.2.0',
log: function(str) {
screen.append(str + "
");
return $('body').scrollTop($('body').attr('scrollHeight'));
},
logResult: function(str) {
$('#'+spinner_id).remove();
var node = $("");
node.append($("
"))
$("code", node).append($("").text(str));
screen.append(node);
return $('body').scrollTop($('body').attr('scrollHeight'));
},
disable: function() {
$('#'+prompt_id).hide();
return input.hide();
},
enable: function() {
$('#'+prompt_id).show();
return input.show();
},
eval: function(input) {
try { var result = eval(input); }
catch(e) { var result = e.name + ': '+ e.message; }
if (typeof(result) == 'undefined') {
result = 'undefined';
} else {
result = result ? result.toString() : '';
result = $('').text(result).html();
}
return result;
},
loop: function(line) {
var result = $.repl.eval(line);
return $.repl.logResult(result);
}
};
})(jQuery);