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