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.html_to_matita = function()
354 var patt1 = /<br(\/|)>/gi;
357 var patt4 = /</gi;
358 var patt5 = />/gi;
360 result = result.replace(patt1,"\n");
361 result = result.replace(patt2,"\005");
362 result = result.replace(patt3,"\006");
363 result = result.replace(patt4,"<");
364 result = result.replace(patt5,">");
365 return (unescape(result));
368 String.prototype.matita_to_html = function()
372 var patt3 = /\005/gi;
373 var patt4 = /\006/gi;
375 result = result.replace(patt1,"<");
376 result = result.replace(patt2,">");
377 result = result.replace(patt3,"<");
378 result = result.replace(patt4,">");
379 return (unescape(result));
384 advanceButton.disabled = true;
385 retractButton.disabled = true;
386 cursorButton.disabled = true;
387 bottomButton.disabled = true;
392 advanceButton.disabled = false;
393 retractButton.disabled = false;
394 cursorButton.disabled = false;
395 bottomButton.disabled = false;
398 function is_defined(x)
400 return (typeof x != "undefined");
403 /* servicename: name of the service being called
404 * reqbody: text of the request
405 * processResponse: processes the server response
406 * (takes the response text in input, undefined in case of error)
408 function callServer(servicename,processResponse,reqbody)
412 if (window.XMLHttpRequest)
414 req = new XMLHttpRequest();
416 else if (window.ActiveXObject)
419 req = new ActiveXObject("Msxml2.XMLHTTP");
423 req = new ActiveXObject("Microsoft.XMLHTTP");
427 req.onreadystatechange = function()
435 stxt = req.statusText;
438 debug(req.responseText);
439 if (window.DOMParser) {
440 parser=new DOMParser();
441 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
443 else // Internet Explorer
445 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
446 xmlDoc.async="false";
447 xmlDoc.loadXML(req.responseText);
449 processResponse(xmlDoc);
455 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
456 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
465 function advanceForm1()
467 processor = function(xml) {
468 if (is_defined(xml)) {
469 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
470 parsed = xml.getElementsByTagName("parsed")[0];
471 len = parseInt(parsed.getAttribute("length"));
472 len0 = unlocked.innerHTML.length;
473 unescaped = unlocked.innerHTML.html_to_matita();
474 parsedtxt = parsed.childNodes[0].nodeValue;
475 //parsedtxt = unescaped.substr(0,len);
476 unparsedtxt = unescaped.substr(len);
477 locked.innerHTML = locked.innerHTML + parsedtxt; //.matita_to_html();
478 unlocked.innerHTML = unparsedtxt.matita_to_html();
479 len1 = unlocked.innerHTML.length;
481 metasenv = xml.getElementsByTagName("meta");
482 populate_goalarray(metasenv);
483 statements = listcons(len2,statements);
484 unlocked.scrollIntoView(true);
486 debug("advance failed");
491 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
495 function gotoBottom()
497 processor = function(xml) {
498 if (is_defined(xml)) {
499 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
500 parsed = xml.getElementsByTagName("parsed")[0];
501 len = parseInt(parsed.getAttribute("length"));
502 len0 = unlocked.innerHTML.length;
503 unescaped = unlocked.innerHTML.html_to_matita();
504 parsedtxt = parsed.childNodes[0].nodeValue;
505 //parsedtxt = unescaped.substr(0,len);
506 unparsedtxt = unescaped.substr(len);
507 locked.innerHTML = locked.innerHTML + parsedtxt; //.matita_to_html();
508 unlocked.innerHTML = unparsedtxt.matita_to_html();
509 len1 = unlocked.innerHTML.length;
511 metasenv = xml.getElementsByTagName("meta");
512 populate_goalarray(metasenv);
513 statements = listcons(len2,statements);
514 unlocked.scrollIntoView(true);
516 debug("goto bottom failed");
521 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
526 function gotoPos(offset)
528 if (!is_defined(offset)) {
529 offset = getCursorPos();
531 processor = function(xml) {
532 if (is_defined(xml)) {
533 parsed = xml.getElementsByTagName("parsed")[0];
534 len = parseInt(parsed.getAttribute("length"));
535 len0 = unlocked.innerHTML.length;
536 unescaped = unlocked.innerHTML.html_to_matita();
537 parsedtxt = parsed.childNodes[0].nodeValue;
538 //parsedtxt = unescaped.substr(0,len);
539 unparsedtxt = unescaped.substr(len);
540 locked.innerHTML = locked.innerHTML + parsedtxt; //.matita_to_html();
541 unlocked.innerHTML = unparsedtxt.matita_to_html();
542 len1 = unlocked.innerHTML.length;
544 metasenv = xml.getElementsByTagName("meta");
545 // populate_goalarray(metasenv);
546 statements = listcons(len2,statements);
547 unlocked.scrollIntoView(true);
548 // la populate non andrebbe fatta a ogni passo
550 populate_goalarray(metasenv);
553 gotoPos(offset - len);
556 unlocked.scrollIntoView(true);
557 populate_goalarray(metasenv);
562 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
567 processor = function(xml) {
568 if (typeof xml != "undefined") {
569 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
570 statementlen = parseInt(listhd(statements));
571 statements = listtl(statements);
572 lockedlen = locked.innerHTML.length - statementlen;
573 statement = locked.innerHTML.substr(lockedlen, statementlen);
574 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
575 unlocked.innerHTML = statement + unlocked.innerHTML;
576 metasenv = xml.getElementsByTagName("meta");
577 populate_goalarray(metasenv);
578 unlocked.scrollIntoView(true);
580 debug("retract failed");
586 callServer("retract",processor);
592 processor = function(xml)
594 if (is_defined(xml)) {
595 locked.innerHTML = "";
596 unlocked.innerHTML = xml.documentElement.textContent;
598 debug("file open failed");
601 callServer("open",processor,"file=" + escape(filename.value));
604 function retrieveFile(thefile)
606 processor = function(xml)
608 if (is_defined(xml)) {
609 locked.innerHTML = "";
610 debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
611 unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
614 debug("file open failed");
617 dialogBox.style.display = "none";
618 callServer("open",processor,"file=" + escape(thefile));
621 function showLibrary()
625 if (window.XMLHttpRequest)
627 req = new XMLHttpRequest();
629 else if (window.ActiveXObject)
632 req = new ActiveXObject("Msxml2.XMLHTTP");
636 req = new ActiveXObject("Microsoft.XMLHTTP");
640 req.onreadystatechange = function()
648 stxt = req.statusText;
651 debug(req.responseText);
652 showDialog("<H2>Library</H2>",req.responseText);
656 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
657 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
664 function hideSequent() {
665 goalcell.parentNode.removeChild(goalcell);
666 scriptcell.setAttribute("colspan","2");
669 function showSequent() {
670 scriptcell.setAttribute("colspan","1");
671 workarea.appendChild(goalcell);
674 function showDialog(title,content) {
675 dialogTitle.innerHTML = title;
676 dialogContent.innerHTML = content;
677 dialogBox.style.display = "block";
680 function removeElement(id) {
681 var element = document.getElementById(id);
682 element.parentNode.removeChild(element);
688 function getCursorPos() {
690 if (window.getSelection) {
691 var selObj = window.getSelection();
692 savedRange = selObj.getRangeAt(0);
693 savedsc = savedRange.startContainer;
694 savedso = savedRange.startOffset;
695 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
696 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
697 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
700 else if (document.selection) {
701 savedRange = document.selection.createRange();
702 var bookmark = savedRange.getBookmark();
703 /* FIXME the following works wrong when the document is longer than 65535 chars */
704 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
709 function findNode(list, node, acc) {
710 for (var i = 0; i < list.length; i++) {
711 if (list[i] == node) {
712 // debug("success " + i);
715 if (list[i].hasChildNodes()) {
717 // debug("recursion on node " + i);
718 return (findNode(list[i].childNodes,node,acc))
720 catch (e) { /* debug("recursion failed"); */ }
722 sandbox = document.getElementById("sandbox");
723 dup = list[i].cloneNode(true);
724 sandbox.appendChild(dup);
725 // debug("fail " + i + ": " + sandbox.innerHTML);
726 acc += sandbox.innerHTML.length;
727 sandbox.removeChild(dup);
733 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
736 function readCookie(name) {
737 var nameEQ = name + "=";
738 var ca = document.cookie.split(';');
739 for(var i=0;i < ca.length;i++) {
741 while (c.charAt(0)==' ') c = c.substring(1,c.length);
742 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
747 function delete_cookie ( cookie_name )
749 var cookie_date = new Date(); // current date & time
750 cookie_date.setTime ( cookie_date.getTime() - 1 );
751 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
754 function delete_session()
756 delete_cookie("session");