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-1);
122 post = unlocked.innerHTML.substring(j+1);
123 if (match == '\\to') {
124 unlocked.innerHTML = pre + "-> " + post;
125 return suppressdefault(e,true);
127 else return suppressdefault(e,false);
129 else return suppressdefault(e,false);
131 return suppressdefault(e,false);
137 // internet explorer (v.9) doesn't work with innerHTML
138 // but google chrome's innerText is, in a sense, "write only"
139 // what should we do?
140 logarea.innerText = txt + "\n" + logarea.innerText;
146 debug("hd of '" + l + "' = '" + ar[0] + "'");
154 debug("tl of '" + l + "' = '" + tl + "'");
158 function listcons(x,l)
160 debug("cons '" + x + "' on '" + l + "'");
161 return (x + "#" + l);
174 function fold_left (f,acc,l)
177 { debug("'" + l + "' is fold end");
180 { debug("'" + l + "' is fold cons");
181 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
184 function listiter (f,l)
187 { debug("'" + l + "' is nil");
192 debug("'" + l + "' is not nil");
194 listiter(f,listtl(l));
198 function listmap (f,l)
200 debug("listmap on " + l);
202 { debug("returning listnil");
206 { debug("cons f(hd) map(f,tl)");
207 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
211 var statements = listnil();
214 var metalist = listnil();
216 function pairmap (f,p)
218 debug("pairmap of '" + p + "'");
220 return (f(ar[0],ar[1]));
223 function tripletmap (f,p)
225 debug("tripletmap of '" + p + "'");
227 return (f(ar[0],ar[1],ar[2]));
233 return (pairmap (function (a,b) { return (a); }, p));
239 return (tripletmap (function (a,b,c) { return (a); }, p));
245 return (tripletmap (function (a,b,c) { return (b); }, p));
251 return (tripletmap (function (a,b,c) { return (c); }, p));
254 function populate_goalarray(menv)
256 debug("metasenv.length = " + menv.length);
257 if (menv.length == 0) {
263 goalarray = new Array();
264 metalist = listnil();
265 var tmp_goallist = "";
266 for (i = 0; i < menv.length; i++) {
267 metano = menv[i].getAttribute("number");
268 metaname = menv[i].childNodes[0].childNodes[0].data;
269 goal = menv[i].childNodes[1].childNodes[0].data;
270 debug ("found meta n. " + metano);
271 debug ("found goal\nBEGIN" + goal + "\nEND");
272 goalarray[metano] = goal;
273 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
274 metalist = listcons(metano,metalist);
275 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
277 goals.innerHTML = tmp_goallist;
278 debug("new metalist is '" + metalist + "'");
279 if (is_nil(metalist)) {
283 switch_goal(listhd(metalist));
288 function switch_goal(meta)
290 if (typeof meta == "undefined") {
291 goalview.innerHTML = "";
294 debug("switch_goal " + meta + "\n" + goalarray[meta]);
295 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
299 String.prototype.sescape = function() {
305 result = result.replace(patt1,"%25");
306 result = result.replace(patt2,"%3D");
307 result = result.replace(patt3,"%26");
308 result = result.replace(patt4,"%2B");
312 String.prototype.unescapeHTML = function()
314 var patt1 = /<br(\/|)>/gi;
315 var patt2 = /</gi;
316 var patt3 = />/gi;
318 result = result.replace(patt1,"\n");
319 result = result.replace(patt2,"<");
320 result = result.replace(patt3,">");
321 return (unescape(result));
326 advanceButton.disabled = true;
327 retractButton.disabled = true;
328 cursorButton.disabled = true;
329 bottomButton.disabled = true;
334 advanceButton.disabled = false;
335 retractButton.disabled = false;
336 cursorButton.disabled = false;
337 bottomButton.disabled = false;
340 function is_defined(x)
342 return (typeof x != "undefined");
345 /* servicename: name of the service being called
346 * reqbody: text of the request
347 * processResponse: processes the server response
348 * (takes the response text in input, undefined in case of error)
350 function callServer(servicename,processResponse,reqbody)
354 if (window.XMLHttpRequest)
356 req = new XMLHttpRequest();
358 else if (window.ActiveXObject)
361 req = new ActiveXObject("Msxml2.XMLHTTP");
365 req = new ActiveXObject("Microsoft.XMLHTTP");
369 req.onreadystatechange = function()
377 stxt = req.statusText;
380 debug(req.responseText);
381 if (window.DOMParser) {
382 parser=new DOMParser();
383 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
385 else // Internet Explorer
387 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
388 xmlDoc.async="false";
389 xmlDoc.loadXML(req.responseText);
391 processResponse(xmlDoc);
397 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
398 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
407 function advanceForm1()
409 processor = function(xml) {
410 if (is_defined(xml)) {
411 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
412 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
413 len0 = unlocked.innerHTML.length;
414 unescaped = unlocked.innerHTML.unescapeHTML();
415 parsedtxt = unescaped.substr(0,len);
416 unparsedtxt = unescaped.substr(len);
417 locked.innerHTML = locked.innerHTML + parsedtxt;
418 unlocked.innerHTML = unparsedtxt;
419 len1 = unlocked.innerHTML.length;
421 metasenv = xml.getElementsByTagName("meta");
422 populate_goalarray(metasenv);
423 statements = listcons(len2,statements);
424 unlocked.scrollIntoView(true);
426 debug("advance failed");
431 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
435 function gotoBottom()
437 processor = function(xml) {
438 if (is_defined(xml)) {
439 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
440 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
441 len0 = unlocked.innerHTML.length;
442 unescaped = unlocked.innerHTML.unescapeHTML();
443 parsedtxt = unescaped.substr(0,len);
444 unparsedtxt = unescaped.substr(len);
445 locked.innerHTML = locked.innerHTML + parsedtxt;
446 unlocked.innerHTML = unparsedtxt;
447 len1 = unlocked.innerHTML.length;
449 metasenv = xml.getElementsByTagName("meta");
450 populate_goalarray(metasenv);
451 statements = listcons(len2,statements);
452 unlocked.scrollIntoView(true);
454 debug("goto bottom failed");
459 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
464 function gotoPos(offset)
466 if (!is_defined(offset)) {
467 offset = getCursorPos();
469 processor = function(xml) {
470 if (is_defined(xml)) {
471 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
472 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
473 len0 = unlocked.innerHTML.length;
474 unescaped = unlocked.innerHTML.unescapeHTML();
475 parsedtxt = unescaped.substr(0,len);
476 unparsedtxt = unescaped.substr(len);
477 locked.innerHTML = locked.innerHTML + parsedtxt;
478 unlocked.innerHTML = unparsedtxt;
479 len1 = unlocked.innerHTML.length;
481 metasenv = xml.getElementsByTagName("meta");
482 // populate_goalarray(metasenv);
483 statements = listcons(len2,statements);
484 unlocked.scrollIntoView(true);
485 // la populate non andrebbe fatta a ogni passo
487 populate_goalarray(metasenv);
490 gotoPos(offset - len);
493 unlocked.scrollIntoView(true);
494 populate_goalarray(metasenv);
499 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
504 processor = function(xml) {
505 if (typeof xml != "undefined") {
506 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
507 statementlen = parseInt(listhd(statements));
508 statements = listtl(statements);
509 lockedlen = locked.innerHTML.length - statementlen;
510 statement = locked.innerHTML.substr(lockedlen, statementlen);
511 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
512 unlocked.innerHTML = statement + unlocked.innerHTML;
513 metasenv = xml.getElementsByTagName("meta");
514 populate_goalarray(metasenv);
515 unlocked.scrollIntoView(true);
517 debug("retract failed");
523 callServer("retract",processor);
529 processor = function(xml)
531 if (is_defined(xml)) {
532 locked.innerHTML = "";
533 unlocked.innerHTML = xml.documentElement.textContent;
535 debug("file open failed");
538 callServer("open",processor,"file=" + escape(filename.value));
541 function retrieveFile(thefile)
543 processor = function(xml)
545 if (is_defined(xml)) {
546 locked.innerHTML = "";
547 unlocked.innerHTML = xml.documentElement.textContent;
549 debug("file open failed");
552 dialogBox.style.display = "none";
553 callServer("open",processor,"file=" + escape(thefile));
556 function showLibrary()
560 if (window.XMLHttpRequest)
562 req = new XMLHttpRequest();
564 else if (window.ActiveXObject)
567 req = new ActiveXObject("Msxml2.XMLHTTP");
571 req = new ActiveXObject("Microsoft.XMLHTTP");
575 req.onreadystatechange = function()
583 stxt = req.statusText;
586 debug(req.responseText);
587 showDialog("<H2>Library</H2>",req.responseText);
591 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
592 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
599 function hideSequent() {
600 goalcell.parentNode.removeChild(goalcell);
601 scriptcell.setAttribute("colspan","2");
604 function showSequent() {
605 scriptcell.setAttribute("colspan","1");
606 workarea.appendChild(goalcell);
609 function showDialog(title,content) {
610 dialogTitle.innerHTML = title;
611 dialogContent.innerHTML = content;
612 dialogBox.style.display = "block";
615 function removeElement(id) {
616 var element = document.getElementById(id);
617 element.parentNode.removeChild(element);
620 function getCursorPos() {
622 if (window.getSelection) {
623 var selObj = window.getSelection();
624 var selRange = selObj.getRangeAt(0);
625 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
626 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
627 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
630 else if (document.selection) {
631 var range = document.selection.createRange();
632 var bookmark = range.getBookmark();
633 /* FIXME the following works wrong when the document is longer than 65535 chars */
634 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
639 function findNode(list, node, acc) {
640 for (var i = 0; i < list.length; i++) {
641 if (list[i] == node) {
642 // debug("success " + i);
645 if (list[i].hasChildNodes()) {
647 // debug("recursion on node " + i);
648 return (findNode(list[i].childNodes,node,acc))
650 catch (e) { /* debug("recursion failed"); */ }
652 sandbox = document.getElementById("sandbox");
653 dup = list[i].cloneNode(true);
654 sandbox.appendChild(dup);
655 // debug("fail " + i + ": " + sandbox.innerHTML);
656 acc += sandbox.innerHTML.length;
657 sandbox.removeChild(dup);
663 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
666 function readCookie(name) {
667 var nameEQ = name + "=";
668 var ca = document.cookie.split(';');
669 for(var i=0;i < ca.length;i++) {
671 while (c.charAt(0)==' ') c = c.substring(1,c.length);
672 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
677 function delete_cookie ( cookie_name )
679 var cookie_date = new Date(); // current date & time
680 cookie_date.setTime ( cookie_date.getTime() - 1 );
681 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
684 function delete_session()
686 delete_cookie("session");