/
presentation.html
292 lines (278 loc) · 14.8 KB
/
presentation.html
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!doctype HTML>
<!--
Web Presentation: a cross-platform replacement for all those office application packages in a single file :-)
Original publication:
https://www.codeproject.com/Articles/5286790/Web-Presentation
Copyright (c) Sergey A Kryukov, 2017, 2020-2021
http://www.SAKryukov.org
http://www.codeproject.com/Members/SAKryukov
https://github.com/SAKryukov
-->
<html lang="en-us">
<head>
<title>, Copyright © Sergey A Kryukov, 2020</title>
<style>
* { font-family: sans-serif; margin: 0; padding: 0; border: none; touch-action: none; user-select: none; }
html { height: 100%; }
body { overflow: hidden; height: 100%; display: block; justify-content: center; align-items: center; }
section, header {
display: none;
background-color: white; position: absolute;
padding: 1em; left: 1em; top: 1em; border: solid thin black;
font-size: 110%;
}
section > p { margin-top: 0; margin-bottom: 0; }
section > p:first-child { margin-bottom: 0.4em; }
section > li { padding-left: 1em; list-style-type: none; }
section > nav { display: none; } /* RTL direction switch table */
video { display: none; height: 60%; max-width: 90%; padding: 0; margin-left: auto; margin-right: auto; }
</style>
</head>
<body>
<header></header> <!-- errors -->
<section> <!-- help -->
<p><span></span> v. <span></span></p>
<p>F1: Toggle help (click to hide)</p>
<p>F11: Toggle full screen (default for most browsers)</p>
<p>→ ↓ space, click: Next</p>
<p>← ↑ backspace, Ctrl+click: Previous</p>
<p>Touchscreen swipe:</p>
<li>← ↑: Next</li>
<li>→ ↓: Previous</li>
<p>P: Toggle Play/Pause in video mode</p>
<p>S: <a id="repository" href="">Source code repository at GitHub</a></p>
<nav>→←</nav>
</section>
<video controls="controls" disablePictureInPicture="true">
<source/>
<p>Your Web browser does not support standard media content.<br/>Please use REAL Web browser instead.</p>
</video>
<script>
"use strict";
const image = source => source;
const video = (source, options) => {
if (!options) return { source: source, play: true };
const result = new Object(options);
result.source = source;
return result;
};
const definitionSet = {
version: "4.4.0",
productName: "Web Presentation",
repository: "https://github.com/SAKryukov/web-presentation"
};
const show = (element, doShow) => { element.style.display = doShow ? 'block' : 'none'; };
const toPixels = (size) => { return (size).toString() + "px"; };
const findIndirectParent = (element, elementToFind) => {
let current = element;
while (current && current != document.body) {
if (current == elementToFind) return elementToFind;
current = current.parentElement;
} //loop
return null;
}; // findIndirectParent
const textUtility = (() => {
document.title = definitionSet.productName + document.title;
let scriptError = false;
const scriptErrorTest = () => scriptError;
const textElement = document.querySelector("section");
const errorElement = document.querySelector("header");
const productNameElement = document.querySelector("section > p:first-child > span:first-child");
const productVersionElement = document.querySelector("section > p:first-child > span:last-child");
const repositoryAnchorElement = document.querySelector("#repository");
const showError = text => { errorElement.style.display = "block"; errorElement.textContent = text; }
const setPresentationScriptError = scriptPath => {
window.onerror = () => {
scriptError = true;
showError(`Syntax error in ${scriptPath}`);
}; //window.onError
}; //setPresentationScriptError
const toggleHelp = (() => {
let helpActive = false;
return () => {
helpActive = !helpActive;
textElement.style.display = helpActive ? "block" : "none";
};
})();
const closeBoxElement = ((size, background, border, stroke) => {
const ns = "http://www.w3.org/2000/svg";
const createNS = (name) => document.createElementNS(ns, name);
const svg = createNS("svg");
svg.style.cssText = `background-color: ${background}; stroke: ${stroke}; stroke-width: 0.1;
border-left: ${border} thin solid; border-bottom: ${border} thin solid; width: ${size}px; position: absolute; top: 0; right: 0`;
svg.setAttribute("viewBox", "0 0 1 1");
svg.innerHTML = `<g><line x1="0.2" y1="0.2" x2="0.8" y2="0.8"/><line x1="0.2" y1="0.8" x2="0.8" y2="0.2"/></g>`;
return svg;
})(16, "yellow", "transparent", "red"); //closeBoxElement
const setupHelp = isRtl => {
productNameElement.textContent = definitionSet.productName;
productVersionElement.textContent = definitionSet.version;
repositoryAnchorElement.href = definitionSet.repository;
textElement.appendChild(closeBoxElement);
if (!isRtl) return;
const replaceMap = new Map();
const replacePairs = document.querySelectorAll("section > nav");
for (let element of replacePairs) {
replaceMap.set(element.textContent[0], element.textContent[1]);
replaceMap.set(element.textContent[1], element.textContent[0]);
}
for (let child of textElement.children) {
if (child.constructor == HTMLLIElement || child.constructor == HTMLParagraphElement) {
let newContent = "";
for (let index = 0; index < child.textContent.length; ++index) {
const replacement = replaceMap.get(child.textContent[index]);
newContent += replacement ? replacement : child.textContent[index];
} //index loop
child.textContent = newContent;
} //if
} //child loop
}; //setupHelp
const setColors = colors => {
if (!colors.text) return;
for (let element of [textElement, errorElement]) {
if (colors.text.background) element.style.backgroundColor = colors.text.background;
if (colors.text.foreground) element.style.color = colors.text.foreground;
if (colors.text.border) element.style.borderColor = colors.text.border;
} //loop
if (colors.text.border)
closeBoxElement.style.borderColor = colors.text.border;
}; //setColors
return {
setPresentationScriptError: setPresentationScriptError,
textElement: textElement, setupHelp: setupHelp, showError: showError, toggleHelp: toggleHelp,
setColors: setColors,
scriptErrorTest: scriptErrorTest };
})(); //textUtility
(() => { //presentation setup script
const script = document.createElement("script");
const searchUrl = window.location.search;
if (!searchUrl || searchUrl.length < 2 || searchUrl[0] != "?")
throw textUtility.showError("Presentation file needs to be specified in the URL in the form ...presentation.html?path-to/presentation.js");
const scriptPath = searchUrl.slice(1);
textUtility.setPresentationScriptError(scriptPath);
script.src = scriptPath;
document.head.appendChild(script);
})(); //presentation setup script
function initializeViewer(image, video, videoSource, presentation) {
let current = 0;
const resize = image => {
const imageAspect = image.naturalWidth / image.naturalHeight;
const windowAspect = window.innerWidth / window.innerHeight;
if (imageAspect > windowAspect) {
image.width = window.innerWidth;
image.height = image.width / imageAspect;
image.style.marginLeft = 0;
image.style.marginTop = toPixels((window.innerHeight - image.height) / 2);
} else {
image.height = window.innerHeight;
image.width = image.height * imageAspect;
image.style.marginTop = 0;
image.style.marginLeft = toPixels((window.innerWidth - image.width) / 2);
} //if
}; //resize
const move = backward => { //backward true <= backward, backward false => forward, else initialization
video.pause();
document.exitFullscreen();
videoSource.src = undefined;
image.src = undefined;
if (backward != undefined) {
if (backward)
if (current > 0) --current; else current = presentation.images.length - 1;
else
if (current < presentation.images.length - 1) ++current; else current = 0;
} //if
const item = presentation.images[current];
let isVideo = item.constructor != String;
document.body.style.display = isVideo ? "flex" : "block";
if (isVideo) {
if (item.source) {
video.poster = item.poster;
video.title = item.title;
videoSource.src = item.source;
video.onplay = event => event.target.requestFullscreen();
video.onended = event => document.exitFullscreen();
video.load();
if (item.play)
video.play();
} else
video.title = "Video file not specified";
} else {
image.src = item;
resize(image);
} //if
show(video, isVideo);
show(image, !isVideo);
}; //move
move();
image.onload = event => { resize(event.target); };
window.onresize = () => resize(image);
document.body.onclick = event => {
if (event.target.constructor != HTMLAnchorElement) {
if (findIndirectParent(event.target, textUtility.textElement) == textUtility.textElement) {
textUtility.toggleHelp();
event.preventDefault();
} else
move(event.ctrlKey);
} //if
}; //window.onclick
document.body.onkeydown = event => {
switch (event.code) {
case "Space":
case "ArrowDown": move(false); break;
case "Backspace":
case "ArrowUp": move(true); break;
case "ArrowRight": move(presentation.rtl); break;
case "ArrowLeft": move(!presentation.rtl); break;
case "F11": document.requestFullscreen(); event.preventDefault(); break;
case "F1": textUtility.toggleHelp(); event.preventDefault(); break;
case "KeyS": window.location = definitionSet.repository;
case "KeyP":
if (videoSource.src)
if (video.paused) video.play(); else video.pause();
}
}; //document.body.onkeydown
let touchStart = undefined;
addEventListener("touchstart", event => {
touchStart = { x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY };
}, false);
addEventListener("touchend", event => { touchStart = undefined; }, false);
addEventListener("touchmove", event => {
if (touchStart == undefined) return;
const vector = { x: event.changedTouches[0].clientX - touchStart.x, y: event.changedTouches[0].clientY - touchStart.y };
const horizontal = Math.abs(vector.x) > Math.abs(vector.y);
let back = horizontal ? vector.x > 0 : vector.y > 0;
if (horizontal && presentation.rtl) back = !back;
move(back);
touchStart = undefined;
}, false);
}; //initializeViewer
document.body.onload = () => {
window.onerror = undefined;
if (textUtility.scriptErrorTest()) return;
if (typeof (presentation) == typeof (undefined) || !presentation)
return textUtility.showError("presentation not found");
if (!presentation.hideHelpOnStart)
textUtility.toggleHelp();
textUtility.setupHelp(presentation.rtl);
const image = document.createElement("img");
const video = document.querySelector("video");
video.disablePictureInPicture = true;
const videoSource = document.querySelector("video > source");
const setBackground = color => {
document.body.style.backgroundColor = color;
image.style.backgroundColor = color;
}; //setBackground
const setColors = colors => {
if (!colors) return;
if (colors.background) setBackground(colors.background);
textUtility.setColors(colors);
} //colors
if (!presentation.images || !presentation.images.length)
return textUtility.showError("presentation.images not found");
if (presentation.title) document.title = presentation.title;
setColors(presentation.colors);
document.body.appendChild(image);
initializeViewer(image, video, videoSource, presentation);
}; //document.body.onload
</script>
</body></html>