Repository navigation
Expand file tree
/
Copy pathrange-finder.js
More file actions
124 lines (102 loc) · 3.68 KB
/
Copy pathrange-finder.js
File metadata and controls
124 lines (102 loc) · 3.68 KB
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
Fudgel.component('range-finder', {
attr: ['query', 'decimals', 'unit'],
template: `@media
<span *if="invalid">({{query}}) /* {{invalid}} */</span>
<span *if="!invalid">
<span *if="!exact && min === max">
(min-{{query}}: {{min}}{{unit}}) and (max-{{query}}: {{max}}{{unit}}) /* Actual value is within this range */
</span>
<span *if="!exact && min !== max">
({{query}}: {{min}}{{unit}}) and ({{query}}: {{max}}{{unit}}) /* Exact queries match a range */
</span>
<span *if="exact">
({{query}}: {{min}}{{unit}})
</span>
</span>`
}, class {
constructor() {
this.decimalsNum = 0;
this.min = 0;
this.max = 0;
this.removeListeners = [];
}
onChange(propName) {
if (propName !== 'query' && propName !== 'decimals' && propName !== 'unit') {
return;
}
if (!this.query || this.decimals === undefined || this.unit === undefined) {
return;
}
this.decimalsNum = +this.decimals;
this.invalid = false;
if (!this.match('min-', 0)) {
this.invalid = `min-${this.query}: 0 failed to match`;
return;
}
this.recalculate();
}
recalculate() {
// Remove old listeners
for (const removeListener of this.removeListeners) {
removeListener();
}
// Scan and find the edge for maximum using a base-10 search.
let min = 0;
let max = 1;
const minSize = Math.pow(10, -this.decimalsNum);
// Grow while it does not match
while (
!this.match('max-', max) &&
max < Number.MAX_SAFE_INTEGER
) {
max *= 10;
}
if (max >= Number.MAX_SAFE_INTEGER) {
this.invalid = `max-${this.query} appears infinite`;
return;
}
// Shrink
let step = Math.pow(10, Math.floor(Math.log10(max)));
const initialStep = step;
while (step >= minSize) {
while (this.match('max-', max - step)) {
max -= step;
}
step /= 10;
}
// We now have a border where going smaller does not match.
this.min = this.toFixed(max);
// Scan larger to find the maximum
step = initialStep;
while (step >= minSize) {
while (this.match('min-', max + step)) {
max += step;
}
step /= 10;
}
this.max = this.toFixed(max);
this.exact = this.min === this.max && this.match('', max);;
// Finally, set up listeners.
const notify = () => this.recalculate();
const withinQueryList = window.matchMedia(this.matchQuery('', max))
withinQueryList.addEventListener('change', notify);
const beyondQueryList = window.matchMedia(this.matchQuery('', max + minSize))
beyondQueryList.addEventListener('change', notify);
this.removeListeners.push(() => withinQueryList.removeEventListener('change', notify));
this.removeListeners.push(() => beyondQueryList.removeEventListener('change', notify));
}
match(type, value) {
const query = this.matchQuery(type, value);
//console.log(query, window.matchMedia(query).matches);
return window.matchMedia(query).matches;
}
matchQuery(type, value) {
return `(${type}${this.query}: ${this.toFixed(value)}${this.unit})`;
}
toFixed(value) {
if (this.decimalsNum) {
return value.toFixed(this.decimalsNum).replace(/\.?0+$/, '');
}
return `${value}`;
}
});