Skip to content

Commit 4d2697a

Browse files
committed
save doclist state
1 parent bbd6a4d commit 4d2697a

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

_includes/sidebar.html

+55-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
{% endif %}
3434
{% if doc.folder %}
3535
<li class="nav-item">
36-
<input type="checkbox" id="{{ doc.order }}">
36+
<input type="checkbox" id="{{ doc.order }}" class="doc-folder" onclick="saveDocumentListExpansion()">
3737
<label for="{{ doc.order }}" class="nav-item">
38-
<a class="nav-link"><span>{{ doc.bookmark | default: doc.title }}</span></a>
38+
<a class="nav-link">
39+
<span>{{ doc.bookmark | default: doc.title }}</span>
40+
</a>
3941
</label>
4042
<ul class="w-100">
4143
{% elsif doc.close %}
@@ -65,4 +67,55 @@
6567
</div>
6668
<!-- .sidebar-bottom -->
6769
</div>
70+
71+
<script>
72+
73+
function saveDocumentListExpansion() {
74+
const checkboxes = $('.doc-folder');
75+
const data = checkboxes.toArray().reduce((obj, cur) => (obj[cur.id] = cur.checked, obj), {});
76+
const content = JSON.stringify(data);
77+
setCookie('expansion', content, 1);
78+
}
79+
80+
function loadDocumentListExpansion() {
81+
const checkboxes = $('.doc-folder');
82+
const byId = checkboxes.toArray().reduce((obj, cur) => (obj[cur.id] = cur, obj), {});
83+
const cookie = getCookie('expansion');
84+
if (!cookie) return;
85+
86+
const data = JSON.parse(cookie);
87+
for (var key of Object.keys(data)) {
88+
var checkbox = byId[key];
89+
var newState = data[key];
90+
checkbox.checked = newState;
91+
}
92+
}
93+
94+
function setCookie(cname, cvalue, exdays) {
95+
const d = new Date();
96+
d.setTime(d.getTime() + (exdays*24*60*60*1000));
97+
let expires = "expires="+ d.toUTCString();
98+
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
99+
}
100+
101+
function getCookie(cname) {
102+
let name = cname + "=";
103+
let decodedCookie = decodeURIComponent(document.cookie);
104+
let ca = decodedCookie.split(';');
105+
for(let i = 0; i <ca.length; i++) {
106+
let c = ca[i];
107+
while (c.charAt(0) == ' ') {
108+
c = c.substring(1);
109+
}
110+
if (c.indexOf(name) == 0) {
111+
return c.substring(name.length, c.length);
112+
}
113+
}
114+
return "";
115+
}
116+
117+
$(document).ready(loadDocumentListExpansion);
118+
119+
</script>
68120
<!-- #sidebar -->
121+

_javascript/page.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { basic, initSidebar, initTopbar } from './modules/layouts';
2-
import { imgExtra, initClipboard, smoothScroll } from './modules/plugins';
2+
import { imgExtra, initClipboard, smoothScroll, toc } from './modules/plugins';
33

44
basic();
55
initSidebar();
66
initTopbar();
77
imgExtra();
88
initClipboard();
9+
toc();
910
smoothScroll();

0 commit comments

Comments
 (0)