forked from ssoper/jquery-inspect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.inspect.js
125 lines (109 loc) · 2.68 KB
/
jquery.inspect.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
jQuery.fn.inspect = function(output) {
jQuery.inspect($(this), output);
return this;
}
jQuery.inspect = function(obj, output) {
output = (output ? output : 'alert')
var text = "";
var _build = null;
var _dump = null;
switch (output) {
case 'console':
_build = jQuery.inspect._buildText;
_dump = jQuery.inspect._console;
break;
case 'window':
_build = jQuery.inspect._buildHTML;
_dump = jQuery.inspect._window;
break;
case 'jasmine':
_build = jQuery.inspect._buildText;
_dump = jQuery.inspect._jasmine;
break;
case 'text':
_build = jQuery.inspect._buildText;
_dump = jQuery.inspect._thru;
break;
default:
_build = jQuery.inspect._buildText;
_dump = jQuery.inspect._alert;
}
switch (typeof obj) {
case 'string':
text = "String: " + obj;
break;
case 'number':
text = "Number: " + obj;
break;
case 'boolean':
text = "Boolean: " + obj;
break;
case 'undefined':
alert('Object is undefined');
return true;
default:
text = jQuery.inspect._parseObject(obj, _build);
}
return _dump(text);
}
jQuery.inspect._parseObject = function(obj, _dumpTo) {
var text = ""
for (field in obj) {
try {
text += _dumpTo(field, obj[field]);
}
catch (err) {
// do nothing
}
}
return text;
}
jQuery.inspect._buildText = function(key, value) {
return key + ":" + value + "\n";
}
jQuery.inspect._buildHTML = function(key, value) {
return "<tr><td>" + key + "</td><td>" + value + "</td></tr>\n";
}
jQuery.inspect._console = function(text) {
console.log(text);
}
jQuery.inspect._alert = function(text) {
alert(text);
}
jQuery.inspect._window = function(text) {
text = "<html><head>" + jQuery.inspect._windowSettings.styles + "</head><body><table>" + text + "</table></body></html>";
dump_window = window.open('', '', jQuery.inspect._windowSettings.config);
dump_window.document.write(text);
dump_window.document.close();
dump_window.focus();
}
jQuery.inspect._windowSettings = jQuery.extend({
width: 800,
height: 600
});
jQuery.inspect._windowSettings.styles = "\
<style> \
* { \
margin: 0; \
} \
html, body { \
height: 100%; \
text-align: center; \
margin-bottom: 1px; \
font-family: verdana,helvetica,sans-serif; \
} \
table { \
width: " + (jQuery.inspect._windowSettings.width - 20) + "px; \
border: 1px solid black; \
} \
td { \
vertical-align: top; \
} \
</style>";
jQuery.inspect._windowSettings.config = "width=" + jQuery.inspect._windowSettings.width + ",height=" + jQuery.inspect._windowSettings.height + ",scrollbars=yes,location=no,menubar=no,toolbar=no";
jQuery.inspect._jasmine = function(text) {
jasmine.log(text);
}
jQuery.inspect._thru = function(text) {
return text;
}