18 var lockedbackup = "";
20 function text_of_html(h)
29 function unescape_html(s)
31 u = document.getElementById("unescape");
33 return text_of_html(u)
38 if (readCookie("session") == null) {
39 window.location = "/login.html"
41 locked = document.getElementById("locked");
42 unlocked = document.getElementById("unlocked");
43 workarea = document.getElementById("workarea");
44 scriptcell = document.getElementById("scriptcell");
45 goalcell = document.getElementById("goalcell");
46 goals = document.getElementById("goals");
47 goalview = document.getElementById("goalview");
48 filename = document.getElementById("filename");
49 logarea = document.getElementById("logarea");
50 advanceButton = document.getElementById("advance");
51 retractButton = document.getElementById("retract");
52 cursorButton = document.getElementById("cursor");
53 bottomButton = document.getElementById("bottom");
54 dialogBox = document.getElementById("dialogBox");
55 dialogTitle = document.getElementById("dialogTitle");
56 dialogContent = document.getElementById("dialogContent");
58 // hide sequent view at start
61 // initialize keyboard events in the unlocked script
62 init_keyboard(unlocked);
66 function init_keyboard(target)
68 if (target.addEventListener)
70 // target.addEventListener("keydown",keydown,false);
71 target.addEventListener("keypress",keypress,false);
72 // target.addEventListener("keyup",keyup,false);
73 // target.addEventListener("textInput",textinput,false);
75 else if (target.attachEvent)
77 // target.attachEvent("onkeydown", keydown);
78 target.attachEvent("onkeypress", keypress);
79 // target.attachEvent("onkeyup", keyup);
80 // target.attachEvent("ontextInput", textinput);
84 // target.onkeydown= keydown;
85 target.onkeypress= keypress;
86 // target.onkeyup= keyup;
87 // target.ontextinput= textinput; // probably doesn't work
94 if (n == null) return 'undefined';
96 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
97 while (s.length < 9) s+= ' ';
101 function string_of_key(n)
103 if (n == null) return 'undefined';
104 return String.fromCharCode(n);
107 function pressmesg(w,e)
109 debug(w + ' keyCode=' + keyval(e.keyCode) +
110 ' which=' + keyval(e.which) +
111 ' charCode=' + keyval(e.charCode) +
112 '\n shiftKey='+e.shiftKey
113 + ' ctrlKey='+e.ctrlKey
114 + ' altKey='+e.altKey
115 + ' metaKey='+e.metaKey);
118 function suppressdefault(e,flag)
122 if (e.preventDefault) e.preventDefault();
123 if (e.stopPropagation) e.stopPropagation();
128 function restoreSelection(r) {
131 if (window.getSelection)//non IE and there is already a selection
133 var s = window.getSelection();
134 if (s.rangeCount > 0)
139 if (document.createRange)//non IE and no selection
141 window.getSelection().addRange(r);
144 if (document.selection)//IE
151 function lookup_tex(texmacro)
153 texmacro = texmacro.substring(1);
154 return unescape(macro2utf8[texmacro]);
160 pressmesg('keypress',e);
161 var s = string_of_key(e.charCode);
164 i = unlocked.innerHTML.lastIndexOf('\\',j);
166 match = unlocked.innerHTML.substring(i,j);
167 sym = unescape_html(lookup_tex(match));
168 if (sym != "undefined") {
169 if (window.getSelection) { // non IE
170 savedRange.setStart(savedsc,savedso - (j-i));
171 savedRange.deleteContents();
172 savedRange.insertNode(document.createTextNode(sym));
173 savedsc.parentNode.normalize();
174 if (savedRange.collapsed) { // Mozilla
175 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
177 savedRange.collapse(false);
179 savedRange.moveStart(i-j);
180 savedRange.text(sym);
181 savedRange.collapse(false);
183 restoreSelection(savedRange);
184 return suppressdefault(e,true);
187 // restoreSelection(0);
188 return suppressdefault(e,false);
191 else return suppressdefault(e,false);
193 return suppressdefault(e,false);
201 // internet explorer (v.9) doesn't work with innerHTML
202 // but google chrome's innerText is, in a sense, "write only"
203 // what should we do?
204 // logarea.innerText = txt + "\n" + logarea.innerText;
205 logtxt = logtxt + "\n" + txt;
209 logWin = window.open( "", "Matita Log",
210 "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
211 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
212 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
213 logtxt + '</textarea></body></html>');
214 logWin.document.close();
220 debug("hd of '" + l + "' = '" + ar[0] + "'");
228 debug("tl of '" + l + "' = '" + tl + "'");
232 function listcons(x,l)
234 debug("cons '" + x + "' on '" + l + "'");
235 return (x + "#" + l);
248 function fold_left (f,acc,l)
251 { debug("'" + l + "' is fold end");
254 { debug("'" + l + "' is fold cons");
255 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
258 function listiter (f,l)
261 { debug("'" + l + "' is nil");
266 debug("'" + l + "' is not nil");
268 listiter(f,listtl(l));
272 function listmap (f,l)
274 debug("listmap on " + l);
276 { debug("returning listnil");
280 { debug("cons f(hd) map(f,tl)");
281 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
285 var statements = listnil();
288 var metalist = listnil();
290 function pairmap (f,p)
292 debug("pairmap of '" + p + "'");
294 return (f(ar[0],ar[1]));
297 function tripletmap (f,p)
299 debug("tripletmap of '" + p + "'");
301 return (f(ar[0],ar[1],ar[2]));
307 return (pairmap (function (a,b) { return (a); }, p));
313 return (tripletmap (function (a,b,c) { return (a); }, p));
319 return (tripletmap (function (a,b,c) { return (b); }, p));
325 return (tripletmap (function (a,b,c) { return (c); }, p));
328 function populate_goalarray(menv)
330 debug("metasenv.length = " + menv.length);
331 if (menv.length == 0) {
337 goalarray = new Array();
338 metalist = listnil();
339 var tmp_goallist = "";
340 for (i = 0; i < menv.length; i++) {
341 metano = menv[i].getAttribute("number");
342 metaname = menv[i].childNodes[0].childNodes[0].data;
343 goal = menv[i].childNodes[1].childNodes[0].data;
344 debug ("found meta n. " + metano);
345 debug ("found goal\nBEGIN" + goal + "\nEND");
346 goalarray[metano] = goal;
347 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
348 metalist = listcons(metano,metalist);
349 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
351 goals.innerHTML = tmp_goallist;
352 debug("new metalist is '" + metalist + "'");
353 if (is_nil(metalist)) {
357 switch_goal(listhd(metalist));
362 function switch_goal(meta)
364 if (typeof meta == "undefined") {
365 goalview.innerHTML = "";
368 debug("switch_goal " + meta + "\n" + goalarray[meta]);
369 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
373 // the following is used to avoid escaping unicode, which results in
374 // the server being unable to unescape the string
375 String.prototype.sescape = function() {
381 result = result.replace(patt1,"%25");
382 result = result.replace(patt2,"%3D");
383 result = result.replace(patt3,"%26");
384 result = result.replace(patt4,"%2B");
388 String.prototype.html_to_matita = function()
390 var patt1 = /<br(\/|)>/gi;
393 var patt4 = /</gi;
394 var patt5 = />/gi;
396 result = result.replace(patt1,"\n");
397 result = result.replace(patt2,"\005");
398 result = result.replace(patt3,"\006");
399 result = result.replace(patt4,"<");
400 result = result.replace(patt5,">");
401 return (unescape(result));
404 String.prototype.matita_to_html = function()
408 var patt3 = /\005/gi;
409 var patt4 = /\006/gi;
411 result = result.replace(patt1,"<");
412 result = result.replace(patt2,">");
413 result = result.replace(patt3,"<");
414 result = result.replace(patt4,">");
415 return (unescape(result));
420 advanceButton.disabled = true;
421 retractButton.disabled = true;
422 cursorButton.disabled = true;
423 bottomButton.disabled = true;
428 advanceButton.disabled = false;
429 retractButton.disabled = false;
430 cursorButton.disabled = false;
431 bottomButton.disabled = false;
434 function is_defined(x)
436 return (typeof x != "undefined");
439 /* servicename: name of the service being called
440 * reqbody: text of the request
441 * processResponse: processes the server response
442 * (takes the response text in input, undefined in case of error)
444 function callServer(servicename,processResponse,reqbody)
448 if (window.XMLHttpRequest)
450 req = new XMLHttpRequest();
452 else if (window.ActiveXObject)
455 req = new ActiveXObject("Msxml2.XMLHTTP");
459 req = new ActiveXObject("Microsoft.XMLHTTP");
463 req.onreadystatechange = function()
471 stxt = req.statusText;
474 debug(req.responseText);
475 if (window.DOMParser) {
476 parser=new DOMParser();
477 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
479 else // Internet Explorer
481 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
482 xmlDoc.async="false";
483 xmlDoc.loadXML(req.responseText);
485 processResponse(xmlDoc);
491 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
492 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
501 function advanceForm1()
503 processor = function(xml) {
504 if (is_defined(xml)) {
505 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
506 parsed = xml.getElementsByTagName("parsed")[0];
507 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;
515 unlocked.innerHTML = unparsedtxt.matita_to_html();
516 // len1 = unlocked.innerHTML.length;
517 // len2 = len0 - len1;
518 len2 = parsedtxt.length;
519 metasenv = xml.getElementsByTagName("meta");
520 populate_goalarray(metasenv);
521 statements = listcons(len2,statements);
522 unlocked.scrollIntoView(true);
524 debug("advance failed");
529 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
533 function gotoBottom()
535 processor = function(xml) {
536 if (is_defined(xml)) {
537 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
538 parsed = xml.getElementsByTagName("parsed")[0];
539 len = parseInt(parsed.getAttribute("length"));
541 // len0 = unlocked.innerHTML.length;
542 unescaped = unlocked.innerHTML.html_to_matita();
543 // not working in mozilla
544 // parsedtxt = parsed.childNodes[0].nodeValue;
545 parsedtxt = parsed.childNodes[0].wholeText;
546 //parsedtxt = unescaped.substr(0,len);
547 unparsedtxt = unescaped.substr(len);
548 lockedbackup += parsedtxt;
549 locked.innerHTML = lockedbackup; //.matita_to_html();
550 unlocked.innerHTML = unparsedtxt.matita_to_html();
551 // len1 = unlocked.innerHTML.length;
552 len2 = parsedtxt.length;
553 metasenv = xml.getElementsByTagName("meta");
554 populate_goalarray(metasenv);
556 statements = listcons(len2,statements);
557 unlocked.scrollIntoView(true);
560 debug("goto bottom failed");
565 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
572 processor = function(xml) {
573 if (is_defined(xml)) {
574 if (xml.childNodes[0].textContent != "ok") {
575 debug("goto top failed");
578 statements = listnil();
580 lockedlen = locked.innerHTML.length - statementlen;
581 statement = locked.innerHTML.substr(lockedlen, statementlen);
582 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
583 unlocked.innerHTML = statement + unlocked.innerHTML;
585 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
587 locked.innerHTML = lockedbackup;
589 unlocked.scrollIntoView(true);
591 debug("goto top failed");
596 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
599 function gotoPos(offset)
601 if (!is_defined(offset)) {
602 offset = getCursorPos();
604 processor = function(xml) {
605 if (is_defined(xml)) {
606 parsed = xml.getElementsByTagName("parsed")[0];
607 len = parseInt(parsed.getAttribute("length"));
608 // len0 = unlocked.innerHTML.length;
609 unescaped = unlocked.innerHTML.html_to_matita();
610 parsedtxt = parsed.childNodes[0].nodeValue;
611 //parsedtxt = unescaped.substr(0,len);
612 unparsedtxt = unescaped.substr(len);
613 lockedbackup += parsedtxt;
614 locked.innerHTML = lockedbackup; //.matita_to_html();
615 unlocked.innerHTML = unparsedtxt.matita_to_html();
616 // len1 = unlocked.innerHTML.length;
617 len2 = parsedtxt.length;
618 metasenv = xml.getElementsByTagName("meta");
619 // populate_goalarray(metasenv);
620 statements = listcons(len2,statements);
621 unlocked.scrollIntoView(true);
622 // la populate non andrebbe fatta a ogni passo
624 populate_goalarray(metasenv);
627 gotoPos(offset - len);
630 unlocked.scrollIntoView(true);
631 populate_goalarray(metasenv);
636 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
641 processor = function(xml) {
642 if (typeof xml != "undefined") {
643 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
644 statementlen = parseInt(listhd(statements));
645 statements = listtl(statements);
647 lockedlen = locked.innerHTML.length - statementlen;
648 statement = locked.innerHTML.substr(lockedlen, statementlen);
649 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
650 unlocked.innerHTML = statement + unlocked.innerHTML;
652 lockedlen = lockedbackup.length - statementlen;
653 statement = lockedbackup.substr(lockedlen, statementlen);
654 lockedbackup = lockedbackup.substr(0,lockedlen);
655 locked.innerHTML = lockedbackup;
656 unlocked.innerHTML = statement + unlocked.innerHTML;
657 metasenv = xml.getElementsByTagName("meta");
658 populate_goalarray(metasenv);
659 unlocked.scrollIntoView(true);
661 debug("retract failed");
667 callServer("retract",processor);
673 processor = function(xml)
675 if (is_defined(xml)) {
677 locked.innerHTML = lockedbackup;
678 unlocked.innerHTML = xml.documentElement.wholeText;
680 debug("file open failed");
683 callServer("open",processor,"file=" + escape(filename.value));
686 function retrieveFile(thefile)
688 processor = function(xml)
690 if (is_defined(xml)) {
691 current_fname = thefile;
693 locked.innerHTML = lockedbackup;
694 // code originally used in google chrome (problems with mozilla)
695 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
696 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
697 debug(xml.childNodes[0].textContent);
698 unlocked.innerHTML = xml.childNodes[0].textContent;
701 debug("file open failed");
704 dialogBox.style.display = "none";
705 callServer("open",processor,"file=" + escape(thefile));
708 function showLibrary(title,callback,reloadDialog)
711 dialogBox.reload = reloadDialog;
713 if (window.XMLHttpRequest)
715 req = new XMLHttpRequest();
717 else if (window.ActiveXObject)
720 req = new ActiveXObject("Msxml2.XMLHTTP");
724 req = new ActiveXObject("Microsoft.XMLHTTP");
728 req.onreadystatechange = function()
736 stxt = req.statusText;
739 debug(req.responseText);
740 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
744 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
745 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
750 function uploadDialog()
752 uploadBox.style.display = "block";
757 var file = document.getElementById("uploadFilename").files[0];
759 var filecontent = file.getAsText("UTF-8");
760 locked.innerHTML = lockedbackup;
761 unlocked.innerHTML = filecontent;
762 uploadBox.style.display = "none";
765 // var reader = new FileReader();
766 // reader.readAsText(file, "UTF-8");
767 // reader.onloadend = function (evt) {
768 // lockedbackup = "";
769 // locked.innerHTML = lockedbackup
770 // unlocked.innerHTML = evt.target.result;
771 // uploadBox.style.display = "none";
773 // reader.onerror = function (evt) {
774 // debug("file open failed");
775 // uploadBox.style.display = "none";
780 function openDialog()
782 callback = function (fname) { retrieveFile(fname); };
783 showLibrary("Open file", callback, openDialog);
786 function saveDialog()
788 callback = function (fname) {
789 dialogBox.style.display = "none";
791 (locked.innerHTML.html_to_matita()).sescape(),
792 (unlocked.innerHTML.html_to_matita()).sescape(),
795 showLibrary("Save file as", callback, saveDialog);
800 callback = function (fname) {
801 dialogBox.style.display = "none";
802 saveFile(fname,"","",false,newDialog,true);
804 showLibrary("Create new file", callback, newDialog);
808 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
810 if (!is_defined(reloadFile)) { reloadFile = true };
811 if (!is_defined(fname)) {
812 fname = current_fname;
813 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
814 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
816 // when force is true, reloadDialog is not needed
818 processor = function(xml) {
819 current_fname = fname;
820 if (is_defined(xml)) {
821 if (xml.childNodes[0].textContent != "ok") {
822 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
823 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
828 current_fname = fname;
829 debug("file saved!");
830 if (reloadFile) { retrieveFile(fname); }
833 debug("save file failed");
837 if (is_defined(fname)) {
839 callServer("save",processor,"file=" + escape(fname) +
840 "&locked=" + lockedtxt +
841 "&unlocked=" + unlockedtxt +
844 else { debug("no file selected"); }
847 function createDir() {
849 dirname = prompt("New directory name:\ncic:/matita/","newdir");
850 if (dirname != null) {
851 processor = function(xml) {
852 if (is_defined(xml)) {
853 if (xml.childNodes[0].textContent != "ok") {
854 alert("An error occurred :-(");
857 alert("An error occurred :-(");
862 callServer("save",processor,"file=" + escape(dirname) +
863 "&locked=&unlocked=&force=false&dir=true");
871 processor = function(xml) {
872 if (is_defined(xml)) {
873 debug("commit succeeded(?)");
875 debug("commit failed!");
880 callServer("commit",processor);
885 processor = function(xml) {
886 if (is_defined(xml)) {
887 debug("update succeeded(?)");
889 debug("update failed!");
894 callServer("update",processor);
899 function hideSequent() {
900 goalcell.style.display = "none";
901 scriptcell.style.width = "100%";
902 scriptcell.style.minWidth = "100%";
903 scriptcell.style.maxWidth = "100%";
906 function showSequent() {
907 scriptcell.style.width = "67%";
908 scriptcell.style.minWidth = "67%";
909 scriptcell.style.maxWidth = "67%";
910 goalcell.style.display = "inline-block";
913 function showDialog(title,content,callback) {
914 dialogTitle.innerHTML = title;
915 dialogContent.innerHTML = content;
916 dialogBox.callback = callback;
917 dialogBox.style.display = "block";
920 function abortDialog() {
921 dialogBox.style.display = "none";
924 function abortUpload() {
925 uploadBox.style.display = "none";
928 function removeElement(id) {
929 var element = document.getElementById(id);
930 element.parentNode.removeChild(element);
936 function getCursorPos() {
938 if (window.getSelection) {
939 var selObj = window.getSelection();
940 savedRange = selObj.getRangeAt(0);
941 savedsc = savedRange.startContainer;
942 savedso = savedRange.startOffset;
943 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
944 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
945 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
948 else if (document.selection) {
949 savedRange = document.selection.createRange();
950 var bookmark = savedRange.getBookmark();
951 /* FIXME the following works wrong when the document is longer than 65535 chars */
952 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
957 function findNode(list, node, acc) {
958 for (var i = 0; i < list.length; i++) {
959 if (list[i] == node) {
960 // debug("success " + i);
963 if (list[i].hasChildNodes()) {
965 // debug("recursion on node " + i);
966 return (findNode(list[i].childNodes,node,acc))
968 catch (e) { /* debug("recursion failed"); */ }
970 sandbox = document.getElementById("sandbox");
971 dup = list[i].cloneNode(true);
972 sandbox.appendChild(dup);
973 // debug("fail " + i + ": " + sandbox.innerHTML);
974 acc += sandbox.innerHTML.length;
975 sandbox.removeChild(dup);
981 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
984 function readCookie(name) {
985 var nameEQ = name + "=";
986 var ca = document.cookie.split(';');
987 for(var i=0;i < ca.length;i++) {
989 while (c.charAt(0)==' ') c = c.substring(1,c.length);
990 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
995 function delete_cookie ( cookie_name )
997 var cookie_date = new Date(); // current date & time
998 cookie_date.setTime ( cookie_date.getTime() - 1 );
999 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1002 function delete_session()
1004 delete_cookie("session");