@@ -5,15 +5,7 @@ const demoData = {
5
5
simpleData : {
6
6
element1 : 'str' ,
7
7
element2 : 1234 ,
8
- element3 : [
9
- 23 ,
10
- 43 ,
11
- true ,
12
- false ,
13
- null ,
14
- { name : 'special' } ,
15
- { }
16
- ] ,
8
+ element3 : [ 23 , 43 , true , false , null , { name : 'special' } , { } ] ,
17
9
element4 : [ ] ,
18
10
element5 : 'this should be some long text\nwith line break' ,
19
11
element6 : {
@@ -38,19 +30,20 @@ const demoData = {
38
30
}
39
31
}
40
32
return list ;
41
- } ( ) )
33
+ } ) ( )
42
34
} ;
43
35
44
-
45
36
const codeElement = document . getElementById ( 'code' ) as HTMLTextAreaElement ;
46
37
const viewerElement = document . getElementById ( 'viewer' ) as HTMLDivElement ;
47
38
const pathsElement = document . getElementById ( 'paths' ) as HTMLTextAreaElement ;
48
39
const copiedElement = document . getElementById ( 'copied' ) as HTMLInputElement ;
49
40
const searchElement = document . getElementById ( 'search' ) as HTMLInputElement ;
50
- const searchInfoElement = document . getElementById ( 'searchInfo' ) as HTMLSpanElement ;
41
+ const searchInfoElement = document . getElementById (
42
+ 'searchInfo'
43
+ ) as HTMLSpanElement ;
44
+ let viewer = null ;
51
45
let rootNode = document . getElementById ( 'rootNode' ) as JsonNodeElement ;
52
46
53
-
54
47
querySelectorArray ( '[data-load]' ) . forEach ( ( link : any ) => {
55
48
const load = link . getAttribute ( 'data-load' ) ;
56
49
if ( demoData [ load ] && ! link . loadListener ) {
@@ -67,9 +60,10 @@ codeElement.addEventListener('input', e => {
67
60
showData ( codeElement . value ) ;
68
61
} ) ;
69
62
searchElement . addEventListener ( 'input' , async e => {
70
- rootNode . closeNode ( ) ;
71
63
if ( searchElement . value . length >= 2 ) {
72
- const cursor = await rootNode . openBySearch ( new RegExp ( searchElement . value , 'i' ) ) ;
64
+ const cursor = await viewer . openBySearch (
65
+ new RegExp ( searchElement . value , 'i' )
66
+ ) ;
73
67
searchInfoElement . textContent = cursor . matches . length + ' matches' ;
74
68
75
69
searchInfoElement . appendChild ( document . createTextNode ( ' ' ) ) ;
@@ -92,42 +86,42 @@ searchElement.addEventListener('input', async e => {
92
86
} ) ;
93
87
nextBtn . textContent = 'Next' ;
94
88
} else {
95
- rootNode . openBySearch ( null ) ;
89
+ await rootNode . closeNode ( ) ;
90
+ viewer . openBySearch ( null ) ;
96
91
searchInfoElement . textContent = '' ;
97
92
}
98
-
99
93
} ) ;
100
94
101
-
102
95
loadStructureData ( demoData . simpleData ) ;
103
96
104
- function loadStructureData ( structure ) {
97
+ async function loadStructureData ( structure ) {
105
98
const text = JSON . stringify ( structure , null , 2 ) ;
106
99
codeElement . value = text ;
107
- showData ( text ) ;
100
+ await showData ( text ) ;
108
101
showPaths ( ) ;
109
102
}
110
103
111
104
async function showData ( data : string ) {
112
105
if ( viewerElement . children . length ) {
113
106
viewerElement . removeChild ( viewerElement . children [ 0 ] ) ;
114
107
}
108
+ if ( viewer ) {
109
+ viewer . destroy ( ) ;
110
+ }
115
111
try {
116
- const viewer = await BigJsonViewerDom . fromData ( data ) ;
112
+ viewer = await BigJsonViewerDom . fromData ( data ) ;
117
113
rootNode = viewer . getRootElement ( ) ;
118
114
rootNode . id = 'rootNode' ;
119
115
viewerElement . appendChild ( rootNode ) ;
120
- rootNode . openAll ( 1 ) ;
116
+ await rootNode . openAll ( 1 ) ;
121
117
setupRootNode ( ) ;
122
-
123
118
} catch ( e ) {
124
119
console . error ( 'BigJsonViewer error' , e ) ;
125
120
const errEl = document . createElement ( 'div' ) ;
126
121
errEl . classList . add ( 'alert' , 'alert-danger' ) ;
127
122
errEl . appendChild ( document . createTextNode ( e . toString ( ) ) ) ;
128
123
viewerElement . appendChild ( errEl ) ;
129
124
}
130
-
131
125
}
132
126
133
127
function setupRootNode ( ) {
@@ -151,8 +145,10 @@ function showPaths() {
151
145
return ;
152
146
}
153
147
154
- pathsElement . value = rootNode . getOpenPaths ( ) . map ( path => path . join ( '.' ) ) . join ( '\n' ) ;
155
-
148
+ pathsElement . value = rootNode
149
+ . getOpenPaths ( )
150
+ . map ( path => path . join ( '.' ) )
151
+ . join ( '\n' ) ;
156
152
}
157
153
158
154
function querySelectorArray ( selector : string ) {
0 commit comments