-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinx_peek.js
226 lines (212 loc) · 7.68 KB
/
sphinx_peek.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
// @ts-check
/**
* @typedef {Object} Config
* The config object is used to configure the preview.
* It is set globally on the window object.
* @property {string} selector - The selector to match.
* @property {string} notSelector - The selector to exclude.
* @property {string} iconType - The element type of the icon (e.g. span or sup).
* @property {string} iconOpen - The SVG for the open icon.
* @property {string} iconClose - The SVG for the close icon.
* @property {number} width - The width of the preview (or window width if smaller).
* @property {number} height - The height of the preview (or window height if smaller).
* @property {Object} offset - The offset of the preview.
* @property {number} offset.left - The horizontal offset of the preview.
* @property {number} offset.top - The vertical offset of the preview.
* @property {number} openDelay - The delay in milliseconds before the preview opens.
* @global
*/
// add preview icons once the page is loaded
document.addEventListener("DOMContentLoaded", function () {
/**
* @type {Config}
*/
// @ts-ignore
var config = window.sphinxPeekConfig;
if (config === undefined) {
console.warn("Sphinx Peek: window.sphinxPeekConfig not set");
return;
}
// add preview icon after all selected links
document.querySelectorAll(config.selector).forEach(function (element) {
if (element.matches(config.notSelector)) {
return;
}
let href = element.getAttribute("href");
if (href === null) {
console.warn(`Sphinx Peek: Link without href: ${element}`);
return;
}
element.insertAdjacentHTML(
"afterend",
`<${config.iconType} class="sp-icon-container" data-sp-link="${href}">${config.iconOpen}</${config.iconType}>`,
);
});
/**
* The currently active preview anchor.
*
* @type {Element|null}
*/
var current_anchor = null;
/**
* Handles click events on a specific element.
*
* @this {HTMLElement} - The element that was clicked.
*/
let click_function = function () {
// If a preview is already active, we need to close it
if (current_anchor !== null) {
// We need to remove the preview div including the iframe,
// otherwise chromium based browser handle strange and do not reload the iframe correctly.
// They just go to the top of the page and stay there forever.
document.querySelectorAll("#sp_overlay").forEach((e) => {
if (e instanceof HTMLElement) {
e.style.display = "none";
}
e.remove();
});
current_anchor.innerHTML = config.iconOpen;
if (current_anchor.isSameNode(this)) {
// if we have clicked on the same link again,
// we do not need to do anything else
current_anchor = null;
return;
}
}
current_anchor = this;
this.innerHTML = config.iconClose;
// add preview elements
let link_target = this.getAttribute("data-sp-link");
this.insertAdjacentHTML(
"beforeend",
`<div id="sp_overlay" class="sp-overlay"><div id="sp_modal" class="sp-modal"><iframe id="sp_preframe" class="sp-iframe" src="${link_target}" onload=scrollToContent(this)></iframe></div></div>`,
);
// stop click event propagation on the preview window,
// to prevent closing the preview if we resize it (treated as a click)
let preview = document.getElementById("sp_modal");
if (preview instanceof HTMLElement) {
preview.addEventListener("click", function (event) {
event.stopPropagation();
});
setPreviewPosition(preview, this, config);
}
// show iframe after some time, to hide load flickering
window.setTimeout(function () {
let overlay = document.getElementById("sp_overlay");
if (overlay instanceof HTMLElement) {
overlay.style.display = "block";
}
}, config.openDelay);
};
document
.querySelectorAll(`${config.iconType}.sp-icon-container`)
.forEach((e) => e.addEventListener("click", click_function));
for (let eventName of ["scroll", "resize"]) {
addEventListener(eventName, () => {
if (current_anchor instanceof HTMLElement) {
let preview = document.getElementById("sp_modal");
if (preview instanceof HTMLElement) {
setPreviewPosition(preview, current_anchor, config);
}
}
});
}
});
/**
* Set the position and size of the preview window.
*
* @param {HTMLElement} preview - The preview element.
* @param {HTMLElement} anchor - The anchor element.
* @param {Config} config - The preview width will be the min of this and the window width.
*/
function setPreviewPosition(preview, anchor, config) {
// compute required link and window data
let position_anchor = {
left: anchor.offsetLeft - window.scrollX,
top: anchor.offsetTop - window.scrollY,
};
// set iframe position relative to link,
// by default anchoring below and right of it
let pos_left = anchor.offsetLeft + config.offset.left;
let pos_screen_left = position_anchor.left + config.offset.left;
let pos_screen_top = position_anchor.top + config.offset.top;
let width = config.width;
let height = config.height;
// ensure width is not bigger than window width
if (width > window.innerWidth) {
width = window.innerWidth;
}
// anchor left of link if not enough space on right
if (pos_screen_left + width + 50 > window.innerWidth) {
pos_left = window.innerWidth - width - 50;
}
if (pos_left < 0) {
pos_left = 50;
}
// ensure height is not bigger than window height
if (height > window.innerHeight) {
height = window.innerHeight;
}
// anchor above link if not enough space below,
// and if there is enough space above
if (
pos_screen_top + height > window.innerHeight &&
pos_screen_top - height - 10 > 0
) {
pos_screen_top = position_anchor.top - height - 10;
}
// set preview position and size via css
preview.style.width = width + "px";
preview.style.height = height + "px";
preview.style.top = pos_screen_top + "px";
preview.style.left = pos_left + "px";
preview.style.position = "fixed";
}
/**
* Activated on iframe load.
* It ensures that the iframe is scrolled to the target link position,
* since sometimes the browser does not handle this properly automatically.
* Note, this can be blocked by CORS (obtaining the iframe content),
* if the iframe is not on the same domain, or the page is not being served.
*
* @param {HTMLIFrameElement} iframe - The iframe that was loaded.
*/
function scrollToContent(iframe) {
var target = iframe.getAttribute("src");
if (target === null) {
console.warn(
`Sphinx Peek: Cannot get iframe src attribute for '${target}'`,
);
return;
}
var targetId = target.split("#")[1];
if (targetId !== undefined) {
setTimeout(function () {
try {
let contentWindow = iframe.contentWindow;
if (contentWindow === null) {
console.warn(
`Sphinx Peek: Cannot access iframe content window for '${target}'`,
);
return;
}
// try to get the target element by id
var element = contentWindow.document.getElementById(targetId);
} catch (e) {
// if we cannot access the iframe content, we cannot scroll to the target
console.warn(
`Sphinx Peek: Cannot access iframe content for '${target}': ${e}`,
);
return;
}
if (element) {
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView#behavior
element.scrollIntoView({ behavior: "instant" });
} else {
console.warn(`Sphinx Peek: Target does not exist: ${target}`);
// if target link doesn't exist, scroll to the top of the page
// iframe.contentWindow.scrollTo(0, 0);
}
}, 500);
}
}