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