-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgenderswitch.js
66 lines (55 loc) · 2.3 KB
/
genderswitch.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
(function genderSwitchMacros() {
// usage:
// My name is <<genderswitch $isFemale "Mary" "John">> Watson.
// ...
// Your father says: My dear <<gender "daughter" "son">>!
'use strict';
/* globals version, Macro, State */
if (!version || !version.title || 'SugarCube' !== version.title || !version.major || version.major < 2) {
throw new Error('<<gender*>> macros family requires SugarCube 2.0 or greater, aborting load');
}
version.extensions.genderswitch = {major: 1, minor: 0, revision: 0};
const clsPrefix = 'gender';
const styles = `
html.${clsPrefix}-f .${clsPrefix}-m,
html.${clsPrefix}-m .${clsPrefix}-f {
display: none;
}
.${clsPrefix}switch-macro {
border-bottom: 1px dotted;
text-decoration: none;
}
.${clsPrefix}switch-macro:hover, .${clsPrefix}switch-macro:active {
border-bottom: 1px solid;
text-decoration: none;
}`;
jQuery('head').append(`<style type="text/css" id="${clsPrefix}-style">${styles}</style>`);
const html = document.documentElement;
html.classList.add(`${clsPrefix}-f`);
function getLayout(female, male) {
return `<span class="${clsPrefix}-f">${female}</span><span class="${clsPrefix}-m">${male}</span>`;
}
Macro.add('genderswitch', {
handler () {
if (this.args.full.length === 0) {
return this.error('no variable and values specified');
}
const varName = this.args.full.split(' ')[0].replace(/^State\.variables\./, '');
const layout = getLayout(this.args[1], this.args[2]);
const link = jQuery(`<a class="${clsPrefix}switch-macro">${layout}</a>`);
link.appendTo(this.output);
link.ariaClick(() => {
html.classList.toggle(`${clsPrefix}-f`);
html.classList.toggle(`${clsPrefix}-m`);
State.variables[varName] = html.classList.contains(`${clsPrefix}-f`);
});
},
});
Macro.add('gender', {
handler () {
const layout = getLayout(this.args[0], this.args[1]);
const wrapper = jQuery(`<span class="${clsPrefix}-macro">${layout}</span>`);
wrapper.appendTo(this.output);
},
});
}());