This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
jquery.jsonview.js
295 lines (273 loc) · 9.29 KB
/
jquery.jsonview.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*!
jQuery JSONView.
Licensed under the MIT License.
*/
(function(jQuery) {
var $, Collapser, JSONFormatter, JSONView, JSON_VALUE_TYPES;
JSON_VALUE_TYPES = ['object', 'array', 'number', 'string', 'boolean', 'null'];
JSONFormatter = (function() {
function JSONFormatter(options) {
if (options == null) {
options = {};
}
this.options = options;
}
JSONFormatter.prototype.htmlEncode = function(html) {
if (html !== null) {
return html.toString().replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
} else {
return '';
}
};
JSONFormatter.prototype.jsString = function(s) {
s = JSON.stringify(s).slice(1, -1);
return this.htmlEncode(s);
};
JSONFormatter.prototype.decorateWithSpan = function(value, className) {
return "<span class=\"" + className + "\">" + (this.htmlEncode(value)) + "</span>";
};
JSONFormatter.prototype.valueToHTML = function(value, level) {
var valueType;
if (level == null) {
level = 0;
}
valueType = Object.prototype.toString.call(value).match(/\s(.+)]/)[1].toLowerCase();
if (this.options.strict && !jQuery.inArray(valueType, JSON_VALUE_TYPES)) {
throw new Error("" + valueType + " is not a valid JSON value type");
}
return this["" + valueType + "ToHTML"].call(this, value, level);
};
JSONFormatter.prototype.nullToHTML = function(value) {
return this.decorateWithSpan('null', 'null');
};
JSONFormatter.prototype.undefinedToHTML = function() {
return this.decorateWithSpan('undefined', 'undefined');
};
JSONFormatter.prototype.numberToHTML = function(value) {
return this.decorateWithSpan(value, 'num');
};
JSONFormatter.prototype.stringToHTML = function(value) {
var multilineClass, newLinePattern;
if (/^(http|https|file):\/\/[^\s]+$/i.test(value)) {
return "<a href=\"" + (this.htmlEncode(value)) + "\"><span class=\"q\">\"</span>" + (this.jsString(value)) + "<span class=\"q\">\"</span></a>";
} else {
multilineClass = '';
value = this.jsString(value);
if (this.options.nl2br) {
newLinePattern = /([^>\\r\\n]?)(\\r\\n|\\n\\r|\\r|\\n)/g;
if (newLinePattern.test(value)) {
multilineClass = ' multiline';
value = (value + '').replace(newLinePattern, '$1' + '<br />');
}
}
return "<span class=\"string" + multilineClass + "\">\"" + value + "\"</span>";
}
};
JSONFormatter.prototype.booleanToHTML = function(value) {
return this.decorateWithSpan(value, 'bool');
};
JSONFormatter.prototype.arrayToHTML = function(array, level) {
var collapsible, hasContents, index, numProps, output, value, _i, _len;
if (level == null) {
level = 0;
}
hasContents = false;
output = '';
numProps = array.length;
for (index = _i = 0, _len = array.length; _i < _len; index = ++_i) {
value = array[index];
hasContents = true;
output += '<li>' + this.valueToHTML(value, level + 1);
if (numProps > 1) {
output += ',';
}
output += '</li>';
numProps--;
}
if (hasContents) {
collapsible = level === 0 ? '' : ' collapsible';
return "[<ul class=\"array level" + level + collapsible + "\">" + output + "</ul>]";
} else {
return '[ ]';
}
};
JSONFormatter.prototype.objectToHTML = function(object, level) {
var collapsible, hasContents, key, numProps, output, prop, value;
if (level == null) {
level = 0;
}
hasContents = false;
output = '';
numProps = 0;
for (prop in object) {
numProps++;
}
for (prop in object) {
value = object[prop];
hasContents = true;
key = this.options.escape ? this.jsString(prop) : prop;
output += "<li><a class=\"prop\" href=\"javascript:;\"><span class=\"q\">\"</span>" + key + "<span class=\"q\">\"</span></a>: " + (this.valueToHTML(value, level + 1));
if (numProps > 1) {
output += ',';
}
output += '</li>';
numProps--;
}
if (hasContents) {
collapsible = level === 0 ? '' : ' collapsible';
return "{<ul class=\"obj level" + level + collapsible + "\">" + output + "</ul>}";
} else {
return '{ }';
}
};
JSONFormatter.prototype.jsonToHTML = function(json) {
return "<div class=\"jsonview\">" + (this.valueToHTML(json)) + "</div>";
};
return JSONFormatter;
})();
(typeof module !== "undefined" && module !== null) && (module.exports = JSONFormatter);
Collapser = (function() {
function Collapser() {}
Collapser.bindEvent = function(item, options) {
var collapser;
item.firstChild.addEventListener('click', (function(_this) {
return function(event) {
return _this.toggle(event.target.parentNode.firstChild, options);
};
})(this));
collapser = document.createElement('div');
collapser.className = 'collapser';
collapser.innerHTML = options.collapsed ? '+' : '-';
collapser.addEventListener('click', (function(_this) {
return function(event) {
return _this.toggle(event.target, options);
};
})(this));
item.insertBefore(collapser, item.firstChild);
if (options.collapsed) {
return this.collapse(collapser);
}
};
Collapser.expand = function(collapser) {
var ellipsis, target;
target = this.collapseTarget(collapser);
if (target.style.display === '') {
return;
}
ellipsis = target.parentNode.getElementsByClassName('ellipsis')[0];
target.parentNode.removeChild(ellipsis);
target.style.display = '';
return collapser.innerHTML = '-';
};
Collapser.collapse = function(collapser) {
var ellipsis, target;
target = this.collapseTarget(collapser);
if (target.style.display === 'none') {
return;
}
target.style.display = 'none';
ellipsis = document.createElement('span');
ellipsis.className = 'ellipsis';
ellipsis.innerHTML = ' … ';
target.parentNode.insertBefore(ellipsis, target);
return collapser.innerHTML = '+';
};
Collapser.toggle = function(collapser, options) {
var action, collapsers, target, _i, _len, _results;
if (options == null) {
options = {};
}
target = this.collapseTarget(collapser);
action = target.style.display === 'none' ? 'expand' : 'collapse';
if (options.recursive_collapser) {
collapsers = collapser.parentNode.getElementsByClassName('collapser');
_results = [];
for (_i = 0, _len = collapsers.length; _i < _len; _i++) {
collapser = collapsers[_i];
_results.push(this[action](collapser));
}
return _results;
} else {
return this[action](collapser);
}
};
Collapser.collapseTarget = function(collapser) {
var target, targets;
targets = collapser.parentNode.getElementsByClassName('collapsible');
if (!targets.length) {
return;
}
return target = targets[0];
};
return Collapser;
})();
$ = jQuery;
JSONView = {
collapse: function(el) {
if (el.innerHTML === '-') {
return Collapser.collapse(el);
}
},
expand: function(el) {
if (el.innerHTML === '+') {
return Collapser.expand(el);
}
},
toggle: function(el) {
return Collapser.toggle(el);
}
};
return $.fn.JSONView = function() {
var args, defaultOptions, formatter, json, method, options, outputDoc;
args = arguments;
if (JSONView[args[0]] != null) {
method = args[0];
return this.each(function() {
var $this, level;
$this = $(this);
if (args[1] != null) {
level = args[1];
return $this.find(".jsonview .collapsible.level" + level).siblings('.collapser').each(function() {
return JSONView[method](this);
});
} else {
return $this.find('.jsonview > ul > li .collapsible').siblings('.collapser').each(function() {
return JSONView[method](this);
});
}
});
} else {
json = args[0];
options = args[1] || {};
defaultOptions = {
collapsed: false,
nl2br: false,
recursive_collapser: false,
escape: true,
strict: false
};
options = $.extend(defaultOptions, options);
formatter = new JSONFormatter(options);
if (Object.prototype.toString.call(json) === '[object String]') {
json = JSON.parse(json);
}
outputDoc = formatter.jsonToHTML(json);
return this.each(function() {
var $this, item, items, _i, _len, _results;
$this = $(this);
$this.html(outputDoc);
items = $this[0].getElementsByClassName('collapsible');
_results = [];
for (_i = 0, _len = items.length; _i < _len; _i++) {
item = items[_i];
if (item.parentNode.nodeName === 'LI') {
_results.push(Collapser.bindEvent(item.parentNode, options));
} else {
_results.push(void 0);
}
}
return _results;
});
}
};
})(jQuery);