Skip to content

Commit 4932a10

Browse files
committed
Make Tables of Contents Hide-able
Closes #217
1 parent 2a000d2 commit 4932a10

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,6 @@ This repository also contains code authored by third parties that may be license
138138
files indicate the copyright and license terms at the top of the file. Currently these include:
139139

140140
* jQuery and jQuery UI libraries: Copyright 2015 jQuery Foundation and other contributors; [MIT License](http://creativecommons.org/licenses/MIT)
141+
* [jQuery Cookie plugin](https://github.com/js-cookie/js-cookie):
142+
Copyright 2006, 2015 Klaus Hartl & Fagner Brack;
143+
[MIT License](http://creativecommons.org/licenses/MIT)

assets/sass/style.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ footer.pretty-box {
338338
font-size: 80%;
339339
}
340340

341+
#TOC_title {
342+
font-size: 120%;
343+
344+
a {
345+
font-weight: normal;
346+
font-size: 90%;
347+
outline: 0;
348+
}
349+
}
350+
341351
@media (max-width : 560px) {
342352
#header {
343353
border-radius: 10px;

html/css/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ footer.pretty-box {
280280
text-align: center;
281281
font-size: 80%; }
282282

283+
#TOC_title {
284+
font-size: 120%; }
285+
#TOC_title a {
286+
font-weight: normal;
287+
font-size: 90%;
288+
outline: 0; }
289+
283290
@media (max-width: 560px) {
284291
#header {
285292
border-radius: 10px; }

html/js/jquery.cookie.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*!
2+
* JavaScript Cookie v2.0.3
3+
* https://github.com/js-cookie/js-cookie
4+
*
5+
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6+
* Released under the MIT license
7+
*/
8+
(function (factory) {
9+
if (typeof define === 'function' && define.amd) {
10+
define(factory);
11+
} else if (typeof exports === 'object') {
12+
module.exports = factory();
13+
} else {
14+
var _OldCookies = window.Cookies;
15+
var api = window.Cookies = factory();
16+
api.noConflict = function () {
17+
window.Cookies = _OldCookies;
18+
return api;
19+
};
20+
}
21+
}(function () {
22+
function extend () {
23+
var i = 0;
24+
var result = {};
25+
for (; i < arguments.length; i++) {
26+
var attributes = arguments[ i ];
27+
for (var key in attributes) {
28+
result[key] = attributes[key];
29+
}
30+
}
31+
return result;
32+
}
33+
34+
function init (converter) {
35+
function api (key, value, attributes) {
36+
var result;
37+
38+
// Write
39+
40+
if (arguments.length > 1) {
41+
attributes = extend({
42+
path: '/'
43+
}, api.defaults, attributes);
44+
45+
if (typeof attributes.expires === 'number') {
46+
var expires = new Date();
47+
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
48+
attributes.expires = expires;
49+
}
50+
51+
try {
52+
result = JSON.stringify(value);
53+
if (/^[\{\[]/.test(result)) {
54+
value = result;
55+
}
56+
} catch (e) {}
57+
58+
value = encodeURIComponent(String(value));
59+
value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
60+
61+
key = encodeURIComponent(String(key));
62+
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
63+
key = key.replace(/[\(\)]/g, escape);
64+
65+
return (document.cookie = [
66+
key, '=', value,
67+
attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
68+
attributes.path && '; path=' + attributes.path,
69+
attributes.domain && '; domain=' + attributes.domain,
70+
attributes.secure ? '; secure' : ''
71+
].join(''));
72+
}
73+
74+
// Read
75+
76+
if (!key) {
77+
result = {};
78+
}
79+
80+
// To prevent the for loop in the first place assign an empty array
81+
// in case there are no cookies at all. Also prevents odd result when
82+
// calling "get()"
83+
var cookies = document.cookie ? document.cookie.split('; ') : [];
84+
var rdecode = /(%[0-9A-Z]{2})+/g;
85+
var i = 0;
86+
87+
for (; i < cookies.length; i++) {
88+
var parts = cookies[i].split('=');
89+
var name = parts[0].replace(rdecode, decodeURIComponent);
90+
var cookie = parts.slice(1).join('=');
91+
92+
if (cookie.charAt(0) === '"') {
93+
cookie = cookie.slice(1, -1);
94+
}
95+
96+
try {
97+
cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);
98+
99+
if (this.json) {
100+
try {
101+
cookie = JSON.parse(cookie);
102+
} catch (e) {}
103+
}
104+
105+
if (key === name) {
106+
result = cookie;
107+
break;
108+
}
109+
110+
if (!key) {
111+
result[name] = cookie;
112+
}
113+
} catch (e) {}
114+
}
115+
116+
return result;
117+
}
118+
119+
api.get = api.set = api;
120+
api.getJSON = function () {
121+
return api.apply({
122+
json: true
123+
}, [].slice.call(arguments));
124+
};
125+
api.defaults = {};
126+
127+
api.remove = function (key, attributes) {
128+
api(key, '', extend(attributes, {
129+
expires: -1
130+
}));
131+
};
132+
133+
api.withConverter = init;
134+
135+
return api;
136+
}
137+
138+
return init();
139+
}));

html/js/main.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$(function(){
22
setup_search_box();
33
setup_auto_title_anchors();
4+
setup_collapsible_TOC();
45
$(window).resize(setup_search_box);
56
});
67

@@ -25,3 +26,41 @@ function setup_auto_title_anchors() {
2526
);
2627
});
2728
}
29+
30+
function setup_collapsible_TOC() {
31+
var state;
32+
if ( ! $('nav.indexgroup > ol').length ) { return; }
33+
34+
// fix for jumpy .slideDown() effect
35+
$('nav.indexgroup > ol').each( function(){
36+
$(this).css( 'height', $(this).height() );
37+
});
38+
39+
state = Cookies.get('toc_state') || 'shown';
40+
if ( state == 'hidden' ) {
41+
$('nav.indexgroup > ol').hide();
42+
}
43+
44+
$('nav.indexgroup')
45+
.prepend('<h2 id="TOC_title">Table of Contents'
46+
+ ' <a href="#">['
47+
+ ( state == 'hidden' ? 'show' : 'hide')
48+
+ ']</a></h2>'
49+
)
50+
.find('a')
51+
.click(function() {
52+
var el = $(this);
53+
if (el.text() == '[hide]') {
54+
Cookies.set('toc_state', 'hidden');
55+
el.parents('nav').find('ol').slideUp();
56+
el.text('[show]');
57+
}
58+
else {
59+
Cookies.set('toc_state', 'shown');
60+
el.parents('nav').find('ol').slideDown();
61+
el.text('[hide]');
62+
}
63+
64+
return false;x
65+
});
66+
}

template/footer.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
<a href="https://raw.githubusercontent.com/perl6/doc/master/LICENSE">Artistic License 2.0</a>.
1111
The Camelia image is copyright © 2009 by Larry Wall.
1212
</p>
13+
<p>
14+
<small>
15+
This site uses HTTP Cookies to save your browsing preferences. If that's
16+
an issue, please <a href="https://www.google.com/search?q=block+website+from+using+cookie">
17+
configure your browser to block them</a>.
18+
</small>
19+
</p>
1320
</footer>
1421

1522
<script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script>
1623
<script type="text/javascript" src="/js/jquery-ui.min.js"></script>
1724
<script type="text/javascript" src="/js/search.js"></script>
25+
<script type="text/javascript" src="/js/jquery.cookie.js"></script>
1826
<script type="text/javascript" src="/js/main.js"></script>

0 commit comments

Comments
 (0)