UNPKG

3.18 kBJavaScriptView Raw
1'use strict';
2let idToSection = Object.create(null);
3for (let [section, ids] of Object.entries(multipageMap)) {
4 for (let id of ids) {
5 if (!idToSection[id]) {
6 idToSection[id] = section;
7 }
8 }
9}
10if (location.hash) {
11 let targetSec = idToSection[location.hash.substring(1)];
12 if (targetSec != null) {
13 let match = location.pathname.match(/([^/]+)\.html?$/);
14 if ((match != null && match[1] !== targetSec) || location.pathname.endsWith('/multipage/')) {
15 location = (targetSec === 'index' ? './' : targetSec + '.html') + location.hash;
16 }
17 }
18}
19
20function getTocPath(li) {
21 let path = [];
22 let pointer = li;
23 while (true) {
24 let parent = pointer.parentElement;
25 if (parent == null) {
26 return null;
27 }
28 let index = [].indexOf.call(parent.children, pointer);
29 if (index == -1) {
30 return null;
31 }
32 path.unshift(index);
33 pointer = parent.parentElement;
34 if (pointer == null) {
35 return null;
36 }
37 if (pointer.id === 'menu-toc') {
38 break;
39 }
40 if (pointer.tagName !== 'LI') {
41 return null;
42 }
43 }
44 return path;
45}
46
47function activateTocPath(path) {
48 try {
49 let pointer = document.getElementById('menu-toc');
50 for (let index of path) {
51 pointer = pointer.querySelector('ol').children[index];
52 }
53 pointer.classList.add('active');
54 } catch (e) {
55 // pass
56 }
57}
58
59function getActiveTocPaths() {
60 return [...menu.$menu.querySelectorAll('.active')].map(getTocPath).filter(p => p != null);
61}
62
63function loadStateFromSessionStorage() {
64 if (!window.sessionStorage) {
65 return;
66 }
67 if (sessionStorage.referencePaneState != null) {
68 let state = JSON.parse(sessionStorage.referencePaneState);
69 if (state != null) {
70 if (state.type === 'ref') {
71 let entry = menu.search.biblio.byId[state.id];
72 if (entry != null) {
73 referencePane.showReferencesFor(entry);
74 }
75 } else if (state.type === 'sdo') {
76 let sdos = sdoMap[state.id];
77 if (sdos != null) {
78 referencePane.$headerText.innerHTML = state.html;
79 referencePane.showSDOsBody(sdos, state.id);
80 }
81 }
82 delete sessionStorage.referencePaneState;
83 }
84 }
85
86 if (sessionStorage.activeTocPaths != null) {
87 document
88 .getElementById('menu-toc')
89 .querySelectorAll('.active')
90 .forEach(e => {
91 e.classList.remove('active');
92 });
93 let active = JSON.parse(sessionStorage.activeTocPaths);
94 active.forEach(activateTocPath);
95 delete sessionStorage.activeTocPaths;
96 }
97
98 if (sessionStorage.tocScroll != null) {
99 let tocScroll = JSON.parse(sessionStorage.tocScroll);
100 menu.$toc.scrollTop = tocScroll;
101 delete sessionStorage.tocScroll;
102 }
103}
104
105document.addEventListener('DOMContentLoaded', loadStateFromSessionStorage);
106
107window.addEventListener('pageshow', loadStateFromSessionStorage);
108
109window.addEventListener('beforeunload', () => {
110 if (window.sessionStorage) {
111 sessionStorage.referencePaneState = JSON.stringify(referencePane.state || null);
112 sessionStorage.activeTocPaths = JSON.stringify(getActiveTocPaths());
113 sessionStorage.tocScroll = JSON.stringify(menu.$toc.scrollTop);
114 }
115});