forked from jamiew/jsonview-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.js
48 lines (39 loc) · 1.57 KB
/
default.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
// This used to use jQuery, but was rewritten in plan DOM for speed and to get rid of the jQuery dependency.
document.addEventListener('DOMContentLoaded', function() {
// Click handler for collapsing and expanding objects and arrays
function collapse(evt) {
var collapser = evt.target;
var target = collapser.parentNode.getElementsByClassName('collapsible');
if ( ! target.length ) {
return;
}
target = target[0];
if ( target.style.display == 'none' ) {
var ellipsis = target.parentNode.getElementsByClassName('ellipsis')[0];
target.parentNode.removeChild(ellipsis);
target.style.display = '';
} else {
target.style.display = 'none';
var ellipsis = document.createElement('span');
ellipsis.className = 'ellipsis';
ellipsis.innerHTML = ' … ';
target.parentNode.insertBefore(ellipsis, target);
}
collapser.innerHTML = ( collapser.innerHTML == '-' ) ? '+' : '-';
}
function addCollapser(item) {
// This mainly filters out the root object (which shouldn't be collapsible)
if ( item.nodeName != 'LI' ) {
return;
}
var collapser = document.createElement('div');
collapser.className = 'collapser';
collapser.innerHTML = '-';
collapser.addEventListener('click', collapse, false);
item.insertBefore(collapser, item.firstChild);
}
var items = document.getElementsByClassName('collapsible');
for( var i = 0; i < items.length; i++) {
addCollapser(items[i].parentNode);
}
}, false);