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);
153 return suppressdefault(e,false);
156 else return suppressdefault(e,false);
158 return suppressdefault(e,false);
164 // internet explorer (v.9) doesn't work with innerHTML
165 // but google chrome's innerText is, in a sense, "write only"
166 // what should we do?
167 logarea.innerText = txt + "\n" + logarea.innerText;
173 debug("hd of '" + l + "' = '" + ar[0] + "'");
181 debug("tl of '" + l + "' = '" + tl + "'");
185 function listcons(x,l)
187 debug("cons '" + x + "' on '" + l + "'");
188 return (x + "#" + l);
201 function fold_left (f,acc,l)
204 { debug("'" + l + "' is fold end");
207 { debug("'" + l + "' is fold cons");
208 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
211 function listiter (f,l)
214 { debug("'" + l + "' is nil");
219 debug("'" + l + "' is not nil");
221 listiter(f,listtl(l));
225 function listmap (f,l)
227 debug("listmap on " + l);
229 { debug("returning listnil");
233 { debug("cons f(hd) map(f,tl)");
234 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
238 var statements = listnil();
241 var metalist = listnil();
243 function pairmap (f,p)
245 debug("pairmap of '" + p + "'");
247 return (f(ar[0],ar[1]));
250 function tripletmap (f,p)
252 debug("tripletmap of '" + p + "'");
254 return (f(ar[0],ar[1],ar[2]));
260 return (pairmap (function (a,b) { return (a); }, p));
266 return (tripletmap (function (a,b,c) { return (a); }, p));
272 return (tripletmap (function (a,b,c) { return (b); }, p));
278 return (tripletmap (function (a,b,c) { return (c); }, p));
281 function populate_goalarray(menv)
283 debug("metasenv.length = " + menv.length);
284 if (menv.length == 0) {
290 goalarray = new Array();
291 metalist = listnil();
292 var tmp_goallist = "";
293 for (i = 0; i < menv.length; i++) {
294 metano = menv[i].getAttribute("number");
295 metaname = menv[i].childNodes[0].childNodes[0].data;
296 goal = menv[i].childNodes[1].childNodes[0].data;
297 debug ("found meta n. " + metano);
298 debug ("found goal\nBEGIN" + goal + "\nEND");
299 goalarray[metano] = goal;
300 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
301 metalist = listcons(metano,metalist);
302 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
304 goals.innerHTML = tmp_goallist;
305 debug("new metalist is '" + metalist + "'");
306 if (is_nil(metalist)) {
310 switch_goal(listhd(metalist));
315 function switch_goal(meta)
317 if (typeof meta == "undefined") {
318 goalview.innerHTML = "";
321 debug("switch_goal " + meta + "\n" + goalarray[meta]);
322 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
326 String.prototype.sescape = function() {
332 result = result.replace(patt1,"%25");
333 result = result.replace(patt2,"%3D");
334 result = result.replace(patt3,"%26");
335 result = result.replace(patt4,"%2B");
339 String.prototype.unescapeHTML = function()
341 var patt1 = /<br(\/|)>/gi;
342 var patt2 = /</gi;
343 var patt3 = />/gi;
345 result = result.replace(patt1,"\n");
346 result = result.replace(patt2,"<");
347 result = result.replace(patt3,">");
348 return (unescape(result));
353 advanceButton.disabled = true;
354 retractButton.disabled = true;
355 cursorButton.disabled = true;
356 bottomButton.disabled = true;
361 advanceButton.disabled = false;
362 retractButton.disabled = false;
363 cursorButton.disabled = false;
364 bottomButton.disabled = false;
367 function is_defined(x)
369 return (typeof x != "undefined");
372 /* servicename: name of the service being called
373 * reqbody: text of the request
374 * processResponse: processes the server response
375 * (takes the response text in input, undefined in case of error)
377 function callServer(servicename,processResponse,reqbody)
381 if (window.XMLHttpRequest)
383 req = new XMLHttpRequest();
385 else if (window.ActiveXObject)
388 req = new ActiveXObject("Msxml2.XMLHTTP");
392 req = new ActiveXObject("Microsoft.XMLHTTP");
396 req.onreadystatechange = function()
404 stxt = req.statusText;
407 debug(req.responseText);
408 if (window.DOMParser) {
409 parser=new DOMParser();
410 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
412 else // Internet Explorer
414 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
415 xmlDoc.async="false";
416 xmlDoc.loadXML(req.responseText);
418 processResponse(xmlDoc);
424 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
425 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
434 function advanceForm1()
436 processor = function(xml) {
437 if (is_defined(xml)) {
438 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
439 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
440 len0 = unlocked.innerHTML.length;
441 unescaped = unlocked.innerHTML.unescapeHTML();
442 parsedtxt = unescaped.substr(0,len);
443 unparsedtxt = unescaped.substr(len);
444 locked.innerHTML = locked.innerHTML + parsedtxt;
445 unlocked.innerHTML = unparsedtxt;
446 len1 = unlocked.innerHTML.length;
448 metasenv = xml.getElementsByTagName("meta");
449 populate_goalarray(metasenv);
450 statements = listcons(len2,statements);
451 unlocked.scrollIntoView(true);
453 debug("advance failed");
458 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
462 function gotoBottom()
464 processor = function(xml) {
465 if (is_defined(xml)) {
466 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
467 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
468 len0 = unlocked.innerHTML.length;
469 unescaped = unlocked.innerHTML.unescapeHTML();
470 parsedtxt = unescaped.substr(0,len);
471 unparsedtxt = unescaped.substr(len);
472 locked.innerHTML = locked.innerHTML + parsedtxt;
473 unlocked.innerHTML = unparsedtxt;
474 len1 = unlocked.innerHTML.length;
476 metasenv = xml.getElementsByTagName("meta");
477 populate_goalarray(metasenv);
478 statements = listcons(len2,statements);
479 unlocked.scrollIntoView(true);
481 debug("goto bottom failed");
486 callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
491 function gotoPos(offset)
493 if (!is_defined(offset)) {
494 offset = getCursorPos();
496 processor = function(xml) {
497 if (is_defined(xml)) {
498 // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
499 len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
500 len0 = unlocked.innerHTML.length;
501 unescaped = unlocked.innerHTML.unescapeHTML();
502 parsedtxt = unescaped.substr(0,len);
503 unparsedtxt = unescaped.substr(len);
504 locked.innerHTML = locked.innerHTML + parsedtxt;
505 unlocked.innerHTML = unparsedtxt;
506 len1 = unlocked.innerHTML.length;
508 metasenv = xml.getElementsByTagName("meta");
509 // populate_goalarray(metasenv);
510 statements = listcons(len2,statements);
511 unlocked.scrollIntoView(true);
512 // la populate non andrebbe fatta a ogni passo
514 populate_goalarray(metasenv);
517 gotoPos(offset - len);
520 unlocked.scrollIntoView(true);
521 populate_goalarray(metasenv);
526 callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
531 processor = function(xml) {
532 if (typeof xml != "undefined") {
533 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
534 statementlen = parseInt(listhd(statements));
535 statements = listtl(statements);
536 lockedlen = locked.innerHTML.length - statementlen;
537 statement = locked.innerHTML.substr(lockedlen, statementlen);
538 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
539 unlocked.innerHTML = statement + unlocked.innerHTML;
540 metasenv = xml.getElementsByTagName("meta");
541 populate_goalarray(metasenv);
542 unlocked.scrollIntoView(true);
544 debug("retract failed");
550 callServer("retract",processor);
556 processor = function(xml)
558 if (is_defined(xml)) {
559 locked.innerHTML = "";
560 unlocked.innerHTML = xml.documentElement.textContent;
562 debug("file open failed");
565 callServer("open",processor,"file=" + escape(filename.value));
568 function retrieveFile(thefile)
570 processor = function(xml)
572 if (is_defined(xml)) {
573 locked.innerHTML = "";
574 unlocked.innerHTML = xml.documentElement.textContent;
576 debug("file open failed");
579 dialogBox.style.display = "none";
580 callServer("open",processor,"file=" + escape(thefile));
583 function showLibrary()
587 if (window.XMLHttpRequest)
589 req = new XMLHttpRequest();
591 else if (window.ActiveXObject)
594 req = new ActiveXObject("Msxml2.XMLHTTP");
598 req = new ActiveXObject("Microsoft.XMLHTTP");
602 req.onreadystatechange = function()
610 stxt = req.statusText;
613 debug(req.responseText);
614 showDialog("<H2>Library</H2>",req.responseText);
618 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
619 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
626 function hideSequent() {
627 goalcell.parentNode.removeChild(goalcell);
628 scriptcell.setAttribute("colspan","2");
631 function showSequent() {
632 scriptcell.setAttribute("colspan","1");
633 workarea.appendChild(goalcell);
636 function showDialog(title,content) {
637 dialogTitle.innerHTML = title;
638 dialogContent.innerHTML = content;
639 dialogBox.style.display = "block";
642 function removeElement(id) {
643 var element = document.getElementById(id);
644 element.parentNode.removeChild(element);
649 function getCursorPos() {
651 if (window.getSelection) {
652 var selObj = window.getSelection();
653 savedRange = selObj.getRangeAt(0);
654 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
655 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
656 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
659 else if (document.selection) {
660 savedRange = document.selection.createRange();
661 var bookmark = savedRange.getBookmark();
662 /* FIXME the following works wrong when the document is longer than 65535 chars */
663 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
668 function findNode(list, node, acc) {
669 for (var i = 0; i < list.length; i++) {
670 if (list[i] == node) {
671 // debug("success " + i);
674 if (list[i].hasChildNodes()) {
676 // debug("recursion on node " + i);
677 return (findNode(list[i].childNodes,node,acc))
679 catch (e) { /* debug("recursion failed"); */ }
681 sandbox = document.getElementById("sandbox");
682 dup = list[i].cloneNode(true);
683 sandbox.appendChild(dup);
684 // debug("fail " + i + ": " + sandbox.innerHTML);
685 acc += sandbox.innerHTML.length;
686 sandbox.removeChild(dup);
692 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
695 function readCookie(name) {
696 var nameEQ = name + "=";
697 var ca = document.cookie.split(';');
698 for(var i=0;i < ca.length;i++) {
700 while (c.charAt(0)==' ') c = c.substring(1,c.length);
701 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
706 function delete_cookie ( cookie_name )
708 var cookie_date = new Date(); // current date & time
709 cookie_date.setTime ( cookie_date.getTime() - 1 );
710 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
713 function delete_session()
715 delete_cookie("session");