-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathJSONGrid.js
188 lines (155 loc) · 5.29 KB
/
JSONGrid.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
'use strict';
var DOMHelper = {
EXPANDER_TARGET_ATTRIBUTE: 'data-target-id',
TABLE_SHRINKED_CLASSNAME: 'shrinked',
JSON_GRID_CONTAINER_CLASSNAME: 'json-grid-container',
JSON_GRID_ELEMENT_CONTAINER_CLASSNAME: 'json-grid-element-container',
createElement: function (type, valueType, additionalClasses, id) {
var element = document.createElement(type);
var classes = additionalClasses || [];
if (!Array.isArray(classes)) classes = [classes];
if (valueType) classes.push(valueType);
DOMTokenList.prototype.add.apply(element.classList, classes);
if (id) {
element.id = id;
}
return element;
},
createExpander: function (dataItems, target) {
var expander = DOMHelper.createElement('span', 'expander');
expander.textContent = '[' + DOMHelper.getExpanderSign(target) + '] ' + dataItems + ' items';
expander.setAttribute(DOMHelper.EXPANDER_TARGET_ATTRIBUTE, target.id);
expander.onclick = DOMHelper.onExpanderClick;
return expander;
},
onExpanderClick: function (event) {
var tableId = event.target.getAttribute(DOMHelper.EXPANDER_TARGET_ATTRIBUTE);
var target = document.getElementById(tableId);
if (target) {
target.classList.toggle(DOMHelper.TABLE_SHRINKED_CLASSNAME);
event.target.textContent = '[' + DOMHelper.getExpanderSign(target) + event.target.textContent.slice(2);
}
},
getExpanderSign: function (target) {
return target.classList.contains(DOMHelper.TABLE_SHRINKED_CLASSNAME)
? '+'
: '-'
;
}
}
function JSONGrid(data, container) {
this.data = data;
this.container = container instanceof HTMLElement
? container
: null;
this.instanceNumber = JSONGrid.instances || 0;
JSONGrid.instances = (JSONGrid.instances || 0) + 1;
}
JSONGrid.prototype.processArray = function () {
var keys = this.data.reduce(function (acc, val) {
var keys = Object.keys(val);
return acc.concat(keys);
}, []);
keys = keys.filter(function (value, idx) {
return keys.indexOf(value) === idx;
});
var headers = DOMHelper.createElement('tr');
headers.appendChild(DOMHelper.createElement('th'))
keys.forEach(function (value) {
var td = DOMHelper.createElement('th');
td.textContent = value.toString();
headers.appendChild(td);
});
var rows = this.data.map(function (obj, index) {
var tr = DOMHelper.createElement('tr')
var firstTd = DOMHelper.createElement('td', typeof index);
firstTd.appendChild(new JSONGrid(index).generateDOM());
tr.appendChild(firstTd);
keys.forEach(function (key, keyIdx) {
var td = DOMHelper.createElement('td', typeof obj, 'table-wrapper');
var value = (obj[key] === undefined || obj[key] === null)
? '' + obj[key]
: obj[key]
;
td.appendChild(new JSONGrid(value).generateDOM());
tr.appendChild(td);
});
return tr;
});
return {
headers: [headers],
rows: rows,
};
}
JSONGrid.prototype.processObject = function () {
var keys = Object.keys(this.data);
var headers = DOMHelper.createElement('tr');
keys.forEach(function (value) {
var td = DOMHelper.createElement('td');
td.textContent = '' + value;
headers.appendChild(td);
});
var that = this;
var rows = keys.map(function (key, index) {
var tr = DOMHelper.createElement('tr')
var keyTd = DOMHelper.createElement('td', 'string', 'rowName');
var value = that.data[key];
var tdType = typeof value;
var tdValue;
if (tdType === 'object' && value) {
var grid = new JSONGrid(value);
tdValue = grid.generateDOM();
} else {
tdValue = DOMHelper.createElement('span', tdType, 'value');
tdValue.textContent = '' + value;
}
var valTd = DOMHelper.createElement('td', tdType);
keyTd.textContent = key;
valTd.appendChild(tdValue);
tr.appendChild(keyTd);
tr.appendChild(valTd);
return tr;
});
return {
headers: [],
rows: rows,
};
}
JSONGrid.prototype.generateDOM = function () {
var dom;
if (Array.isArray(this.data)) {
dom = this.processArray();
} else if (typeof this.data === 'object') {
dom = this.processObject();
} else {
// -- Create a span element and early return since this is a "leaf"
var span = DOMHelper.createElement('span', typeof this.data);
span.textContent = '' + this.data;
return span;
}
var container = DOMHelper.createElement(
'div',
DOMHelper.JSON_GRID_ELEMENT_CONTAINER_CLASSNAME
);
var tableId = 'table-' + this.instanceNumber;
var intialClasses = this.instanceNumber !== 0 ? [DOMHelper.TABLE_SHRINKED_CLASSNAME] : [];
var table = DOMHelper.createElement('table', 'table', intialClasses, tableId);
var tbody = DOMHelper.createElement('tbody');
var expander = DOMHelper.createExpander(dom.rows.length, table);
container.appendChild(expander);
dom.headers.forEach(function (val) { tbody.appendChild(val); });
dom.rows.forEach(function (val) { tbody.appendChild(val); });
table.appendChild(tbody);
container.appendChild(table);
return container;
};
JSONGrid.prototype.render = function () {
if (!this.container || !this.data) {
return;
}
// -- Remove the children for re-render
this.container.innerHTML = '';
this.container.appendChild(this.generateDOM());
this.container.classList.add(DOMHelper.JSON_GRID_CONTAINER_CLASSNAME);
};
window.JSONGrid = JSONGrid;