-
Notifications
You must be signed in to change notification settings - Fork 9
/
hubnav.js
143 lines (120 loc) · 4.45 KB
/
hubnav.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
(function hubnavMacro(renderLinkWrapper) {
'use strict';
/* globals version, Story, Config, Macro, passage, Wikifier, State */
const macroName = 'hubnav';
if (!version || !version.title || 'SugarCube' !== version.title || !version.major || version.major < 2) {
throw new Error(`<<${macroName}>> macros family requires SugarCube 2.0 or greater, aborting load`);
}
renderLinkWrapper = renderLinkWrapper || function(link) {
const span = link.wrap('<span>');
span.append('<br/>');
return span;
};
function has(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function parseLinkArg(arg) {
let passage;
let $content;
if (arg.isImage) {
// Argument was in wiki image syntax.
$content = jQuery(document.createElement('img'))
.attr('src', arg.source);
if (has(arg, 'passage')) {
$content.attr('data-passage', arg.passage);
}
if (has(arg,'title')) {
$content.attr('title', arg.title);
}
if (has(arg, 'align')) {
$content.attr('align', arg.align);
}
passage = arg.link;
} else {
// Argument was in wiki link syntax.
$content = jQuery(document.createTextNode(arg.text));
passage = arg.link;
}
return {
passage,
$content,
setFn: arg.setFn,
};
}
function createLink({passage, $content, setFn}) {
const $link = jQuery(Wikifier.createInternalLink(
null,
passage,
null,
((passage, fn) => () => {
if (typeof fn === 'function') {
fn();
}
})(passage, setFn)
))
.addClass(`macro-${macroName}`)
.append($content);
if (passage != null) { // lazy equality for null
$link.attr('data-passage', passage);
if (Story.has(passage)) {
$link.addClass('link-internal');
if (Config.addVisitedLinkClass && State.hasPlayed(passage)) {
$link.addClass('link-visited');
}
} else {
$link.addClass('link-broken');
}
} else {
$link.addClass('link-internal');
}
return $link;
}
function isLink(arg) {
return arg && jQuery.isPlainObject(arg) && ('link' in arg);
}
Macro.add(macroName, {
handler() {
if (this.args.length === 0) {
return this.error(`no ${macroName} links specified`);
}
const currentPassage = passage();
const links = [];
if (this.args.length === 1 && typeof this.args[0] === 'string') {
// single string argument: find all passages with given tag and use them as navigation
const passages = Story.lookupWith(p => p.tags.includes(this.args[0]))
.map(p => p.title);
for (const passageTitle of passages) {
if (passageTitle !== currentPassage) {
links.push({
$content: jQuery(document.createTextNode(passageTitle)),
passage: passageTitle,
});
}
}
} else {
for (let i = 0, len = this.args.length; i < len; i++) {
const arg = this.args[i];
if (isLink(arg)) {
const parsed = parseLinkArg(arg);
if (parsed.passage === currentPassage) {
continue;
}
const nextArg = this.args[i + 1];
if (i < len - 1 && !isLink(nextArg)) { // next is not a link
if (nextArg) {
links.push(parsed);
i++; // skip next round
}
} else {
links.push(parsed);
}
}
}
}
const $output = jQuery(this.output);
links.forEach((link) => {
$output.append(renderLinkWrapper(createLink(link)));
});
},
});
}());