-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug.js
106 lines (92 loc) · 3.49 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2003-2011 MarkLogic Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The use of the Apache License does not indicate that this project is
// affiliated with the Apache Software Foundation.
//
//////////////////////////////////////////////////////////////////////
var debug = new DebugClass(false);
// handy way to get the body element
function BodyClass() {
this.node = document.body;
if (this.node == null) {
this.node = document.createElement("body");
document.appendChild(body);
}
this.getNode = function() { return this.node; };
}
// define a debug class
function DebugClass(flag) {
if (flag == null) {
flag = false;
}
this.enabled = flag;
this.setEnabled = function(flag) { this.enabled = flag; };
this.isEnabled = function() { return this.enabled };
this.setEnabled(flag);
this.print = function(message) {
if (this.enabled != true) { return; }
var id = "__debugNode";
var debugNode = $(id);
if (debugNode == null) {
// add as new child of the html body
var debugNode = document.createElement("div");
var bodyNode = new BodyClass();
bodyNode.getNode().appendChild(debugNode);
}
var newNode = document.createElement("pre");
// we want the pre to wrap: this is hard without CSS3
// but we'll hack it for now.
// don't use a class, so it's as self-contained as possible.
newNode.setAttribute("style", ""
+ "white-space: -moz-pre-wrap;"
// these seem to annoy firefox 1.5
// TODO check UA string?
//+ "white-space: -pre-wrap;"
//+ "white-space: -o-pre-wrap;"
//+ "white-space: pre-wrap;"
//+ "word-wrap: break-word;"
);
newNode.appendChild(document.createTextNode("[" + new Date() + "] "
+ message));
debugNode.appendChild(newNode);
};
this.dump = function(item, level) {
var out = "";
if (!level) {
level = 0;
}
// start-of-line padding
var level_padding = "";
for (var j = 0; j < 1 + level; j++) {
level_padding += " ";
}
if (typeof(item) == 'object') {
// obj is an array, hash, or object
for (var i in item) {
var v = item[i];
if (typeof(v) == 'object') {
out += level_padding + "'" + i + "' ...\n";
out += dump(v, 1 + level);
} else {
out += level_padding + "'" + i + "' => \"" + v + "\"\n";
}
}
return out;
}
// primitives
return "===>" + item + "<===(" + typeof(item) + ")";
};
}
// debug.js