moved js code from html to js files
[xynt.git] / xynt-console.js
1 function xynt_console(ena) {
2     var conbody, condiv;
3
4     this.enable = ena;
5     if (ena) {
6         this.win = window.open("","xynt console","navigation=no,scrollbars=yes,height=500,width=800,left=0,top=800");
7         // titl = this.win.document.createElement("title");
8         // titl.innerHTML = "xynt console";
9         conbody = this.win.document.createElement("body");
10         this.div = condiv = this.win.document.createElement("div");
11
12         conbody.id = "console_body";
13         // this.win.document.head.appendChild(titl);
14         this.win.document.body.appendChild(condiv);
15         this.win.document.title = "xynt console";
16     }
17 }
18
19 xynt_console.prototype = {
20     win: null,
21     div: null,
22     enable: false,
23
24     escapeHTML: function(s) {
25         var v = s+"";
26         return v.replace(/&/g,'&').
27                 replace(/ /g,' ').
28                 replace(/"/g,'"').
29             // replace(/'/g,''').
30                 replace(/>/g,'>').
31                 replace(/</g,'&lt;').                        
32                 replace(/\n/g, "<br>\n");
33     },
34
35     log: function(s) {
36         if (!this.enable) {
37             return;
38         }
39         if (typeof(s) == "string" || typeof(s) == "function") {
40             this.div.innerHTML += this.escapeHTML(s);
41         }
42         else {
43             ind = 4;
44             this.dump_obj(s,ind);
45         }
46         this.div.innerHTML += "<hr style=\"height: 1px;\">\n";
47         this.win.document.body.scrollTop = 10000000;
48     },
49
50     dump_obj: function(s, ind) {
51         var sind = "";
52
53         sind = "<span style=\"background-color:#f0f0f0;\">";
54         for (i = 0 ; i < ind ; i++) {
55             sind += "&nbsp;";
56         }
57         sind += "</span>";
58         for (i in s) {
59             if (typeof(s[i]) == 'string' || typeof(s[i]) == "function") {
60                 var ret = "";
61                 var arr = this.escapeHTML(s[i]).split("\n");
62                 for (el in arr) {
63                     ret += sind + arr[el] + "\n";
64                 }
65                 // this.div.innerHTML += "xx["+this.escapeHTML(i) + "] : [" + ret + "]<hr style=\"height: 1px; width: 100px;\"><br>\n";
66                 this.div.innerHTML += this.escapeHTML(i)+"<br>\n";
67                 this.div.innerHTML += ret + "<hr style=\"height: 1px; width: 100px;\"><br>\n";
68             }
69             else {
70                 this.dump_obj(s[i], ind+4);
71             }
72         }       
73         // this.div.innerHTML += "post-loop<br>";
74     },
75
76     logger: function(s) {
77         if (!this.enable) {
78             return;
79         }
80         this.div.innerHTML += s + "<br>";
81         this.win.document.body.scrollTop = 10000000;
82     },
83
84     close: function() {
85         if (this.enable) {
86             this.win.close();
87         }
88     }
89 }
90
91 /*
92  *  create and destroy 
93  */
94 var is_xynt_console = false;
95 var console_enable = true;
96
97 if(typeof(console) == "undefined") {
98     var console;
99     
100     console = new xynt_console(console_enable);
101
102     is_xynt_console = true;
103 }
104 else {
105     // console.logger = console.log;
106     // console.log = function () { return 0; }
107 }
108
109 function deconsole() {
110     if (is_xynt_console) {
111         console.close();
112     }
113 }
114
115 function log_walk(curtag)
116 {
117     var ind = 0;
118     var ancestor = curtag;
119     do {
120         console.log(spcs("_", "+", ind)+" ["+curtag.tagName+"]  nodeType: "+curtag.nodeType+" inner: ["+curtag.innerHTML+"]");
121         if (curtag.firstChild != null && curtag.tagName != "TD") {
122             ind += 2;
123             curtag = curtag.firstChild;
124         }
125         else if (curtag.nextSibling != null) {
126             curtag = curtag.nextSibling;
127         }
128         else if (curtag.parentNode.nextSibling != null) {
129             ind -= 2;
130             curtag = curtag.parentNode.nextSibling;
131         }
132         else
133             curtag = null;
134     } while (curtag != null && curtag != ancestor);
135 }
136
137