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
|
/* eslint-env browser */
import {cssProp} from '../client-util.js';
import {info as hashLinkInfo} from './hash-link.js';
import {info as stickyHeadingInfo} from './sticky-heading.js';
export const info = {
id: 'additionalNamesBoxInfo',
box: null,
links: null,
stickyHeadingLink: null,
contentContainer: null,
mainContentContainer: null,
state: {
visible: false,
},
};
export function getPageReferences() {
info.box =
document.getElementById('additional-names-box');
info.links =
document.querySelectorAll('a[href="#additional-names-box"]');
info.stickyHeadingLink =
document.querySelector(
'.content-sticky-heading-container ' +
'a[href="#additional-names-box"]');
info.contentContainer =
document.querySelector('#content');
info.mainContentContainer =
document.querySelector('#content .main-content-container');
}
export function addInternalListeners() {
hashLinkInfo.event.beforeHashLinkScrolls.push(({target}) => {
if (target === info.box) {
return false;
}
});
stickyHeadingInfo.event.whenStuckStatusChanges.push((index, stuck) => {
const {state} = info;
if (!info.stickyHeadingLink) return;
const container = stickyHeadingInfo.contentContainers[index];
if (container !== info.contentContainer) return;
if (stuck) {
if (!state.visible) {
info.stickyHeadingLink.removeAttribute('href');
if (info.stickyHeadingLink.hasAttribute('title')) {
info.stickyHeadingLink.dataset.restoreTitle = info.stickyHeadingLink.getAttribute('title');
info.stickyHeadingLink.removeAttribute('title');
}
}
} else {
info.stickyHeadingLink.setAttribute('href', '#additional-names-box');
const {restoreTitle} = info.stickyHeadingLink.dataset;
if (restoreTitle) {
info.stickyHeadingLink.setAttribute('title', restoreTitle);
delete info.stickyHeadingLink.dataset.restoreTitle;
}
}
});
}
export function addPageListeners() {
for (const link of info.links) {
link.addEventListener('click', domEvent => {
handleAdditionalNamesBoxLinkClicked(domEvent);
});
}
}
function handleAdditionalNamesBoxLinkClicked(domEvent) {
const {state} = info;
domEvent.preventDefault();
if (!domEvent.target.hasAttribute('href')) return;
if (!info.box || !info.mainContentContainer) return;
const margin =
+(cssProp(info.box, 'scroll-margin-top').replace('px', ''));
const {top} =
(state.visible
? info.box.getBoundingClientRect()
: info.mainContentContainer.getBoundingClientRect());
if (top + 20 < margin || top > 0.4 * window.innerHeight) {
if (!state.visible) {
toggleAdditionalNamesBox();
}
window.scrollTo({
top: window.scrollY + top - margin,
behavior: 'smooth',
});
} else {
toggleAdditionalNamesBox();
}
}
export function toggleAdditionalNamesBox() {
const {state} = info;
state.visible = !state.visible;
info.box.style.display =
(state.visible
? 'block'
: 'none');
}
|