-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
69 lines (62 loc) · 1.7 KB
/
main.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
/*
This file was generated by Dashcode.
You may edit this file to customize your widget or web page
according to the license.txt file included in the project.
*/
//
// Function: load()
// Called by HTML body element's onload event when the widget is ready to start
//
function load()
{
window.console = {};
var consoleVal = '';
window.log = console.log = function() {
var args = Array.prototype.slice.call(arguments);
if (consoleVal !== '') consoleVal += '\n';
consoleVal += '> ' + args.join(' ');
$('#console')
.val(consoleVal)
.prop({scrollTop: $('#console').prop('scrollHeight') - $('#console').height()});
};
compile();
run();
$('#coffeeCode')
.keyup(compile)
.blur(run);
$('#run')
.mousedown(function() {
if ($('#coffeeCode').is(':focus')) return; // the blur will trigger run anyway
run();
});
var originalCoffee = $('#coffeeCode').val();
$('#reset')
.mousedown(function() {
$('#coffeeCode').val(originalCoffee);
compile();
});
}
var coffee, js;
function compile()
{
coffee = $('#coffeeCode').val();
try {
js = CoffeeScript.compile(coffee, {bare: true});
if (js[0] === '\n') js = js.slice(1)
$('#jsCode').val(js);
} catch (e) {
js = null;
var errString = e.toString().slice(7);
var colonIndex = errString.indexOf(':');
errString = errString.slice(0, colonIndex + 1) + '\n' + errString.slice(colonIndex + 2);
$('#jsCode').val(errString);
}
}
function run()
{
try {
if (js) eval(js);
} catch (e) {
log(e);
}
}