18 var lockedbackup = "";
21 function text_of_html(h)
30 function unescape_html(s)
32 u = document.getElementById("unescape");
34 return text_of_html(u)
37 function filterByClass (elements,cname){
38 var itemsfound = new Array;
39 for(var i=0;i<elements.length;i++){
40 if(elements[i].className == cname){
41 itemsfound.push(elements[i]);
49 if (readCookie("session") == null) {
50 window.location = "/login.html"
53 titlebar = document.getElementById("titlebar");
54 matitaTitle = document.getElementById("matitaTitle");
55 apparea = document.getElementById("matitaapparea");
56 locked = document.getElementById("locked");
57 unlocked = document.getElementById("unlocked");
58 toparea = document.getElementById("toparea");
59 workarea = document.getElementById("workarea");
60 scriptcell = document.getElementById("scriptcell");
61 sidearea = document.getElementById("sidearea");
62 disambcell = document.getElementById("disambcell");
63 goalcell = document.getElementById("goalcell");
64 goals = document.getElementById("goals");
65 goalview = document.getElementById("goalview");
66 filename = document.getElementById("filename");
67 logarea = document.getElementById("logarea");
68 advanceButton = document.getElementById("advance");
69 retractButton = document.getElementById("retract");
70 cursorButton = document.getElementById("cursor");
71 bottomButton = document.getElementById("bottom");
72 dialogBox = document.getElementById("dialogBox");
73 uploadBox = document.getElementById("uploadBox");
74 dialogTitle = document.getElementById("dialogTitle");
75 dialogContent = document.getElementById("dialogContent");
77 matita = new Object();
78 matita.disambMode = matita.proofMode = false;
80 // hide sequent view at start
84 changeFile("test.ma");
86 // initialize keyboard events in the unlocked script
87 init_keyboard(unlocked);
94 function init_autotraces() {
95 $("#unlocked .autotactic").tooltip({
98 bodyHandler: function() {
99 return (trace_of($(this)[0]));
102 $("#locked .autotactic").tooltip({
105 bodyHandler: function() {
106 return (trace_of($(this)[0]));
111 function trace_of(node) {
112 return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
115 function changeFile(name) {
116 current_fname = name;
117 matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
120 function init_keyboard(target)
122 if (target.addEventListener)
124 // target.addEventListener("keydown",keydown,false);
125 target.addEventListener("keypress",keypress,false);
126 // target.addEventListener("keyup",keyup,false);
127 // target.addEventListener("textInput",textinput,false);
129 else if (target.attachEvent)
131 // target.attachEvent("onkeydown", keydown);
132 target.attachEvent("onkeypress", keypress);
133 // target.attachEvent("onkeyup", keyup);
134 // target.attachEvent("ontextInput", textinput);
138 // target.onkeydown= keydown;
139 target.onkeypress= keypress;
140 // target.onkeyup= keyup;
141 // target.ontextinput= textinput; // probably doesn't work
148 if (n == null) return 'undefined';
150 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
151 while (s.length < 9) s+= ' ';
155 function string_of_key(n)
157 if (n == null) return 'undefined';
158 return String.fromCharCode(n);
161 function pressmesg(w,e)
163 debug(w + ' keyCode=' + keyval(e.keyCode) +
164 ' which=' + keyval(e.which) +
165 ' charCode=' + keyval(e.charCode) +
166 '\n shiftKey='+e.shiftKey
167 + ' ctrlKey='+e.ctrlKey
168 + ' altKey='+e.altKey
169 + ' metaKey='+e.metaKey);
172 function suppressdefault(e,flag)
176 if (e.preventDefault) e.preventDefault();
177 if (e.stopPropagation) e.stopPropagation();
182 function restoreSelection(r) {
185 if (window.getSelection)//non IE and there is already a selection
187 var s = window.getSelection();
188 if (s.rangeCount > 0)
193 if (document.createRange)//non IE and no selection
195 window.getSelection().addRange(r);
198 if (document.selection)//IE
205 function lookup_tex(texmacro)
207 texmacro = texmacro.substring(1);
208 return unescape(macro2utf8[texmacro]);
211 function strip_tags(tagname,classname)
213 var tags = unlocked.getElementsByTagName(tagname);
214 if (is_defined(classname)) {
215 tags = filterByClass(tags,classname);
217 for (i = 0; i < tags.length; i++) {
218 var children = tags[i].childNodes;
219 for (j = 0; j < children.length; j++) {
220 tags[i].parentNode.insertBefore(children[j],tags[i]);
223 while (tags.length > 0) {
224 tags[0].parentNode.removeChild(tags[0]);
228 function strip_interpr() {
230 alert("strip_interpr ended");
236 pressmesg('keypress',e);
237 var s = string_of_key(e.charCode);
238 strip_tags("span","error");
241 i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
243 match = unlocked.innerHTML.html_to_matita().substring(i,j);
244 sym = unescape_html(lookup_tex(match));
245 if (sym != "undefined") {
246 if (window.getSelection) { // non IE
247 savedRange.setStart(savedsc,savedso - (j-i));
248 savedRange.deleteContents();
249 savedRange.insertNode(document.createTextNode(sym));
250 savedsc.parentNode.normalize();
251 if (savedRange.collapsed) { // Mozilla
252 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
254 savedRange.collapse(false);
256 savedRange.moveStart(i-j);
257 savedRange.text(sym);
258 savedRange.collapse(false);
260 restoreSelection(savedRange);
261 return suppressdefault(e,true);
264 // restoreSelection(0);
265 return suppressdefault(e,false);
268 else return suppressdefault(e,false);
270 return suppressdefault(e,false);
278 // internet explorer (v.9) doesn't work with innerHTML
279 // but google chrome's innerText is, in a sense, "write only"
280 // what should we do?
281 // logarea.innerText = txt + "\n" + logarea.innerText;
282 logtxt = /* logtxt + "\n" +*/ txt;
286 logWin = window.open( "", "Matita Log",
287 "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
288 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
289 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
290 logtxt + '</textarea></body></html>');
291 logWin.document.close();
297 debug("hd of '" + l + "' = '" + ar[0] + "'");
305 debug("tl of '" + l + "' = '" + tl + "'");
309 function listcons(x,l)
311 debug("cons '" + x + "' on '" + l + "'");
312 return (x + "#" + l);
320 function list_append(l1,l2)
328 function fold_left (f,acc,l)
331 { debug("'" + l + "' is fold end");
334 { debug("'" + l + "' is fold cons");
335 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
338 function listiter (f,l)
341 { debug("'" + l + "' is nil");
346 debug("'" + l + "' is not nil");
348 listiter(f,listtl(l));
352 function listmap (f,l)
354 debug("listmap on " + l);
356 { debug("returning listnil");
360 { debug("cons f(hd) map(f,tl)");
361 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
365 var statements = listnil();
368 var metalist = listnil();
370 function pairmap (f,p)
372 debug("pairmap of '" + p + "'");
374 return (f(ar[0],ar[1]));
377 function tripletmap (f,p)
379 debug("tripletmap of '" + p + "'");
381 return (f(ar[0],ar[1],ar[2]));
387 return (pairmap (function (a,b) { return (a); }, p));
393 return (tripletmap (function (a,b,c) { return (a); }, p));
399 return (tripletmap (function (a,b,c) { return (b); }, p));
405 return (tripletmap (function (a,b,c) { return (c); }, p));
408 function populate_goalarray(menv)
410 debug("metasenv.length = " + menv.length);
411 if (menv.length == 0) {
417 goalarray = new Array();
418 metalist = listnil();
419 var tmp_goallist = "";
420 for (i = 0; i < menv.length; i++) {
421 metano = menv[i].getAttribute("number");
422 metaname = menv[i].childNodes[0].childNodes[0].data;
423 goal = menv[i].childNodes[1].childNodes[0].data;
424 debug ("found meta n. " + metano);
425 debug ("found goal\nBEGIN" + goal + "\nEND");
426 goalarray[metano] = goal;
427 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
428 metalist = listcons(metano,metalist);
429 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
431 goals.innerHTML = tmp_goallist;
432 debug("new metalist is '" + metalist + "'");
433 if (is_nil(metalist)) {
437 switch_goal(listhd(metalist));
442 function switch_goal(meta)
444 if (typeof meta == "undefined") {
445 goalview.innerHTML = "";
448 debug("switch_goal " + meta + "\n" + goalarray[meta]);
449 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
453 // the following is used to avoid escaping unicode, which results in
454 // the server being unable to unescape the string
455 String.prototype.sescape = function() {
461 result = result.replace(patt1,"%25");
462 result = result.replace(patt2,"%3D");
463 result = result.replace(patt3,"%26");
464 result = result.replace(patt4,"%2B");
468 String.prototype.html_to_matita = function()
470 var patt1 = /<br(\/|)>/gi;
473 var patt4 = /</gi;
474 var patt5 = />/gi;
475 var patt6 = / /gi;
477 result = result.replace(patt1,"\n");
478 result = result.replace(patt2,"\005");
479 result = result.replace(patt3,"\006");
480 result = result.replace(patt4,"<");
481 result = result.replace(patt5,">");
482 result = result.replace(patt6," ");
483 return (unescape(result));
486 String.prototype.matita_to_html = function()
490 var patt3 = /\005/gi;
491 var patt4 = /\006/gi;
493 result = result.replace(patt1,"<");
494 result = result.replace(patt2,">");
495 result = result.replace(patt3,"<");
496 result = result.replace(patt4,">");
497 return (unescape(result));
502 advanceButton.disabled = true;
503 retractButton.disabled = true;
504 cursorButton.disabled = true;
505 bottomButton.disabled = true;
510 advanceButton.disabled = false;
511 retractButton.disabled = false;
512 cursorButton.disabled = false;
513 bottomButton.disabled = false;
516 function is_defined(x)
518 return (typeof x != "undefined");
521 /* servicename: name of the service being called
522 * reqbody: text of the request
523 * processResponse: processes the server response
524 * (takes the response text in input, undefined in case of error)
526 function callServer(servicename,processResponse,reqbody)
530 if (window.XMLHttpRequest)
532 req = new XMLHttpRequest();
534 else if (window.ActiveXObject)
537 req = new ActiveXObject("Msxml2.XMLHTTP");
541 req = new ActiveXObject("Microsoft.XMLHTTP");
545 req.onreadystatechange = function()
553 stxt = req.statusText;
556 debug(req.responseText);
557 if (window.DOMParser) {
558 parser=new DOMParser();
559 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
561 else // Internet Explorer
563 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
564 xmlDoc.async="false";
565 xmlDoc.loadXML(req.responseText);
567 processResponse(xmlDoc);
573 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
574 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
583 function advanceForm1()
585 processor = function(xml) {
586 if (is_defined(xml)) {
587 var parsed = xml.getElementsByTagName("parsed")[0];
588 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
589 var disamberr = xml.getElementsByTagName("disamberror")[0];
590 if (is_defined(parsed)) {
591 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
592 var len = parseInt(parsed.getAttribute("length"));
593 // len0 = unlocked.innerHTML.length;
594 var unescaped = unlocked.innerHTML.html_to_matita();
595 var parsedtxt = parsed.childNodes[0].wholeText;
596 //parsedtxt = unescaped.substr(0,len);
597 var unparsedtxt = unescaped.substr(len);
598 lockedbackup += parsedtxt;
599 locked.innerHTML = lockedbackup;
600 unlocked.innerHTML = unparsedtxt.matita_to_html();
601 // len1 = unlocked.innerHTML.length;
602 // len2 = len0 - len1;
603 var len2 = parsedtxt.length;
604 var metasenv = xml.getElementsByTagName("meta");
605 populate_goalarray(metasenv);
607 statements = listcons(len2,statements);
608 unlocked.scrollIntoView(true);
610 else if (is_defined(ambiguity)) {
611 var start = parseInt(ambiguity.getAttribute("start"));
612 var stop = parseInt(ambiguity.getAttribute("stop"));
613 var choices = xml.getElementsByTagName("choice");
615 matita.ambiguityStart = start;
616 matita.ambiguityStop = stop;
617 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
618 matita.interpretations = [];
620 var unlockedtxt = unlocked.innerHTML.html_to_matita();
621 var pre = unlockedtxt.substring(0,start).matita_to_html();
622 var mid = unlockedtxt.substring(start,stop).matita_to_html();
623 var post = unlockedtxt.substring(stop).matita_to_html();
624 unlocked.innerHTML = pre +
625 "<span class=\"error\" title=\"disambiguation error\">" +
626 mid + "</span>" + post;
628 var title = "<H3>Ambiguous input</H3>";
629 disambcell.innerHTML = title;
630 for (i = 0;i < choices.length;i++) {
631 matita.interpretations[i] = new Object();
633 var href = choices[i].getAttribute("href");
634 var title = choices[i].getAttribute("title");
635 var desc = choices[i].childNodes[0].nodeValue;
637 matita.interpretations[i].href = href;
638 matita.interpretations[i].title = title;
639 matita.interpretations[i].desc = desc;
641 var choice = document.createElement("input");
642 choice.setAttribute("type","radio");
643 choice.setAttribute("name","interpr");
644 choice.setAttribute("href",href);
645 choice.setAttribute("title",title);
646 if (i == 0) choice.setAttribute("checked","");
648 disambcell.appendChild(choice);
649 disambcell.appendChild(document.createTextNode(desc));
650 disambcell.appendChild(document.createElement("br"));
653 var okbutton = document.createElement("input");
654 okbutton.setAttribute("type","button");
655 okbutton.setAttribute("value","OK");
656 okbutton.setAttribute("onclick","do_disambiguate()");
657 var cancelbutton = document.createElement("input");
658 cancelbutton.setAttribute("type","button");
659 cancelbutton.setAttribute("value","Cancel");
660 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
662 disambcell.appendChild(okbutton);
663 disambcell.appendChild(cancelbutton);
667 matita.disambMode = true;
670 else if (is_defined(disamberr)) {
671 set_cps(disamberr.childNodes);
672 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
673 matita.disambMode = true;
678 var error = xml.getElementsByTagName("error")[0];
679 unlocked.innerHTML = error.childNodes[0].wholeText;
680 // debug(xml.childNodes[0].nodeValue);
683 debug("advance failed");
688 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
692 // get or set <choicepoint>'s (in case of disamb error)
694 return matita.choicepoints
697 function set_cps(cps) {
698 matita.choicepoints = cps;
701 // get radio buttons for <choice>'s in a given cp
702 function get_choice_opts(i) {
704 var choices = get_cps()[i].childNodes;
705 for (var j = 0;j < choices.length;j++) {
706 var href = choices[j].getAttribute("href");
707 var title = choices[j].getAttribute("title");
709 if (is_defined(title)) {
715 res[j] = document.createElement("input");
716 res[j].setAttribute("type","radio");
717 res[j].setAttribute("name","choice");
718 res[j].setAttribute("choicepointno",i);
719 res[j].setAttribute("choiceno",j);
720 res[j].setAttribute("href",href);
721 res[j].setAttribute("title",title);
722 res[j].setAttribute("desc",desc);
724 if (j == 0) res[j].setAttribute("checked","");
729 // get radio buttons for <failure>'s in a given choice
730 function get_failure_opts(i,j) {
732 var failures = get_cps()[i].childNodes[j].childNodes;
733 for (var k = 0;k < failures.length;k++) {
734 var start = failures[k].getAttribute("start");
735 var stop = failures[k].getAttribute("stop");
736 var title = failures[k].getAttribute("title");
738 res[k] = document.createElement("input");
739 res[k].setAttribute("type","radio");
740 res[k].setAttribute("name","failure");
741 res[k].setAttribute("choicepointno",i);
742 res[k].setAttribute("choiceno",j);
743 res[k].setAttribute("failureno",k);
744 res[k].setAttribute("start",start);
745 res[k].setAttribute("stop",stop);
746 res[k].setAttribute("title",title);
748 if (k == 0) res[k].setAttribute("checked","");
753 function next_cp(curcp) {
754 var cp = get_cps()[curcp];
755 var start = parseInt(cp.getAttribute("start"));
756 var stop = parseInt(cp.getAttribute("stop"));
758 matita.errorStart = start;
759 matita.errorStop = stop;
760 // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
762 var unlockedtxt = matita.unlockedbackup;
763 var pre = unlockedtxt.substring(0,start).matita_to_html();
764 var mid = unlockedtxt.substring(start,stop).matita_to_html();
765 var post = unlockedtxt.substring(stop).matita_to_html();
766 unlocked.innerHTML = pre +
767 "<span class=\"error\" title=\"disambiguation failed\">" +
768 mid + "</span>" + post;
770 var title = "<H3>Disambiguation failed</H3>";
771 disambcell.innerHTML = title;
772 var choices = get_choice_opts(curcp);
773 for (var i = 0;i < choices.length;i++) {
774 disambcell.appendChild(choices[i]);
775 disambcell.appendChild(document.createTextNode(choices[i].getAttribute("desc")));
776 disambcell.appendChild(document.createElement("br"));
779 // update index of the next choicepoint
780 new_curcp = (curcp + 1) % get_cps().length;
782 var okbutton = document.createElement("input");
783 okbutton.setAttribute("type","button");
784 okbutton.setAttribute("value","OK");
785 okbutton.setAttribute("onclick","show_failures()");
786 var cancelbutton = document.createElement("input");
787 cancelbutton.setAttribute("type","button");
788 cancelbutton.setAttribute("value","Close");
789 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
790 var tryagainbutton = document.createElement("input");
791 tryagainbutton.setAttribute("type","button");
793 tryagainbutton.setAttribute("value","Try something else");
795 tryagainbutton.setAttribute("value","Restart");
797 tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
799 disambcell.appendChild(okbutton);
800 disambcell.appendChild(tryagainbutton);
801 disambcell.appendChild(cancelbutton);
805 //matita.disambMode = true;
810 function show_failures() {
812 var choice = document.getElementsByName("choice")[get_checked_index("choice")];
813 var cpno = parseInt(choice.getAttribute("choicepointno"));
814 var choiceno = parseInt(choice.getAttribute("choiceno"));
815 var choicedesc = choice.getAttribute("desc");
817 var title = "<H3>Disambiguation failed</H3>";
818 var subtitle = "<p>Errors at node " + choiceno + " = " + choicedesc + "</p>";
820 disambcell.innerHTML = title + subtitle;
821 var failures = get_failure_opts(cpno,choiceno);
822 for (var i = 0;i < failures.length;i++) {
823 disambcell.appendChild(failures[i]);
824 disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
825 disambcell.appendChild(document.createElement("br"));
828 var okbutton = document.createElement("input");
829 okbutton.setAttribute("type","button");
830 okbutton.setAttribute("value","Show error loc");
831 okbutton.setAttribute("onclick","show_err()");
832 var cancelbutton = document.createElement("input");
833 cancelbutton.setAttribute("type","button");
834 cancelbutton.setAttribute("value","Close");
835 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
836 var backbutton = document.createElement("input");
837 backbutton.setAttribute("type","button");
838 backbutton.setAttribute("value","<< Back");
839 backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
841 disambcell.appendChild(backbutton);
842 disambcell.appendChild(okbutton);
843 disambcell.appendChild(cancelbutton);
847 function show_err() {
848 var radios = document.getElementsByName("failure");
849 for (i = 0; i < radios.length; i++) {
850 if (radios[i].checked) {
851 var start = radios[i].getAttribute("start");
852 var stop = radios[i].getAttribute("stop");
853 var title = radios[i].getAttribute("title");
854 var unlockedtxt = matita.unlockedbackup;
855 var pre = unlockedtxt.substring(0,start).matita_to_html();
856 var mid = unlockedtxt.substring(start,stop).matita_to_html();
857 var post = unlockedtxt.substring(stop).matita_to_html();
858 unlocked.innerHTML = pre +
859 "<span class=\"error\" title=\"Disambiguation failure\">" +
860 mid + "</span>" + post;
866 function gotoBottom()
868 processor = function(xml) {
869 if (is_defined(xml)) {
870 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
871 var parsed = xml.getElementsByTagName("parsed");
872 var localized = xml.getElementsByTagName("localized")[0];
873 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
874 var generic_err = xml.getElementsByTagName("error")[0];
875 for (var i = 0;i < parsed.length; i++) {
876 var len = parsed[i].getAttribute("length");
877 // len0 = unlocked.innerHTML.length;
878 var unescaped = unlocked.innerHTML.html_to_matita();
879 // the browser may decide to split textnodes: use wholeText!
880 var parsedtxt = parsed[i].childNodes[0].wholeText;
881 //parsedtxt = unescaped.substr(0,len);
882 var unparsedtxt = unescaped.substr(len);
883 lockedbackup += parsedtxt;
884 locked.innerHTML = lockedbackup; //.matita_to_html();
885 unlocked.innerHTML = unparsedtxt.matita_to_html();
886 // len1 = unlocked.innerHTML.length;
887 var len2 = parsedtxt.length;
888 statements = listcons(len2,statements);
890 unlocked.scrollIntoView(true);
891 metasenv = xml.getElementsByTagName("meta");
893 populate_goalarray(metasenv);
895 if (is_defined(ambiguity)) {
896 var start = parseInt(ambiguity.getAttribute("start"));
897 var stop = parseInt(ambiguity.getAttribute("stop"));
898 var choices = xml.getElementsByTagName("choice");
900 matita.ambiguityStart = start;
901 matita.ambiguityStop = stop;
902 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
903 matita.interpretations = [];
905 var unlockedtxt = unlocked.innerHTML.html_to_matita();
906 var pre = unlockedtxt.substring(0,start).matita_to_html();
907 var mid = unlockedtxt.substring(start,stop).matita_to_html();
908 var post = unlockedtxt.substring(stop).matita_to_html();
909 unlocked.innerHTML = pre +
910 "<span class=\"error\" title=\"disambiguation error\">" +
911 mid + "</span>" + post;
913 var title = "<H3>Ambiguous input</H3>";
914 disambcell.innerHTML = title;
915 for (i = 0;i < choices.length;i++) {
916 matita.interpretations[i] = new Object();
918 var href = choices[i].getAttribute("href");
919 var title = choices[i].getAttribute("title");
920 var desc = choices[i].childNodes[0].nodeValue;
922 matita.interpretations[i].href = href;
923 matita.interpretations[i].title = title;
924 matita.interpretations[i].desc = desc;
926 var choice = document.createElement("input");
927 choice.setAttribute("type","radio");
928 choice.setAttribute("name","interpr");
929 choice.setAttribute("href",href);
930 choice.setAttribute("title",title);
931 if (i == 0) choice.setAttribute("checked","");
933 disambcell.appendChild(choice);
934 disambcell.appendChild(document.createTextNode(desc));
935 disambcell.appendChild(document.createElement("br"));
938 var okbutton = document.createElement("input");
939 okbutton.setAttribute("type","button");
940 okbutton.setAttribute("value","OK");
941 okbutton.setAttribute("onclick","do_disambiguate()");
942 var cancelbutton = document.createElement("input");
943 cancelbutton.setAttribute("type","button");
944 cancelbutton.setAttribute("value","Cancel");
945 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
947 disambcell.appendChild(okbutton);
948 disambcell.appendChild(cancelbutton);
952 matita.disambMode = true;
955 if (is_defined(localized)) {
956 unlocked.innerHTML = localized.wholeText;
958 if (is_defined(generic_err)) {
959 debug("Unmanaged error:\n" ^ generic_err.wholeText);
962 debug("goto bottom failed");
967 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
973 processor = function(xml) {
974 if (is_defined(xml)) {
975 if (xml.childNodes[0].textContent != "ok") {
976 debug("goto top failed");
979 statements = listnil();
981 lockedlen = locked.innerHTML.length - statementlen;
982 statement = locked.innerHTML.substr(lockedlen, statementlen);
983 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
984 unlocked.innerHTML = statement + unlocked.innerHTML;
986 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
988 locked.innerHTML = lockedbackup;
991 unlocked.scrollIntoView(true);
993 debug("goto top failed");
998 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1001 function gotoPos(offset)
1003 if (!is_defined(offset)) {
1004 offset = getCursorPos();
1006 processor = function(xml) {
1007 if (is_defined(xml)) {
1008 parsed = xml.getElementsByTagName("parsed")[0];
1009 len = parseInt(parsed.getAttribute("length"));
1010 // len0 = unlocked.innerHTML.length;
1011 unescaped = unlocked.innerHTML.html_to_matita();
1012 parsedtxt = parsed.childNodes[0].wholeText;
1013 //parsedtxt = unescaped.substr(0,len);
1014 unparsedtxt = unescaped.substr(len);
1015 lockedbackup += parsedtxt;
1016 locked.innerHTML = lockedbackup; //.matita_to_html();
1017 unlocked.innerHTML = unparsedtxt.matita_to_html();
1018 // len1 = unlocked.innerHTML.length;
1019 len2 = parsedtxt.length;
1020 metasenv = xml.getElementsByTagName("meta");
1021 // populate_goalarray(metasenv);
1022 statements = listcons(len2,statements);
1023 unlocked.scrollIntoView(true);
1024 // la populate non andrebbe fatta a ogni passo
1025 if (offset <= len) {
1027 populate_goalarray(metasenv);
1030 gotoPos(offset - len);
1034 unlocked.scrollIntoView(true);
1035 populate_goalarray(metasenv);
1040 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1045 processor = function(xml) {
1046 if (typeof xml != "undefined") {
1047 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
1048 statementlen = parseInt(listhd(statements));
1049 statements = listtl(statements);
1051 lockedlen = locked.innerHTML.length - statementlen;
1052 statement = locked.innerHTML.substr(lockedlen, statementlen);
1053 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1054 unlocked.innerHTML = statement + unlocked.innerHTML;
1056 lockedlen = lockedbackup.length - statementlen;
1057 statement = lockedbackup.substr(lockedlen, statementlen);
1058 lockedbackup = lockedbackup.substr(0,lockedlen);
1059 locked.innerHTML = lockedbackup;
1060 unlocked.innerHTML = statement + unlocked.innerHTML;
1061 metasenv = xml.getElementsByTagName("meta");
1063 populate_goalarray(metasenv);
1064 unlocked.scrollIntoView(true);
1066 debug("retract failed");
1072 callServer("retract",processor);
1078 processor = function(xml)
1080 if (is_defined(xml)) {
1082 locked.innerHTML = lockedbackup;
1083 unlocked.innerHTML = xml.documentElement.wholeText;
1085 debug("file open failed");
1088 callServer("open",processor,"file=" + escape(filename.value));
1091 function retrieveFile(thefile)
1093 processor = function(xml)
1095 if (is_defined(xml)) {
1096 changeFile(thefile);
1098 locked.innerHTML = lockedbackup;
1099 // code originally used in google chrome (problems with mozilla)
1100 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1101 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1102 debug(xml.childNodes[0].textContent);
1103 if (document.all) { // IE
1104 unlocked.innerHTML = xml.childNodes[0].text;
1106 unlocked.innerHTML = xml.childNodes[0].textContent;
1111 debug("file open failed");
1115 callServer("open",processor,"file=" + escape(thefile));
1118 function showLibrary(title,callback,reloadDialog)
1121 dialogBox.reload = reloadDialog;
1123 if (window.XMLHttpRequest)
1125 req = new XMLHttpRequest();
1127 else if (window.ActiveXObject)
1130 req = new ActiveXObject("Msxml2.XMLHTTP");
1134 req = new ActiveXObject("Microsoft.XMLHTTP");
1138 req.onreadystatechange = function()
1141 rs = req.readyState;
1146 stxt = req.statusText;
1149 debug(req.responseText);
1150 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
1154 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
1155 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
1160 function uploadDialog()
1162 uploadBox.style.display = "block";
1167 var file = document.getElementById("uploadFilename").files[0];
1169 var filecontent = file.getAsText("UTF-8");
1170 locked.innerHTML = lockedbackup;
1171 unlocked.innerHTML = filecontent;
1172 uploadBox.style.display = "none";
1175 // var reader = new FileReader();
1176 // reader.readAsText(file, "UTF-8");
1177 // reader.onloadend = function (evt) {
1178 // lockedbackup = "";
1179 // locked.innerHTML = lockedbackup
1180 // unlocked.innerHTML = evt.target.result;
1181 // uploadBox.style.display = "none";
1183 // reader.onerror = function (evt) {
1184 // debug("file open failed");
1185 // uploadBox.style.display = "none";
1190 function openDialog()
1192 callback = function (fname) { retrieveFile(fname); };
1193 showLibrary("Open file", callback, openDialog);
1196 function saveDialog()
1198 callback = function (fname) {
1201 (locked.innerHTML.html_to_matita()).sescape(),
1202 (unlocked.innerHTML.html_to_matita()).sescape(),
1205 showLibrary("Save file as", callback, saveDialog);
1208 function newDialog()
1210 callback = function (fname) {
1212 saveFile(fname,"","",false,newDialog,true);
1214 showLibrary("Create new file", callback, newDialog);
1218 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
1220 if (!is_defined(reloadFile)) { reloadFile = true };
1221 if (!is_defined(fname)) {
1222 fname = current_fname;
1223 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
1224 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
1226 // when force is true, reloadDialog is not needed
1228 processor = function(xml) {
1229 if (is_defined(xml)) {
1230 if (xml.childNodes[0].textContent != "ok") {
1231 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
1232 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
1238 debug("file saved!");
1239 if (reloadFile) { retrieveFile(fname); }
1242 debug("save file failed");
1246 if (is_defined(fname)) {
1248 callServer("save",processor,"file=" + escape(fname) +
1249 "&locked=" + lockedtxt +
1250 "&unlocked=" + unlockedtxt +
1253 else { debug("no file selected"); }
1256 function createDir() {
1258 dirname = prompt("New directory name:\ncic:/matita/","newdir");
1259 if (dirname != null) {
1260 processor = function(xml) {
1261 if (is_defined(xml)) {
1262 if (xml.childNodes[0].textContent != "ok") {
1263 alert("An error occurred :-(");
1266 alert("An error occurred :-(");
1271 callServer("save",processor,"file=" + escape(dirname) +
1272 "&locked=&unlocked=&force=false&dir=true");
1278 function commitAll()
1280 processor = function(xml) {
1281 if (is_defined(xml)) {
1282 debug(xml.getElementsByTagName("details")[0].textContent);
1283 alert("Commit executed: see details in the log.\n\n" +
1284 "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1286 alert("Commit failed!");
1291 callServer("commit",processor);
1294 function updateAll()
1296 processor = function(xml) {
1297 if (is_defined(xml)) {
1298 alert("Update executed.\n\n" +
1300 xml.getElementsByTagName("details")[0].textContent);
1302 alert("Update failed!");
1307 callServer("update",processor);
1312 function hideSequent() {
1313 matita.proofMode = false;
1317 function showSequent() {
1318 matita.proofMode = true;
1322 function showDialog(title,content,callback) {
1323 dialogTitle.innerHTML = title;
1324 dialogContent.innerHTML = content;
1325 dialogBox.callback = callback;
1327 //Get the screen height and width
1328 var maskHeight = $(document).height();
1329 var maskWidth = $(window).width();
1331 //Set heigth and width to mask to fill up the whole screen
1332 $('#mask').css({'width':maskWidth,'height':maskHeight});
1335 $('#mask').fadeIn(100);
1336 $('#mask').fadeTo(200,0.8);
1338 //Get the window height and width
1339 var winH = $(window).height();
1340 var winW = $(window).width();
1342 //Set the popup window to center
1343 $('#dialogBox').css('top', winH/2-$('#dialogBox').height()/2);
1344 $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1347 $('#dialogBox').fadeIn(200);
1349 dialogBox.style.display = "block";
1352 function abortDialog(dialog) {
1354 dialogBox.style.display = "none";
1357 function removeElement(id) {
1358 var element = document.getElementById(id);
1359 element.parentNode.removeChild(element);
1365 function getCursorPos() {
1367 if (window.getSelection) {
1368 var selObj = window.getSelection();
1369 savedRange = selObj.getRangeAt(0);
1370 savedsc = savedRange.startContainer;
1371 savedso = savedRange.startOffset;
1372 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1373 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1374 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1377 else if (document.selection) {
1378 savedRange = document.selection.createRange();
1379 var bookmark = savedRange.getBookmark();
1380 /* FIXME the following works wrong when the document is longer than 65535 chars */
1381 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1386 function findNode(list, node, acc) {
1387 for (var i = 0; i < list.length; i++) {
1388 if (list[i] == node) {
1389 // debug("success " + i);
1392 if (list[i].hasChildNodes()) {
1394 // debug("recursion on node " + i);
1395 return (findNode(list[i].childNodes,node,acc))
1397 catch (e) { /* debug("recursion failed"); */ }
1399 sandbox = document.getElementById("sandbox");
1400 dup = list[i].cloneNode(true);
1401 sandbox.appendChild(dup);
1402 // debug("fail " + i + ": " + sandbox.innerHTML);
1403 acc += sandbox.innerHTML.html_to_matita().length;
1404 sandbox.removeChild(dup);
1410 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1413 function get_checked_index(name) {
1414 var radios = document.getElementsByName(name);
1415 for (i = 0; i < radios.length; i++) {
1416 if (radios[i].checked) {
1423 function cancel_disambiguate() {
1424 matita.disambMode = false;
1425 $('#whitemask').hide();
1429 function do_disambiguate() {
1430 var i = get_checked_index("interpr");
1432 var pre = matita.unlockedbackup
1433 .substring(0,matita.ambiguityStart).matita_to_html();
1434 var mid = matita.unlockedbackup
1435 .substring(matita.ambiguityStart,matita.ambiguityStop)
1437 var post = matita.unlockedbackup
1438 .substring(matita.ambiguityStop).matita_to_html();
1440 var href = matita.interpretations[i].href;
1441 var title = matita.interpretations[i].title;
1443 if (is_defined(title)) {
1444 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1446 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1449 unlocked.innerHTML = pre + mid + post;
1451 matita.disambMode = false;
1452 $('#whitemask').hide();
1457 function do_showerror() {
1458 var i = get_checked_index("choice");
1460 var pre = matita.unlockedbackup
1461 .substring(0,matita.ambiguityStart).matita_to_html();
1462 var mid = matita.unlockedbackup
1463 .substring(matita.ambiguityStart,matita.ambiguityStop)
1465 var post = matita.unlockedbackup
1466 .substring(matita.ambiguityStop).matita_to_html();
1468 var href = matita.interpretations[i].href;
1469 var title = matita.interpretations[i].title;
1471 if (is_defined(title)) {
1472 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1474 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1477 unlocked.innerHTML = pre + mid + post;
1482 function readCookie(name) {
1483 var nameEQ = name + "=";
1484 var ca = document.cookie.split(';');
1485 for(var i=0;i < ca.length;i++) {
1487 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1488 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1493 function delete_cookie ( cookie_name )
1495 var cookie_date = new Date(); // current date & time
1496 cookie_date.setTime ( cookie_date.getTime() - 1 );
1497 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1500 function delete_session()
1502 delete_cookie("session");
1505 function disable_toparea() {
1506 var offset = $('#toparea').offset();
1507 $('#whitemask').css('top',offset.top);
1508 $('#whitemask').css('left',offset.left);
1509 $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1510 $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1511 $('#whitemask').fadeTo('fast',0.7);