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 if (savedRange != null) {
127 if (window.getSelection)//non IE and there is already a selection
129 var s = window.getSelection();
130 if (s.rangeCount > 0)
132 s.addRange(savedRange);
135 if (document.createRange)//non IE and no selection
137 window.getSelection().addRange(savedRange);
140 if (document.selection)//IE
146 return suppressdefault(e,true);
148 else return suppressdefault(e,false);
150 else return suppressdefault(e,false);
152 return suppressdefault(e,false);
158 // internet explorer (v.9) doesn't work with innerHTML
159 // but google chrome's innerText is, in a sense, "write only"
160 // what should we do?
161 logarea.innerText = txt + "\n" + logarea.innerText;
167 debug("hd of '" + l + "' = '" + ar[0] + "'");
175 debug("tl of '" + l + "' = '" + tl + "'");
179 function listcons(x,l)
181 debug("cons '" + x + "' on '" + l + "'");
182 return (x + "#" + l);
195 function fold_left (f,acc,l)
198 { debug("'" + l + "' is fold end");
201 { debug("'" + l + "' is fold cons");
202 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
205 function listiter (f,l)
208 { debug("'" + l + "' is nil");
213 debug("'" + l + "' is not nil");
215 listiter(f,listtl(l));
219 function listmap (f,l)
221 debug("listmap on " + l);
223 { debug("returning listnil");
227 { debug("cons f(hd) map(f,tl)");
228 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
232 var statements = listnil();
235 var metalist = listnil();
237 function pairmap (f,p)
239 debug("pairmap of '" + p + "'");
241 return (f(ar[0],ar[1]));
244 function tripletmap (f,p)
246 debug("tripletmap of '" + p + "'");
248 return (f(ar[0],ar[1],ar[2]));
254 return (pairmap (function (a,b) { return (a); }, p));
260 return (tripletmap (function (a,b,c) { return (a); }, p));
266 return (tripletmap (function (a,b,c) { return (b); }, p));
272 return (tripletmap (function (a,b,c) { return (c); }, p));
275 function populate_goalarray(menv)
277 debug("metasenv.length = " + menv.length);
278 if (menv.length == 0) {
284 goalarray = new Array();
285 metalist = listnil();
286 var tmp_goallist = "";
287 for (i = 0; i < menv.length; i++) {
288 metano = menv[i].getAttribute("number");
289 metaname = menv[i].childNodes[0].childNodes[0].data;
290 goal = menv[i].childNodes[1].childNodes[0].data;
291 debug ("found meta n. " + metano);
292 debug ("found goal\nBEGIN" + goal + "\nEND");
293 goalarray[metano] = goal;
294 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
295 metalist = listcons(metano,metalist);
296 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
298 goals.innerHTML = tmp_goallist;
299 debug("new metalist is '" + metalist + "'");
300 if (is_nil(metalist)) {
304 switch_goal(listhd(metalist));
309 function switch_goal(meta)
311 if (typeof meta == "undefined") {
312 goalview.innerHTML = "";
315 debug("switch_goal " + meta + "\n" + goalarray[meta]);
316 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
320 String.prototype.sescape = function() {
326 result = result.replace(patt1,"%25");
327 result = result.replace(patt2,"%3D");
328 result = result.replace(patt3,"%26");
329 result = result.replace(patt4,"%2B");
333 String.prototype.unescapeHTML = function()
335 var patt1 = /<br(\/|)>/gi;
336 var patt2 = /</gi;
337 var patt3 = />/gi;
339 result = result.replace(patt1,"\n");
340 result = result.replace(patt2,"<");
341 result = result.replace(patt3,">");
342 return (unescape(result));
347 advanceButton.disabled = true;
348 retractButton.disabled = true;
349 cursorButton.disabled = true;
350 bottomButton.disabled = true;
355 advanceButton.disabled = false;
356 retractButton.disabled = false;
357 cursorButton.disabled = false;
358 bottomButton.disabled = false;
361 function is_defined(x)
363 return (typeof x != "undefined");
366 /* servicename: name of the service being called
367 * reqbody: text of the request
368 * processResponse: processes the server response
369 * (takes the response text in input, undefined in case of error)
371 function callServer(servicename,processResponse,reqbody)
375 if (window.XMLHttpRequest)
377 req = new XMLHttpRequest();
379 else if (window.ActiveXObject)
382 req = new ActiveXObject("Msxml2.XMLHTTP");
386 req = new ActiveXObject("Microsoft.XMLHTTP");
390 req.onreadystatechange = function()
398 stxt = req.statusText;
401 debug(req.responseText);
402 if (window.DOMParser) {
403 parser=new DOMParser();
404 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
406 else // Internet Explorer
408 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
409 xmlDoc.async="false";
410 xmlDoc.loadXML(req.responseText);
412 processResponse(xmlDoc);
418 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
419 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
428 function advanceForm1()
430 processor = function(xml) {
431 if (is_defined(xml)) {
432 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
433 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
434 len0 = unlocked.innerHTML.length;
435 unescaped = unlocked.innerHTML.unescapeHTML();
436 parsedtxt = unescaped.substr(0,len);
437 unparsedtxt = unescaped.substr(len);
438 locked.innerHTML = locked.innerHTML + parsedtxt;
439 unlocked.innerHTML = unparsedtxt;
440 len1 = unlocked.innerHTML.length;
442 metasenv = xml.getElementsByTagName("meta");
443 populate_goalarray(metasenv);
444 statements = listcons(len2,statements);
445 unlocked.scrollIntoView(true);
447 debug("advance failed");
452 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
456 function gotoBottom()
458 processor = function(xml) {
459 if (is_defined(xml)) {
460 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
461 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
462 len0 = unlocked.innerHTML.length;
463 unescaped = unlocked.innerHTML.unescapeHTML();
464 parsedtxt = unescaped.substr(0,len);
465 unparsedtxt = unescaped.substr(len);
466 locked.innerHTML = locked.innerHTML + parsedtxt;
467 unlocked.innerHTML = unparsedtxt;
468 len1 = unlocked.innerHTML.length;
470 metasenv = xml.getElementsByTagName("meta");
471 populate_goalarray(metasenv);
472 statements = listcons(len2,statements);
473 unlocked.scrollIntoView(true);
475 debug("goto bottom failed");
480 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
485 function gotoPos(offset)
487 if (!is_defined(offset)) {
488 offset = getCursorPos();
490 processor = function(xml) {
491 if (is_defined(xml)) {
492 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
493 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
494 len0 = unlocked.innerHTML.length;
495 unescaped = unlocked.innerHTML.unescapeHTML();
496 parsedtxt = unescaped.substr(0,len);
497 unparsedtxt = unescaped.substr(len);
498 locked.innerHTML = locked.innerHTML + parsedtxt;
499 unlocked.innerHTML = unparsedtxt;
500 len1 = unlocked.innerHTML.length;
502 metasenv = xml.getElementsByTagName("meta");
503 // populate_goalarray(metasenv);
504 statements = listcons(len2,statements);
505 unlocked.scrollIntoView(true);
506 // la populate non andrebbe fatta a ogni passo
508 populate_goalarray(metasenv);
511 gotoPos(offset - len);
514 unlocked.scrollIntoView(true);
515 populate_goalarray(metasenv);
520 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
525 processor = function(xml) {
526 if (typeof xml != "undefined") {
527 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
528 statementlen = parseInt(listhd(statements));
529 statements = listtl(statements);
530 lockedlen = locked.innerHTML.length - statementlen;
531 statement = locked.innerHTML.substr(lockedlen, statementlen);
532 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
533 unlocked.innerHTML = statement + unlocked.innerHTML;
534 metasenv = xml.getElementsByTagName("meta");
535 populate_goalarray(metasenv);
536 unlocked.scrollIntoView(true);
538 debug("retract failed");
544 callServer("retract",processor);
550 processor = function(xml)
552 if (is_defined(xml)) {
553 locked.innerHTML = "";
554 unlocked.innerHTML = xml.documentElement.textContent;
556 debug("file open failed");
559 callServer("open",processor,"file=" + escape(filename.value));
562 function retrieveFile(thefile)
564 processor = function(xml)
566 if (is_defined(xml)) {
567 locked.innerHTML = "";
568 unlocked.innerHTML = xml.documentElement.textContent;
570 debug("file open failed");
573 dialogBox.style.display = "none";
574 callServer("open",processor,"file=" + escape(thefile));
577 function showLibrary()
581 if (window.XMLHttpRequest)
583 req = new XMLHttpRequest();
585 else if (window.ActiveXObject)
588 req = new ActiveXObject("Msxml2.XMLHTTP");
592 req = new ActiveXObject("Microsoft.XMLHTTP");
596 req.onreadystatechange = function()
604 stxt = req.statusText;
607 debug(req.responseText);
608 showDialog("<H2>Library</H2>",req.responseText);
612 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
613 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
620 function hideSequent() {
621 goalcell.parentNode.removeChild(goalcell);
622 scriptcell.setAttribute("colspan","2");
625 function showSequent() {
626 scriptcell.setAttribute("colspan","1");
627 workarea.appendChild(goalcell);
630 function showDialog(title,content) {
631 dialogTitle.innerHTML = title;
632 dialogContent.innerHTML = content;
633 dialogBox.style.display = "block";
636 function removeElement(id) {
637 var element = document.getElementById(id);
638 element.parentNode.removeChild(element);
643 function getCursorPos() {
645 if (window.getSelection) {
646 var selObj = window.getSelection();
647 savedRange = selObj.getRangeAt(0);
648 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
649 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
650 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
653 else if (document.selection) {
654 savedRange = document.selection.createRange();
655 var bookmark = savedRange.getBookmark();
656 /* FIXME the following works wrong when the document is longer than 65535 chars */
657 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
662 function findNode(list, node, acc) {
663 for (var i = 0; i < list.length; i++) {
664 if (list[i] == node) {
665 // debug("success " + i);
668 if (list[i].hasChildNodes()) {
670 // debug("recursion on node " + i);
671 return (findNode(list[i].childNodes,node,acc))
673 catch (e) { /* debug("recursion failed"); */ }
675 sandbox = document.getElementById("sandbox");
676 dup = list[i].cloneNode(true);
677 sandbox.appendChild(dup);
678 // debug("fail " + i + ": " + sandbox.innerHTML);
679 acc += sandbox.innerHTML.length;
680 sandbox.removeChild(dup);
686 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
689 function readCookie(name) {
690 var nameEQ = name + "=";
691 var ca = document.cookie.split(';');
692 for(var i=0;i < ca.length;i++) {
694 while (c.charAt(0)==' ') c = c.substring(1,c.length);
695 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
700 function delete_cookie ( cookie_name )
702 var cookie_date = new Date(); // current date & time
703 cookie_date.setTime ( cookie_date.getTime() - 1 );
704 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
707 function delete_session()
709 delete_cookie("session");