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)
36 function filterByClass (elements,class){
37 var itemsfound = new Array;
38 for(var i=0;i<elements.length;i++){
39 if(elements[i].className == class){
40 itemsfound.push(elements[i]);
48 if (readCookie("session") == null) {
49 window.location = "/login.html"
51 matitaTitle = document.getElementById("matitaTitle");
52 locked = document.getElementById("locked");
53 unlocked = document.getElementById("unlocked");
54 workarea = document.getElementById("workarea");
55 scriptcell = document.getElementById("scriptcell");
56 goalcell = document.getElementById("goalcell");
57 goals = document.getElementById("goals");
58 goalview = document.getElementById("goalview");
59 filename = document.getElementById("filename");
60 logarea = document.getElementById("logarea");
61 advanceButton = document.getElementById("advance");
62 retractButton = document.getElementById("retract");
63 cursorButton = document.getElementById("cursor");
64 bottomButton = document.getElementById("bottom");
65 dialogBox = document.getElementById("dialogBox");
66 dialogTitle = document.getElementById("dialogTitle");
67 dialogContent = document.getElementById("dialogContent");
69 changeFile("test.ma");
71 // hide sequent view at start
74 // initialize keyboard events in the unlocked script
75 init_keyboard(unlocked);
79 function changeFile(name) {
81 matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
84 function init_keyboard(target)
86 if (target.addEventListener)
88 // target.addEventListener("keydown",keydown,false);
89 target.addEventListener("keypress",keypress,false);
90 // target.addEventListener("keyup",keyup,false);
91 // target.addEventListener("textInput",textinput,false);
93 else if (target.attachEvent)
95 // target.attachEvent("onkeydown", keydown);
96 target.attachEvent("onkeypress", keypress);
97 // target.attachEvent("onkeyup", keyup);
98 // target.attachEvent("ontextInput", textinput);
102 // target.onkeydown= keydown;
103 target.onkeypress= keypress;
104 // target.onkeyup= keyup;
105 // target.ontextinput= textinput; // probably doesn't work
112 if (n == null) return 'undefined';
114 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
115 while (s.length < 9) s+= ' ';
119 function string_of_key(n)
121 if (n == null) return 'undefined';
122 return String.fromCharCode(n);
125 function pressmesg(w,e)
127 debug(w + ' keyCode=' + keyval(e.keyCode) +
128 ' which=' + keyval(e.which) +
129 ' charCode=' + keyval(e.charCode) +
130 '\n shiftKey='+e.shiftKey
131 + ' ctrlKey='+e.ctrlKey
132 + ' altKey='+e.altKey
133 + ' metaKey='+e.metaKey);
136 function suppressdefault(e,flag)
140 if (e.preventDefault) e.preventDefault();
141 if (e.stopPropagation) e.stopPropagation();
146 function restoreSelection(r) {
149 if (window.getSelection)//non IE and there is already a selection
151 var s = window.getSelection();
152 if (s.rangeCount > 0)
157 if (document.createRange)//non IE and no selection
159 window.getSelection().addRange(r);
162 if (document.selection)//IE
169 function lookup_tex(texmacro)
171 texmacro = texmacro.substring(1);
172 return unescape(macro2utf8[texmacro]);
175 function strip_errors()
177 errors = filterByClass(unlocked.getElementsByTagName("span"),"error");
178 for (i = 0; i < errors.length; i++) {
179 children = errors[i].childNodes;
180 for (j = 0; j < children.length; j++) {
181 unlocked.insertBefore(children[j],errors[i]);
183 unlocked.removeChild(errors[i]);
190 pressmesg('keypress',e);
191 var s = string_of_key(e.charCode);
195 i = unlocked.innerHTML.lastIndexOf('\\',j);
197 match = unlocked.innerHTML.substring(i,j);
198 sym = unescape_html(lookup_tex(match));
199 if (sym != "undefined") {
200 if (window.getSelection) { // non IE
201 savedRange.setStart(savedsc,savedso - (j-i));
202 savedRange.deleteContents();
203 savedRange.insertNode(document.createTextNode(sym));
204 savedsc.parentNode.normalize();
205 if (savedRange.collapsed) { // Mozilla
206 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
208 savedRange.collapse(false);
210 savedRange.moveStart(i-j);
211 savedRange.text(sym);
212 savedRange.collapse(false);
214 restoreSelection(savedRange);
215 return suppressdefault(e,true);
218 // restoreSelection(0);
219 return suppressdefault(e,false);
222 else return suppressdefault(e,false);
224 return suppressdefault(e,false);
232 // internet explorer (v.9) doesn't work with innerHTML
233 // but google chrome's innerText is, in a sense, "write only"
234 // what should we do?
235 // logarea.innerText = txt + "\n" + logarea.innerText;
236 logtxt = logtxt + "\n" + txt;
240 logWin = window.open( "", "Matita Log",
241 "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
242 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
243 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
244 logtxt + '</textarea></body></html>');
245 logWin.document.close();
251 debug("hd of '" + l + "' = '" + ar[0] + "'");
259 debug("tl of '" + l + "' = '" + tl + "'");
263 function listcons(x,l)
265 debug("cons '" + x + "' on '" + l + "'");
266 return (x + "#" + l);
279 function fold_left (f,acc,l)
282 { debug("'" + l + "' is fold end");
285 { debug("'" + l + "' is fold cons");
286 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
289 function listiter (f,l)
292 { debug("'" + l + "' is nil");
297 debug("'" + l + "' is not nil");
299 listiter(f,listtl(l));
303 function listmap (f,l)
305 debug("listmap on " + l);
307 { debug("returning listnil");
311 { debug("cons f(hd) map(f,tl)");
312 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
316 var statements = listnil();
319 var metalist = listnil();
321 function pairmap (f,p)
323 debug("pairmap of '" + p + "'");
325 return (f(ar[0],ar[1]));
328 function tripletmap (f,p)
330 debug("tripletmap of '" + p + "'");
332 return (f(ar[0],ar[1],ar[2]));
338 return (pairmap (function (a,b) { return (a); }, p));
344 return (tripletmap (function (a,b,c) { return (a); }, p));
350 return (tripletmap (function (a,b,c) { return (b); }, p));
356 return (tripletmap (function (a,b,c) { return (c); }, p));
359 function populate_goalarray(menv)
361 debug("metasenv.length = " + menv.length);
362 if (menv.length == 0) {
368 goalarray = new Array();
369 metalist = listnil();
370 var tmp_goallist = "";
371 for (i = 0; i < menv.length; i++) {
372 metano = menv[i].getAttribute("number");
373 metaname = menv[i].childNodes[0].childNodes[0].data;
374 goal = menv[i].childNodes[1].childNodes[0].data;
375 debug ("found meta n. " + metano);
376 debug ("found goal\nBEGIN" + goal + "\nEND");
377 goalarray[metano] = goal;
378 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
379 metalist = listcons(metano,metalist);
380 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
382 goals.innerHTML = tmp_goallist;
383 debug("new metalist is '" + metalist + "'");
384 if (is_nil(metalist)) {
388 switch_goal(listhd(metalist));
393 function switch_goal(meta)
395 if (typeof meta == "undefined") {
396 goalview.innerHTML = "";
399 debug("switch_goal " + meta + "\n" + goalarray[meta]);
400 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
404 // the following is used to avoid escaping unicode, which results in
405 // the server being unable to unescape the string
406 String.prototype.sescape = function() {
412 result = result.replace(patt1,"%25");
413 result = result.replace(patt2,"%3D");
414 result = result.replace(patt3,"%26");
415 result = result.replace(patt4,"%2B");
419 String.prototype.html_to_matita = function()
421 var patt1 = /<br(\/|)>/gi;
424 var patt4 = /</gi;
425 var patt5 = />/gi;
426 var patt6 = / /gi;
428 result = result.replace(patt1,"\n");
429 result = result.replace(patt2,"\005");
430 result = result.replace(patt3,"\006");
431 result = result.replace(patt4,"<");
432 result = result.replace(patt5,">");
433 result = result.replace(patt6," ");
434 return (unescape(result));
437 String.prototype.matita_to_html = function()
441 var patt3 = /\005/gi;
442 var patt4 = /\006/gi;
444 result = result.replace(patt1,"<");
445 result = result.replace(patt2,">");
446 result = result.replace(patt3,"<");
447 result = result.replace(patt4,">");
448 return (unescape(result));
453 advanceButton.disabled = true;
454 retractButton.disabled = true;
455 cursorButton.disabled = true;
456 bottomButton.disabled = true;
461 advanceButton.disabled = false;
462 retractButton.disabled = false;
463 cursorButton.disabled = false;
464 bottomButton.disabled = false;
467 function is_defined(x)
469 return (typeof x != "undefined");
472 /* servicename: name of the service being called
473 * reqbody: text of the request
474 * processResponse: processes the server response
475 * (takes the response text in input, undefined in case of error)
477 function callServer(servicename,processResponse,reqbody)
481 if (window.XMLHttpRequest)
483 req = new XMLHttpRequest();
485 else if (window.ActiveXObject)
488 req = new ActiveXObject("Msxml2.XMLHTTP");
492 req = new ActiveXObject("Microsoft.XMLHTTP");
496 req.onreadystatechange = function()
504 stxt = req.statusText;
507 debug(req.responseText);
508 if (window.DOMParser) {
509 parser=new DOMParser();
510 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
512 else // Internet Explorer
514 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
515 xmlDoc.async="false";
516 xmlDoc.loadXML(req.responseText);
518 processResponse(xmlDoc);
524 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
525 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
534 function advanceForm1()
536 processor = function(xml) {
537 if (is_defined(xml)) {
538 parsed = xml.getElementsByTagName("parsed")[0];
539 if (is_defined(parsed)) {
540 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
541 len = parseInt(parsed.getAttribute("length"));
542 // len0 = unlocked.innerHTML.length;
543 unescaped = unlocked.innerHTML.html_to_matita();
544 parsedtxt = parsed.childNodes[0].nodeValue;
545 //parsedtxt = unescaped.substr(0,len);
546 unparsedtxt = unescaped.substr(len);
547 lockedbackup += parsedtxt;
548 locked.innerHTML = lockedbackup;
549 unlocked.innerHTML = unparsedtxt.matita_to_html();
550 // len1 = unlocked.innerHTML.length;
551 // len2 = len0 - len1;
552 len2 = parsedtxt.length;
553 metasenv = xml.getElementsByTagName("meta");
554 populate_goalarray(metasenv);
555 statements = listcons(len2,statements);
556 unlocked.scrollIntoView(true);
559 error = xml.getElementsByTagName("error")[0];
560 unlocked.innerHTML = error.childNodes[0].nodeValue;
561 // debug(xml.childNodes[0].nodeValue);
564 debug("advance failed");
569 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
573 function gotoBottom()
575 processor = function(xml) {
576 if (is_defined(xml)) {
577 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
578 parsed = xml.getElementsByTagName("parsed")[0];
579 len = parseInt(parsed.getAttribute("length"));
581 // len0 = unlocked.innerHTML.length;
582 unescaped = unlocked.innerHTML.html_to_matita();
583 // not working in mozilla
584 // parsedtxt = parsed.childNodes[0].nodeValue;
585 parsedtxt = parsed.childNodes[0].wholeText;
586 //parsedtxt = unescaped.substr(0,len);
587 unparsedtxt = unescaped.substr(len);
588 lockedbackup += parsedtxt;
589 locked.innerHTML = lockedbackup; //.matita_to_html();
590 unlocked.innerHTML = unparsedtxt.matita_to_html();
591 // len1 = unlocked.innerHTML.length;
592 len2 = parsedtxt.length;
593 metasenv = xml.getElementsByTagName("meta");
594 populate_goalarray(metasenv);
596 statements = listcons(len2,statements);
597 unlocked.scrollIntoView(true);
600 debug("goto bottom failed");
605 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
612 processor = function(xml) {
613 if (is_defined(xml)) {
614 if (xml.childNodes[0].textContent != "ok") {
615 debug("goto top failed");
618 statements = listnil();
620 lockedlen = locked.innerHTML.length - statementlen;
621 statement = locked.innerHTML.substr(lockedlen, statementlen);
622 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
623 unlocked.innerHTML = statement + unlocked.innerHTML;
625 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
627 locked.innerHTML = lockedbackup;
629 unlocked.scrollIntoView(true);
631 debug("goto top failed");
636 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
639 function gotoPos(offset)
641 if (!is_defined(offset)) {
642 offset = getCursorPos();
644 processor = function(xml) {
645 if (is_defined(xml)) {
646 parsed = xml.getElementsByTagName("parsed")[0];
647 len = parseInt(parsed.getAttribute("length"));
648 // len0 = unlocked.innerHTML.length;
649 unescaped = unlocked.innerHTML.html_to_matita();
650 parsedtxt = parsed.childNodes[0].nodeValue;
651 //parsedtxt = unescaped.substr(0,len);
652 unparsedtxt = unescaped.substr(len);
653 lockedbackup += parsedtxt;
654 locked.innerHTML = lockedbackup; //.matita_to_html();
655 unlocked.innerHTML = unparsedtxt.matita_to_html();
656 // len1 = unlocked.innerHTML.length;
657 len2 = parsedtxt.length;
658 metasenv = xml.getElementsByTagName("meta");
659 // populate_goalarray(metasenv);
660 statements = listcons(len2,statements);
661 unlocked.scrollIntoView(true);
662 // la populate non andrebbe fatta a ogni passo
664 populate_goalarray(metasenv);
667 gotoPos(offset - len);
670 unlocked.scrollIntoView(true);
671 populate_goalarray(metasenv);
676 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
681 processor = function(xml) {
682 if (typeof xml != "undefined") {
683 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
684 statementlen = parseInt(listhd(statements));
685 statements = listtl(statements);
687 lockedlen = locked.innerHTML.length - statementlen;
688 statement = locked.innerHTML.substr(lockedlen, statementlen);
689 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
690 unlocked.innerHTML = statement + unlocked.innerHTML;
692 lockedlen = lockedbackup.length - statementlen;
693 statement = lockedbackup.substr(lockedlen, statementlen);
694 lockedbackup = lockedbackup.substr(0,lockedlen);
695 locked.innerHTML = lockedbackup;
696 unlocked.innerHTML = statement + unlocked.innerHTML;
697 metasenv = xml.getElementsByTagName("meta");
698 populate_goalarray(metasenv);
699 unlocked.scrollIntoView(true);
701 debug("retract failed");
707 callServer("retract",processor);
713 processor = function(xml)
715 if (is_defined(xml)) {
717 locked.innerHTML = lockedbackup;
718 unlocked.innerHTML = xml.documentElement.wholeText;
720 debug("file open failed");
723 callServer("open",processor,"file=" + escape(filename.value));
726 function retrieveFile(thefile)
728 processor = function(xml)
730 if (is_defined(xml)) {
733 locked.innerHTML = lockedbackup;
734 // code originally used in google chrome (problems with mozilla)
735 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
736 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
737 debug(xml.childNodes[0].textContent);
738 if (document.all) { // IE
739 unlocked.innerHTML = xml.childNodes[0].text;
741 unlocked.innerHTML = xml.childNodes[0].textContent;
745 debug("file open failed");
748 dialogBox.style.display = "none";
749 callServer("open",processor,"file=" + escape(thefile));
752 function showLibrary(title,callback,reloadDialog)
755 dialogBox.reload = reloadDialog;
757 if (window.XMLHttpRequest)
759 req = new XMLHttpRequest();
761 else if (window.ActiveXObject)
764 req = new ActiveXObject("Msxml2.XMLHTTP");
768 req = new ActiveXObject("Microsoft.XMLHTTP");
772 req.onreadystatechange = function()
780 stxt = req.statusText;
783 debug(req.responseText);
784 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
788 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
789 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
794 function uploadDialog()
796 uploadBox.style.display = "block";
801 var file = document.getElementById("uploadFilename").files[0];
803 var filecontent = file.getAsText("UTF-8");
804 locked.innerHTML = lockedbackup;
805 unlocked.innerHTML = filecontent;
806 uploadBox.style.display = "none";
809 // var reader = new FileReader();
810 // reader.readAsText(file, "UTF-8");
811 // reader.onloadend = function (evt) {
812 // lockedbackup = "";
813 // locked.innerHTML = lockedbackup
814 // unlocked.innerHTML = evt.target.result;
815 // uploadBox.style.display = "none";
817 // reader.onerror = function (evt) {
818 // debug("file open failed");
819 // uploadBox.style.display = "none";
824 function openDialog()
826 callback = function (fname) { retrieveFile(fname); };
827 showLibrary("Open file", callback, openDialog);
830 function saveDialog()
832 callback = function (fname) {
833 dialogBox.style.display = "none";
835 (locked.innerHTML.html_to_matita()).sescape(),
836 (unlocked.innerHTML.html_to_matita()).sescape(),
839 showLibrary("Save file as", callback, saveDialog);
844 callback = function (fname) {
845 dialogBox.style.display = "none";
846 saveFile(fname,"","",false,newDialog,true);
848 showLibrary("Create new file", callback, newDialog);
852 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
854 if (!is_defined(reloadFile)) { reloadFile = true };
855 if (!is_defined(fname)) {
856 fname = current_fname;
857 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
858 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
860 // when force is true, reloadDialog is not needed
862 processor = function(xml) {
863 if (is_defined(xml)) {
864 if (xml.childNodes[0].textContent != "ok") {
865 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
866 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
872 debug("file saved!");
873 if (reloadFile) { retrieveFile(fname); }
876 debug("save file failed");
880 if (is_defined(fname)) {
882 callServer("save",processor,"file=" + escape(fname) +
883 "&locked=" + lockedtxt +
884 "&unlocked=" + unlockedtxt +
887 else { debug("no file selected"); }
890 function createDir() {
892 dirname = prompt("New directory name:\ncic:/matita/","newdir");
893 if (dirname != null) {
894 processor = function(xml) {
895 if (is_defined(xml)) {
896 if (xml.childNodes[0].textContent != "ok") {
897 alert("An error occurred :-(");
900 alert("An error occurred :-(");
905 callServer("save",processor,"file=" + escape(dirname) +
906 "&locked=&unlocked=&force=false&dir=true");
914 processor = function(xml) {
915 if (is_defined(xml)) {
916 debug(xml.getElementsByTagName("details")[0].textContent);
917 alert("Commit executed: see details in the log.\n\n" +
918 "NOTICE: this message does NOT imply (yet) that the commit was successful.");
920 alert("Commit failed!");
925 callServer("commit",processor);
930 processor = function(xml) {
931 if (is_defined(xml)) {
932 alert("Update executed.\n\n" +
934 xml.getElementsByTagName("details")[0].textContent);
936 alert("Update failed!");
941 callServer("update",processor);
946 function hideSequent() {
947 goalcell.style.display = "none";
948 scriptcell.style.width = "100%";
949 scriptcell.style.minWidth = "100%";
950 scriptcell.style.maxWidth = "100%";
953 function showSequent() {
954 scriptcell.style.width = "67%";
955 scriptcell.style.minWidth = "67%";
956 scriptcell.style.maxWidth = "67%";
957 goalcell.style.display = "inline-block";
960 function showDialog(title,content,callback) {
961 dialogTitle.innerHTML = title;
962 dialogContent.innerHTML = content;
963 dialogBox.callback = callback;
964 dialogBox.style.display = "block";
967 function abortDialog() {
968 dialogBox.style.display = "none";
971 function abortUpload() {
972 uploadBox.style.display = "none";
975 function removeElement(id) {
976 var element = document.getElementById(id);
977 element.parentNode.removeChild(element);
983 function getCursorPos() {
985 if (window.getSelection) {
986 var selObj = window.getSelection();
987 savedRange = selObj.getRangeAt(0);
988 savedsc = savedRange.startContainer;
989 savedso = savedRange.startOffset;
990 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
991 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
992 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
995 else if (document.selection) {
996 savedRange = document.selection.createRange();
997 var bookmark = savedRange.getBookmark();
998 /* FIXME the following works wrong when the document is longer than 65535 chars */
999 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1004 function findNode(list, node, acc) {
1005 for (var i = 0; i < list.length; i++) {
1006 if (list[i] == node) {
1007 // debug("success " + i);
1010 if (list[i].hasChildNodes()) {
1012 // debug("recursion on node " + i);
1013 return (findNode(list[i].childNodes,node,acc))
1015 catch (e) { /* debug("recursion failed"); */ }
1017 sandbox = document.getElementById("sandbox");
1018 dup = list[i].cloneNode(true);
1019 sandbox.appendChild(dup);
1020 // debug("fail " + i + ": " + sandbox.innerHTML);
1021 acc += sandbox.innerHTML.length;
1022 sandbox.removeChild(dup);
1028 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1031 function readCookie(name) {
1032 var nameEQ = name + "=";
1033 var ca = document.cookie.split(';');
1034 for(var i=0;i < ca.length;i++) {
1036 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1037 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1042 function delete_cookie ( cookie_name )
1044 var cookie_date = new Date(); // current date & time
1045 cookie_date.setTime ( cookie_date.getTime() - 1 );
1046 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1049 function delete_session()
1051 delete_cookie("session");