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(adjust) {
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.setStart(savedsc,savedso + adjust);
121 range.collapse(true);
125 if (document.createRange)//non IE and no selection
127 window.getSelection().addRange(savedRange);
130 if (document.selection)//IE
137 function lookup_tex(texmacro)
139 texmacro = texmacro.substring(1);
140 return unescape(macro2utf8[texmacro]);
146 pressmesg('keypress',e);
147 var s = string_of_key(e.charCode);
150 i = unlocked.innerHTML.lastIndexOf('\\',j);
152 match = unlocked.innerHTML.substring(i,j);
153 pre = unlocked.innerHTML.substring(0,i);
154 post = unlocked.innerHTML.substring(j);
156 sym = lookup_tex(match);
157 if (typeof sym != "undefined") {
158 unlocked.innerHTML = pre + sym + " " + post;
159 restoreSelection(sym.length - match.length);
160 return suppressdefault(e,true);
163 // restoreSelection(0);
164 return suppressdefault(e,false);
167 else return suppressdefault(e,false);
169 return suppressdefault(e,false);
175 // internet explorer (v.9) doesn't work with innerHTML
176 // but google chrome's innerText is, in a sense, "write only"
177 // what should we do?
178 logarea.innerText = txt + "\n" + logarea.innerText;
184 debug("hd of '" + l + "' = '" + ar[0] + "'");
192 debug("tl of '" + l + "' = '" + tl + "'");
196 function listcons(x,l)
198 debug("cons '" + x + "' on '" + l + "'");
199 return (x + "#" + l);
212 function fold_left (f,acc,l)
215 { debug("'" + l + "' is fold end");
218 { debug("'" + l + "' is fold cons");
219 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
222 function listiter (f,l)
225 { debug("'" + l + "' is nil");
230 debug("'" + l + "' is not nil");
232 listiter(f,listtl(l));
236 function listmap (f,l)
238 debug("listmap on " + l);
240 { debug("returning listnil");
244 { debug("cons f(hd) map(f,tl)");
245 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
249 var statements = listnil();
252 var metalist = listnil();
254 function pairmap (f,p)
256 debug("pairmap of '" + p + "'");
258 return (f(ar[0],ar[1]));
261 function tripletmap (f,p)
263 debug("tripletmap of '" + p + "'");
265 return (f(ar[0],ar[1],ar[2]));
271 return (pairmap (function (a,b) { return (a); }, p));
277 return (tripletmap (function (a,b,c) { return (a); }, p));
283 return (tripletmap (function (a,b,c) { return (b); }, p));
289 return (tripletmap (function (a,b,c) { return (c); }, p));
292 function populate_goalarray(menv)
294 debug("metasenv.length = " + menv.length);
295 if (menv.length == 0) {
301 goalarray = new Array();
302 metalist = listnil();
303 var tmp_goallist = "";
304 for (i = 0; i < menv.length; i++) {
305 metano = menv[i].getAttribute("number");
306 metaname = menv[i].childNodes[0].childNodes[0].data;
307 goal = menv[i].childNodes[1].childNodes[0].data;
308 debug ("found meta n. " + metano);
309 debug ("found goal\nBEGIN" + goal + "\nEND");
310 goalarray[metano] = goal;
311 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
312 metalist = listcons(metano,metalist);
313 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
315 goals.innerHTML = tmp_goallist;
316 debug("new metalist is '" + metalist + "'");
317 if (is_nil(metalist)) {
321 switch_goal(listhd(metalist));
326 function switch_goal(meta)
328 if (typeof meta == "undefined") {
329 goalview.innerHTML = "";
332 debug("switch_goal " + meta + "\n" + goalarray[meta]);
333 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
337 String.prototype.sescape = function() {
343 result = result.replace(patt1,"%25");
344 result = result.replace(patt2,"%3D");
345 result = result.replace(patt3,"%26");
346 result = result.replace(patt4,"%2B");
350 String.prototype.unescapeHTML = function()
352 var patt1 = /<br(\/|)>/gi;
353 var patt2 = /</gi;
354 var patt3 = />/gi;
356 result = result.replace(patt1,"\n");
357 result = result.replace(patt2,"<");
358 result = result.replace(patt3,">");
359 return (unescape(result));
364 advanceButton.disabled = true;
365 retractButton.disabled = true;
366 cursorButton.disabled = true;
367 bottomButton.disabled = true;
372 advanceButton.disabled = false;
373 retractButton.disabled = false;
374 cursorButton.disabled = false;
375 bottomButton.disabled = false;
378 function is_defined(x)
380 return (typeof x != "undefined");
383 /* servicename: name of the service being called
384 * reqbody: text of the request
385 * processResponse: processes the server response
386 * (takes the response text in input, undefined in case of error)
388 function callServer(servicename,processResponse,reqbody)
392 if (window.XMLHttpRequest)
394 req = new XMLHttpRequest();
396 else if (window.ActiveXObject)
399 req = new ActiveXObject("Msxml2.XMLHTTP");
403 req = new ActiveXObject("Microsoft.XMLHTTP");
407 req.onreadystatechange = function()
415 stxt = req.statusText;
418 debug(req.responseText);
419 if (window.DOMParser) {
420 parser=new DOMParser();
421 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
423 else // Internet Explorer
425 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
426 xmlDoc.async="false";
427 xmlDoc.loadXML(req.responseText);
429 processResponse(xmlDoc);
435 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
436 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
445 function advanceForm1()
447 processor = function(xml) {
448 if (is_defined(xml)) {
449 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
450 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
451 len0 = unlocked.innerHTML.length;
452 unescaped = unlocked.innerHTML.unescapeHTML();
453 parsedtxt = unescaped.substr(0,len);
454 unparsedtxt = unescaped.substr(len);
455 locked.innerHTML = locked.innerHTML + parsedtxt;
456 unlocked.innerHTML = unparsedtxt;
457 len1 = unlocked.innerHTML.length;
459 metasenv = xml.getElementsByTagName("meta");
460 populate_goalarray(metasenv);
461 statements = listcons(len2,statements);
462 unlocked.scrollIntoView(true);
464 debug("advance failed");
469 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
473 function gotoBottom()
475 processor = function(xml) {
476 if (is_defined(xml)) {
477 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
478 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
479 len0 = unlocked.innerHTML.length;
480 unescaped = unlocked.innerHTML.unescapeHTML();
481 parsedtxt = unescaped.substr(0,len);
482 unparsedtxt = unescaped.substr(len);
483 locked.innerHTML = locked.innerHTML + parsedtxt;
484 unlocked.innerHTML = unparsedtxt;
485 len1 = unlocked.innerHTML.length;
487 metasenv = xml.getElementsByTagName("meta");
488 populate_goalarray(metasenv);
489 statements = listcons(len2,statements);
490 unlocked.scrollIntoView(true);
492 debug("goto bottom failed");
497 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
502 function gotoPos(offset)
504 if (!is_defined(offset)) {
505 offset = getCursorPos();
507 processor = function(xml) {
508 if (is_defined(xml)) {
509 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
510 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
511 len0 = unlocked.innerHTML.length;
512 unescaped = unlocked.innerHTML.unescapeHTML();
513 parsedtxt = unescaped.substr(0,len);
514 unparsedtxt = unescaped.substr(len);
515 locked.innerHTML = locked.innerHTML + parsedtxt;
516 unlocked.innerHTML = unparsedtxt;
517 len1 = unlocked.innerHTML.length;
519 metasenv = xml.getElementsByTagName("meta");
520 // populate_goalarray(metasenv);
521 statements = listcons(len2,statements);
522 unlocked.scrollIntoView(true);
523 // la populate non andrebbe fatta a ogni passo
525 populate_goalarray(metasenv);
528 gotoPos(offset - len);
531 unlocked.scrollIntoView(true);
532 populate_goalarray(metasenv);
537 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
542 processor = function(xml) {
543 if (typeof xml != "undefined") {
544 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
545 statementlen = parseInt(listhd(statements));
546 statements = listtl(statements);
547 lockedlen = locked.innerHTML.length - statementlen;
548 statement = locked.innerHTML.substr(lockedlen, statementlen);
549 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
550 unlocked.innerHTML = statement + unlocked.innerHTML;
551 metasenv = xml.getElementsByTagName("meta");
552 populate_goalarray(metasenv);
553 unlocked.scrollIntoView(true);
555 debug("retract failed");
561 callServer("retract",processor);
567 processor = function(xml)
569 if (is_defined(xml)) {
570 locked.innerHTML = "";
571 unlocked.innerHTML = xml.documentElement.textContent;
573 debug("file open failed");
576 callServer("open",processor,"file=" + escape(filename.value));
579 function retrieveFile(thefile)
581 processor = function(xml)
583 if (is_defined(xml)) {
584 locked.innerHTML = "";
585 unlocked.innerHTML = xml.documentElement.textContent;
587 debug("file open failed");
590 dialogBox.style.display = "none";
591 callServer("open",processor,"file=" + escape(thefile));
594 function showLibrary()
598 if (window.XMLHttpRequest)
600 req = new XMLHttpRequest();
602 else if (window.ActiveXObject)
605 req = new ActiveXObject("Msxml2.XMLHTTP");
609 req = new ActiveXObject("Microsoft.XMLHTTP");
613 req.onreadystatechange = function()
621 stxt = req.statusText;
624 debug(req.responseText);
625 showDialog("<H2>Library</H2>",req.responseText);
629 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
630 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
637 function hideSequent() {
638 goalcell.parentNode.removeChild(goalcell);
639 scriptcell.setAttribute("colspan","2");
642 function showSequent() {
643 scriptcell.setAttribute("colspan","1");
644 workarea.appendChild(goalcell);
647 function showDialog(title,content) {
648 dialogTitle.innerHTML = title;
649 dialogContent.innerHTML = content;
650 dialogBox.style.display = "block";
653 function removeElement(id) {
654 var element = document.getElementById(id);
655 element.parentNode.removeChild(element);
661 function getCursorPos() {
663 if (window.getSelection) {
664 var selObj = window.getSelection();
665 savedRange = selObj.getRangeAt(0);
666 savedsc = savedRange.startContainer;
667 savedso = savedRange.startOffset;
668 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
669 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
670 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
673 else if (document.selection) {
674 savedRange = document.selection.createRange();
675 var bookmark = savedRange.getBookmark();
676 /* FIXME the following works wrong when the document is longer than 65535 chars */
677 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
682 function findNode(list, node, acc) {
683 for (var i = 0; i < list.length; i++) {
684 if (list[i] == node) {
685 // debug("success " + i);
688 if (list[i].hasChildNodes()) {
690 // debug("recursion on node " + i);
691 return (findNode(list[i].childNodes,node,acc))
693 catch (e) { /* debug("recursion failed"); */ }
695 sandbox = document.getElementById("sandbox");
696 dup = list[i].cloneNode(true);
697 sandbox.appendChild(dup);
698 // debug("fail " + i + ": " + sandbox.innerHTML);
699 acc += sandbox.innerHTML.length;
700 sandbox.removeChild(dup);
706 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
709 function readCookie(name) {
710 var nameEQ = name + "=";
711 var ca = document.cookie.split(';');
712 for(var i=0;i < ca.length;i++) {
714 while (c.charAt(0)==' ') c = c.substring(1,c.length);
715 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
720 function delete_cookie ( cookie_name )
722 var cookie_date = new Date(); // current date & time
723 cookie_date.setTime ( cookie_date.getTime() - 1 );
724 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
727 function delete_session()
729 delete_cookie("session");