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();
111 function restoreSelection() {
113 if (savedRange != null) {
114 if (window.getSelection)//non IE and there is already a selection
116 var s = window.getSelection();
117 if (s.rangeCount > 0)
119 range = document.createRange();
120 range.startContainer = savedsc;
121 range.startOffset = savedso;
122 range.collapse(true);
126 if (document.createRange)//non IE and no selection
128 window.getSelection().addRange(savedRange);
131 if (document.selection)//IE
141 pressmesg('keypress',e);
142 var s = string_of_key(e.charCode);
145 i = unlocked.innerHTML.lastIndexOf('\\',j);
147 match = unlocked.innerHTML.substring(i,j);
148 pre = unlocked.innerHTML.substring(0,i);
149 post = unlocked.innerHTML.substring(j);
150 if (match == '\\to') {
151 unlocked.innerHTML = pre + "-> " + post;
153 return suppressdefault(e,true);
157 return suppressdefault(e,false);
160 else return suppressdefault(e,false);
162 return suppressdefault(e,false);
168 // internet explorer (v.9) doesn't work with innerHTML
169 // but google chrome's innerText is, in a sense, "write only"
170 // what should we do?
171 logarea.innerText = txt + "\n" + logarea.innerText;
177 debug("hd of '" + l + "' = '" + ar[0] + "'");
185 debug("tl of '" + l + "' = '" + tl + "'");
189 function listcons(x,l)
191 debug("cons '" + x + "' on '" + l + "'");
192 return (x + "#" + l);
205 function fold_left (f,acc,l)
208 { debug("'" + l + "' is fold end");
211 { debug("'" + l + "' is fold cons");
212 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
215 function listiter (f,l)
218 { debug("'" + l + "' is nil");
223 debug("'" + l + "' is not nil");
225 listiter(f,listtl(l));
229 function listmap (f,l)
231 debug("listmap on " + l);
233 { debug("returning listnil");
237 { debug("cons f(hd) map(f,tl)");
238 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
242 var statements = listnil();
245 var metalist = listnil();
247 function pairmap (f,p)
249 debug("pairmap of '" + p + "'");
251 return (f(ar[0],ar[1]));
254 function tripletmap (f,p)
256 debug("tripletmap of '" + p + "'");
258 return (f(ar[0],ar[1],ar[2]));
264 return (pairmap (function (a,b) { return (a); }, p));
270 return (tripletmap (function (a,b,c) { return (a); }, p));
276 return (tripletmap (function (a,b,c) { return (b); }, p));
282 return (tripletmap (function (a,b,c) { return (c); }, p));
285 function populate_goalarray(menv)
287 debug("metasenv.length = " + menv.length);
288 if (menv.length == 0) {
294 goalarray = new Array();
295 metalist = listnil();
296 var tmp_goallist = "";
297 for (i = 0; i < menv.length; i++) {
298 metano = menv[i].getAttribute("number");
299 metaname = menv[i].childNodes[0].childNodes[0].data;
300 goal = menv[i].childNodes[1].childNodes[0].data;
301 debug ("found meta n. " + metano);
302 debug ("found goal\nBEGIN" + goal + "\nEND");
303 goalarray[metano] = goal;
304 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
305 metalist = listcons(metano,metalist);
306 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
308 goals.innerHTML = tmp_goallist;
309 debug("new metalist is '" + metalist + "'");
310 if (is_nil(metalist)) {
314 switch_goal(listhd(metalist));
319 function switch_goal(meta)
321 if (typeof meta == "undefined") {
322 goalview.innerHTML = "";
325 debug("switch_goal " + meta + "\n" + goalarray[meta]);
326 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
330 String.prototype.sescape = function() {
336 result = result.replace(patt1,"%25");
337 result = result.replace(patt2,"%3D");
338 result = result.replace(patt3,"%26");
339 result = result.replace(patt4,"%2B");
343 String.prototype.unescapeHTML = function()
345 var patt1 = /<br(\/|)>/gi;
346 var patt2 = /</gi;
347 var patt3 = />/gi;
349 result = result.replace(patt1,"\n");
350 result = result.replace(patt2,"<");
351 result = result.replace(patt3,">");
352 return (unescape(result));
357 advanceButton.disabled = true;
358 retractButton.disabled = true;
359 cursorButton.disabled = true;
360 bottomButton.disabled = true;
365 advanceButton.disabled = false;
366 retractButton.disabled = false;
367 cursorButton.disabled = false;
368 bottomButton.disabled = false;
371 function is_defined(x)
373 return (typeof x != "undefined");
376 /* servicename: name of the service being called
377 * reqbody: text of the request
378 * processResponse: processes the server response
379 * (takes the response text in input, undefined in case of error)
381 function callServer(servicename,processResponse,reqbody)
385 if (window.XMLHttpRequest)
387 req = new XMLHttpRequest();
389 else if (window.ActiveXObject)
392 req = new ActiveXObject("Msxml2.XMLHTTP");
396 req = new ActiveXObject("Microsoft.XMLHTTP");
400 req.onreadystatechange = function()
408 stxt = req.statusText;
411 debug(req.responseText);
412 if (window.DOMParser) {
413 parser=new DOMParser();
414 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
416 else // Internet Explorer
418 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
419 xmlDoc.async="false";
420 xmlDoc.loadXML(req.responseText);
422 processResponse(xmlDoc);
428 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
429 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
438 function advanceForm1()
440 processor = function(xml) {
441 if (is_defined(xml)) {
442 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
443 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
444 len0 = unlocked.innerHTML.length;
445 unescaped = unlocked.innerHTML.unescapeHTML();
446 parsedtxt = unescaped.substr(0,len);
447 unparsedtxt = unescaped.substr(len);
448 locked.innerHTML = locked.innerHTML + parsedtxt;
449 unlocked.innerHTML = unparsedtxt;
450 len1 = unlocked.innerHTML.length;
452 metasenv = xml.getElementsByTagName("meta");
453 populate_goalarray(metasenv);
454 statements = listcons(len2,statements);
455 unlocked.scrollIntoView(true);
457 debug("advance failed");
462 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
466 function gotoBottom()
468 processor = function(xml) {
469 if (is_defined(xml)) {
470 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
471 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
472 len0 = unlocked.innerHTML.length;
473 unescaped = unlocked.innerHTML.unescapeHTML();
474 parsedtxt = unescaped.substr(0,len);
475 unparsedtxt = unescaped.substr(len);
476 locked.innerHTML = locked.innerHTML + parsedtxt;
477 unlocked.innerHTML = unparsedtxt;
478 len1 = unlocked.innerHTML.length;
480 metasenv = xml.getElementsByTagName("meta");
481 populate_goalarray(metasenv);
482 statements = listcons(len2,statements);
483 unlocked.scrollIntoView(true);
485 debug("goto bottom failed");
490 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
495 function gotoPos(offset)
497 if (!is_defined(offset)) {
498 offset = getCursorPos();
500 processor = function(xml) {
501 if (is_defined(xml)) {
502 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
503 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
504 len0 = unlocked.innerHTML.length;
505 unescaped = unlocked.innerHTML.unescapeHTML();
506 parsedtxt = unescaped.substr(0,len);
507 unparsedtxt = unescaped.substr(len);
508 locked.innerHTML = locked.innerHTML + parsedtxt;
509 unlocked.innerHTML = unparsedtxt;
510 len1 = unlocked.innerHTML.length;
512 metasenv = xml.getElementsByTagName("meta");
513 // populate_goalarray(metasenv);
514 statements = listcons(len2,statements);
515 unlocked.scrollIntoView(true);
516 // la populate non andrebbe fatta a ogni passo
518 populate_goalarray(metasenv);
521 gotoPos(offset - len);
524 unlocked.scrollIntoView(true);
525 populate_goalarray(metasenv);
530 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
535 processor = function(xml) {
536 if (typeof xml != "undefined") {
537 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
538 statementlen = parseInt(listhd(statements));
539 statements = listtl(statements);
540 lockedlen = locked.innerHTML.length - statementlen;
541 statement = locked.innerHTML.substr(lockedlen, statementlen);
542 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
543 unlocked.innerHTML = statement + unlocked.innerHTML;
544 metasenv = xml.getElementsByTagName("meta");
545 populate_goalarray(metasenv);
546 unlocked.scrollIntoView(true);
548 debug("retract failed");
554 callServer("retract",processor);
560 processor = function(xml)
562 if (is_defined(xml)) {
563 locked.innerHTML = "";
564 unlocked.innerHTML = xml.documentElement.textContent;
566 debug("file open failed");
569 callServer("open",processor,"file=" + escape(filename.value));
572 function retrieveFile(thefile)
574 processor = function(xml)
576 if (is_defined(xml)) {
577 locked.innerHTML = "";
578 unlocked.innerHTML = xml.documentElement.textContent;
580 debug("file open failed");
583 dialogBox.style.display = "none";
584 callServer("open",processor,"file=" + escape(thefile));
587 function showLibrary()
591 if (window.XMLHttpRequest)
593 req = new XMLHttpRequest();
595 else if (window.ActiveXObject)
598 req = new ActiveXObject("Msxml2.XMLHTTP");
602 req = new ActiveXObject("Microsoft.XMLHTTP");
606 req.onreadystatechange = function()
614 stxt = req.statusText;
617 debug(req.responseText);
618 showDialog("<H2>Library</H2>",req.responseText);
622 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
623 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
630 function hideSequent() {
631 goalcell.parentNode.removeChild(goalcell);
632 scriptcell.setAttribute("colspan","2");
635 function showSequent() {
636 scriptcell.setAttribute("colspan","1");
637 workarea.appendChild(goalcell);
640 function showDialog(title,content) {
641 dialogTitle.innerHTML = title;
642 dialogContent.innerHTML = content;
643 dialogBox.style.display = "block";
646 function removeElement(id) {
647 var element = document.getElementById(id);
648 element.parentNode.removeChild(element);
654 function getCursorPos() {
656 if (window.getSelection) {
657 var selObj = window.getSelection();
658 savedRange = selObj.getRangeAt(0);
659 savedsc = savedRange.startContainer;
660 savedso = savedRange.startOffset;
661 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
662 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
663 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
666 else if (document.selection) {
667 savedRange = document.selection.createRange();
668 var bookmark = savedRange.getBookmark();
669 /* FIXME the following works wrong when the document is longer than 65535 chars */
670 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
675 function findNode(list, node, acc) {
676 for (var i = 0; i < list.length; i++) {
677 if (list[i] == node) {
678 // debug("success " + i);
681 if (list[i].hasChildNodes()) {
683 // debug("recursion on node " + i);
684 return (findNode(list[i].childNodes,node,acc))
686 catch (e) { /* debug("recursion failed"); */ }
688 sandbox = document.getElementById("sandbox");
689 dup = list[i].cloneNode(true);
690 sandbox.appendChild(dup);
691 // debug("fail " + i + ": " + sandbox.innerHTML);
692 acc += sandbox.innerHTML.length;
693 sandbox.removeChild(dup);
699 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
702 function readCookie(name) {
703 var nameEQ = name + "=";
704 var ca = document.cookie.split(';');
705 for(var i=0;i < ca.length;i++) {
707 while (c.charAt(0)==' ') c = c.substring(1,c.length);
708 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
713 function delete_cookie ( cookie_name )
715 var cookie_date = new Date(); // current date & time
716 cookie_date.setTime ( cookie_date.getTime() - 1 );
717 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
720 function delete_session()
722 delete_cookie("session");