]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaweb.js
more tests on keyboard events.
[helm.git] / matitaB / matita / matitaweb.js
1 var locked;
2 var unlocked;
3 var workarea;
4 var scriptcell;
5 var goalcell;
6 var goals;
7 var goalview;
8 var filename;
9 var logarea;
10 var advanceButton;
11 var retractButton;
12 var cursorButton;
13 var bottomButton;
14 var dialogBox;
15 var dialogTitle;
16 var dialogContent;
17 var metasenv = "";
18
19 function initialize()
20 {
21   if (readCookie("session") == null) {
22     window.location = "/login.html"
23   } else {
24     locked = document.getElementById("locked");
25     unlocked = document.getElementById("unlocked");
26     workarea = document.getElementById("workarea");
27     scriptcell = document.getElementById("scriptcell");
28     goalcell = document.getElementById("goalcell");
29     goals = document.getElementById("goals");
30     goalview = document.getElementById("goalview");
31     filename = document.getElementById("filename");
32     logarea = document.getElementById("logarea");
33     advanceButton = document.getElementById("advance");
34     retractButton = document.getElementById("retract");
35     cursorButton = document.getElementById("cursor");
36     bottomButton = document.getElementById("bottom");
37     dialogBox = document.getElementById("dialogBox");
38     dialogTitle = document.getElementById("dialogTitle");
39     dialogContent = document.getElementById("dialogContent");
40   
41     // hide sequent view at start
42     hideSequent();
43
44     // initialize keyboard events in the unlocked script
45     init_keyboard(unlocked);
46   }
47 }
48
49 function init_keyboard(target)
50 {
51     if (target.addEventListener)
52     {
53 //       target.addEventListener("keydown",keydown,false);
54        target.addEventListener("keypress",keypress,false);
55 //       target.addEventListener("keyup",keyup,false);
56 //       target.addEventListener("textInput",textinput,false);
57     }
58     else if (target.attachEvent)
59     {
60 //       target.attachEvent("onkeydown", keydown);
61        target.attachEvent("onkeypress", keypress);
62 //       target.attachEvent("onkeyup", keyup);
63 //       target.attachEvent("ontextInput", textinput);
64     }
65     else
66     {
67 //       target.onkeydown= keydown;
68        target.onkeypress= keypress;
69 //       target.onkeyup= keyup;
70 //       target.ontextinput= textinput;   // probably doesn't work
71     }
72  
73 }
74
75 function keyval(n)
76 {
77     if (n == null) return 'undefined';
78     var s= '' + n;
79     if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
80     while (s.length < 9) s+= ' ';
81     return s;
82 }
83  
84 function string_of_key(n)
85 {
86     if (n == null) return 'undefined';
87     return String.fromCharCode(n);
88 }
89
90 function pressmesg(w,e)
91 {
92    debug(w + '  keyCode=' + keyval(e.keyCode) +
93                  ' which=' + keyval(e.which) +
94                  ' charCode=' + keyval(e.charCode) +
95                  '\n          shiftKey='+e.shiftKey
96               + ' ctrlKey='+e.ctrlKey
97               + ' altKey='+e.altKey
98               + ' metaKey='+e.metaKey);
99 }
100  
101 function suppressdefault(e,flag)
102 {
103    if (flag)
104    {
105        if (e.preventDefault) e.preventDefault();
106        if (e.stopPropagation) e.stopPropagation();
107    }
108    return !flag;
109 }
110
111 function restoreSelection() {
112     unlocked.focus();
113     if (savedRange != null) {
114         if (window.getSelection)//non IE and there is already a selection
115         {
116             var s = window.getSelection();
117             if (s.rangeCount > 0) 
118                 s.removeAllRanges();
119             s.addRange(savedRange);
120         }
121         else 
122             if (document.createRange)//non IE and no selection
123             {
124                 window.getSelection().addRange(savedRange);
125             }
126             else 
127                 if (document.selection)//IE
128                 {
129                     savedRange.select();
130                 }
131     }
132 }
133  
134 function keypress(e)
135 {
136    if (!e) e= event;
137    pressmesg('keypress',e);
138    var s = string_of_key(e.charCode);
139    if (s == " ") {
140         j = getCursorPos();
141         i = unlocked.innerHTML.lastIndexOf('\\',j);
142         if (i >= 0) {
143           match = unlocked.innerHTML.substring(i,j);
144           pre = unlocked.innerHTML.substring(0,i);
145           post = unlocked.innerHTML.substring(j);
146           if (match == '\\to') {
147              unlocked.innerHTML = pre + "-> " + post;
148              restoreSelection(); 
149              return suppressdefault(e,true);
150           }
151           restoreSelection(); 
152           else return suppressdefault(e,false);
153         }
154         else return suppressdefault(e,false);
155    } else {
156         return suppressdefault(e,false);
157    }
158 }
159  
160 function debug(txt)
161 {
162         // internet explorer (v.9) doesn't work with innerHTML
163         // but google chrome's innerText is, in a sense, "write only"
164         // what should we do?
165         logarea.innerText = txt + "\n" + logarea.innerText;
166 }
167
168 function listhd(l)
169 {
170         ar = l.split("#");
171         debug("hd of '" + l + "' = '" + ar[0] + "'");
172         return (ar[0]);
173 }
174
175 function listtl(l)
176 {
177         i = l.indexOf("#");
178         tl = l.substr(i+1);
179         debug("tl of '" + l + "' = '" + tl + "'");
180         return (tl);
181 }
182
183 function listcons(x,l)
184 {
185         debug("cons '" + x + "' on '" + l + "'");
186         return (x + "#" + l);
187 }
188
189 function listnil()
190 {
191         return ("");
192 }
193
194 function is_nil(l)
195 {
196         return (l == "");
197 }
198
199 function fold_left (f,acc,l)
200 {
201         if (is_nil(l))
202            { debug("'" + l + "' is fold end");
203            return (acc); }
204         else
205            { debug("'" + l + "' is fold cons");
206              return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
207 }
208
209 function listiter (f,l)
210 {
211         if (is_nil(l))
212         { debug("'" + l + "' is nil");
213            return;
214         }
215         else
216         {
217            debug("'" + l + "' is not nil");
218            f(listhd(l));
219            listiter(f,listtl(l));
220         }
221 }
222
223 function listmap (f,l)
224 {
225         debug("listmap on " + l);
226         if (is_nil(l)) 
227            { debug("returning listnil");
228              return(listnil());
229            }
230         else 
231            { debug("cons f(hd) map(f,tl)");
232              return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
233            }
234 }
235
236 var statements = listnil();
237
238 var goalarray;
239 var metalist = listnil();
240
241 function pairmap (f,p)
242 {
243   debug("pairmap of '" + p + "'");
244   ar = p.split("|");
245   return (f(ar[0],ar[1])); 
246 }
247
248 function tripletmap (f,p)
249 {
250   debug("tripletmap of '" + p + "'");
251   ar = p.split("|");
252   return (f(ar[0],ar[1],ar[2])); 
253 }
254
255 function fst (p)
256 {
257   debug("fst");
258   return (pairmap (function (a,b) { return (a); }, p));
259 }
260
261 function p13 (p)
262 {
263   debug("p13");
264   return (tripletmap (function (a,b,c) { return (a); }, p));
265 }
266
267 function p23 (p)
268 {
269   debug("p23");
270   return (tripletmap (function (a,b,c) { return (b); }, p));
271 }
272
273 function p33 (p)
274 {
275   debug("f33");
276   return (tripletmap (function (a,b,c) { return (c); }, p));
277 }
278
279 function populate_goalarray(menv)
280 {
281   debug("metasenv.length = " + menv.length);
282   if (menv.length == 0) {
283       try {
284           hideSequent();
285       } catch (err) { };
286   } else {
287       showSequent();
288       goalarray = new Array();
289       metalist = listnil();
290       var tmp_goallist = "";
291       for (i = 0; i < menv.length; i++) {
292         metano = menv[i].getAttribute("number");
293         metaname = menv[i].childNodes[0].childNodes[0].data;
294         goal = menv[i].childNodes[1].childNodes[0].data;
295         debug ("found meta n. " + metano);
296         debug ("found goal\nBEGIN" + goal + "\nEND");
297         goalarray[metano] = goal;
298         tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
299         metalist = listcons(metano,metalist);
300         debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]); 
301       }
302       goals.innerHTML = tmp_goallist;
303       debug("new metalist is '" + metalist + "'");
304       if (is_nil(metalist)) {
305         switch_goal();
306       }
307       else {
308         switch_goal(listhd(metalist));
309       }
310   }
311 }
312
313 function switch_goal(meta)
314 {
315   if (typeof meta == "undefined") {
316     goalview.innerHTML = "";
317   }
318   else {
319     debug("switch_goal " + meta + "\n" + goalarray[meta]);
320     goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
321   }
322 }
323
324 String.prototype.sescape = function() {
325         var patt1 = /%/gi;
326         var patt2 = /=/gi;
327         var patt3 = /&/gi;
328         var patt4 = /\+/gi;
329         var result = this;
330         result = result.replace(patt1,"%25");
331         result = result.replace(patt2,"%3D");
332         result = result.replace(patt3,"%26");
333         result = result.replace(patt4,"%2B");
334         return (result);
335 }
336
337 String.prototype.unescapeHTML = function()
338 {
339         var patt1 = /<br(\/|)>/gi;
340         var patt2 = /&lt;/gi;
341         var patt3 = /&gt;/gi;
342         var result = this;
343         result = result.replace(patt1,"\n");
344         result = result.replace(patt2,"<");
345         result = result.replace(patt3,">");
346         return (unescape(result));
347 }
348
349 function pause()
350 {
351         advanceButton.disabled = true;
352         retractButton.disabled = true;
353         cursorButton.disabled = true;
354         bottomButton.disabled = true;
355 }
356
357 function resume()
358 {
359         advanceButton.disabled = false;
360         retractButton.disabled = false;
361         cursorButton.disabled = false;
362         bottomButton.disabled = false;
363 }
364
365 function is_defined(x)
366 {
367         return (typeof x != "undefined");
368 }
369
370 /* servicename: name of the service being called
371  * reqbody: text of the request
372  * processResponse: processes the server response
373  *     (takes the response text in input, undefined in case of error)
374  */
375 function callServer(servicename,processResponse,reqbody)
376 {
377         var req = null; 
378         // pause();
379         if (window.XMLHttpRequest)
380         {
381                 req = new XMLHttpRequest();
382         } 
383         else if (window.ActiveXObject) 
384         {
385                 try {
386                                 req = new ActiveXObject("Msxml2.XMLHTTP");
387                 } catch (e)
388                 {
389                         try {
390                                 req = new ActiveXObject("Microsoft.XMLHTTP");
391                                 } catch (e) {}
392                 }
393         }
394         req.onreadystatechange = function()
395         { 
396
397                 rs = req.readyState;
398
399                 if(rs == 4)
400                 {
401                         stat = req.status;
402                         stxt = req.statusText;
403                         if(stat == 200)
404                         {
405                           debug(req.responseText);
406                           if (window.DOMParser) {
407                             parser=new DOMParser();
408                             xmlDoc=parser.parseFromString(req.responseText,"text/xml");
409                           }
410                           else // Internet Explorer
411                           {
412                             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
413                             xmlDoc.async="false";
414                             xmlDoc.loadXML(req.responseText);
415                           }     
416                           processResponse(xmlDoc);
417                         } else {
418                           processResponse();
419                         }
420                 } 
421         };
422         req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
423         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
424         if (reqbody) {
425                 req.send(reqbody); 
426         } else {
427                 req.send();
428         }
429   
430 }
431
432 function advanceForm1()
433 {
434         processor = function(xml) {
435                 if (is_defined(xml)) {
436                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
437                         len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
438                         len0 = unlocked.innerHTML.length;
439                         unescaped = unlocked.innerHTML.unescapeHTML();
440                         parsedtxt = unescaped.substr(0,len); 
441                         unparsedtxt = unescaped.substr(len);
442                         locked.innerHTML = locked.innerHTML + parsedtxt;
443                         unlocked.innerHTML = unparsedtxt;
444                         len1 = unlocked.innerHTML.length;
445                         len2 = len0 - len1;
446                         metasenv = xml.getElementsByTagName("meta");
447                         populate_goalarray(metasenv);
448                         statements = listcons(len2,statements);
449                         unlocked.scrollIntoView(true);
450                 } else {
451                         debug("advance failed");
452                 }
453                 resume();
454         };
455         pause();
456         callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
457   
458 }
459
460 function gotoBottom()
461 {
462         processor = function(xml) {
463                 if (is_defined(xml)) {
464                         // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
465                         len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
466                         len0 = unlocked.innerHTML.length;
467                         unescaped = unlocked.innerHTML.unescapeHTML();
468                         parsedtxt = unescaped.substr(0,len); 
469                         unparsedtxt = unescaped.substr(len);
470                         locked.innerHTML = locked.innerHTML + parsedtxt;
471                         unlocked.innerHTML = unparsedtxt;
472                         len1 = unlocked.innerHTML.length;
473                         len2 = len0 - len1;
474                         metasenv = xml.getElementsByTagName("meta");
475                         populate_goalarray(metasenv);
476                         statements = listcons(len2,statements);
477                         unlocked.scrollIntoView(true);
478                 } else {
479                         debug("goto bottom failed");
480                 } 
481                 resume();
482         };
483         pause();
484         callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
485   
486 }
487
488
489 function gotoPos(offset)
490 {
491         if (!is_defined(offset)) {
492                 offset = getCursorPos();
493         }
494         processor = function(xml) {
495                 if (is_defined(xml)) {
496                         // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
497                         len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
498                         len0 = unlocked.innerHTML.length;
499                         unescaped = unlocked.innerHTML.unescapeHTML();
500                         parsedtxt = unescaped.substr(0,len); 
501                         unparsedtxt = unescaped.substr(len);
502                         locked.innerHTML = locked.innerHTML + parsedtxt;
503                         unlocked.innerHTML = unparsedtxt;
504                         len1 = unlocked.innerHTML.length;
505                         len2 = len0 - len1;
506                         metasenv = xml.getElementsByTagName("meta");
507                         // populate_goalarray(metasenv);
508                         statements = listcons(len2,statements);
509                         unlocked.scrollIntoView(true);
510                         // la populate non andrebbe fatta a ogni passo
511                         if (offset <= len) {
512                                 populate_goalarray(metasenv);
513                                 resume();
514                         } else {
515                                 gotoPos(offset - len);
516                         }
517                 } else {
518                         unlocked.scrollIntoView(true);
519                         populate_goalarray(metasenv);
520                         resume();
521                 }
522         }
523         pause();
524         callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
525 }
526
527 function retract()
528 {
529         processor = function(xml) {
530                 if (typeof xml != "undefined") {
531                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
532                         statementlen = parseInt(listhd(statements));
533                         statements = listtl(statements);
534                         lockedlen = locked.innerHTML.length - statementlen;
535                         statement = locked.innerHTML.substr(lockedlen, statementlen);
536                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
537                         unlocked.innerHTML = statement + unlocked.innerHTML;
538                         metasenv = xml.getElementsByTagName("meta");
539                         populate_goalarray(metasenv);
540                         unlocked.scrollIntoView(true);
541                 } else {
542                         debug("retract failed");
543                 }
544                 resume();
545         };
546         debug("retract 1");
547         pause();
548         callServer("retract",processor);
549         debug("retract 2");
550 }
551
552 function openFile()
553
554         processor = function(xml)
555         {
556                 if (is_defined(xml)) {  
557                         locked.innerHTML = "";
558                         unlocked.innerHTML = xml.documentElement.textContent;
559                 } else {
560                         debug("file open failed");
561                 }
562         };
563         callServer("open",processor,"file=" + escape(filename.value)); 
564 }
565
566 function retrieveFile(thefile)
567
568         processor = function(xml)
569         {
570                 if (is_defined(xml)) {  
571                         locked.innerHTML = "";
572                         unlocked.innerHTML = xml.documentElement.textContent;
573                 } else {
574                         debug("file open failed");
575                 }
576         };
577         dialogBox.style.display = "none";
578         callServer("open",processor,"file=" + escape(thefile)); 
579 }
580
581 function showLibrary()
582
583         var req = null; 
584         // pause();
585         if (window.XMLHttpRequest)
586         {
587                 req = new XMLHttpRequest();
588         } 
589         else if (window.ActiveXObject) 
590         {
591                 try {
592                                 req = new ActiveXObject("Msxml2.XMLHTTP");
593                 } catch (e)
594                 {
595                         try {
596                                 req = new ActiveXObject("Microsoft.XMLHTTP");
597                                 } catch (e) {}
598                 }
599         }
600         req.onreadystatechange = function()
601         { 
602
603                 rs = req.readyState;
604
605                 if(rs == 4)
606                 {
607                         stat = req.status;
608                         stxt = req.statusText;
609                         if(stat == 200)
610                         {
611                           debug(req.responseText);
612                           showDialog("<H2>Library</H2>",req.responseText);
613                         } 
614                 } 
615         };
616         req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
617         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
618         req.send();
619   
620 }
621
622 var goalcell;
623
624 function hideSequent() {
625   goalcell.parentNode.removeChild(goalcell);
626   scriptcell.setAttribute("colspan","2");
627 }
628
629 function showSequent() {
630   scriptcell.setAttribute("colspan","1");
631   workarea.appendChild(goalcell);
632 }
633
634 function showDialog(title,content) {
635   dialogTitle.innerHTML = title;
636   dialogContent.innerHTML = content;
637   dialogBox.style.display = "block";
638 }
639
640 function removeElement(id) {
641   var element = document.getElementById(id);
642   element.parentNode.removeChild(element);
643
644
645 var savedRange;
646
647 function getCursorPos() {
648   var cursorPos;
649   if (window.getSelection) {
650     var selObj = window.getSelection();
651     savedRange = selObj.getRangeAt(0);
652     //cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
653     cursorPos =  findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
654     /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
655     return(cursorPos);
656   }
657   else if (document.selection) {
658     savedRange = document.selection.createRange();
659     var bookmark = savedRange.getBookmark();
660     /* FIXME the following works wrong when the document is longer than 65535 chars */
661     cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
662     return(cursorPos);
663   }
664 }
665
666 function findNode(list, node, acc) {
667   for (var i = 0; i < list.length; i++) {
668     if (list[i] == node) {
669    //   debug("success " + i);
670       return acc;
671     }
672     if (list[i].hasChildNodes()) {
673        try {
674    //      debug("recursion on node " + i);
675          return (findNode(list[i].childNodes,node,acc))
676        }
677        catch (e) { /* debug("recursion failed"); */ }
678     }
679     sandbox = document.getElementById("sandbox");
680     dup = list[i].cloneNode(true);
681     sandbox.appendChild(dup);
682 //    debug("fail " + i + ": " + sandbox.innerHTML);
683     acc += sandbox.innerHTML.length;
684     sandbox.removeChild(dup);
685   }
686   throw "not found";
687 }
688
689 function test () {
690   debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
691 }
692
693 function readCookie(name) {
694         var nameEQ = name + "=";
695         var ca = document.cookie.split(';');
696         for(var i=0;i < ca.length;i++) {
697                 var c = ca[i];
698                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
699                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
700         }
701         return null;
702 }
703
704 function delete_cookie ( cookie_name )
705 {
706   var cookie_date = new Date();  // current date & time
707   cookie_date.setTime ( cookie_date.getTime() - 1 );
708   document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
709 }
710
711 function delete_session()
712 {
713         delete_cookie("session");
714 }