-
Notifications
You must be signed in to change notification settings - Fork 465
/
settings.js
147 lines (123 loc) · 4.17 KB
/
settings.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
144
145
146
147
import { createNanoEvents } from 'nanoevents';
import { domReady } from '../utils.js';
import MaterialSlider from './material-slider.js';
import Ripple from './ripple.js';
export default class Settings {
constructor() {
this.emitter = createNanoEvents();
this._throttleTimeout = null;
domReady.then(() => {
this._pluginInputs = [
...document.querySelectorAll('.settings .plugins input'),
];
this._globalInputs = [
...document.querySelectorAll('.settings .global input'),
];
this._resetRipple = new Ripple();
this._resetBtn = document.querySelector('.setting-reset');
this._resetBtn.append(this._resetRipple.container);
// map real range elements to Slider instances
this._sliderMap = new WeakMap();
// enhance ranges
const ranges = document.querySelectorAll('.settings input[type=range]');
for (const range of ranges) {
this._sliderMap.set(range, new MaterialSlider(range));
}
this.container = document.querySelector('.settings');
this._scroller = document.querySelector('.settings-scroller');
this.container.addEventListener('input', (error) =>
this._onChange(error)
);
this._scroller.addEventListener('wheel', (error) =>
this._onMouseWheel(error)
);
this._resetBtn.addEventListener('click', (error) => this._onReset(error));
// Stop double-tap text selection.
// This stops all text selection which is kinda sad.
// I think this code will bite me.
this._scroller.addEventListener('mousedown', (event) => {
if (event.target.closest('input[type=range]')) return;
event.preventDefault();
});
});
}
_onMouseWheel(event) {
// Prevents bounce effect on desktop.
// Firefox uses DELTA_LINE on a mouse wheel, ignore it
// 0 is "pixels"
if (!event.deltaMode) {
event.preventDefault();
event.currentTarget.scrollTop += event.deltaY;
}
}
_onChange(event) {
clearTimeout(this._throttleTimeout);
// throttle range
if (event.target.type === 'range') {
this._throttleTimeout = setTimeout(
() => this.emitter.emit('change'),
150
);
} else {
this.emitter.emit('change');
}
}
_onReset() {
this._resetRipple.animate();
const oldSettings = this.getSettings();
// Set all inputs according to their initial attributes
for (const inputEl of this._globalInputs) {
if (inputEl.type === 'checkbox') {
inputEl.checked = inputEl.hasAttribute('checked');
} else if (inputEl.type === 'range') {
this._sliderMap.get(inputEl).value = inputEl.getAttribute('value');
}
}
for (const inputEl of this._pluginInputs) {
inputEl.checked = inputEl.hasAttribute('checked');
}
this.emitter.emit('reset', oldSettings);
this.emitter.emit('change');
}
setSettings(settings) {
for (const inputEl of this._globalInputs) {
if (!(inputEl.name in settings)) continue;
if (inputEl.type === 'checkbox') {
inputEl.checked = settings[inputEl.name];
} else if (inputEl.type === 'range') {
this._sliderMap.get(inputEl).value = settings[inputEl.name];
}
}
for (const inputEl of this._pluginInputs) {
if (!(inputEl.name in settings.plugins)) continue;
inputEl.checked = settings.plugins[inputEl.name];
}
}
getSettings() {
// fingerprint is used for cache lookups
const fingerprint = [];
const output = {
plugins: {},
};
for (const inputEl of this._globalInputs) {
if (inputEl.name !== 'gzip' && inputEl.name !== 'original') {
if (inputEl.type === 'checkbox') {
fingerprint.push(Number(inputEl.checked));
} else {
fingerprint.push(`|${inputEl.value}|`);
}
}
if (inputEl.type === 'checkbox') {
output[inputEl.name] = inputEl.checked;
} else {
output[inputEl.name] = inputEl.value;
}
}
for (const inputEl of this._pluginInputs) {
fingerprint.push(Number(inputEl.checked));
output.plugins[inputEl.name] = inputEl.checked;
}
output.fingerprint = fingerprint.join(',');
return output;
}
}