18 var lockedbackup = "";
22 if (readCookie("session") == null) {
23 window.location = "/login.html"
25 locked = document.getElementById("locked");
26 unlocked = document.getElementById("unlocked");
27 workarea = document.getElementById("workarea");
28 scriptcell = document.getElementById("scriptcell");
29 goalcell = document.getElementById("goalcell");
30 goals = document.getElementById("goals");
31 goalview = document.getElementById("goalview");
32 filename = document.getElementById("filename");
33 logarea = document.getElementById("logarea");
34 advanceButton = document.getElementById("advance");
35 retractButton = document.getElementById("retract");
36 cursorButton = document.getElementById("cursor");
37 bottomButton = document.getElementById("bottom");
38 dialogBox = document.getElementById("dialogBox");
39 dialogTitle = document.getElementById("dialogTitle");
40 dialogContent = document.getElementById("dialogContent");
42 // hide sequent view at start
45 // initialize keyboard events in the unlocked script
46 init_keyboard(unlocked);
50 function init_keyboard(target)
52 if (target.addEventListener)
54 // target.addEventListener("keydown",keydown,false);
55 target.addEventListener("keypress",keypress,false);
56 // target.addEventListener("keyup",keyup,false);
57 // target.addEventListener("textInput",textinput,false);
59 else if (target.attachEvent)
61 // target.attachEvent("onkeydown", keydown);
62 target.attachEvent("onkeypress", keypress);
63 // target.attachEvent("onkeyup", keyup);
64 // target.attachEvent("ontextInput", textinput);
68 // target.onkeydown= keydown;
69 target.onkeypress= keypress;
70 // target.onkeyup= keyup;
71 // target.ontextinput= textinput; // probably doesn't work
78 if (n == null) return 'undefined';
80 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
81 while (s.length < 9) s+= ' ';
85 function string_of_key(n)
87 if (n == null) return 'undefined';
88 return String.fromCharCode(n);
91 function pressmesg(w,e)
93 debug(w + ' keyCode=' + keyval(e.keyCode) +
94 ' which=' + keyval(e.which) +
95 ' charCode=' + keyval(e.charCode) +
96 '\n shiftKey='+e.shiftKey
97 + ' ctrlKey='+e.ctrlKey
99 + ' metaKey='+e.metaKey);
102 function suppressdefault(e,flag)
106 if (e.preventDefault) e.preventDefault();
107 if (e.stopPropagation) e.stopPropagation();
112 function restoreSelection(adjust) {
114 if (savedRange != null) {
115 if (window.getSelection)//non IE and there is already a selection
117 var s = window.getSelection();
118 if (s.rangeCount > 0)
120 range = document.createRange();
121 range.setStart(savedsc,savedso + adjust);
122 range.collapse(true);
126 if (document.createRange)//non IE and no selection
128 window.getSelection().addRange(savedRange);
131 if (document.selection)//IE
138 function lookup_tex(texmacro)
140 texmacro = texmacro.substring(1);
141 return unescape(macro2utf8[texmacro]);
147 pressmesg('keypress',e);
148 var s = string_of_key(e.charCode);
151 i = unlocked.innerHTML.lastIndexOf('\\',j);
153 match = unlocked.innerHTML.substring(i,j);
154 pre = unlocked.innerHTML.substring(0,i);
155 post = unlocked.innerHTML.substring(j);
157 sym = lookup_tex(match);
158 if (typeof sym != "undefined") {
159 len1 = unlocked.innerText.length;
160 unlocked.innerHTML = pre + sym + post;
161 len2 = unlocked.innerText.length;
162 restoreSelection(len2 - len1);
163 return suppressdefault(e,true);
166 // restoreSelection(0);
167 return suppressdefault(e,false);
170 else return suppressdefault(e,false);
172 return suppressdefault(e,false);
178 // internet explorer (v.9) doesn't work with innerHTML
179 // but google chrome's innerText is, in a sense, "write only"
180 // what should we do?
181 logarea.innerText = txt + "\n" + logarea.innerText;
187 debug("hd of '" + l + "' = '" + ar[0] + "'");
195 debug("tl of '" + l + "' = '" + tl + "'");
199 function listcons(x,l)
201 debug("cons '" + x + "' on '" + l + "'");
202 return (x + "#" + l);
215 function fold_left (f,acc,l)
218 { debug("'" + l + "' is fold end");
221 { debug("'" + l + "' is fold cons");
222 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
225 function listiter (f,l)
228 { debug("'" + l + "' is nil");
233 debug("'" + l + "' is not nil");
235 listiter(f,listtl(l));
239 function listmap (f,l)
241 debug("listmap on " + l);
243 { debug("returning listnil");
247 { debug("cons f(hd) map(f,tl)");
248 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
252 var statements = listnil();
255 var metalist = listnil();
257 function pairmap (f,p)
259 debug("pairmap of '" + p + "'");
261 return (f(ar[0],ar[1]));
264 function tripletmap (f,p)
266 debug("tripletmap of '" + p + "'");
268 return (f(ar[0],ar[1],ar[2]));
274 return (pairmap (function (a,b) { return (a); }, p));
280 return (tripletmap (function (a,b,c) { return (a); }, p));
286 return (tripletmap (function (a,b,c) { return (b); }, p));
292 return (tripletmap (function (a,b,c) { return (c); }, p));
295 function populate_goalarray(menv)
297 debug("metasenv.length = " + menv.length);
298 if (menv.length == 0) {
304 goalarray = new Array();
305 metalist = listnil();
306 var tmp_goallist = "";
307 for (i = 0; i < menv.length; i++) {
308 metano = menv[i].getAttribute("number");
309 metaname = menv[i].childNodes[0].childNodes[0].data;
310 goal = menv[i].childNodes[1].childNodes[0].data;
311 debug ("found meta n. " + metano);
312 debug ("found goal\nBEGIN" + goal + "\nEND");
313 goalarray[metano] = goal;
314 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
315 metalist = listcons(metano,metalist);
316 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
318 goals.innerHTML = tmp_goallist;
319 debug("new metalist is '" + metalist + "'");
320 if (is_nil(metalist)) {
324 switch_goal(listhd(metalist));
329 function switch_goal(meta)
331 if (typeof meta == "undefined") {
332 goalview.innerHTML = "";
335 debug("switch_goal " + meta + "\n" + goalarray[meta]);
336 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
340 // the following is used to avoid escaping unicode, which results in
341 // the server being unable to unescape the string
342 String.prototype.sescape = function() {
348 result = result.replace(patt1,"%25");
349 result = result.replace(patt2,"%3D");
350 result = result.replace(patt3,"%26");
351 result = result.replace(patt4,"%2B");
355 String.prototype.html_to_matita = function()
357 var patt1 = /<br(\/|)>/gi;
360 var patt4 = /</gi;
361 var patt5 = />/gi;
363 result = result.replace(patt1,"\n");
364 result = result.replace(patt2,"\005");
365 result = result.replace(patt3,"\006");
366 result = result.replace(patt4,"<");
367 result = result.replace(patt5,">");
368 return (unescape(result));
371 String.prototype.matita_to_html = function()
375 var patt3 = /\005/gi;
376 var patt4 = /\006/gi;
378 result = result.replace(patt1,"<");
379 result = result.replace(patt2,">");
380 result = result.replace(patt3,"<");
381 result = result.replace(patt4,">");
382 return (unescape(result));
387 advanceButton.disabled = true;
388 retractButton.disabled = true;
389 cursorButton.disabled = true;
390 bottomButton.disabled = true;
395 advanceButton.disabled = false;
396 retractButton.disabled = false;
397 cursorButton.disabled = false;
398 bottomButton.disabled = false;
401 function is_defined(x)
403 return (typeof x != "undefined");
406 /* servicename: name of the service being called
407 * reqbody: text of the request
408 * processResponse: processes the server response
409 * (takes the response text in input, undefined in case of error)
411 function callServer(servicename,processResponse,reqbody)
415 if (window.XMLHttpRequest)
417 req = new XMLHttpRequest();
419 else if (window.ActiveXObject)
422 req = new ActiveXObject("Msxml2.XMLHTTP");
426 req = new ActiveXObject("Microsoft.XMLHTTP");
430 req.onreadystatechange = function()
438 stxt = req.statusText;
441 debug(req.responseText);
442 if (window.DOMParser) {
443 parser=new DOMParser();
444 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
446 else // Internet Explorer
448 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
449 xmlDoc.async="false";
450 xmlDoc.loadXML(req.responseText);
452 processResponse(xmlDoc);
458 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
459 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
468 function advanceForm1()
470 processor = function(xml) {
471 if (is_defined(xml)) {
472 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
473 parsed = xml.getElementsByTagName("parsed")[0];
474 len = parseInt(parsed.getAttribute("length"));
475 // len0 = unlocked.innerHTML.length;
476 unescaped = unlocked.innerHTML.html_to_matita();
477 parsedtxt = parsed.childNodes[0].nodeValue;
478 //parsedtxt = unescaped.substr(0,len);
479 unparsedtxt = unescaped.substr(len);
480 lockedbackup += parsedtxt;
481 locked.innerHTML = lockedbackup;
482 unlocked.innerHTML = unparsedtxt.matita_to_html();
483 // len1 = unlocked.innerHTML.length;
484 // len2 = len0 - len1;
485 len2 = parsedtxt.length;
486 metasenv = xml.getElementsByTagName("meta");
487 populate_goalarray(metasenv);
488 statements = listcons(len2,statements);
489 unlocked.scrollIntoView(true);
491 debug("advance failed");
496 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
500 function gotoBottom()
502 processor = function(xml) {
503 if (is_defined(xml)) {
504 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
505 parsed = xml.getElementsByTagName("parsed")[0];
506 len = parseInt(parsed.getAttribute("length"));
508 // len0 = unlocked.innerHTML.length;
509 unescaped = unlocked.innerHTML.html_to_matita();
510 parsedtxt = parsed.childNodes[0].nodeValue;
511 //parsedtxt = unescaped.substr(0,len);
512 unparsedtxt = unescaped.substr(len);
513 lockedbackup += parsedtxt;
514 locked.innerHTML = lockedbackup; //.matita_to_html();
515 unlocked.innerHTML = unparsedtxt.matita_to_html();
516 // len1 = unlocked.innerHTML.length;
517 len2 = parsedtxt.length;
518 metasenv = xml.getElementsByTagName("meta");
519 populate_goalarray(metasenv);
521 statements = listcons(len2,statements);
522 unlocked.scrollIntoView(true);
525 debug("goto bottom failed");
530 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
535 function gotoPos(offset)
537 if (!is_defined(offset)) {
538 offset = getCursorPos();
540 processor = function(xml) {
541 if (is_defined(xml)) {
542 parsed = xml.getElementsByTagName("parsed")[0];
543 len = parseInt(parsed.getAttribute("length"));
544 // len0 = unlocked.innerHTML.length;
545 unescaped = unlocked.innerHTML.html_to_matita();
546 parsedtxt = parsed.childNodes[0].nodeValue;
547 //parsedtxt = unescaped.substr(0,len);
548 unparsedtxt = unescaped.substr(len);
549 lockedbackup += parsedtxt;
550 locked.innerHTML = lockedbackup; //.matita_to_html();
551 unlocked.innerHTML = unparsedtxt.matita_to_html();
552 // len1 = unlocked.innerHTML.length;
553 len2 = parsedtxt.length;
554 metasenv = xml.getElementsByTagName("meta");
555 // populate_goalarray(metasenv);
556 statements = listcons(len2,statements);
557 unlocked.scrollIntoView(true);
558 // la populate non andrebbe fatta a ogni passo
560 populate_goalarray(metasenv);
563 gotoPos(offset - len);
566 unlocked.scrollIntoView(true);
567 populate_goalarray(metasenv);
572 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
577 processor = function(xml) {
578 if (typeof xml != "undefined") {
579 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
580 statementlen = parseInt(listhd(statements));
581 statements = listtl(statements);
583 lockedlen = locked.innerHTML.length - statementlen;
584 statement = locked.innerHTML.substr(lockedlen, statementlen);
585 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
586 unlocked.innerHTML = statement + unlocked.innerHTML;
588 lockedlen = lockedbackup.length - statementlen;
589 statement = lockedbackup.substr(lockedlen, statementlen);
590 lockedbackup = lockedbackup.substr(0,lockedlen);
591 locked.innerHTML = lockedbackup;
592 unlocked.innerHTML = statement + unlocked.innerHTML;
593 metasenv = xml.getElementsByTagName("meta");
594 populate_goalarray(metasenv);
595 unlocked.scrollIntoView(true);
597 debug("retract failed");
603 callServer("retract",processor);
609 processor = function(xml)
611 if (is_defined(xml)) {
613 locked.innerHTML = lockedbackup;
614 unlocked.innerHTML = xml.documentElement.textContent;
616 debug("file open failed");
619 callServer("open",processor,"file=" + escape(filename.value));
622 function retrieveFile(thefile)
624 processor = function(xml)
626 if (is_defined(xml)) {
628 locked.innerHTML = lockedbackup;
629 debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
630 unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
633 debug("file open failed");
636 dialogBox.style.display = "none";
637 current_fname = thefile;
638 callServer("open",processor,"file=" + escape(thefile));
641 function showLibrary()
645 if (window.XMLHttpRequest)
647 req = new XMLHttpRequest();
649 else if (window.ActiveXObject)
652 req = new ActiveXObject("Msxml2.XMLHTTP");
656 req = new ActiveXObject("Microsoft.XMLHTTP");
660 req.onreadystatechange = function()
668 stxt = req.statusText;
671 debug(req.responseText);
672 showDialog("<H2>Library</H2>",req.responseText);
676 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
677 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
684 processor = function(xml) {
685 if (is_defined(xml)) {
686 debug("file saved!");
688 debug("save file failed");
692 if (is_defined(current_fname)) {
694 callServer("save",processor,"file=" + escape(current_fname) +
695 "&locked=" + (locked.innerHTML.html_to_matita()).sescape() +
696 "&unlocked=" + (unlocked.innerHTML.html_to_matita()).sescape());
698 else { debug("no file selected"); }
703 processor = function(xml) {
704 if (is_defined(xml)) {
705 debug("commit succeeded(?)");
707 debug("commit failed!");
712 callServer("commit",processor);
717 function hideSequent() {
718 goalcell.parentNode.removeChild(goalcell);
719 scriptcell.setAttribute("colspan","2");
722 function showSequent() {
723 scriptcell.setAttribute("colspan","1");
724 workarea.appendChild(goalcell);
727 function showDialog(title,content) {
728 dialogTitle.innerHTML = title;
729 dialogContent.innerHTML = content;
730 dialogBox.style.display = "block";
733 function removeElement(id) {
734 var element = document.getElementById(id);
735 element.parentNode.removeChild(element);
741 function getCursorPos() {
743 if (window.getSelection) {
744 var selObj = window.getSelection();
745 savedRange = selObj.getRangeAt(0);
746 savedsc = savedRange.startContainer;
747 savedso = savedRange.startOffset;
748 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
749 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
750 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
753 else if (document.selection) {
754 savedRange = document.selection.createRange();
755 var bookmark = savedRange.getBookmark();
756 /* FIXME the following works wrong when the document is longer than 65535 chars */
757 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
762 function findNode(list, node, acc) {
763 for (var i = 0; i < list.length; i++) {
764 if (list[i] == node) {
765 // debug("success " + i);
768 if (list[i].hasChildNodes()) {
770 // debug("recursion on node " + i);
771 return (findNode(list[i].childNodes,node,acc))
773 catch (e) { /* debug("recursion failed"); */ }
775 sandbox = document.getElementById("sandbox");
776 dup = list[i].cloneNode(true);
777 sandbox.appendChild(dup);
778 // debug("fail " + i + ": " + sandbox.innerHTML);
779 acc += sandbox.innerHTML.length;
780 sandbox.removeChild(dup);
786 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
789 function readCookie(name) {
790 var nameEQ = name + "=";
791 var ca = document.cookie.split(';');
792 for(var i=0;i < ca.length;i++) {
794 while (c.charAt(0)==' ') c = c.substring(1,c.length);
795 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
800 function delete_cookie ( cookie_name )
802 var cookie_date = new Date(); // current date & time
803 cookie_date.setTime ( cookie_date.getTime() - 1 );
804 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
807 function delete_session()
809 delete_cookie("session");