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 len1 = unlocked.innerText.length;
159 unlocked.innerHTML = pre + sym + post;
160 len2 = unlocked.innerText.length;
161 restoreSelection(len2 - len1);
162 return suppressdefault(e,true);
165 // restoreSelection(0);
166 return suppressdefault(e,false);
169 else return suppressdefault(e,false);
171 return suppressdefault(e,false);
177 // internet explorer (v.9) doesn't work with innerHTML
178 // but google chrome's innerText is, in a sense, "write only"
179 // what should we do?
180 logarea.innerText = txt + "\n" + logarea.innerText;
186 debug("hd of '" + l + "' = '" + ar[0] + "'");
194 debug("tl of '" + l + "' = '" + tl + "'");
198 function listcons(x,l)
200 debug("cons '" + x + "' on '" + l + "'");
201 return (x + "#" + l);
214 function fold_left (f,acc,l)
217 { debug("'" + l + "' is fold end");
220 { debug("'" + l + "' is fold cons");
221 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
224 function listiter (f,l)
227 { debug("'" + l + "' is nil");
232 debug("'" + l + "' is not nil");
234 listiter(f,listtl(l));
238 function listmap (f,l)
240 debug("listmap on " + l);
242 { debug("returning listnil");
246 { debug("cons f(hd) map(f,tl)");
247 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
251 var statements = listnil();
254 var metalist = listnil();
256 function pairmap (f,p)
258 debug("pairmap of '" + p + "'");
260 return (f(ar[0],ar[1]));
263 function tripletmap (f,p)
265 debug("tripletmap of '" + p + "'");
267 return (f(ar[0],ar[1],ar[2]));
273 return (pairmap (function (a,b) { return (a); }, p));
279 return (tripletmap (function (a,b,c) { return (a); }, p));
285 return (tripletmap (function (a,b,c) { return (b); }, p));
291 return (tripletmap (function (a,b,c) { return (c); }, p));
294 function populate_goalarray(menv)
296 debug("metasenv.length = " + menv.length);
297 if (menv.length == 0) {
303 goalarray = new Array();
304 metalist = listnil();
305 var tmp_goallist = "";
306 for (i = 0; i < menv.length; i++) {
307 metano = menv[i].getAttribute("number");
308 metaname = menv[i].childNodes[0].childNodes[0].data;
309 goal = menv[i].childNodes[1].childNodes[0].data;
310 debug ("found meta n. " + metano);
311 debug ("found goal\nBEGIN" + goal + "\nEND");
312 goalarray[metano] = goal;
313 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
314 metalist = listcons(metano,metalist);
315 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
317 goals.innerHTML = tmp_goallist;
318 debug("new metalist is '" + metalist + "'");
319 if (is_nil(metalist)) {
323 switch_goal(listhd(metalist));
328 function switch_goal(meta)
330 if (typeof meta == "undefined") {
331 goalview.innerHTML = "";
334 debug("switch_goal " + meta + "\n" + goalarray[meta]);
335 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
339 String.prototype.sescape = function() {
345 result = result.replace(patt1,"%25");
346 result = result.replace(patt2,"%3D");
347 result = result.replace(patt3,"%26");
348 result = result.replace(patt4,"%2B");
352 String.prototype.unescapeHTML = function()
354 var patt1 = /<br(\/|)>/gi;
355 var patt2 = /</gi;
356 var patt3 = />/gi;
358 result = result.replace(patt1,"\n");
359 result = result.replace(patt2,"<");
360 result = result.replace(patt3,">");
361 return (unescape(result));
366 advanceButton.disabled = true;
367 retractButton.disabled = true;
368 cursorButton.disabled = true;
369 bottomButton.disabled = true;
374 advanceButton.disabled = false;
375 retractButton.disabled = false;
376 cursorButton.disabled = false;
377 bottomButton.disabled = false;
380 function is_defined(x)
382 return (typeof x != "undefined");
385 /* servicename: name of the service being called
386 * reqbody: text of the request
387 * processResponse: processes the server response
388 * (takes the response text in input, undefined in case of error)
390 function callServer(servicename,processResponse,reqbody)
394 if (window.XMLHttpRequest)
396 req = new XMLHttpRequest();
398 else if (window.ActiveXObject)
401 req = new ActiveXObject("Msxml2.XMLHTTP");
405 req = new ActiveXObject("Microsoft.XMLHTTP");
409 req.onreadystatechange = function()
417 stxt = req.statusText;
420 debug(req.responseText);
421 if (window.DOMParser) {
422 parser=new DOMParser();
423 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
425 else // Internet Explorer
427 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
428 xmlDoc.async="false";
429 xmlDoc.loadXML(req.responseText);
431 processResponse(xmlDoc);
437 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
438 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
447 function advanceForm1()
449 processor = function(xml) {
450 if (is_defined(xml)) {
451 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
452 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
453 len0 = unlocked.innerHTML.length;
454 unescaped = unlocked.innerHTML.unescapeHTML();
455 parsedtxt = unescaped.substr(0,len);
456 unparsedtxt = unescaped.substr(len);
457 locked.innerHTML = locked.innerHTML + parsedtxt;
458 unlocked.innerHTML = unparsedtxt;
459 len1 = unlocked.innerHTML.length;
461 metasenv = xml.getElementsByTagName("meta");
462 populate_goalarray(metasenv);
463 statements = listcons(len2,statements);
464 unlocked.scrollIntoView(true);
466 debug("advance failed");
471 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
475 function gotoBottom()
477 processor = function(xml) {
478 if (is_defined(xml)) {
479 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
480 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
481 len0 = unlocked.innerHTML.length;
482 unescaped = unlocked.innerHTML.unescapeHTML();
483 parsedtxt = unescaped.substr(0,len);
484 unparsedtxt = unescaped.substr(len);
485 locked.innerHTML = locked.innerHTML + parsedtxt;
486 unlocked.innerHTML = unparsedtxt;
487 len1 = unlocked.innerHTML.length;
489 metasenv = xml.getElementsByTagName("meta");
490 populate_goalarray(metasenv);
491 statements = listcons(len2,statements);
492 unlocked.scrollIntoView(true);
494 debug("goto bottom failed");
499 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
504 function gotoPos(offset)
506 if (!is_defined(offset)) {
507 offset = getCursorPos();
509 processor = function(xml) {
510 if (is_defined(xml)) {
511 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
512 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
513 len0 = unlocked.innerHTML.length;
514 unescaped = unlocked.innerHTML.unescapeHTML();
515 parsedtxt = unescaped.substr(0,len);
516 unparsedtxt = unescaped.substr(len);
517 locked.innerHTML = locked.innerHTML + parsedtxt;
518 unlocked.innerHTML = unparsedtxt;
519 len1 = unlocked.innerHTML.length;
521 metasenv = xml.getElementsByTagName("meta");
522 // populate_goalarray(metasenv);
523 statements = listcons(len2,statements);
524 unlocked.scrollIntoView(true);
525 // la populate non andrebbe fatta a ogni passo
527 populate_goalarray(metasenv);
530 gotoPos(offset - len);
533 unlocked.scrollIntoView(true);
534 populate_goalarray(metasenv);
539 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
544 processor = function(xml) {
545 if (typeof xml != "undefined") {
546 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
547 statementlen = parseInt(listhd(statements));
548 statements = listtl(statements);
549 lockedlen = locked.innerHTML.length - statementlen;
550 statement = locked.innerHTML.substr(lockedlen, statementlen);
551 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
552 unlocked.innerHTML = statement + unlocked.innerHTML;
553 metasenv = xml.getElementsByTagName("meta");
554 populate_goalarray(metasenv);
555 unlocked.scrollIntoView(true);
557 debug("retract failed");
563 callServer("retract",processor);
569 processor = function(xml)
571 if (is_defined(xml)) {
572 locked.innerHTML = "";
573 unlocked.innerHTML = xml.documentElement.textContent;
575 debug("file open failed");
578 callServer("open",processor,"file=" + escape(filename.value));
581 function retrieveFile(thefile)
583 processor = function(xml)
585 if (is_defined(xml)) {
586 locked.innerHTML = "";
587 unlocked.innerHTML = xml.documentElement.textContent;
589 debug("file open failed");
592 dialogBox.style.display = "none";
593 callServer("open",processor,"file=" + escape(thefile));
596 function showLibrary()
600 if (window.XMLHttpRequest)
602 req = new XMLHttpRequest();
604 else if (window.ActiveXObject)
607 req = new ActiveXObject("Msxml2.XMLHTTP");
611 req = new ActiveXObject("Microsoft.XMLHTTP");
615 req.onreadystatechange = function()
623 stxt = req.statusText;
626 debug(req.responseText);
627 showDialog("<H2>Library</H2>",req.responseText);
631 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
632 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
639 function hideSequent() {
640 goalcell.parentNode.removeChild(goalcell);
641 scriptcell.setAttribute("colspan","2");
644 function showSequent() {
645 scriptcell.setAttribute("colspan","1");
646 workarea.appendChild(goalcell);
649 function showDialog(title,content) {
650 dialogTitle.innerHTML = title;
651 dialogContent.innerHTML = content;
652 dialogBox.style.display = "block";
655 function removeElement(id) {
656 var element = document.getElementById(id);
657 element.parentNode.removeChild(element);
663 function getCursorPos() {
665 if (window.getSelection) {
666 var selObj = window.getSelection();
667 savedRange = selObj.getRangeAt(0);
668 savedsc = savedRange.startContainer;
669 savedso = savedRange.startOffset;
670 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
671 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
672 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
675 else if (document.selection) {
676 savedRange = document.selection.createRange();
677 var bookmark = savedRange.getBookmark();
678 /* FIXME the following works wrong when the document is longer than 65535 chars */
679 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
684 function findNode(list, node, acc) {
685 for (var i = 0; i < list.length; i++) {
686 if (list[i] == node) {
687 // debug("success " + i);
690 if (list[i].hasChildNodes()) {
692 // debug("recursion on node " + i);
693 return (findNode(list[i].childNodes,node,acc))
695 catch (e) { /* debug("recursion failed"); */ }
697 sandbox = document.getElementById("sandbox");
698 dup = list[i].cloneNode(true);
699 sandbox.appendChild(dup);
700 // debug("fail " + i + ": " + sandbox.innerHTML);
701 acc += sandbox.innerHTML.length;
702 sandbox.removeChild(dup);
708 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
711 function readCookie(name) {
712 var nameEQ = name + "=";
713 var ca = document.cookie.split(';');
714 for(var i=0;i < ca.length;i++) {
716 while (c.charAt(0)==' ') c = c.substring(1,c.length);
717 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
722 function delete_cookie ( cookie_name )
724 var cookie_date = new Date(); // current date & time
725 cookie_date.setTime ( cookie_date.getTime() - 1 );
726 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
729 function delete_session()
731 delete_cookie("session");