-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjsontree.js
77 lines (70 loc) · 2.5 KB
/
jsontree.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
!function ($) {
"use strict"; // jshint ;_;
var _render = function(obj, p){
var parent = $(p);
var ic = 0;
var count = 0 ;
for (var key in obj) {
if (!obj.hasOwnProperty(key)){
continue;
}
count+=1;
}
for (var key in obj) {
if (!obj.hasOwnProperty(key)){
continue;
}
ic +=1;
var coma = '';
if (ic < count){
coma = ',';
}
if (obj[key] === null){
parent.append('<li><span class="key">'+key+':</span><span class="null"> null </span>'+coma+'</li>');
} else if (typeof obj[key] === 'boolean'){
parent.append('<li><span class="key">'+key+':</span><span class="boolean">'+obj[key]+'</span>'+coma+'</li>');
} else if (typeof obj[key] === 'number'){
parent.append('<li><span class="key">'+key+':</span><span class="number">'+obj[key]+'</span>'+coma+'</li>');
} else if (typeof obj[key] === 'string'){
parent.append('<li><span class="key">'+key+':</span><span class="string">"'+obj[key]+'"</span>'+coma+'</li>');
} else if ($.isArray(obj[key])) {
var arval = $('<li><span class="key">'+key+':</span><span class="fold">[</span><ul class="array"></ul><span>]</span>'+coma+'</li>');
parent.append(arval);
arval.find('.unfold').data('card', _render(obj[key], arval.find('.array'))) ;
}else{
var oval = $('<li><span class="key">'+key+':</span><span class="fold">{</span><ul class="object"></ul><span>}</span>'+coma+'</li>');
parent.append(oval);
oval.find('.unfold').data('card', _render(obj[key], oval.find('.object')));
}
}
return ic;
};
$(document).on("click", '.jsontree .fold', function(e){
e.preventDefault();
$(this).addClass('folded').parent().find('ul').slideUp();
});
$(document).on('click', '.jsontree .fold.folded', function(e){
e.preventDefault();
$(this).removeClass('folded').parent().find('ul').slideDown();
});
var JsonTree = function(self){
var j = $.parseJSON(self.data('jsontree'));
self.append('<ul class="jsontree"></ul>');
_render([j], self.find('.jsontree'));
};
$.fn.jsontree = function (option) {
return this.each(function () {
var self = $(this), data = self.data('jsontree');
if (!data) {
if (typeof option == 'string') {
data = option;
self.data('jsontree', option);
}else{
data = {};
self.data('jsontree', '');
}
}
new JsonTree(self);
});
};
}(window.jQuery);