|
1
|
/* Javascript to implement the file hierarchy tree. |
|
2
|
*/ |
|
3
|
(function(){ |
|
4
|
function isExpanded(ul){ |
|
5
|
return ul.className==''; |
|
6
|
} |
|
7
|
|
|
8
|
function toggleDir(ul, useInitValue){ |
|
9
|
if( !useInitValue ){ |
|
10
|
expandMap[ul.id] = !isExpanded(ul); |
|
11
|
history.replaceState(expandMap, ''); |
|
12
|
} |
|
13
|
ul.className = expandMap[ul.id] ? '' : 'collapsed'; |
|
14
|
} |
|
15
|
|
|
16
|
function toggleAll(tree, useInitValue){ |
|
17
|
var lists = tree.querySelectorAll('.subdir > ul > li ul'); |
|
18
|
if( !useInitValue ){ |
|
19
|
var expand = true; /* Default action: make all sublists visible */ |
|
20
|
for( var i=0; lists[i]; i++ ){ |
|
21
|
if( isExpanded(lists[i]) ){ |
|
22
|
expand = false; /* Any already visible - make them all hidden */ |
|
23
|
break; |
|
24
|
} |
|
25
|
} |
|
26
|
expandMap = {'*': expand}; |
|
27
|
history.replaceState(expandMap, ''); |
|
28
|
} |
|
29
|
var className = expandMap['*'] ? '' : 'collapsed'; |
|
30
|
for( var i=0; lists[i]; i++ ){ |
|
31
|
lists[i].className = className; |
|
32
|
} |
|
33
|
} |
|
34
|
|
|
35
|
function checkState(){ |
|
36
|
expandMap = history.state || {}; |
|
37
|
if( '*' in expandMap ) toggleAll(outer_ul, true); |
|
38
|
for( var id in expandMap ){ |
|
39
|
if( id!=='*' ) toggleDir(document.getElementById(id), true); |
|
40
|
} |
|
41
|
} |
|
42
|
|
|
43
|
function belowSubdir(node){ |
|
44
|
do{ |
|
45
|
node = node.parentNode; |
|
46
|
if( node==subdir ) return true; |
|
47
|
} while( node && node!=outer_ul ); |
|
48
|
return false; |
|
49
|
} |
|
50
|
|
|
51
|
var history = window.history || {}; |
|
52
|
if( !history.replaceState ) history.replaceState = function(){}; |
|
53
|
var outer_ul = document.querySelector('.filetree > ul'); |
|
54
|
var subdir = outer_ul.querySelector('.subdir'); |
|
55
|
var expandMap = {}; |
|
56
|
checkState(); |
|
57
|
outer_ul.onclick = function(e){ |
|
58
|
e = e || window.event; |
|
59
|
var a = e.target || e.srcElement; |
|
60
|
if( a.nodeName!='A' ) return true; |
|
61
|
if( a.parentNode.parentNode==subdir ){ |
|
62
|
toggleAll(outer_ul); |
|
63
|
return false; |
|
64
|
} |
|
65
|
if( !belowSubdir(a) ) return true; |
|
66
|
var ul = a.parentNode.nextSibling; |
|
67
|
while( ul && ul.nodeName!='UL' ) ul = ul.nextSibling; |
|
68
|
if( !ul ) return true; /* This is a file link, not a directory */ |
|
69
|
toggleDir(ul); |
|
70
|
return false; |
|
71
|
} |
|
72
|
}()) |
|
73
|
|