forked from dhcode/big-json-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
197 lines (181 loc) · 5.63 KB
/
demo.ts
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
import 'babel-polyfill';
import { BigJsonViewerDom, JsonNodeElement } from '../src';
const demoData = {
simpleData: {
element1: 'str',
element2: 1234,
element3: [23, 43, true, false, null, { name: 'special' }, {}],
element4: [],
element5: 'this should be some long text\nwith line break',
element6: {
name: 'Hero',
age: 32,
birthday: { year: 1986, month: 4, day: 30 }
},
element7: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
},
jsData: {
element1: 'str',
element2: 1234,
element3: [23, 43, true, false, null, { name: 'special' }, {}],
element4: [],
element5: 'this should be some long text\nwith line break',
element6: {
name: 'Hero',
age: 32,
birthday: { year: 1986, month: 4, day: 30 }
},
element7: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
element8: { un: undefined, nu: null }
},
largeData: (function() {
const list = new Array(Math.floor(Math.random() * 1000));
for (let i = 0; i < list.length; i++) {
list[i] = Math.random();
if (list[i] < 0.2) {
list[i] = 'hey ' + list[i];
}
if (list[i] > 0.8) {
list[i] = {};
const entries = Math.floor(Math.random() * 1000);
for (let j = 0; j < entries; j++) {
list[i]['entry-' + j] = Math.random();
}
}
}
return list;
})()
};
const codeElement = document.getElementById('code') as HTMLTextAreaElement;
const viewerElement = document.getElementById('viewer') as HTMLDivElement;
const pathsElement = document.getElementById('paths') as HTMLTextAreaElement;
const copiedElement = document.getElementById('copied') as HTMLInputElement;
const searchElement = document.getElementById('search') as HTMLInputElement;
const searchInfoElement = document.getElementById(
'searchInfo'
) as HTMLSpanElement;
let viewer = null;
let rootNode = document.getElementById('rootNode') as JsonNodeElement;
querySelectorArray('[data-load]').forEach((link: any) => {
const load = link.getAttribute('data-load');
if (demoData[load] && !link.loadListener) {
link.loadListener = true;
link.addEventListener('click', e => {
e.preventDefault();
loadStructureData(demoData[load], load === 'jsData');
});
}
});
codeElement.addEventListener('input', e => {
console.log('show data based on input');
showData(codeElement.value);
});
searchElement.addEventListener('input', async e => {
if (searchElement.value.length >= 2) {
const cursor = await viewer.openBySearch(
new RegExp(searchElement.value, 'i')
);
searchInfoElement.textContent = cursor.matches.length + ' matches';
searchInfoElement.appendChild(document.createTextNode(' '));
const prevBtn = searchInfoElement.appendChild(document.createElement('a'));
prevBtn.href = 'javascript:';
prevBtn.addEventListener('click', e => {
e.preventDefault();
cursor.previous();
});
prevBtn.textContent = 'Prev';
searchInfoElement.appendChild(document.createTextNode(' '));
const nextBtn = searchInfoElement.appendChild(document.createElement('a'));
nextBtn.href = 'javascript:';
nextBtn.addEventListener('click', e => {
e.preventDefault();
cursor.next();
});
nextBtn.textContent = 'Next';
} else {
await rootNode.closeNode();
viewer.openBySearch(null);
searchInfoElement.textContent = '';
}
});
loadStructureData(demoData.simpleData);
async function loadStructureData(structure, jsData = false) {
if (jsData) {
codeElement.style.display = 'none';
await showData(structure, jsData);
} else {
const text = JSON.stringify(structure, null, 2);
codeElement.style.display = '';
codeElement.value = text;
await showData(text, jsData);
}
showPaths();
}
async function showData(data: any, jsData = false) {
const index =
'showDataIndex' in viewerElement
? ++viewerElement['showDataIndex']
: (viewerElement['showDataIndex'] = 0);
if (viewerElement.children.length) {
viewerElement.removeChild(viewerElement.children[0]);
}
if (viewer) {
viewer.destroy();
}
try {
let _viewer;
if (jsData) {
_viewer = await BigJsonViewerDom.fromObject(data);
} else {
_viewer = await BigJsonViewerDom.fromData(data);
}
if (viewerElement['showDataIndex'] !== index) {
_viewer.destroy();
return;
}
viewer = _viewer;
rootNode = viewer.getRootElement();
rootNode.id = 'rootNode';
viewerElement.appendChild(rootNode);
await rootNode.openAll(1);
setupRootNode();
} catch (e) {
console.error('BigJsonViewer error', e);
const errEl = document.createElement('div');
errEl.classList.add('alert', 'alert-danger');
errEl.appendChild(document.createTextNode(e.toString()));
viewerElement.appendChild(errEl);
}
}
function setupRootNode() {
const listener = e => {
console.log('event', e.type);
showPaths();
};
rootNode.addEventListener('openNode', listener);
rootNode.addEventListener('closeNode', listener);
rootNode.addEventListener('openedNodes', listener);
rootNode.addEventListener('openStub', listener);
rootNode.addEventListener('closeStub', listener);
rootNode.addEventListener('copyPath', e => {
const node = e.target as JsonNodeElement;
copiedElement.value = node.jsonNode.path.join('.');
});
}
function showPaths() {
if (!rootNode || !rootNode.getOpenPaths) {
return;
}
pathsElement.value = rootNode
.getOpenPaths()
.map(path => path.join('.'))
.join('\n');
}
function querySelectorArray(selector: string) {
const list = document.querySelectorAll(selector);
const result = [];
for (let i = 0; i < list.length; i++) {
result.push(list[i]);
}
return result;
}