-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvolumeButtons.js
119 lines (95 loc) · 2.77 KB
/
volumeButtons.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
(function volumeButton() {
'use strict';
// requires menuButton.js
/* globals scUtils, l10nStrings, storage */
function saveVolume(value) {
storage.set('masterVolume', value);
}
function loadVolume() {
const loaded = storage.get('masterVolume') || '1.0';
let value = parseFloat(loaded);
if (isNaN(value)) {
return 1;
} else {
return value;
}
}
function applyVolume(value) {
jQuery.wiki(`<<masteraudio volume ${value}>>`);
}
function saveMute(value) {
storage.set('masterMute', value);
}
function loadMute() {
const loaded = storage.get('masterMute') || 'false';
return loaded === 'true';
}
function applyMute(value) {
jQuery.wiki(`<<masteraudio ${value ? 'mute' : 'unmute'}>>`);
}
function clampVolume(volume) {
return parseFloat(
Math.min(
Math.max(volume, 0.0),
1.0
).toFixed(1)
);
}
function createVolumeButtons(interval = 0.2, labels = ['🔈', '🔇', '🔊']) {
let volume = loadVolume();
if (volume !== 1.0) {
applyVolume(volume);
saveVolume(volume);
}
let mute = loadMute();
if (mute !== true) {
applyMute(mute);
saveMute(mute);
}
const ops = {
dec() {
volume = clampVolume(volume - interval);
},
mute() {
mute = !mute;
},
inc() {
volume = clampVolume(volume + interval);
},
};
function updateUI(button) {
button.find('a').removeAttr('disabled');
if (volume === 0) {
button.find('a:eq(0)').attr('disabled', true);
} else if (volume === 1) {
button.find('a:eq(2)').attr('disabled', true);
}
if (mute) {
button.find('a:eq(1)').addClass('active');
} else {
button.find('a:eq(1)').removeClass('active');
}
}
const {button} = scUtils.createMultiButton('fontSize', l10nStrings.uiVolumeControl || 'Volume', labels, (event, index) => {
if (index === 0) {
ops.dec();
} else if (index === 1) {
ops.mute();
} else {
ops.inc();
}
updateUI(button);
applyVolume(volume);
saveVolume(volume);
applyMute(mute);
saveMute(mute);
});
updateUI(button);
}
window.scUtils = Object.assign(
window.scUtils || {},
{
createVolumeButtons,
},
);
}());