-
Notifications
You must be signed in to change notification settings - Fork 28
/
jsontree.js
227 lines (202 loc) · 5.63 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
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
var JSONTree = (function() { // eslint-disable-line no-unused-vars
var escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'/': '/',
};
var internalId = 0;
var instances = 0;
this.create = function(data, settings) {
instances += 1;
return '<div class="jstTree">' + _jsVal(data) + '</div>';
};
this.click = function(elem) {
if (elem.nextElementSibling.className !== 'jstHiddenBlock') {
var block = findNextWithClass(elem, 'jstBracket');
var siblings = _nextUntil(block, block.id + '_end');
_hide(elem, siblings);
elem.className = 'jstExpand';
} else {
var block = findNextWithClass(elem, 'jstBracket');
var hiddenElements = findNextWithClass(elem, 'jstHiddenBlock');
var children = hiddenElements.children;
for (var i = children.length; i > 0; i--) {
var child = children[i - 1];
block.after(child);
}
hiddenElements.remove();
elem.className = 'jstCollapse';
}
};
var findNextWithClass = function(element, clazz) {
var next = element.nextElementSibling;
while (true) {
if (next.className === clazz) {
return next;
}
next = next.nextElementSibling;
}
};
var _id = function() {
return instances + '_' + internalId++;
};
var _escape = function(text) {
return text.replace(/[&<>'"]/g, function(c) {
return escapeMap[c];
});
};
var _jsVal = function(value) {
var type = typeof value;
switch (type) {
case 'boolean':
return _jsBool(value);
case 'number':
return _jsNum(value);
case 'string':
return _jsStr(value);
default:
if (value === null) {
return _jsNull();
} else if (value instanceof Array) {
return _jsArr(value);
} else {
return _jsObj(value);
}
}
};
var _jsObj = function(object) {
var id = _id();
var elements = [];
var keys = Object.keys(object);
keys.forEach(function(key, index) {
var html = [];
html.push('<li class="jstItem">');
if (_canCollapse(object[key])) {
html.push(_collapseElem());
}
html.push(_property(key, object[key]));
if (index !== keys.length - 1) {
html.push(_comma());
}
html.push('</li>');
elements.push(html.join(''));
});
var body = elements.join('');
return _collection(_open('{', id), body, _close('}', id));
};
var _collapseElem = function() {
var onClick = 'onclick="JSONTree.click(this); return false;"';
return '<span class="jstCollapse" ' + onClick + '></span>';
};
var _canCollapse = function(data) {
var type = typeof data;
switch (type) {
case 'boolean':
return false;
case 'number':
return false;
case 'string':
return false;
default:
if (data === null) {
return false;
} else if (data instanceof Array) {
return data.length > 0;
} else {
return Object.keys(data).length > 0;
}
}
};
var _collection = function(opening, data, closing) {
if (data.length > 0) {
return [
opening,
'<ul class="jstList">',
data,
'</ul>',
closing,
].join('');
} else {
return opening + closing;
}
};
var _jsArr = function(array) {
var id = _id();
var elements = [];
array.forEach(function(element, index) {
var html = ['<li class="jstItem">'];
if (_canCollapse(element)) {
html.push(_collapseElem());
}
html.push(_jsVal(element));
if (index !== array.length - 1) {
html.push(_comma());
}
html.push('</li>');
elements.push(html.join(''));
});
var body = elements.join('');
return _collection(_open('[', id), body, _close(']', id));
};
var _jsStr = function(value) {
var jsonString = _escape(JSON.stringify(value));
return _element(jsonString, {class: 'jstStr'});
};
var _jsNum = function(value) {
return _element(value, {class: 'jstNum'});
};
var _jsBool = function(value) {
return _element(value, {class: 'jstBool'});
};
var _jsNull = function() {
return _element('null', {class: 'jstNull'});
};
var _property = function(name, value) {
var escapedValue = _escape(JSON.stringify(name));
var property = _element(escapedValue, {class: 'jstProperty'});
var propertyValue = _jsVal(value);
return [property + _colon(), propertyValue].join('');
};
var _colon = function() {
return _element(': ', {class: 'jstColon'});
};
var _comma = function() {
return _element(',', {class: 'jstComma'});
};
var _element = function(content, attrs) {
var attrsStr = Object.keys(attrs).map(function(attr) {
return ' ' + attr + '="' + attrs[attr] + '"';
}).join('');
return '<span' + attrsStr + '>' + content + '</span>';
};
var _open = function(sym, id) {
return _element(sym, {id: 'opening_' + id, class: 'jstBracket'});
};
var _close = function(sym, id) {
return _element(sym, {id: 'opening_' + id + '_end', class: 'jstBracket'});
};
var _nextUntil = function(elem, id) {
var siblings = [];
elem = elem.nextElementSibling;
while (elem) {
if (elem.id == id) {
break;
}
siblings.push(elem);
elem = elem.nextElementSibling;
}
return siblings;
};
var _hide = function(elem, siblings) {
var wrapper = document.createElement('div');
wrapper.className = 'jstHiddenBlock';
siblings.forEach(function(s) {
wrapper.appendChild(s);
});
elem.after(wrapper);
};
return this;
})();