1
|
1 var Debug = null;
|
|
2 var _now = {};
|
|
3
|
|
4 function element(name, content) {
|
|
5 var E = document.createElement(name);
|
|
6 if (content) {
|
|
7 if (content.nodeType) // it's a node
|
|
8 E.appendChild(content)
|
|
9 else // it's text
|
|
10 E.appendChild(document.createTextNode(content))
|
|
11 };
|
|
12 return E;
|
|
13 }
|
|
14
|
|
15 function appendElement(node, name, content) {
|
|
16 if (node == null || ! node.appendChild)
|
|
17 return node;
|
|
18 return node.appendChild(element(name,content));
|
|
19 }
|
|
20
|
|
21 function getDebug() {
|
|
22 if (Debug == null) {
|
|
23 Debug = document.getElementById('debug');
|
|
24 if (Debug == null) {
|
|
25 Debug = appendElement(document.body, 'div');
|
|
26 Debug.setAttribute('class', 'debug');
|
|
27 Debug.setAttribute('id', 'debug');
|
|
28 Debug.innerhtml = '<h3>Debug</h3>';
|
|
29 };
|
|
30 };
|
|
31 return Debug;
|
|
32 }
|
|
33
|
|
34 function debug() {
|
|
35 var D = getDebug();
|
|
36 // return alertObject(D);
|
|
37 var msg = "";
|
|
38 for (var i = 0; i<arguments.length; i++) msg += arguments[i] + " ";
|
|
39 return appendElement(D, "p", msg);
|
|
40 }
|
|
41
|
|
42 function debugObject(obj, exclude) {
|
|
43 if (exclude == null)
|
|
44 exclude = '';
|
|
45 var noConst = exclude.indexOf('const') > -1;
|
|
46 var A = new Array();
|
|
47 for (var prop in obj) A.push(prop);
|
|
48 A.sort();
|
|
49 var D = getDebug();
|
|
50 var T = appendElement(D, "table");
|
|
51 for (var i = 0; i < A.length; i++) {
|
|
52 var key = A[i];
|
|
53 var value = obj[key];
|
|
54 var type = typeof(value);
|
|
55 // exclude specified types
|
|
56 if (exclude.indexOf(type) > -1)
|
|
57 continue;
|
|
58 // exclude uppercase-only properties (= constants)
|
|
59 if (noConst && key.search(/^[A-Z0-9_]+$/) > -1)
|
|
60 continue;
|
|
61 var TR = appendElement(T, "tr");
|
|
62 appendElement(TR, "td", element("b", key));
|
|
63 appendElement(TR, "td", type);
|
|
64 appendElement(TR, "td", value + "");
|
|
65 if (type == "function")
|
|
66 appendElement(TR, "td", value.toSource());
|
|
67 };
|
|
68 }
|
|
69
|
|
70 function strObject(obj) {
|
|
71 var res = "";
|
|
72 var A = new Array();
|
|
73 try {
|
|
74 for (var i in obj) A[i] = typeof(obj[i]);
|
|
75 }
|
|
76 catch(e) { typ = "unknown" };
|
|
77 var count = 0;
|
|
78 for (var item in A) {
|
|
79 count++;
|
|
80 typ = A[item];
|
|
81 res += item + " (" + typ + "): ";
|
|
82 if (typ != "function") res += obj[item];
|
|
83 res += "\t";
|
|
84 if (count % 4 == 0)
|
|
85 res += "\n";
|
|
86 }
|
|
87 return res;
|
|
88 }
|
|
89
|
|
90 function alertObject(obj) {
|
|
91 return alert(strObject(obj));
|
|
92 }
|
|
93
|
|
94 function alertHTML(obj) {
|
|
95 if (obj)
|
|
96 return alert(obj.tagName + ".innerHTML:\n" + obj.innerHTML);
|
|
97 }
|
|
98
|
|
99 function serialize(xmlobject) {
|
|
100 return (new XMLSerializer()).serializeToString(xmlobject);
|
|
101 }
|
|
102
|
|
103 function startTime(s) {
|
|
104 _now[s] = new Date();
|
|
105 }
|
|
106
|
|
107 function elapsedTime(s) {
|
|
108 var diff = new Date(new Date - _now[s]);
|
|
109 debug(s + ": " + diff.getSeconds() + "." + diff.getMilliseconds() + " sec.");
|
|
110 }
|
|
111
|