-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathskipIntro.js
70 lines (58 loc) · 2.14 KB
/
skipIntro.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
(function skipIntroFactory() {
'use strict';
/* globals visited, storage, l10nStrings, Engine */
function addStyles() {
const styles = `
p.skipIntro {
text-align: right;
margin-top: 3em;
font-size: 80%;
}
`;
const $style = jQuery(`<style type="text/css" id="skip-intro">${styles}</style>`);
$style.appendTo('head');
}
function prepareLink(firstNonIntroPassage, label) {
const $p = jQuery(`<p class="skipIntro"><a class="link-internal" tabindex="0">${label}</a></p>`);
$p.on('click', 'a', () => Engine.play(firstNonIntroPassage));
return $p;
}
function skipPassageFactory(skipPassages, skipTags) {
return function skipPassageDetector(passage) {
return skipPassages.includes(passage.title) || skipTags.includesAny(passage.tags);
};
}
function skipIntro(firstNonIntroPassage, label = l10nStrings.uiSkipIntro || 'Skip intro', skipPassages = [], skipTags = []) {
const $doc = jQuery(document);
const skipPassageDetector = skipPassageFactory(skipPassages, skipTags);
let $p;
function onPassageDisplay(event) {
if (visited(firstNonIntroPassage) === 0) {
if (!skipPassageDetector(event.passage, skipPassages, skipTags)) {
$p.appendTo(event.content);
}
} else {
$doc.off(':passagedisplay', onPassageDisplay);
}
}
function onPassageDisplayMaiden() {
if (visited(firstNonIntroPassage) === 1) {
storage.set('skipIntro', true);
$doc.off(':passagedisplay', onPassageDisplayMaiden);
}
}
if (storage.get('skipIntro')) {
addStyles();
$p = prepareLink(firstNonIntroPassage, label);
$doc.on(':passagedisplay', onPassageDisplay);
} else {
$doc.on(':passagedisplay', onPassageDisplayMaiden);
}
}
window.scUtils = Object.assign(
window.scUtils || {},
{
skipIntro,
}
);
}());