Added shellRun function for newer bonescript installs
[beagleboard.org.git] / static / bonescript-demo.js
blobb2fc2a09f359daffb1beabbf640495c4543f3290
1 var cssUrls = [
2     '/static/jquery.terminal.css',         // http://terminal.jcubic.pl/js/jquery.terminal.css
3     '/static/jquery-ui.css',               // http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css
4     '/static/client.css'
5 ];
7 var scriptUrls = [
8     '/static/jquery.js',
9     '/static/jquery.dimensions.js',
10     '/static/jquery.ui.core.js',
11     '/static/jquery.ui.widget.js',
12     '/static/jquery.ui.accordion.js',
13     '/static/jquery.svg.js',
14     '/static/jquery.terminal.js',          // http://terminal.jcubic.pl/js/jquery.terminal-0.4.12.min.js
15     '/static/jquery.mousewheel.js',        // http://terminal.jcubic.pl/js/jquery.mousewheel-min.js
16     '/static/jquery-ui.min.js',            // http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js
17     '/static/bonescript.js',
18     '/static/beagle-ui.js',
19     '/static/ajaxorg-ace-builds-c2f3abb/ace.js'// https://github.com/ajaxorg/ace-builds/commit/c2f3abb2ecd3287f90225d804132f0fd26cfb639
22 var oldLog = console.log;
23 console.log = mylog;
24 loadCss();
26 function mylog() {
27     var str = '';
28     var area = $('#console-output');
29     for(var i in arguments) {
30         str += arguments[i].toString() + '\n';
31     }
32     oldLog.apply(console, arguments);
33     if(area.length) {
34         area.append(str);
35         area.scrollTop(area[0].scrollHeight - area.height());
36     }
39 function loadCss() {
40     var url = cssUrls.shift();
41     if(url) {
42         var head = document.getElementsByTagName('head')[0];
43         var link = document.createElement('link');
44         link.rel = 'stylesheet';
45         link.type = 'text/css';
46         link.href = url;
47         var linkObj = head.appendChild(link);
48         loadCss();
49     } else {
50         loadScripts();
51     }
54 // based loosely on http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file
55 function loadScripts() {
56     var url = scriptUrls.shift();
57     if(url) {
58         loadScript(url, loadScripts);
59     } else {
60         initClient();
61     }
64 function loadScript(url, callback) {
65     var head = document.getElementsByTagName('head')[0];
66     var script = document.createElement('script');
67     script.type = 'text/javascript';
68     script.src = url;
69     script.charset = 'UTF-8';
70     var scriptObj = head.appendChild(script);
71     scriptObj.onload = callback;
74 function initClient() {
75     $('.use-editor').each(demoEdit);
76     var co_style = $('#console-output').attr('style');
77     $('#console-output').replaceWith('<textarea id="console-output" />')
78     $('#console-output').attr('style', co_style);
80     function demoEdit(index) {
81         if(typeof editor == 'undefined') editor = {};
82         editor[this.id] = {};
83         editor[this.id].original = this.innerHTML;
84         editor[this.id].editor = ace.edit(this.id);
85         editor[this.id].editor.setTheme("ace/theme/textmate");
86         editor[this.id].editor.getSession().setMode("ace/mode/javascript");
87         var originalDemoRun = demoRun;
88         demoRun = function(myid) {
89             if(typeof editor[myid].editor != 'undefined') {
90                 var code = editor[myid].editor.getValue();
91                 myeval(code);
92             } else {
93                 originalDemoRun(myid);
94             }
95         }
96     }
99 function doAlert(m) {
100     alert(JSON.stringify(m));
103 function demoRun(id) {
104     var myScript = document.getElementById(id).innerHTML;
105     myScript = myScript.replace("&lt;", "<");
106     myScript = myScript.replace("&gt;", ">");
107     myScript = myScript.replace("&amp;", "&");
108     myeval(myScript);
111 function onShell(x) {
112     console.log(x);
115 function shellRun(id) {
116     var myScript = document.getElementById(id).innerHTML;
117     var b = require('bonescript');
118     b.socket.on('shell', onShell);
119     b.socket.emit('shell', myScript);
122 function demoRestore(id) {
123     if(typeof editor[id] == 'undefined') return;
124     editor[id].editor.setValue(editor[id].original);
127 function myeval(script) {
128     try {
129         eval(script);
130     } catch(ex) {
131         console.log('Exception: ' + ex);
132     }