-
Notifications
You must be signed in to change notification settings - Fork 1
/
muntineer.js
225 lines (182 loc) · 6.75 KB
/
muntineer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// This file is part of muntineer, which visualizes window-pane algebra.
//
// Copyright (c) 2021 Eliah Kagan
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
(function() {
'use strict';
const CH = Object.freeze({
HAIRSP: '\u200a',
NDASH: '\u2013',
RSQUO: '\u2019',
});
const COLORS = Object.freeze({
CASING: '#414a4c',
MUNTIN: '#414a4c', // For now, casing and muntin have the same color.
PANE: '#d7ecff',
});
const SCALABLE_PARAMETERS = Object.freeze([
'totalHeight',
'totalWidth',
'casing',
'muntin',
'paneWidth',
'paneHeight',
]);
const inputs = {};
let drawing = null;
function getNumberKindValidator(mustBeInteger) {
return mustBeInteger ? Number.isSafeInteger : Number.isFinite;
}
function isValid(value, mustBeInteger = false) {
return getNumberKindValidator(mustBeInteger)(value) && value > 0;
}
function parse(field) {
const value = Number(field.value);
return isValid(value, field.id === 'panes') ? value : NaN;
}
function setBadness(element, isBad) {
if (isBad) {
element.classList.add('bad');
} else {
element.classList.remove('bad');
}
}
function setParentBadness(element, parentIsBad) {
setBadness(element.parentElement, parentIsBad);
}
function updateInput(field) {
const value = parse(field);
inputs[field.id] = value;
setParentBadness(field, Number.isNaN(value));
}
function updateResult() {
inputs.paneWidth =
(inputs.totalWidth
- 2 * inputs.casing
+ (1 - inputs.panes) * inputs.muntin) / inputs.panes;
const paneWidthField = document.getElementById('paneWidth');
setBadness(paneWidthField, !isValid(inputs.paneWidth));
paneWidthField.textContent = (Number.isNaN(inputs.paneWidth)
? `?${CH.HAIRSP}?${CH.HAIRSP}?`
: inputs.paneWidth.toFixed(4));
}
function getScaledDrawingParameters() {
const params = {};
for (const name of SCALABLE_PARAMETERS) {
params[name] = inputs[name] * inputs.scale;
}
return params;
}
function resetDrawing(scaled) {
if (drawing === null) {
drawing = SVG().addTo('#drawing-div');
} else {
drawing.clear();
}
drawing.size(scaled.totalWidth, scaled.totalHeight);
}
function populateDrawing(scaled) {
drawing.rect('100%', '100%').fill(COLORS.CASING);
drawing.rect(scaled.totalWidth - scaled.casing * 2,
scaled.totalHeight - scaled.casing * 2)
.move(scaled.casing, scaled.casing)
.fill(COLORS.MUNTIN);
const stride = scaled.paneWidth + scaled.muntin;
for (let i = 0; i < inputs.panes; ++i) {
drawing.rect(scaled.paneWidth, scaled.paneHeight)
.move(scaled.casing + i * stride, scaled.casing)
.fill(COLORS.PANE);
}
}
// Returns true if the drawing could be updated, false otherwise.
function updateDrawing() {
inputs.paneHeight = inputs.totalHeight - 2 * inputs.casing;
if (!isValid(inputs.paneHeight)) {
if (!Number.isNaN(inputs.paneHeight)) {
// The computed pane height is invalid, but the given casing
// width is allowed. So the given total height is the problem.
setParentBadness(document.getElementById('totalHeight'), true);
}
return false;
}
setParentBadness(document.getElementById('totalHeight'), false);
if (Number.isNaN(inputs.scale) || !isValid(inputs.paneWidth)) {
return false;
}
const scaled = getScaledDrawingParameters();
resetDrawing(scaled);
populateDrawing(scaled);
return true;
}
function getBadFieldShortNames() {
return [...document.getElementsByTagName('fieldset')]
.filter(fieldset => fieldset.classList.contains('bad'))
.map(fieldset => fieldset.dataset.shortname);
}
function asHumanReadableList(phrases) {
switch (phrases.length) {
case 0:
throw "Can't make a human-readable list of zero phrases.";
case 1:
return phrases[0];
case 2:
return phrases.join(' and ');
default:
return `${phrases.slice(0, -1).join(', ')}, `
+ `and ${phrases[phrases.length - 1]}`;
}
}
function buildErrorDetail() {
const shortnames = getBadFieldShortNames();
if (shortnames.length > 0) {
return `Check ${asHumanReadableList(shortnames)}`;
} else if (inputs.paneWidth <= 0) {
return 'Need $p > 0$';
} else {
return `No details ${CH.NDASH} this is a bug`;
}
}
function buildErrorMessage() {
const prefix = `Can${CH.RSQUO}t draw window: ${buildErrorDetail()}.`;
return drawing === null ? prefix : prefix + ' (Old drawing shown.)';
}
function updateOutput() {
updateResult();
const status = document.getElementById('statusMessage');
if (updateDrawing()) {
setBadness(drawing.node, false);
setBadness(status, false);
status.textContent = 'OK';
status.title =
'The current inputs were illustrated with no errors.';
} else {
if (drawing !== null) {
setBadness(drawing.node, true);
}
setBadness(status, true);
status.textContent = buildErrorMessage();
renderFormulas(status);
status.title = 'An error occurred. The current inputs could not'
+ ' be illustrated.';
}
}
function handleInput(e) {
updateInput(e.target);
updateOutput();
}
for (const field of document.getElementsByTagName('input')) {
field.addEventListener('input', handleInput);
updateInput(field);
}
updateOutput();
})();