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;
182 logarea.innerHTML = txt; // + "\n" + logarea.innerText;
188 debug("hd of '" + l + "' = '" + ar[0] + "'");
196 debug("tl of '" + l + "' = '" + tl + "'");
200 function listcons(x,l)
202 debug("cons '" + x + "' on '" + l + "'");
203 return (x + "#" + l);
216 function fold_left (f,acc,l)
219 { debug("'" + l + "' is fold end");
222 { debug("'" + l + "' is fold cons");
223 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
226 function listiter (f,l)
229 { debug("'" + l + "' is nil");
234 debug("'" + l + "' is not nil");
236 listiter(f,listtl(l));
240 function listmap (f,l)
242 debug("listmap on " + l);
244 { debug("returning listnil");
248 { debug("cons f(hd) map(f,tl)");
249 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
253 var statements = listnil();
256 var metalist = listnil();
258 function pairmap (f,p)
260 debug("pairmap of '" + p + "'");
262 return (f(ar[0],ar[1]));
265 function tripletmap (f,p)
267 debug("tripletmap of '" + p + "'");
269 return (f(ar[0],ar[1],ar[2]));
275 return (pairmap (function (a,b) { return (a); }, p));
281 return (tripletmap (function (a,b,c) { return (a); }, p));
287 return (tripletmap (function (a,b,c) { return (b); }, p));
293 return (tripletmap (function (a,b,c) { return (c); }, p));
296 function populate_goalarray(menv)
298 debug("metasenv.length = " + menv.length);
299 if (menv.length == 0) {
305 goalarray = new Array();
306 metalist = listnil();
307 var tmp_goallist = "";
308 for (i = 0; i < menv.length; i++) {
309 metano = menv[i].getAttribute("number");
310 metaname = menv[i].childNodes[0].childNodes[0].data;
311 goal = menv[i].childNodes[1].childNodes[0].data;
312 debug ("found meta n. " + metano);
313 debug ("found goal\nBEGIN" + goal + "\nEND");
314 goalarray[metano] = goal;
315 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
316 metalist = listcons(metano,metalist);
317 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
319 goals.innerHTML = tmp_goallist;
320 debug("new metalist is '" + metalist + "'");
321 if (is_nil(metalist)) {
325 switch_goal(listhd(metalist));
330 function switch_goal(meta)
332 if (typeof meta == "undefined") {
333 goalview.innerHTML = "";
336 debug("switch_goal " + meta + "\n" + goalarray[meta]);
337 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
341 // the following is used to avoid escaping unicode, which results in
342 // the server being unable to unescape the string
343 String.prototype.sescape = function() {
349 result = result.replace(patt1,"%25");
350 result = result.replace(patt2,"%3D");
351 result = result.replace(patt3,"%26");
352 result = result.replace(patt4,"%2B");
356 String.prototype.html_to_matita = function()
358 var patt1 = /<br(\/|)>/gi;
361 var patt4 = /</gi;
362 var patt5 = />/gi;
364 result = result.replace(patt1,"\n");
365 result = result.replace(patt2,"\005");
366 result = result.replace(patt3,"\006");
367 result = result.replace(patt4,"<");
368 result = result.replace(patt5,">");
369 return (unescape(result));
372 String.prototype.matita_to_html = function()
376 var patt3 = /\005/gi;
377 var patt4 = /\006/gi;
379 result = result.replace(patt1,"<");
380 result = result.replace(patt2,">");
381 result = result.replace(patt3,"<");
382 result = result.replace(patt4,">");
383 return (unescape(result));
388 advanceButton.disabled = true;
389 retractButton.disabled = true;
390 cursorButton.disabled = true;
391 bottomButton.disabled = true;
396 advanceButton.disabled = false;
397 retractButton.disabled = false;
398 cursorButton.disabled = false;
399 bottomButton.disabled = false;
402 function is_defined(x)
404 return (typeof x != "undefined");
407 /* servicename: name of the service being called
408 * reqbody: text of the request
409 * processResponse: processes the server response
410 * (takes the response text in input, undefined in case of error)
412 function callServer(servicename,processResponse,reqbody)
416 if (window.XMLHttpRequest)
418 req = new XMLHttpRequest();
420 else if (window.ActiveXObject)
423 req = new ActiveXObject("Msxml2.XMLHTTP");
427 req = new ActiveXObject("Microsoft.XMLHTTP");
431 req.onreadystatechange = function()
439 stxt = req.statusText;
442 debug(req.responseText);
443 if (window.DOMParser) {
444 parser=new DOMParser();
445 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
447 else // Internet Explorer
449 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
450 xmlDoc.async="false";
451 xmlDoc.loadXML(req.responseText);
453 processResponse(xmlDoc);
459 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
460 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
469 function advanceForm1()
471 processor = function(xml) {
472 if (is_defined(xml)) {
473 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
474 parsed = xml.getElementsByTagName("parsed")[0];
475 len = parseInt(parsed.getAttribute("length"));
476 // len0 = unlocked.innerHTML.length;
477 unescaped = unlocked.innerHTML.html_to_matita();
478 parsedtxt = parsed.childNodes[0].nodeValue;
479 //parsedtxt = unescaped.substr(0,len);
480 unparsedtxt = unescaped.substr(len);
481 lockedbackup += parsedtxt;
482 locked.innerHTML = lockedbackup;
483 unlocked.innerHTML = unparsedtxt.matita_to_html();
484 // len1 = unlocked.innerHTML.length;
485 // len2 = len0 - len1;
486 len2 = parsedtxt.length;
487 metasenv = xml.getElementsByTagName("meta");
488 populate_goalarray(metasenv);
489 statements = listcons(len2,statements);
490 unlocked.scrollIntoView(true);
492 debug("advance failed");
497 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
501 function gotoBottom()
503 processor = function(xml) {
504 if (is_defined(xml)) {
505 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
506 parsed = xml.getElementsByTagName("parsed")[0];
507 len = parseInt(parsed.getAttribute("length"));
509 // len0 = unlocked.innerHTML.length;
510 unescaped = unlocked.innerHTML.html_to_matita();
511 // not working in mozilla
512 // parsedtxt = parsed.childNodes[0].nodeValue;
513 parsedtxt = parsed.childNodes[0].wholeText;
514 //parsedtxt = unescaped.substr(0,len);
515 unparsedtxt = unescaped.substr(len);
516 lockedbackup += parsedtxt;
517 locked.innerHTML = lockedbackup; //.matita_to_html();
518 unlocked.innerHTML = unparsedtxt.matita_to_html();
519 // len1 = unlocked.innerHTML.length;
520 len2 = parsedtxt.length;
521 metasenv = xml.getElementsByTagName("meta");
522 populate_goalarray(metasenv);
524 statements = listcons(len2,statements);
525 unlocked.scrollIntoView(true);
528 debug("goto bottom failed");
533 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
540 processor = function(xml) {
541 if (is_defined(xml)) {
542 if (xml.childNodes[0].textContent != "ok") {
543 debug("goto top failed");
546 unlocked.innerHTML = locked.innerHTML + unlocked.innerHTML
547 locked.innerHTML = ""
549 unlocked.scrollIntoView(true);
551 debug("goto top failed");
556 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
559 function gotoPos(offset)
561 if (!is_defined(offset)) {
562 offset = getCursorPos();
564 processor = function(xml) {
565 if (is_defined(xml)) {
566 parsed = xml.getElementsByTagName("parsed")[0];
567 len = parseInt(parsed.getAttribute("length"));
568 // len0 = unlocked.innerHTML.length;
569 unescaped = unlocked.innerHTML.html_to_matita();
570 parsedtxt = parsed.childNodes[0].nodeValue;
571 //parsedtxt = unescaped.substr(0,len);
572 unparsedtxt = unescaped.substr(len);
573 lockedbackup += parsedtxt;
574 locked.innerHTML = lockedbackup; //.matita_to_html();
575 unlocked.innerHTML = unparsedtxt.matita_to_html();
576 // len1 = unlocked.innerHTML.length;
577 len2 = parsedtxt.length;
578 metasenv = xml.getElementsByTagName("meta");
579 // populate_goalarray(metasenv);
580 statements = listcons(len2,statements);
581 unlocked.scrollIntoView(true);
582 // la populate non andrebbe fatta a ogni passo
584 populate_goalarray(metasenv);
587 gotoPos(offset - len);
590 unlocked.scrollIntoView(true);
591 populate_goalarray(metasenv);
596 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
601 processor = function(xml) {
602 if (typeof xml != "undefined") {
603 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
604 statementlen = parseInt(listhd(statements));
605 statements = listtl(statements);
607 lockedlen = locked.innerHTML.length - statementlen;
608 statement = locked.innerHTML.substr(lockedlen, statementlen);
609 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
610 unlocked.innerHTML = statement + unlocked.innerHTML;
612 lockedlen = lockedbackup.length - statementlen;
613 statement = lockedbackup.substr(lockedlen, statementlen);
614 lockedbackup = lockedbackup.substr(0,lockedlen);
615 locked.innerHTML = lockedbackup;
616 unlocked.innerHTML = statement + unlocked.innerHTML;
617 metasenv = xml.getElementsByTagName("meta");
618 populate_goalarray(metasenv);
619 unlocked.scrollIntoView(true);
621 debug("retract failed");
627 callServer("retract",processor);
633 processor = function(xml)
635 if (is_defined(xml)) {
637 locked.innerHTML = lockedbackup;
638 unlocked.innerHTML = xml.documentElement.wholeText;
640 debug("file open failed");
643 callServer("open",processor,"file=" + escape(filename.value));
646 function retrieveFile(thefile)
648 processor = function(xml)
650 if (is_defined(xml)) {
652 locked.innerHTML = lockedbackup;
653 // code originally used in google chrome (problems with mozilla)
654 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
655 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
656 debug(xml.childNodes[0].textContent);
657 unlocked.innerHTML = xml.childNodes[0].textContent;
660 debug("file open failed");
663 dialogBox.style.display = "none";
664 current_fname = thefile;
665 callServer("open",processor,"file=" + escape(thefile));
668 function showLibrary(title,callback)
672 if (window.XMLHttpRequest)
674 req = new XMLHttpRequest();
676 else if (window.ActiveXObject)
679 req = new ActiveXObject("Msxml2.XMLHTTP");
683 req = new ActiveXObject("Microsoft.XMLHTTP");
687 req.onreadystatechange = function()
695 stxt = req.statusText;
698 debug(req.responseText);
699 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
703 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
704 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
709 function uploadDialog()
711 uploadBox.style.display = "block";
716 var file = document.getElementById("uploadFilename").files[0];
718 var filecontent = file.getAsText("UTF-8");
719 locked.innerHTML = lockedbackup;
720 unlocked.innerHTML = filecontent;
721 uploadBox.style.display = "none";
724 // var reader = new FileReader();
725 // reader.readAsText(file, "UTF-8");
726 // reader.onloadend = function (evt) {
727 // lockedbackup = "";
728 // locked.innerHTML = lockedbackup
729 // unlocked.innerHTML = evt.target.result;
730 // uploadBox.style.display = "none";
732 // reader.onerror = function (evt) {
733 // debug("file open failed");
734 // uploadBox.style.display = "none";
739 function openDialog()
741 callback = function (fname) { retrieveFile(fname); };
742 showLibrary("Open file", callback);
745 function saveDialog()
747 callback = function (fname) {
748 dialogBox.style.display = "none";
750 (locked.innerHTML.html_to_matita()).sescape(),
751 (unlocked.innerHTML.html_to_matita()).sescape(),
754 showLibrary("Save file as", callback);
759 callback = function (fname) {
760 dialogBox.style.display = "none";
761 saveFile(fname,"","",false,newDialog,true);
763 showLibrary("Create new file", callback);
767 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
769 if (!is_defined(reloadFile)) { reloadFile = true };
770 if (!is_defined(fname)) {
771 fname = current_fname;
772 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
773 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
775 // when force is true, reloadDialog is not needed
777 processor = function(xml) {
778 if (is_defined(xml)) {
779 if (xml.childNodes[0].textContent != "ok") {
780 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
781 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
786 current_fname = fname;
787 debug("file saved!");
788 if (reloadFile) { retrieveFile(fname); }
791 debug("save file failed");
795 if (is_defined(fname)) {
797 callServer("save",processor,"file=" + escape(fname) +
798 "&locked=" + lockedtxt +
799 "&unlocked=" + unlockedtxt +
802 else { debug("no file selected"); }
807 processor = function(xml) {
808 if (is_defined(xml)) {
809 debug("commit succeeded(?)");
811 debug("commit failed!");
816 callServer("commit",processor);
821 function hideSequent() {
822 goalcell.style.display = "none";
823 scriptcell.style.width = "100%";
824 scriptcell.style.minWidth = "100%";
825 scriptcell.style.maxWidth = "100%";
828 function showSequent() {
829 scriptcell.style.width = "67%";
830 scriptcell.style.minWidth = "67%";
831 scriptcell.style.maxWidth = "67%";
832 goalcell.style.display = "inline-block";
835 function showDialog(title,content,callback) {
836 dialogTitle.innerHTML = title;
837 dialogContent.innerHTML = content;
838 dialogBox.callback = callback;
839 dialogBox.style.display = "block";
842 function abortDialog() {
843 dialogBox.style.display = "none";
846 function abortUpload() {
847 uploadBox.style.display = "none";
850 function removeElement(id) {
851 var element = document.getElementById(id);
852 element.parentNode.removeChild(element);
858 function getCursorPos() {
860 if (window.getSelection) {
861 var selObj = window.getSelection();
862 savedRange = selObj.getRangeAt(0);
863 savedsc = savedRange.startContainer;
864 savedso = savedRange.startOffset;
865 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
866 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
867 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
870 else if (document.selection) {
871 savedRange = document.selection.createRange();
872 var bookmark = savedRange.getBookmark();
873 /* FIXME the following works wrong when the document is longer than 65535 chars */
874 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
879 function findNode(list, node, acc) {
880 for (var i = 0; i < list.length; i++) {
881 if (list[i] == node) {
882 // debug("success " + i);
885 if (list[i].hasChildNodes()) {
887 // debug("recursion on node " + i);
888 return (findNode(list[i].childNodes,node,acc))
890 catch (e) { /* debug("recursion failed"); */ }
892 sandbox = document.getElementById("sandbox");
893 dup = list[i].cloneNode(true);
894 sandbox.appendChild(dup);
895 // debug("fail " + i + ": " + sandbox.innerHTML);
896 acc += sandbox.innerHTML.length;
897 sandbox.removeChild(dup);
903 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
906 function readCookie(name) {
907 var nameEQ = name + "=";
908 var ca = document.cookie.split(';');
909 for(var i=0;i < ca.length;i++) {
911 while (c.charAt(0)==' ') c = c.substring(1,c.length);
912 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
917 function delete_cookie ( cookie_name )
919 var cookie_date = new Date(); // current date & time
920 cookie_date.setTime ( cookie_date.getTime() - 1 );
921 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
924 function delete_session()
926 delete_cookie("session");