bonescript-demo.js: add shellRun function
[beagleboard.org.git] / static / bonescript-demo.js
blobf3a582f4fe3038fc2624119ac3f67955d96f1dcc
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         if($(this).attr('syntax') == 'sh') 
87             editor[this.id].editor.getSession().setMode("ace/mode/sh");
88         else editor[this.id].editor.getSession().setMode("ace/mode/javascript");
89         var originalDemoRun = demoRun;
90         demoRun = function(myid) {
91             if(typeof editor[myid].editor != 'undefined') {
92                 var code = editor[myid].editor.getValue();
93                 myeval(code);
94             } else {
95                 originalDemoRun(myid);
96             }
97         }
98         var originalShellRun = shellRun;
99         shellRun = function(myid) {
100             if(typeof editor[myid].editor != 'undefined') {
101                 var code = editor[myid].editor.getValue();
102                 myShell(code);
103             } else {
104                 originalShellRun(myid);
105             }
106         }
107     }
110 function doAlert(m) {
111     alert(JSON.stringify(m));
114 function demoRun(id) {
115     var myScript = document.getElementById(id).innerHTML;
116     myScript = myScript.replace("&lt;", "<");
117     myScript = myScript.replace("&gt;", ">");
118     myScript = myScript.replace("&amp;", "&");
119     myeval(myScript);
122 function onShell(x) {
123     console.log(x);
126 function myShell(code) {
127     var b = require('bonescript');
128     b.socket.on('shell', onShell);
129     b.socket.emit('shell', code);
132 function shellRun(id) {
133     var myScript = document.getElementById(id).innerHTML;
134     myShell(myScript);
137 function demoRestore(id) {
138     if(typeof editor[id] == 'undefined') return;
139     editor[id].editor.setValue(editor[id].original);
142 function myeval(script) {
143     try {
144         eval(script);
145     } catch(ex) {
146         console.log('Exception: ' + ex);
147     }