|
(function () { |
|
var lines = [], printed = false, mrb, load_string_func; |
|
|
|
window.Module = {}; |
|
window.Module['print'] = function (x) { |
|
lines.push(x); |
|
printed = true; |
|
}; |
|
|
|
jQuery.fn.autogrow = function(options) { |
|
var settings = $.extend({ |
|
extraLineHeight: 15, |
|
timeoutBuffer: 100 |
|
}, options) |
|
var self = this; |
|
var timerId = self.removeData('timerId'); |
|
if (timerId) |
|
clearTimeout(timerId); |
|
var handler = function() { |
|
self.each(function (i,e) { |
|
var scrollHeight = e.scrollHeight; |
|
var clientHeight = e.clientHeight; |
|
if (clientHeight < scrollHeight) { |
|
$(e).height(scrollHeight + settings.extraLineHeight); |
|
} |
|
}); |
|
self.removeData('timerId'); |
|
}; |
|
self.data('timerId', setTimeout(handler, settings.timeoutBuffer)) |
|
} |
|
$(document).on('keyup.autogrow', 'textarea', function() { $(this).autogrow(); }); |
|
|
|
$(document).ready(function() { |
|
mrb = Module['_driver_open'](); |
|
load_string_func = Module.cwrap("driver_execute_string", |
|
"number", |
|
["number", "string"]); |
|
|
|
$("pre").on("click.replace", function() { |
|
var $this = $(this); |
|
$this.off("click.replace"); |
|
var form = $("<form>"); |
|
var btn = $("<button>Eval</button>"); |
|
var textarea = $("<textarea style='width:100%'></textarea>").html($this.html()); |
|
var output = $("<pre style='background:snow;display:none'></pre>"); |
|
form.append(btn).append("<br>").append(textarea).append("<br>").append(output); |
|
textarea.autogrow(); |
|
btn.on("click", function(e) { |
|
lines = []; |
|
printed = false; |
|
load_string_func(mrb, textarea.val()); |
|
if (!printed) { |
|
window.Module['print']('<small><i>(no output)</i></small>'); |
|
} |
|
output.html(lines.join("<br>")).show(); |
|
return false; |
|
}); |
|
$this.html(form); |
|
}); |
|
|
|
window.onbeforeunload = function () { |
|
Module['_driver_close'](mrb); |
|
} |
|
}); |
|
}()); |