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 s.addRange(savedRange);
122 if (document.createRange)//non IE and no selection
124 window.getSelection().addRange(savedRange);
127 if (document.selection)//IE
137 pressmesg('keypress',e);
138 var s = string_of_key(e.charCode);
141 i = unlocked.innerHTML.lastIndexOf('\\',j);
143 match = unlocked.innerHTML.substring(i,j);
144 pre = unlocked.innerHTML.substring(0,i);
145 post = unlocked.innerHTML.substring(j);
146 if (match == '\\to') {
147 unlocked.innerHTML = pre + "-> " + post;
149 return suppressdefault(e,true);
152 else return suppressdefault(e,false);
154 else return suppressdefault(e,false);
156 return suppressdefault(e,false);
162 // internet explorer (v.9) doesn't work with innerHTML
163 // but google chrome's innerText is, in a sense, "write only"
164 // what should we do?
165 logarea.innerText = txt + "\n" + logarea.innerText;
171 debug("hd of '" + l + "' = '" + ar[0] + "'");
179 debug("tl of '" + l + "' = '" + tl + "'");
183 function listcons(x,l)
185 debug("cons '" + x + "' on '" + l + "'");
186 return (x + "#" + l);
199 function fold_left (f,acc,l)
202 { debug("'" + l + "' is fold end");
205 { debug("'" + l + "' is fold cons");
206 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
209 function listiter (f,l)
212 { debug("'" + l + "' is nil");
217 debug("'" + l + "' is not nil");
219 listiter(f,listtl(l));
223 function listmap (f,l)
225 debug("listmap on " + l);
227 { debug("returning listnil");
231 { debug("cons f(hd) map(f,tl)");
232 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
236 var statements = listnil();
239 var metalist = listnil();
241 function pairmap (f,p)
243 debug("pairmap of '" + p + "'");
245 return (f(ar[0],ar[1]));
248 function tripletmap (f,p)
250 debug("tripletmap of '" + p + "'");
252 return (f(ar[0],ar[1],ar[2]));
258 return (pairmap (function (a,b) { return (a); }, p));
264 return (tripletmap (function (a,b,c) { return (a); }, p));
270 return (tripletmap (function (a,b,c) { return (b); }, p));
276 return (tripletmap (function (a,b,c) { return (c); }, p));
279 function populate_goalarray(menv)
281 debug("metasenv.length = " + menv.length);
282 if (menv.length == 0) {
288 goalarray = new Array();
289 metalist = listnil();
290 var tmp_goallist = "";
291 for (i = 0; i < menv.length; i++) {
292 metano = menv[i].getAttribute("number");
293 metaname = menv[i].childNodes[0].childNodes[0].data;
294 goal = menv[i].childNodes[1].childNodes[0].data;
295 debug ("found meta n. " + metano);
296 debug ("found goal\nBEGIN" + goal + "\nEND");
297 goalarray[metano] = goal;
298 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
299 metalist = listcons(metano,metalist);
300 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
302 goals.innerHTML = tmp_goallist;
303 debug("new metalist is '" + metalist + "'");
304 if (is_nil(metalist)) {
308 switch_goal(listhd(metalist));
313 function switch_goal(meta)
315 if (typeof meta == "undefined") {
316 goalview.innerHTML = "";
319 debug("switch_goal " + meta + "\n" + goalarray[meta]);
320 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
324 String.prototype.sescape = function() {
330 result = result.replace(patt1,"%25");
331 result = result.replace(patt2,"%3D");
332 result = result.replace(patt3,"%26");
333 result = result.replace(patt4,"%2B");
337 String.prototype.unescapeHTML = function()
339 var patt1 = /<br(\/|)>/gi;
340 var patt2 = /</gi;
341 var patt3 = />/gi;
343 result = result.replace(patt1,"\n");
344 result = result.replace(patt2,"<");
345 result = result.replace(patt3,">");
346 return (unescape(result));
351 advanceButton.disabled = true;
352 retractButton.disabled = true;
353 cursorButton.disabled = true;
354 bottomButton.disabled = true;
359 advanceButton.disabled = false;
360 retractButton.disabled = false;
361 cursorButton.disabled = false;
362 bottomButton.disabled = false;
365 function is_defined(x)
367 return (typeof x != "undefined");
370 /* servicename: name of the service being called
371 * reqbody: text of the request
372 * processResponse: processes the server response
373 * (takes the response text in input, undefined in case of error)
375 function callServer(servicename,processResponse,reqbody)
379 if (window.XMLHttpRequest)
381 req = new XMLHttpRequest();
383 else if (window.ActiveXObject)
386 req = new ActiveXObject("Msxml2.XMLHTTP");
390 req = new ActiveXObject("Microsoft.XMLHTTP");
394 req.onreadystatechange = function()
402 stxt = req.statusText;
405 debug(req.responseText);
406 if (window.DOMParser) {
407 parser=new DOMParser();
408 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
410 else // Internet Explorer
412 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
413 xmlDoc.async="false";
414 xmlDoc.loadXML(req.responseText);
416 processResponse(xmlDoc);
422 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
423 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
432 function advanceForm1()
434 processor = function(xml) {
435 if (is_defined(xml)) {
436 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
437 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
438 len0 = unlocked.innerHTML.length;
439 unescaped = unlocked.innerHTML.unescapeHTML();
440 parsedtxt = unescaped.substr(0,len);
441 unparsedtxt = unescaped.substr(len);
442 locked.innerHTML = locked.innerHTML + parsedtxt;
443 unlocked.innerHTML = unparsedtxt;
444 len1 = unlocked.innerHTML.length;
446 metasenv = xml.getElementsByTagName("meta");
447 populate_goalarray(metasenv);
448 statements = listcons(len2,statements);
449 unlocked.scrollIntoView(true);
451 debug("advance failed");
456 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
460 function gotoBottom()
462 processor = function(xml) {
463 if (is_defined(xml)) {
464 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
465 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
466 len0 = unlocked.innerHTML.length;
467 unescaped = unlocked.innerHTML.unescapeHTML();
468 parsedtxt = unescaped.substr(0,len);
469 unparsedtxt = unescaped.substr(len);
470 locked.innerHTML = locked.innerHTML + parsedtxt;
471 unlocked.innerHTML = unparsedtxt;
472 len1 = unlocked.innerHTML.length;
474 metasenv = xml.getElementsByTagName("meta");
475 populate_goalarray(metasenv);
476 statements = listcons(len2,statements);
477 unlocked.scrollIntoView(true);
479 debug("goto bottom failed");
484 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
489 function gotoPos(offset)
491 if (!is_defined(offset)) {
492 offset = getCursorPos();
494 processor = function(xml) {
495 if (is_defined(xml)) {
496 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
497 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
498 len0 = unlocked.innerHTML.length;
499 unescaped = unlocked.innerHTML.unescapeHTML();
500 parsedtxt = unescaped.substr(0,len);
501 unparsedtxt = unescaped.substr(len);
502 locked.innerHTML = locked.innerHTML + parsedtxt;
503 unlocked.innerHTML = unparsedtxt;
504 len1 = unlocked.innerHTML.length;
506 metasenv = xml.getElementsByTagName("meta");
507 // populate_goalarray(metasenv);
508 statements = listcons(len2,statements);
509 unlocked.scrollIntoView(true);
510 // la populate non andrebbe fatta a ogni passo
512 populate_goalarray(metasenv);
515 gotoPos(offset - len);
518 unlocked.scrollIntoView(true);
519 populate_goalarray(metasenv);
524 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
529 processor = function(xml) {
530 if (typeof xml != "undefined") {
531 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
532 statementlen = parseInt(listhd(statements));
533 statements = listtl(statements);
534 lockedlen = locked.innerHTML.length - statementlen;
535 statement = locked.innerHTML.substr(lockedlen, statementlen);
536 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
537 unlocked.innerHTML = statement + unlocked.innerHTML;
538 metasenv = xml.getElementsByTagName("meta");
539 populate_goalarray(metasenv);
540 unlocked.scrollIntoView(true);
542 debug("retract failed");
548 callServer("retract",processor);
554 processor = function(xml)
556 if (is_defined(xml)) {
557 locked.innerHTML = "";
558 unlocked.innerHTML = xml.documentElement.textContent;
560 debug("file open failed");
563 callServer("open",processor,"file=" + escape(filename.value));
566 function retrieveFile(thefile)
568 processor = function(xml)
570 if (is_defined(xml)) {
571 locked.innerHTML = "";
572 unlocked.innerHTML = xml.documentElement.textContent;
574 debug("file open failed");
577 dialogBox.style.display = "none";
578 callServer("open",processor,"file=" + escape(thefile));
581 function showLibrary()
585 if (window.XMLHttpRequest)
587 req = new XMLHttpRequest();
589 else if (window.ActiveXObject)
592 req = new ActiveXObject("Msxml2.XMLHTTP");
596 req = new ActiveXObject("Microsoft.XMLHTTP");
600 req.onreadystatechange = function()
608 stxt = req.statusText;
611 debug(req.responseText);
612 showDialog("<H2>Library</H2>",req.responseText);
616 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
617 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
624 function hideSequent() {
625 goalcell.parentNode.removeChild(goalcell);
626 scriptcell.setAttribute("colspan","2");
629 function showSequent() {
630 scriptcell.setAttribute("colspan","1");
631 workarea.appendChild(goalcell);
634 function showDialog(title,content) {
635 dialogTitle.innerHTML = title;
636 dialogContent.innerHTML = content;
637 dialogBox.style.display = "block";
640 function removeElement(id) {
641 var element = document.getElementById(id);
642 element.parentNode.removeChild(element);
647 function getCursorPos() {
649 if (window.getSelection) {
650 var selObj = window.getSelection();
651 savedRange = selObj.getRangeAt(0);
652 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
653 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
654 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
657 else if (document.selection) {
658 savedRange = document.selection.createRange();
659 var bookmark = savedRange.getBookmark();
660 /* FIXME the following works wrong when the document is longer than 65535 chars */
661 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
666 function findNode(list, node, acc) {
667 for (var i = 0; i < list.length; i++) {
668 if (list[i] == node) {
669 // debug("success " + i);
672 if (list[i].hasChildNodes()) {
674 // debug("recursion on node " + i);
675 return (findNode(list[i].childNodes,node,acc))
677 catch (e) { /* debug("recursion failed"); */ }
679 sandbox = document.getElementById("sandbox");
680 dup = list[i].cloneNode(true);
681 sandbox.appendChild(dup);
682 // debug("fail " + i + ": " + sandbox.innerHTML);
683 acc += sandbox.innerHTML.length;
684 sandbox.removeChild(dup);
690 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
693 function readCookie(name) {
694 var nameEQ = name + "=";
695 var ca = document.cookie.split(';');
696 for(var i=0;i < ca.length;i++) {
698 while (c.charAt(0)==' ') c = c.substring(1,c.length);
699 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
704 function delete_cookie ( cookie_name )
706 var cookie_date = new Date(); // current date & time
707 cookie_date.setTime ( cookie_date.getTime() - 1 );
708 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
711 function delete_session()
713 delete_cookie("session");