console surrogate added
[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         alert(ind);
54         sind = "<span style=\"background-color:#f0f0f0;\">";
55         for (i = 0 ; i < ind ; i++) {
56             sind += "&nbsp;";
57         }
58         sind += "</span>";
59         for (i in s) {
60             if (typeof(s[i]) == 'string' || typeof(s[i]) == "function") {
61                 var ret = "";
62                 var arr = this.escapeHTML(s[i]).split("\n");
63                 for (el in arr) {
64                     ret += sind + arr[el] + "\n";
65                 }
66                 // this.div.innerHTML += "xx["+this.escapeHTML(i) + "] : [" + ret + "]<hr style=\"height: 1px; width: 100px;\"><br>\n";
67                 this.div.innerHTML += this.escapeHTML(i)+"<br>\n";
68                 this.div.innerHTML += ret + "<hr style=\"height: 1px; width: 100px;\"><br>\n";
69             }
70             else {
71                 this.dump_obj(s[i], ind+4);
72             }
73         }       
74         // this.div.innerHTML += "post-loop<br>";
75     },
76
77     logger: function(s) {
78         if (!this.enable) {
79             return;
80         }
81         this.div.innerHTML += s + "<br>";
82         this.win.document.body.scrollTop = 10000000;
83     },
84
85     close: function() {
86         if (this.enable) {
87             this.win.close();
88         }
89     }
90 }
91
92 /*
93  *  create and destroy 
94  */
95 var is_xynt_console = false;
96 var console_enable = true;
97
98 if(typeof(console) == "undefined") {
99     var console;
100     
101     console = new xynt_console(console_enable);
102
103     is_xynt_console = true;
104 }
105 else {
106     // console.logger = console.log;
107     // console.log = function () { return 0; }
108 }
109
110 function deconsole() {
111     if (is_xynt_console) {
112         console.close();
113     }
114 }
115
116 function log_walk(curtag)
117 {
118     var ind = 0;
119     var ancestor = curtag;
120     do {
121         console.log(spcs("_", "+", ind)+" ["+curtag.tagName+"]  nodeType: "+curtag.nodeType+" inner: ["+curtag.innerHTML+"]");
122         if (curtag.firstChild != null && curtag.tagName != "TD") {
123             ind += 2;
124             curtag = curtag.firstChild;
125         }
126         else if (curtag.nextSibling != null) {
127             curtag = curtag.nextSibling;
128         }
129         else if (curtag.parentNode.nextSibling != null) {
130             ind -= 2;
131             curtag = curtag.parentNode.nextSibling;
132         }
133         else
134             curtag = null;
135     } while (curtag != null && curtag != ancestor);
136 }
137
138