# HG changeset patch # User Mike Pavone # Date 1332389732 25200 # Node ID 37d7f60a8ea192d75544af9b5fbd7d240e8b94a0 # Parent 04ae32e91598542f896e688becda5179380f7cca Allow use of tabletproglang programs in script tags diff -r 04ae32e91598 -r 37d7f60a8ea1 jsbackend.js --- a/jsbackend.js Wed Mar 21 20:33:39 2012 -0700 +++ b/jsbackend.js Wed Mar 21 21:15:32 2012 -0700 @@ -46,7 +46,6 @@ } strlit.prototype.toJS = function(symbols) { - console.log('string:', this.val); return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"'; } diff -r 04ae32e91598 -r 37d7f60a8ea1 mquery.js --- a/mquery.js Wed Mar 21 20:33:39 2012 -0700 +++ b/mquery.js Wed Mar 21 21:15:32 2012 -0700 @@ -32,7 +32,7 @@ if (dtype === undefined) { dtype = 'block'; } - el.style.display + el.style.display = dtype; } function onReady(fun) @@ -68,3 +68,43 @@ } } +function ajax(method, url, data, onSuccess, onFail, onOthers) +{ + var req; + try { + req = new XMLHttpRequest(); + } catch (e) { + req = new ActiveXObject("Microsoft.XMLHTTP"); + } + req.onreadystatechange = function() { + if (req.readyState == 4) { + if ((req.status >= 200 && req.status <= 299) || req.status == 0) { + onSuccess(req); + } else if(onFail) { + onFail(req); + } else { + console.log('request failed:', req); + } + } else if(onOthers) { + onOthers(req); + } + } + req.open(method, url); + if (data && 'mime' in data) { + req.setRequestHeader('Content-Type', data.mime); + req.send(data); + } else { + req.send(data); + } +} + +function get(url, onSuccess, onFail, onOthers) +{ + ajax('GET', url, undefined, onSuccess, onFail, onOthers); +} + +function post(url, data, onSuccess, onFail, onOthers) +{ + ajax('POST', url, data, onSuccess, onFail, onOthers); +} + diff -r 04ae32e91598 -r 37d7f60a8ea1 scripttags.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripttags.js Wed Mar 21 21:15:32 2012 -0700 @@ -0,0 +1,21 @@ + +function compileAndRun(src) +{ + var ast = parser.parse(src); + var js = ast.toJSModule(); + mainModule = eval(js)(); + mainModule.main(); +} + +onReady(function() { + var tags = qall('script[type="text/tabletprog"]'); + for (var i = 0; i < tags.length; ++i) { + if (tags[i].src) { + get(tags[i].src, function(req) { + compileAndRun(req.responseText); + }); + } else { + compileAndRun(tags[i].innerHTML); + } + } +}); diff -r 04ae32e91598 -r 37d7f60a8ea1 testscripttags.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testscripttags.html Wed Mar 21 21:15:32 2012 -0700 @@ -0,0 +1,26 @@ + + + + Parser Test + + + + + + + + + + + Testing 1 2 3 + +