forked from mohsen1/json-formatter-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
176 lines (160 loc) · 4.85 KB
/
index.html
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
<html>
<head>
<script src="dist/json-formatter.js"></script>
<style type="text/css">
body {
max-width: 800px;
margin: 20px auto;
font-family: sans-serif;
}
.gh {
text-align: right;
}
.result h3 + div {
border: 1px solid lightgray;
border-radius: 5px;
padding: 10px;
}
h1 code {
font-size: 2em;
}
pre {
font-size: 1.5rem;
background: #efefef;
padding: 1em;
}
textarea {
font-family: monospace;
}
</style>
</head>
<body>
<div class="intro">
<div class="gh">
<iframe
src="http://ghbtns.com/github-btn.html?user=mohsen1&repo=json-formatter-js&type=watch&size=large&count=true"
frameborder="0"
width="170"
height="30"
scrolling="0">
</iframe>
<iframe
src="http://ghbtns.com/github-btn.html?user=mohsen1&repo=json-formatter-js&type=fork&size=large&count=true"
frameborder="0"
width="170"
height="30"
scrolling="0">
</iframe>
</div>
<h1><code>JSONFormatter</code></h1>
<p>
Render JSON objects in HTML with a <b>collapsible</b> navigation.
</p>
<p><b><a href="https://github.com/mohsen1/json-formatter-js">JSON Formatter project on GitHub</a></b></p>
</div>
<h2>Usage</h2>
<div class="usage">
<p>Simple usage in ES6</p>
<pre>
import JSONFormatter from 'json-formatter-js'
const myJSON = {ans: 42};
const formatter = new JSONFormatter(myJSON);
document.body.appendChild(formatter.render());
</pre>
</div>
<h2>Playground</h2>
<div class="playground">
<p>Updating the JSON value in this textarea renders a new formatter on the right.</p>
<table>
<tr>
<td>
<textarea id="live" name="" id="" cols="30" rows="10">{"foo": 42}</textarea>
</td>
<td>
<div id="live-result"></div>
</td>
</tr>
<tr>
<p>
<input type="checkbox" id="hoverPreviewEnabled">
<label for="hoverPreviewEnabled"><code>hoverPreviewEnabled</code></label>
</p>
</tr>
</table>
</div>
<h2>Examples</h2>
<div class="result"></div>
<script>
var live = document.getElementById('live');
var hoverPreviewEnabledCheckbox = document.getElementById('hoverPreviewEnabled');
function render() {
live.style.backgroundColor = 'transparent';
var result = document.getElementById('live-result');
try {
var formatter = new JSONFormatter(JSON.parse(live.value), 1, {hoverPreviewEnabled: hoverPreviewEnabledCheckbox.checked});
result.innerHTML = '';
result.appendChild(formatter.render());
} catch(e) {
live.style.backgroundColor = 'rgba(255, 87, 34, 0.35)';
}
}
live.addEventListener('keyup', render);
hoverPreviewEnabledCheckbox.addEventListener('change', render);
render();
</script>
<script>
var complex = {
numbers: [
1,
2,
3
],
boolean: true,
'null': null,
number: 123,
anObject: {
a: 'b',
c: 'd',
e: 'f\"'
},
string: 'Hello World',
url: 'https://github.com/mohsen1/json-formatter-js',
date: 'Sun Aug 03 2014 20:46:55 GMT-0700 (PDT)',
func: function add(a,b){return a + b; }
};
var deep = {a:{b:{c:{d:{}}}}};
var examples = [
{title: 'Complex', json: complex},
{title: 'Number', json: 42},
{title: 'null', json: null},
{title: 'Empty Object', json: Object.create(null)},
{title: 'Empty Array', json: []},
{title: 'Deep', json: deep},
{title: 'Dark', json: complex, config: {theme: 'dark'}}
];
var result = document.querySelector('.result');
examples.forEach(function(example) {
var title = document.createElement('h3');
var formatter = new JSONFormatter(example.json, 1, example.config);
title.innerText = example.title;
result.appendChild(title)
var el = formatter.render();
if (example.config && example.config.theme === 'dark') {
el.style.backgroundColor = '#1E1E1E';
}
result.appendChild(el);
});
fetch('demo/giant.json').then(function(resp) {
resp.json().then(function(giant) {
var giantFormatter = new JSONFormatter(giant, 2, {hoverPreviewEnabled: true});
var title = document.createElement('h3');
title.innerText = 'Giant JSON';
result.appendChild(title);
console.time('Rendering giant JSON');
result.appendChild(giantFormatter.render());
console.timeEnd('Rendering giant JSON');
});
})
</script>
</body>
</html>