-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-viewer.js
211 lines (163 loc) · 5.28 KB
/
json-viewer.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
/**
* @author renhongl
* @email [email protected]
* @desc This lib is for render a pretty json data on browser
*/
// require('./style.css');
const toString = Object.prototype.toString;
function isString(val) {
return typeof val === 'string';
}
function isNumber(val) {
return typeof val === 'number';
}
function isBoolean(val) {
return typeof val === 'boolean';
}
function isUndefined(val) {
return typeof val === 'undefined';
}
function isArray(val) {
return toString.call(val) === '[object Array]';
}
function isObject(val) {
return toString.call(val) === '[object Object]';
}
function isNull(val) {
return toString.call(val) === '[object Null]';
}
function JsonViewer(options) {
const defaults = {
theme: 'light',
container: null,
data: '{}',
expand: false,
};
this.options = Object.assign(defaults, options);
if (isNull(options.container)) {
throw new Error('Container: dom element is required');
}
this.render();
}
JsonViewer.prototype.renderRight = function(theme, right, val) {
if (isNumber(val)) {
right.setAttribute('class', theme + 'rightNumber');
} else if (isBoolean(val)) {
right.setAttribute('class', theme + 'rightBoolean');
} else if (val === 'null') {
right.setAttribute('class', theme + 'rightNull');
} else {
right.setAttribute('class', theme + 'rightString');
}
right.innerText = val;
}
JsonViewer.prototype.renderChildren = function(theme, key, val, right, indent, left) {
let self = this;
let folder = this.createElement('span');
let rotate90 = this.options.expand ? 'rotate90' : '';
let addHeight = this.options.expand ? 'add-height' : '';
folder.setAttribute('class', theme + 'folder ' + rotate90);
folder.onclick = function (e) {
let nextSibling = e.target.parentNode.nextSibling;
self.toggleItem(nextSibling, e.target);
}
let len = 0;
let isObj = false;
if (isObject(val)) {
len = Object.keys(val).length;
isObj = true;
} else {
len = val.length;
}
left.innerHTML = isObj ? key + '  {' + len + '}' : key + '  [' + len + ']';
left.prepend(folder);
right.setAttribute('class', theme + 'rightObj ' + addHeight);
self.parse(val, right, indent + 0, theme);
}
JsonViewer.prototype.parse = function(dataObj, parent, indent, theme) {
const self = this;
this.forEach(dataObj, function (val, key) {
const { left, right } = self.createItem(indent, theme, parent, key, typeof val !== 'object');
if (typeof val !== 'object') {
self.renderRight(theme, right, val);
} else {
self.renderChildren(theme, key, val, right, indent, left);
}
});
}
JsonViewer.prototype.createItem = function(indent, theme, parent, key, basicType) {
let self = this;
let current = this.createElement('div');
let left = this.createElement('div');
let right = this.createElement('div');
let wrap = this.createElement('div');
current.style.marginLeft = indent * 2 + 'px';
left.innerHTML = `${key}<span class="jv-${theme}-symbol"> : </span>`;
if (basicType) {
current.appendChild(wrap);
wrap.appendChild(left);
wrap.appendChild(right);
parent.appendChild(current);
current.setAttribute('class', theme + 'current');
wrap.setAttribute('class', 'jv-wrap');
left.setAttribute('class', theme + 'left');
} else {
current.appendChild(left);
current.appendChild(right);
parent.appendChild(current);
current.setAttribute('class', theme + 'current');
left.setAttribute('class', theme + 'left jv-folder');
left.onclick = function (e) {
let nextSibling = e.target.nextSibling;
self.toggleItem(nextSibling, e.target.querySelector('span'));
}
}
return {
left,
right,
current,
};
}
JsonViewer.prototype.render = function () {
let data = this.options.data;
let theme = 'jv-' + this.options.theme + '-';
let indent = 0;
let parent = this.options.container;
let key = 'object';
let dataObj;
parent.setAttribute('class', theme + 'con');
try {
dataObj = JSON.parse(data);
} catch (error) {
throw new Error('It is not a json format');
}
if (isArray(dataObj)) {
key = 'array';
}
const { left, right } = this.createItem(indent, theme, parent, key);
this.renderChildren(theme, key, dataObj, right, indent, left);
}
JsonViewer.prototype.toggleItem = function (ele, target) {
ele.classList.toggle('add-height');
target.classList.toggle('rotate90');
}
JsonViewer.prototype.createElement = function (type) {
return document.createElement(type);
}
JsonViewer.prototype.forEach = function (obj, fn) {
if (isUndefined(obj) || isNull(obj)) {
return;
}
if (typeof obj === 'object' && isArray(obj)) {
for (let i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
} else {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
fn.call(null, obj[key] ?? 'null', key, obj);
}
}
}
}
// module.exports = JsonViewer;