This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
98 lines (91 loc) · 2.69 KB
/
main.ts
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
document.addEventListener('DOMContentLoaded', () => {
// Page d'accueil
document.querySelector('span[data-target="#1"]').addEventListener('click', onLink);
actualise();
});
// Réecrire le pull-to-reload pour recommencer l'aventure
let _startY;
document.body.addEventListener('touchstart', e => {
_startY = e.touches[0].pageY;
}, {passive: true});
document.body.addEventListener('touchmove', e => {
const y = e.touches[0].pageY;
if (document.scrollingElement.scrollTop === 0 && y > _startY) {
document.location.href = "/";
}
}, {passive: true});
document.onkeyup = (e: KeyboardEvent) => {
// f5, backspace, escape
if ([116, 8, 27].indexOf(e.keyCode) !== -1) {
e.preventDefault();
document.location.href = "/";
}
}
// Contenu de la page
function actualise(precise?: number) {
let hash = precise || window.location.hash.split('#')[1] || -1;
let maintext: Element = document.querySelector('.maintext');
let choices: Element = document.querySelector('.links');
if (hash !== -1)
req('/chapitres/chapitre'+hash+'.json', (chapitre: Chapitre) => {
if (chapitre.err === undefined) {
hideOrShow('.card > *');
setTimeout(() => {
if (chapitre.links.length === 0) {
maintext.classList.toggle('theEnd', true);
} else {
maintext.classList.toggle('theEnd', false);
}
maintext.innerHTML = chapitre.txt;
choices.innerHTML = "";
for (let link of chapitre.links) {
let span: Element = document.createElement('span');
span.innerHTML = link.txt.replace(/([0-9]+)$/, '<em>$1</em>');
span.setAttribute('data-target', link.link);
span.addEventListener('click', onLink);
choices.appendChild(span);
}
hideOrShow('.card > *');
}, 250);
}
});
}
// Naviguer dans le jeu
function onLink() {
window.location.hash = this.getAttribute('data-target');
actualise();
}
// Afficher ou masquer un élément
function hideOrShow(sel: string) {
let all = document.querySelectorAll(sel);
all.forEach((el: Element) => {
el.classList.toggle('hide');
});
}
// Requête GET / POST
function req(url: string, callback: Function) {
let req = new XMLHttpRequest();
req.open('GET', url);
req.onerror = function() {
callback({err:"Erreur de chargement",dataerr:url});
}
req.onload = function() {
if (req.status === 200) {
let data = JSON.parse(req.responseText);
callback(data);
} else {
callback({err:"Erreur",dataerr:req.status});
}
};
req.send();
}
class Link {
link: string;
txt: string;
}
class Chapitre {
links: Array<Link>;
txt: string;
err: string;
dataerr: string;
}