21 if (readCookie("session") == null) {
22 window.location = "/login.html"
24 locked = document.getElementById("locked");
25 unlocked = document.getElementById("unlocked");
26 workarea = document.getElementById("workarea");
27 scriptcell = document.getElementById("scriptcell");
28 goalcell = document.getElementById("goalcell");
29 goals = document.getElementById("goals");
30 goalview = document.getElementById("goalview");
31 filename = document.getElementById("filename");
32 logarea = document.getElementById("logarea");
33 advanceButton = document.getElementById("advance");
34 retractButton = document.getElementById("retract");
35 cursorButton = document.getElementById("cursor");
36 bottomButton = document.getElementById("bottom");
37 dialogBox = document.getElementById("dialogBox");
38 dialogTitle = document.getElementById("dialogTitle");
39 dialogContent = document.getElementById("dialogContent");
41 // hide sequent view at start
44 // initialize keyboard events in the unlocked script
45 init_keyboard(unlocked);
49 function init_keyboard(target)
51 if (target.addEventListener)
53 // target.addEventListener("keydown",keydown,false);
54 target.addEventListener("keypress",keypress,false);
55 // target.addEventListener("keyup",keyup,false);
56 // target.addEventListener("textInput",textinput,false);
58 else if (target.attachEvent)
60 // target.attachEvent("onkeydown", keydown);
61 target.attachEvent("onkeypress", keypress);
62 // target.attachEvent("onkeyup", keyup);
63 // target.attachEvent("ontextInput", textinput);
67 // target.onkeydown= keydown;
68 target.onkeypress= keypress;
69 // target.onkeyup= keyup;
70 // target.ontextinput= textinput; // probably doesn't work
77 if (n == null) return 'undefined';
79 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
80 while (s.length < 9) s+= ' ';
84 function string_of_key(n)
86 if (n == null) return 'undefined';
87 return String.fromCharCode(n);
90 function pressmesg(w,e)
92 debug(w + ' keyCode=' + keyval(e.keyCode) +
93 ' which=' + keyval(e.which) +
94 ' charCode=' + keyval(e.charCode) +
95 '\n shiftKey='+e.shiftKey
96 + ' ctrlKey='+e.ctrlKey
98 + ' metaKey='+e.metaKey);
101 function suppressdefault(e,flag)
105 if (e.preventDefault) e.preventDefault();
106 if (e.stopPropagation) e.stopPropagation();
114 pressmesg('keypress',e);
115 var s = string_of_key(e.charCode);
118 i = unlocked.innerHTML.lastIndexOf('\\',j);
120 match = unlocked.innerHTML.substring(i,j);
121 pre = unlocked.innerHTML.substring(0,i);
122 post = unlocked.innerHTML.substring(j);
123 if (match == '\\to') {
124 unlocked.innerHTML = pre + "-> " + post;
126 return suppressdefault(e,true);
128 else return suppressdefault(e,false);
130 else return suppressdefault(e,false);
132 return suppressdefault(e,false);
138 // internet explorer (v.9) doesn't work with innerHTML
139 // but google chrome's innerText is, in a sense, "write only"
140 // what should we do?
141 logarea.innerText = txt + "\n" + logarea.innerText;
147 debug("hd of '" + l + "' = '" + ar[0] + "'");
155 debug("tl of '" + l + "' = '" + tl + "'");
159 function listcons(x,l)
161 debug("cons '" + x + "' on '" + l + "'");
162 return (x + "#" + l);
175 function fold_left (f,acc,l)
178 { debug("'" + l + "' is fold end");
181 { debug("'" + l + "' is fold cons");
182 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
185 function listiter (f,l)
188 { debug("'" + l + "' is nil");
193 debug("'" + l + "' is not nil");
195 listiter(f,listtl(l));
199 function listmap (f,l)
201 debug("listmap on " + l);
203 { debug("returning listnil");
207 { debug("cons f(hd) map(f,tl)");
208 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
212 var statements = listnil();
215 var metalist = listnil();
217 function pairmap (f,p)
219 debug("pairmap of '" + p + "'");
221 return (f(ar[0],ar[1]));
224 function tripletmap (f,p)
226 debug("tripletmap of '" + p + "'");
228 return (f(ar[0],ar[1],ar[2]));
234 return (pairmap (function (a,b) { return (a); }, p));
240 return (tripletmap (function (a,b,c) { return (a); }, p));
246 return (tripletmap (function (a,b,c) { return (b); }, p));
252 return (tripletmap (function (a,b,c) { return (c); }, p));
255 function populate_goalarray(menv)
257 debug("metasenv.length = " + menv.length);
258 if (menv.length == 0) {
264 goalarray = new Array();
265 metalist = listnil();
266 var tmp_goallist = "";
267 for (i = 0; i < menv.length; i++) {
268 metano = menv[i].getAttribute("number");
269 metaname = menv[i].childNodes[0].childNodes[0].data;
270 goal = menv[i].childNodes[1].childNodes[0].data;
271 debug ("found meta n. " + metano);
272 debug ("found goal\nBEGIN" + goal + "\nEND");
273 goalarray[metano] = goal;
274 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
275 metalist = listcons(metano,metalist);
276 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
278 goals.innerHTML = tmp_goallist;
279 debug("new metalist is '" + metalist + "'");
280 if (is_nil(metalist)) {
284 switch_goal(listhd(metalist));
289 function switch_goal(meta)
291 if (typeof meta == "undefined") {
292 goalview.innerHTML = "";
295 debug("switch_goal " + meta + "\n" + goalarray[meta]);
296 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
300 String.prototype.sescape = function() {
306 result = result.replace(patt1,"%25");
307 result = result.replace(patt2,"%3D");
308 result = result.replace(patt3,"%26");
309 result = result.replace(patt4,"%2B");
313 String.prototype.unescapeHTML = function()
315 var patt1 = /<br(\/|)>/gi;
316 var patt2 = /</gi;
317 var patt3 = />/gi;
319 result = result.replace(patt1,"\n");
320 result = result.replace(patt2,"<");
321 result = result.replace(patt3,">");
322 return (unescape(result));
327 advanceButton.disabled = true;
328 retractButton.disabled = true;
329 cursorButton.disabled = true;
330 bottomButton.disabled = true;
335 advanceButton.disabled = false;
336 retractButton.disabled = false;
337 cursorButton.disabled = false;
338 bottomButton.disabled = false;
341 function is_defined(x)
343 return (typeof x != "undefined");
346 /* servicename: name of the service being called
347 * reqbody: text of the request
348 * processResponse: processes the server response
349 * (takes the response text in input, undefined in case of error)
351 function callServer(servicename,processResponse,reqbody)
355 if (window.XMLHttpRequest)
357 req = new XMLHttpRequest();
359 else if (window.ActiveXObject)
362 req = new ActiveXObject("Msxml2.XMLHTTP");
366 req = new ActiveXObject("Microsoft.XMLHTTP");
370 req.onreadystatechange = function()
378 stxt = req.statusText;
381 debug(req.responseText);
382 if (window.DOMParser) {
383 parser=new DOMParser();
384 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
386 else // Internet Explorer
388 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
389 xmlDoc.async="false";
390 xmlDoc.loadXML(req.responseText);
392 processResponse(xmlDoc);
398 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
399 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
408 function advanceForm1()
410 processor = function(xml) {
411 if (is_defined(xml)) {
412 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
413 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
414 len0 = unlocked.innerHTML.length;
415 unescaped = unlocked.innerHTML.unescapeHTML();
416 parsedtxt = unescaped.substr(0,len);
417 unparsedtxt = unescaped.substr(len);
418 locked.innerHTML = locked.innerHTML + parsedtxt;
419 unlocked.innerHTML = unparsedtxt;
420 len1 = unlocked.innerHTML.length;
422 metasenv = xml.getElementsByTagName("meta");
423 populate_goalarray(metasenv);
424 statements = listcons(len2,statements);
425 unlocked.scrollIntoView(true);
427 debug("advance failed");
432 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
436 function gotoBottom()
438 processor = function(xml) {
439 if (is_defined(xml)) {
440 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
441 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
442 len0 = unlocked.innerHTML.length;
443 unescaped = unlocked.innerHTML.unescapeHTML();
444 parsedtxt = unescaped.substr(0,len);
445 unparsedtxt = unescaped.substr(len);
446 locked.innerHTML = locked.innerHTML + parsedtxt;
447 unlocked.innerHTML = unparsedtxt;
448 len1 = unlocked.innerHTML.length;
450 metasenv = xml.getElementsByTagName("meta");
451 populate_goalarray(metasenv);
452 statements = listcons(len2,statements);
453 unlocked.scrollIntoView(true);
455 debug("goto bottom failed");
460 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
465 function gotoPos(offset)
467 if (!is_defined(offset)) {
468 offset = getCursorPos();
470 processor = function(xml) {
471 if (is_defined(xml)) {
472 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
473 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
474 len0 = unlocked.innerHTML.length;
475 unescaped = unlocked.innerHTML.unescapeHTML();
476 parsedtxt = unescaped.substr(0,len);
477 unparsedtxt = unescaped.substr(len);
478 locked.innerHTML = locked.innerHTML + parsedtxt;
479 unlocked.innerHTML = unparsedtxt;
480 len1 = unlocked.innerHTML.length;
482 metasenv = xml.getElementsByTagName("meta");
483 // populate_goalarray(metasenv);
484 statements = listcons(len2,statements);
485 unlocked.scrollIntoView(true);
486 // la populate non andrebbe fatta a ogni passo
488 populate_goalarray(metasenv);
491 gotoPos(offset - len);
494 unlocked.scrollIntoView(true);
495 populate_goalarray(metasenv);
500 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
505 processor = function(xml) {
506 if (typeof xml != "undefined") {
507 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
508 statementlen = parseInt(listhd(statements));
509 statements = listtl(statements);
510 lockedlen = locked.innerHTML.length - statementlen;
511 statement = locked.innerHTML.substr(lockedlen, statementlen);
512 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
513 unlocked.innerHTML = statement + unlocked.innerHTML;
514 metasenv = xml.getElementsByTagName("meta");
515 populate_goalarray(metasenv);
516 unlocked.scrollIntoView(true);
518 debug("retract failed");
524 callServer("retract",processor);
530 processor = function(xml)
532 if (is_defined(xml)) {
533 locked.innerHTML = "";
534 unlocked.innerHTML = xml.documentElement.textContent;
536 debug("file open failed");
539 callServer("open",processor,"file=" + escape(filename.value));
542 function retrieveFile(thefile)
544 processor = function(xml)
546 if (is_defined(xml)) {
547 locked.innerHTML = "";
548 unlocked.innerHTML = xml.documentElement.textContent;
550 debug("file open failed");
553 dialogBox.style.display = "none";
554 callServer("open",processor,"file=" + escape(thefile));
557 function showLibrary()
561 if (window.XMLHttpRequest)
563 req = new XMLHttpRequest();
565 else if (window.ActiveXObject)
568 req = new ActiveXObject("Msxml2.XMLHTTP");
572 req = new ActiveXObject("Microsoft.XMLHTTP");
576 req.onreadystatechange = function()
584 stxt = req.statusText;
587 debug(req.responseText);
588 showDialog("<H2>Library</H2>",req.responseText);
592 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
593 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
600 function hideSequent() {
601 goalcell.parentNode.removeChild(goalcell);
602 scriptcell.setAttribute("colspan","2");
605 function showSequent() {
606 scriptcell.setAttribute("colspan","1");
607 workarea.appendChild(goalcell);
610 function showDialog(title,content) {
611 dialogTitle.innerHTML = title;
612 dialogContent.innerHTML = content;
613 dialogBox.style.display = "block";
616 function removeElement(id) {
617 var element = document.getElementById(id);
618 element.parentNode.removeChild(element);
621 function getCursorPos() {
623 if (window.getSelection) {
624 var selObj = window.getSelection();
625 var selRange = selObj.getRangeAt(0);
626 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
627 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
628 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
631 else if (document.selection) {
632 var range = document.selection.createRange();
633 var bookmark = range.getBookmark();
634 /* FIXME the following works wrong when the document is longer than 65535 chars */
635 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
640 function findNode(list, node, acc) {
641 for (var i = 0; i < list.length; i++) {
642 if (list[i] == node) {
643 // debug("success " + i);
646 if (list[i].hasChildNodes()) {
648 // debug("recursion on node " + i);
649 return (findNode(list[i].childNodes,node,acc))
651 catch (e) { /* debug("recursion failed"); */ }
653 sandbox = document.getElementById("sandbox");
654 dup = list[i].cloneNode(true);
655 sandbox.appendChild(dup);
656 // debug("fail " + i + ": " + sandbox.innerHTML);
657 acc += sandbox.innerHTML.length;
658 sandbox.removeChild(dup);
664 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
667 function readCookie(name) {
668 var nameEQ = name + "=";
669 var ca = document.cookie.split(';');
670 for(var i=0;i < ca.length;i++) {
672 while (c.charAt(0)==' ') c = c.substring(1,c.length);
673 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
678 function delete_cookie ( cookie_name )
680 var cookie_date = new Date(); // current date & time
681 cookie_date.setTime ( cookie_date.getTime() - 1 );
682 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
685 function delete_session()
687 delete_cookie("session");