-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathscript.js
228 lines (198 loc) · 7.5 KB
/
script.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// # res/script.js
//
// This is the script file that gets copied into the output. It mainly manages the display
// of the folder tree. The idea of this script file is to be minimal and standalone. So
// that means no jQuery.
// Use localStorage to store data about the tree's state: whether or not
// the tree is visible and which directories are expanded. Unless the state
var sidebarVisible = (window.localStorage && window.localStorage.docker_showSidebar) ?
window.localStorage.docker_showSidebar == 'yes' :
defaultSidebar;
/**
* ## makeTree
*
* Consructs the folder tree view
*
* @param {object} treeData Folder structure as in [queueFile](../src/docker.js.html#docker.prototype.queuefile)
* @param {string} root Path from current file to root (ie `'../../'` etc.)
* @param {string} filename The current file name
*/
function makeTree(treeData, root, filename) {
var treeNode = document.getElementById('tree');
var treeHandle = document.getElementById('sidebar-toggle');
treeHandle.addEventListener('click', toggleTree, false);
// Build the html and add it to the container.
treeNode.innerHTML = nodeHtml('', treeData, '', root);
// Root folder (whole tree) should always be open
treeNode.childNodes[0].className += ' open';
// Attach click event handler
treeNode.addEventListener('click', nodeClicked, false);
if (sidebarVisible) document.body.className += ' sidebar';
// Restore scroll position from localStorage if set. And attach scroll handler
if (window.localStorage && window.localStorage.docker_treeScroll) treeNode.scrollTop = window.localStorage.docker_treeScroll;
treeNode.onscroll = treeScrolled;
// Only set a class to allow CSS transitions after the tree state has been painted
setTimeout(function() { document.body.className += ' slidey'; }, 100);
}
/**
* ## treeScrolled
*
* Called when the tree is scrolled. Stores the scroll position in localStorage
* so it can be restored on the next pageview.
*/
function treeScrolled() {
var tree = document.getElementById('tree');
if (window.localStorage) window.localStorage.docker_treeScroll = tree.scrollTop;
}
/**
* ## nodeClicked
*
* Called when a directory is clicked. Toggles open state of the directory
*
* @param {Event} e The click event
*/
function nodeClicked(e) {
// Find the target
var t = e.target;
// If the click target is actually a file (rather than a directory), ignore it
if (t.tagName.toLowerCase() !== 'div' || t.className === 'children') return;
// Recurse upwards until we find the actual directory node
while (t && t.className.substring(0, 3) != 'dir') t = t.parentNode;
// If we're at the root node, then do nothing (we don't allow collapsing of the whole tree)
if (!t || t.parentNode.id == 'tree') return;
// Find the path and toggle the state, saving the state in the localStorage variable
var path = t.getAttribute('rel');
if (t.className.indexOf('open') !== -1) {
t.className = t.className.replace(/\s*open/g, '');
if (window.localStorage) window.localStorage.removeItem('docker_openPath:' + path);
} else {
t.className += ' open';
if (window.localStorage) window.localStorage['docker_openPath:' + path] = 'yes';
}
}
/**
* ## nodeHtml
*
* Constructs the markup for a directory in the tree
*
* @param {string} nodename The node name.
* @param {object} node Node object of same format as whole tree.
* @param {string} path The path form the base to this node
* @param {string} root Relative path from current page to root
*/
function nodeHtml(nodename, node, path, root) {
// Firstly, figure out whether or not the directory is expanded from localStorage
var isOpen = window.localStorage && window.localStorage['docker_openPath:' + path] == 'yes';
var out = '<div class="dir' + (isOpen ? ' open' : '') + '" rel="' + path + '">';
out += '<div class="nodename">' + nodename + '</div>';
out += '<div class="children">';
// Loop through all child directories first
if (node.dirs) {
var dirs = [];
for (var i in node.dirs) {
if (node.dirs.hasOwnProperty(i)) dirs.push({ name: i, html: nodeHtml(i, node.dirs[i], path + i + '/', root) });
}
// Have to store them in an array first and then sort them alphabetically here
dirs.sort(function(a, b) { return (a.name > b.name) ? 1 : (a.name == b.name) ? 0 : -1; });
for (var k = 0; k < dirs.length; k += 1) out += dirs[k].html;
}
// Now loop through all the child files alphabetically
if (node.files) {
node.files.sort();
for (var j = 0; j < node.files.length; j += 1) {
out += '<a class="file" href="' + root + path + node.files[j] + '.html">' + node.files[j] + '</a>';
}
}
// Close things off
out += '</div></div>';
return out;
}
/**
* ## toggleTree
*
* Toggles the visibility of the folder tree
*/
function toggleTree() {
// Do the actual toggling by modifying the class on the body element. That way we can get some nice CSS transitions going.
if (sidebarVisible) {
document.body.className = document.body.className.replace(/\s*sidebar/g, '');
sidebarVisible = false;
} else {
document.body.className += ' sidebar';
sidebarVisible = true;
}
if (window.localStorage) {
if (sidebarVisible) {
window.localStorage.docker_showSidebar = 'yes';
} else {
window.localStorage.docker_showSidebar = 'no';
}
}
}
/**
* ## wireUpTabs
*
* Wires up events on the sidebar tabe
*/
function wireUpTabs() {
var tabEl = document.getElementById('sidebar_switch');
var children = tabEl.childNodes;
// Each tab has a class corresponding of the id of its tab pane
for (var i = 0, l = children.length; i < l; i += 1) {
// Ignore text nodes
if (children[i].nodeType !== 1) continue;
children[i].addEventListener('click', function(c) {
return function() { switchTab(c); };
}(children[i].className));
}
}
/**
* ## switchTab
*
* Switches tabs in the sidebar
*
* @param {string} tab The ID of the tab to switch to
*/
function switchTab(tab) {
var tabEl = document.getElementById('sidebar_switch');
var children = tabEl.childNodes;
// Easiest way to go through tabs without any kind of selector is just to look at the tab bar
for (var i = 0, l = children.length; i < l; i += 1) {
// Ignore text nodes
if (children[i].nodeType !== 1) continue;
// Figure out what tab pane this tab button corresponts to
var t = children[i].className.replace(/\s.*$/, '');
if (t === tab) {
// Show the tab pane, select the tab button
document.getElementById(t).style.display = 'block';
if (children[i].className.indexOf('selected') === -1) children[i].className += ' selected';
} else {
// Hide the tab pane, deselect the tab button
document.getElementById(t).style.display = 'none';
children[i].className = children[i].className.replace(/\sselected/, '');
}
}
// Store the last open tab in localStorage
if (window.localStorage) window.localStorage.docker_sidebarTab = tab;
}
/**
* ## window.onload
*
* When the document is ready, make the sidebar and all that jazz
*/
(function(init) {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', init);
} else { // IE8 and below
window.onload = init;
}
}(function() {
makeTree(tree, relativeDir, thisFile);
wireUpTabs();
// Switch to the last viewed sidebar tab if stored, otherwise default to folder tree
if (window.localStorage && window.localStorage.docker_sidebarTab) {
switchTab(window.localStorage.docker_sidebarTab);
} else {
switchTab('tree');
}
}));