dechatter 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +167 -0
- package/README.md +93 -19
- package/package.json +3 -1
- package/web/dechatter.zip +0 -0
- package/web/engine.js +3 -2
- package/web/index.html +15 -3
- package/web/stepper.js +402 -0
- package/web/units.js +43 -0
- package/web/widget.css +15 -1
- package/web/widget.js +613 -208
- package/web/wizard.js +210 -47
package/web/wizard.js
CHANGED
|
@@ -1,22 +1,74 @@
|
|
|
1
1
|
// Setup wizard: the session's setup as five short forms. Values are kept in millimetres, as
|
|
2
2
|
// the setup file has them; on a machine in inches they are only shown and typed in inches.
|
|
3
|
+
// Each step shows what has to be answered about this machine, tool and stock. Fields in the
|
|
4
|
+
// 'advanced' section (the default is 'basic') have a sensible default or computed value and
|
|
5
|
+
// sit in a collapsed section.
|
|
6
|
+
|
|
7
|
+
import { MM_PER_INCH, tidy } from './units.js';
|
|
3
8
|
|
|
4
|
-
const MM_PER_INCH = 25.4;
|
|
5
9
|
const LENGTH_UNITS = {
|
|
6
10
|
length: { mm: 'mm', in: 'in' },
|
|
7
11
|
lengths: { mm: 'mm', in: 'in' },
|
|
8
12
|
feed: { mm: 'mm/min', in: 'in/min' },
|
|
9
13
|
};
|
|
10
14
|
|
|
15
|
+
// The longer explanation behind each field's (i) button. An entry is text, or a function of
|
|
16
|
+
// the units lengths are shown in. Every field has one.
|
|
17
|
+
const SPEED_RANGE = 'The speeds this machine really runs at. Test speeds stay inside them. They only bound the search, so they may change after cuts.';
|
|
18
|
+
const FIXED = 'Fixed once a cut is made.';
|
|
19
|
+
const FREE = 'Safe to change after cuts.';
|
|
20
|
+
const INFO = {
|
|
21
|
+
'machine.name': 'A label for your own records. It changes nothing else.',
|
|
22
|
+
'machine.max_rpm': SPEED_RANGE,
|
|
23
|
+
'machine.min_rpm': SPEED_RANGE,
|
|
24
|
+
'machine.spindle_power_hp': `From the motor plate. It only sets the power cap, which leaves out cuts that would remove metal faster than the spindle can drive. ${FREE}`,
|
|
25
|
+
'machine.weight_kg': `A lighter machine starts with a lighter chip load: under 150 kg ×0.5, under 400 kg ×0.7, under 1500 kg ×0.85. ${FIXED} It sets the default chip load.`,
|
|
26
|
+
'machine.spindle': `"By hand": the program stops (M0) before each new speed and the widget shows the rpm to set. The detector allows for the spindle being up to 5% off. Cuts are grouped by speed, so there is one stop per speed. ${FIXED}`,
|
|
27
|
+
'tool.diameter_mm': `Sets the default chip load and the widest cut tried (half the diameter), and with the cutter material the top speed. ${FIXED}`,
|
|
28
|
+
'tool.flutes': `Feed = chip load × flutes × rpm, and the detector listens for the tooth-passing frequency. ${FIXED}`,
|
|
29
|
+
'tool.stickout_mm': 'From the holder face to the cutter tip. Chatter depends strongly on it, so the results apply only at this stickout. Change it and start a new session.',
|
|
30
|
+
'tool.holder': `Part of the tool assembly: it changes how the tool vibrates. ${FIXED}`,
|
|
31
|
+
'tool.material': `Sets the top speed: high-speed steel gets 0.3× the carbide surface speed for the work material. ${FIXED}`,
|
|
32
|
+
'cut.material': `Sets the default chip load, the top surface speed and the power each cut needs. ${FIXED}`,
|
|
33
|
+
'cut.axial_depth_mm': `The deepest cut to test, from the top of the stock. The face must be clear of jaws and parallels down past this depth, along its whole length and about one cutter radius plus the clearance beyond each end, where the cutter plunges. ${FIXED}`,
|
|
34
|
+
'cut.other_depths_mm': 'Tested after the deepest depth settles, deepest first. A shallower round leaves the face flat only down to its own depth, so a deeper one after it needs fresh stock.',
|
|
35
|
+
'cut.direction': `Conventional or climb. The results apply to the direction tested. ${FIXED}`,
|
|
36
|
+
'cut.width_max_mm': `The widest radial cut tried. Default: half the cutter diameter. ${FREE}`,
|
|
37
|
+
'cut.chip_load_mm': `Feed per tooth, held constant for the whole session: feed = chip load × flutes × rpm. The default comes from the work material, the cutter diameter and the machine weight. Chatter barely depends on it, so the default is fine for mapping; the chip-load sweep offered once the map converges finds how far the best cut can be pushed. Very light chip loads make narrow cuts rub and squeal, which can pass for chatter. ${FIXED}`,
|
|
38
|
+
'cut.speed_min_rpm': `The slowest test speed. Default: a quarter of the fastest, and no lower than the machine allows. ${FREE}`,
|
|
39
|
+
'cut.speed_max_rpm': `The fastest test speed. Default: the machine's top speed or the material's surface-speed limit for this cutter, whichever is lower. ${FREE}`,
|
|
40
|
+
'stock.length_mm': `Along X. Every pass cuts the full length. Work zero X is the left end. ${FIXED}`,
|
|
41
|
+
'stock.width_mm': `Along Y: how much the test may cut back from the front face. Work zero Y is the front face as first mounted. ${FREE}`,
|
|
42
|
+
'program.coolant': `Coolant noise makes chatter harder to hear: none is best for detection. ${FIXED}`,
|
|
43
|
+
'program.dwell_s': `Before each cut the spindle runs in the air beside the stock, already at depth. That lets it reach speed and records the quiet sound each cut is compared with. Do not shorten it much. With a manual spindle, the time at each M0 stop comes on top. ${FIXED}`,
|
|
44
|
+
'program.clearance_mm': `The cutter plunges in the air this far beyond the end of the stock, and backs off this far from the face after each cut. The plunge must never touch the stock. ${FREE}`,
|
|
45
|
+
'program.safe_z_mm': (units) => `Height above the top of the stock (Z0) for every rapid move. Set it above the tallest clamp, jaw, parallel or stop the tool passes over, plus ${units === 'in' ? 'about 0.1 in' : 'a few mm'}, and within the Z travel left above your work zero. ${FREE}`,
|
|
46
|
+
'program.plunge_feed_mm_min': `The feed of the plunge from the clearance height down to depth, in the air beside the stock. ${FREE}`,
|
|
47
|
+
'program.truing_width_mm': `A light first pass that leaves the face flat, at a known position, before the test cuts. ${FREE}`,
|
|
48
|
+
'planner.round1_speeds': 'More speeds in round 1 find the stable pockets sooner, but take more cuts and more stock.',
|
|
49
|
+
'planner.round1_width_fractions': 'Round 1 cuts each speed at these widths, as fractions of the widest cut. Wider fractions learn more per cut but chatter more often.',
|
|
50
|
+
'planner.batch_size': 'More cuts per round means fewer rounds, but each round leans on guesses the earlier cuts of the same round have not confirmed.',
|
|
51
|
+
'planner.min_probability': 'Higher is more cautious: smaller steps, fewer cuts that chatter, more rounds. The defaults suit most sessions.',
|
|
52
|
+
'planner.probability_floor': 'The same, judged on measured cuts only, ignoring the round\'s own earlier cuts. Higher is more cautious.',
|
|
53
|
+
'planner.stop_improvement': 'Lower keeps searching for smaller gains, at the cost of more rounds.',
|
|
54
|
+
'planner.chip_load_sweep_low': 'Once the map converges, the best cut can be run again at 5 chip loads, lightest first, from this many times the session\'s chip load up to the next setting. The power cap trims the heaviest. The result is the highest chip load that stayed stable. Its cuts do not change the map.',
|
|
55
|
+
'planner.chip_load_sweep_high': 'The heaviest chip load of the sweep, as a multiple of the session\'s. Higher finds the limit of a stout tool, but the last passes may chatter hard: watch them, and feed-hold if a cut gets violent.',
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const infoFor = (field, units) => {
|
|
59
|
+
const text = INFO[field.at];
|
|
60
|
+
return typeof text === 'function' ? text(units) : text;
|
|
61
|
+
};
|
|
62
|
+
|
|
11
63
|
export const STEPS = [
|
|
12
64
|
{
|
|
13
65
|
title: 'Machine',
|
|
14
66
|
hint: 'The mill this tool runs in.',
|
|
15
67
|
fields: [
|
|
16
|
-
{ at: 'machine.name', label: 'Name', type: 'text', optional: true },
|
|
68
|
+
{ at: 'machine.name', section: 'advanced', label: 'Name', type: 'text', optional: true },
|
|
17
69
|
{ at: 'machine.max_rpm', label: 'Highest spindle speed', type: 'int', unit: 'rpm' },
|
|
18
70
|
{ at: 'machine.min_rpm', label: 'Lowest usable spindle speed', type: 'int', unit: 'rpm' },
|
|
19
|
-
{ at: 'machine.
|
|
71
|
+
{ at: 'machine.spindle_power_hp', label: 'Spindle motor power', type: 'number', unit: 'hp' },
|
|
20
72
|
{ at: 'machine.weight_kg', label: 'Machine weight', type: 'number', unit: 'kg', note: 'Light machines start with a lighter chip load.' },
|
|
21
73
|
{
|
|
22
74
|
at: 'machine.spindle',
|
|
@@ -34,7 +86,7 @@ export const STEPS = [
|
|
|
34
86
|
{ at: 'tool.diameter_mm', label: 'Cutter diameter', type: 'length' },
|
|
35
87
|
{ at: 'tool.flutes', label: 'Flutes', type: 'int' },
|
|
36
88
|
{ at: 'tool.stickout_mm', label: 'Stickout from the holder', type: 'length' },
|
|
37
|
-
{ at: 'tool.holder', label: 'Holder', type: 'text', optional: true },
|
|
89
|
+
{ at: 'tool.holder', section: 'advanced', label: 'Holder', type: 'text', optional: true },
|
|
38
90
|
{ at: 'tool.material', label: 'Cutter material', type: 'choice', options: [['carbide', 'carbide'], ['hss', 'high-speed steel']] },
|
|
39
91
|
],
|
|
40
92
|
},
|
|
@@ -46,10 +98,10 @@ export const STEPS = [
|
|
|
46
98
|
{ at: 'cut.axial_depth_mm', label: 'Axial depth of cut', type: 'length', note: 'The deepest cut you want. It is tested first.' },
|
|
47
99
|
{ at: 'cut.other_depths_mm', label: 'Shallower depths to compare', type: 'lengths', optional: true, placeholder: 'none', note: 'Separated by commas. Tested afterwards, starting from the widths that were stable.' },
|
|
48
100
|
{ at: 'cut.direction', label: 'Direction', type: 'choice', options: [['conventional', 'conventional'], ['climb', 'climb']] },
|
|
49
|
-
{ at: 'cut.width_max_mm', label: 'Widest radial cut to try', type: 'length', optional: true, placeholder: 'half the diameter' },
|
|
50
|
-
{ at: 'cut.chip_load_mm', label: 'Chip load per tooth', type: 'length', optional: true, placeholder: 'from the material' },
|
|
51
|
-
{ at: 'cut.speed_min_rpm', label: 'Slowest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'a quarter of the fastest' },
|
|
52
|
-
{ at: 'cut.speed_max_rpm', label: 'Fastest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'machine or material limit' },
|
|
101
|
+
{ at: 'cut.width_max_mm', section: 'advanced', label: 'Widest radial cut to try', type: 'length', optional: true, placeholder: 'half the diameter' },
|
|
102
|
+
{ at: 'cut.chip_load_mm', section: 'advanced', label: 'Chip load per tooth', type: 'length', optional: true, placeholder: 'from the material' },
|
|
103
|
+
{ at: 'cut.speed_min_rpm', section: 'advanced', label: 'Slowest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'a quarter of the fastest' },
|
|
104
|
+
{ at: 'cut.speed_max_rpm', section: 'advanced', label: 'Fastest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'machine or material limit' },
|
|
53
105
|
],
|
|
54
106
|
},
|
|
55
107
|
{
|
|
@@ -58,30 +110,30 @@ export const STEPS = [
|
|
|
58
110
|
fields: [
|
|
59
111
|
{ at: 'stock.length_mm', label: 'Stock length (X)', type: 'length', note: 'Every pass cuts the full length.' },
|
|
60
112
|
{ at: 'stock.width_mm', label: 'Stock width to use (Y)', type: 'length', note: 'Material the test may consume from the front face.' },
|
|
61
|
-
{ at: 'program.coolant', label: 'Coolant', type: 'choice', options: [['none', 'none'], ['flood', 'flood'], ['mist', 'mist']], note: 'Coolant noise makes chatter harder to hear.' },
|
|
62
|
-
{ at: 'program.dwell_s', label: 'Pause at speed before each pass', type: 'number', unit: 's' },
|
|
63
|
-
{ at: 'program.clearance_mm', label: 'Clearance around the stock', type: 'length' },
|
|
113
|
+
{ at: 'program.coolant', section: 'advanced', label: 'Coolant', type: 'choice', options: [['none', 'none'], ['flood', 'flood'], ['mist', 'mist']], note: 'Coolant noise makes chatter harder to hear.' },
|
|
114
|
+
{ at: 'program.dwell_s', section: 'advanced', label: 'Pause at speed before each pass', type: 'number', unit: 's' },
|
|
115
|
+
{ at: 'program.clearance_mm', section: 'advanced', label: 'Clearance around the stock', type: 'length' },
|
|
64
116
|
{ at: 'program.safe_z_mm', label: 'Safe height (Z)', type: 'length' },
|
|
65
|
-
{ at: 'program.plunge_feed_mm_min', label: 'Plunge feed', type: 'feed' },
|
|
66
|
-
{ at: 'program.truing_width_mm', label: 'Truing pass width', type: 'length' },
|
|
117
|
+
{ at: 'program.plunge_feed_mm_min', section: 'advanced', label: 'Plunge feed', type: 'feed' },
|
|
118
|
+
{ at: 'program.truing_width_mm', section: 'advanced', label: 'Truing pass width', type: 'length' },
|
|
67
119
|
],
|
|
68
120
|
},
|
|
69
121
|
{
|
|
70
122
|
title: 'Planner',
|
|
71
123
|
hint: 'How boldly the next cuts are chosen. The defaults suit most sessions.',
|
|
72
124
|
fields: [
|
|
73
|
-
{ at: 'planner.round1_speeds', label: 'Speeds in round 1', type: 'int' },
|
|
74
|
-
{ at: 'planner.round1_width_fractions', label: 'Round 1 widths', type: 'list', note: 'Fractions of the widest radial cut, separated by commas.' },
|
|
75
|
-
{ at: 'planner.batch_size', label: 'Most cuts in a later round', type: 'int' },
|
|
76
|
-
{ at: 'planner.min_probability', label: 'Chance of being stable a cut needs', type: 'number', note: 'Assuming the cut before it passes. Between 0 and 1.' },
|
|
77
|
-
{ at: 'planner.probability_floor', label: 'The same, on measured cuts alone', type: 'number' },
|
|
78
|
-
{ at: 'planner.stop_improvement', label: 'Stop when a cut would add less than', type: 'number', note: 'As a fraction of the best removal rate so far.' },
|
|
125
|
+
{ at: 'planner.round1_speeds', section: 'advanced', label: 'Speeds in round 1', type: 'int' },
|
|
126
|
+
{ at: 'planner.round1_width_fractions', section: 'advanced', label: 'Round 1 widths', type: 'list', note: 'Fractions of the widest radial cut, separated by commas.' },
|
|
127
|
+
{ at: 'planner.batch_size', section: 'advanced', label: 'Most cuts in a later round', type: 'int' },
|
|
128
|
+
{ at: 'planner.min_probability', section: 'advanced', label: 'Chance of being stable a cut needs', type: 'number', note: 'Assuming the cut before it passes. Between 0 and 1.' },
|
|
129
|
+
{ at: 'planner.probability_floor', section: 'advanced', label: 'The same, on measured cuts alone', type: 'number' },
|
|
130
|
+
{ at: 'planner.stop_improvement', section: 'advanced', label: 'Stop when a cut would add less than', type: 'number', note: 'As a fraction of the best removal rate so far.' },
|
|
131
|
+
{ at: 'planner.chip_load_sweep_low', section: 'advanced', label: 'Chip-load sweep from', type: 'number', unit: '×', note: 'Times the session\'s chip load.' },
|
|
132
|
+
{ at: 'planner.chip_load_sweep_high', section: 'advanced', label: 'Chip-load sweep up to', type: 'number', unit: '×' },
|
|
79
133
|
],
|
|
80
134
|
},
|
|
81
135
|
];
|
|
82
136
|
|
|
83
|
-
// Enough decimals that a hundred-thousandth of an inch survives the trip to millimetres.
|
|
84
|
-
const tidy = (number) => Number(number.toFixed(5));
|
|
85
137
|
|
|
86
138
|
function shown(field, value, units) {
|
|
87
139
|
if (value === null || value === undefined) {
|
|
@@ -104,7 +156,8 @@ function typed(field, text, units) {
|
|
|
104
156
|
return text.trim();
|
|
105
157
|
}
|
|
106
158
|
if (field.type === 'list') {
|
|
107
|
-
|
|
159
|
+
// Empty: the default applies.
|
|
160
|
+
return text.trim() === '' ? null : text.split(',').map((part) => Number(part.trim()));
|
|
108
161
|
}
|
|
109
162
|
if (field.type === 'lengths') {
|
|
110
163
|
const lengths = text.trim() === '' ? [] : text.split(',').map((part) => Number(part.trim()));
|
|
@@ -123,7 +176,7 @@ function typed(field, text, units) {
|
|
|
123
176
|
return number;
|
|
124
177
|
}
|
|
125
178
|
|
|
126
|
-
function control(field, value, units, materials) {
|
|
179
|
+
function control(field, value, units, materials, placeholder, optional) {
|
|
127
180
|
if (field.type === 'choice') {
|
|
128
181
|
const options = field.options === 'materials' ? materials.map((name) => [name, name]) : field.options;
|
|
129
182
|
const select = document.createElement('select');
|
|
@@ -133,8 +186,8 @@ function control(field, value, units, materials) {
|
|
|
133
186
|
}
|
|
134
187
|
const input = document.createElement('input');
|
|
135
188
|
input.value = shown(field, value, units);
|
|
136
|
-
input.required = !
|
|
137
|
-
input.placeholder =
|
|
189
|
+
input.required = !optional;
|
|
190
|
+
input.placeholder = placeholder;
|
|
138
191
|
if (field.type === 'list' || field.type === 'lengths') {
|
|
139
192
|
input.pattern = String.raw`\s*\d*\.?\d+(\s*,\s*\d*\.?\d+)*\s*`;
|
|
140
193
|
input.title = 'Numbers separated by commas';
|
|
@@ -149,29 +202,139 @@ function control(field, value, units, materials) {
|
|
|
149
202
|
return input;
|
|
150
203
|
}
|
|
151
204
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
205
|
+
const has = (object, key) => Object.prototype.hasOwnProperty.call(object ?? {}, key);
|
|
206
|
+
|
|
207
|
+
// `setup` holds the form's choices besides the values: `materials`, the setup's `defaults`
|
|
208
|
+
// and, where known, the `computed` values the planner uses for entries left empty.
|
|
209
|
+
const advanced = (field) => (field.section ?? 'basic') === 'advanced';
|
|
210
|
+
|
|
211
|
+
// What an entry left empty becomes: the planner's computed value, or the default.
|
|
212
|
+
function placeholderFor(field, units, setup) {
|
|
213
|
+
const [section, key] = field.at.split('.');
|
|
214
|
+
const computed = setup.computed?.[section]?.[key];
|
|
215
|
+
const fallback = computed ?? (has(setup.defaults[section], key) ? setup.defaults[section][key] : null);
|
|
216
|
+
return fallback === null || fallback === '' ? (field.placeholder ?? '') : shown(field, fallback, units);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function row(field, values, units, setup) {
|
|
220
|
+
const [section, key] = field.at.split('.');
|
|
221
|
+
const placeholder = placeholderFor(field, units, setup);
|
|
222
|
+
const optional = field.optional || (advanced(field) && has(setup.defaults[section], key));
|
|
223
|
+
const input = control(field, values[section][key], units, setup.materials, placeholder, optional);
|
|
224
|
+
input.name = field.at;
|
|
225
|
+
// What the user was shown. Only a field they change is written back: a length
|
|
226
|
+
// shown rounded in inches must not come back as a slightly different length.
|
|
227
|
+
input.dataset.shown = input.value;
|
|
228
|
+
const id = field.at.replace('.', '-');
|
|
229
|
+
input.id = `field-${id}`;
|
|
230
|
+
const unit = LENGTH_UNITS[field.type]?.[units] ?? field.unit ?? '';
|
|
231
|
+
const bubble = Object.assign(document.createElement('p'), {
|
|
232
|
+
className: 'bubble',
|
|
233
|
+
id: `info-${id}`,
|
|
234
|
+
hidden: true,
|
|
235
|
+
textContent: infoFor(field, units),
|
|
236
|
+
});
|
|
237
|
+
bubble.setAttribute('role', 'note');
|
|
238
|
+
const about = Object.assign(document.createElement('button'), { type: 'button', className: 'info', textContent: 'i' });
|
|
239
|
+
about.setAttribute('aria-label', `About ${field.label}`);
|
|
240
|
+
about.setAttribute('aria-expanded', 'false');
|
|
241
|
+
about.setAttribute('aria-controls', bubble.id);
|
|
242
|
+
about.onclick = () => toggle(about, bubble);
|
|
243
|
+
// The label names only the input; the (i) button and its bubble sit beside it.
|
|
244
|
+
const label = Object.assign(document.createElement('label'), { htmlFor: input.id, textContent: field.label });
|
|
245
|
+
const name = document.createElement('span');
|
|
246
|
+
name.append(label, ' ', about);
|
|
247
|
+
const row = Object.assign(document.createElement('div'), { className: 'field' });
|
|
248
|
+
row.append(name, input, Object.assign(document.createElement('small'), { textContent: unit }));
|
|
249
|
+
if (field.note) {
|
|
250
|
+
row.append(Object.assign(document.createElement('em'), { textContent: field.note }));
|
|
251
|
+
}
|
|
252
|
+
row.append(bubble);
|
|
253
|
+
return row;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// One info bubble open at a time. It closes on its button, Escape, or a click elsewhere.
|
|
257
|
+
let openInfo = null; // { about, bubble } while a bubble is open
|
|
258
|
+
|
|
259
|
+
function closeInfo() {
|
|
260
|
+
if (openInfo === null) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
openInfo.bubble.hidden = true;
|
|
264
|
+
openInfo.about.setAttribute('aria-expanded', 'false');
|
|
265
|
+
openInfo = null;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function toggle(about, bubble) {
|
|
269
|
+
const wasOpen = openInfo?.bubble === bubble;
|
|
270
|
+
closeInfo();
|
|
271
|
+
if (wasOpen) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
bubble.hidden = false;
|
|
275
|
+
about.setAttribute('aria-expanded', 'true');
|
|
276
|
+
openInfo = { about, bubble };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
document.addEventListener('keydown', (event) => {
|
|
280
|
+
if (event.key !== 'Escape' || openInfo === null) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const { about } = openInfo;
|
|
284
|
+
closeInfo();
|
|
285
|
+
about.focus();
|
|
286
|
+
});
|
|
287
|
+
document.addEventListener('click', (event) => {
|
|
288
|
+
if (openInfo !== null && !openInfo.bubble.contains(event.target) && !openInfo.about.contains(event.target)) {
|
|
289
|
+
closeInfo();
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// Whether a field holds something other than its default, so it must not stay hidden.
|
|
294
|
+
function changed(field, values, defaults) {
|
|
295
|
+
const [section, key] = field.at.split('.');
|
|
296
|
+
const fallback = has(defaults[section], key) ? defaults[section][key] : null;
|
|
297
|
+
return JSON.stringify(values[section][key] ?? null) !== JSON.stringify(fallback);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Fill `form` with the fields of one step: the ones to answer, then the advanced ones in a
|
|
301
|
+
// section that starts open when any of them differs from its default.
|
|
302
|
+
export function draw(form, step, values, units, setup) {
|
|
303
|
+
closeInfo();
|
|
304
|
+
const basic = step.fields.filter((field) => !advanced(field));
|
|
305
|
+
const more = step.fields.filter(advanced);
|
|
306
|
+
form.replaceChildren(...basic.map((field) => row(field, values, units, setup)));
|
|
307
|
+
if (more.length === 0) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const details = document.createElement('details');
|
|
311
|
+
details.className = 'advanced';
|
|
312
|
+
details.open = more.some((field) => changed(field, values, setup.defaults));
|
|
313
|
+
details.append(
|
|
314
|
+
Object.assign(document.createElement('summary'), { textContent: 'Advanced' }),
|
|
315
|
+
...more.map((field) => row(field, values, units, setup)),
|
|
174
316
|
);
|
|
317
|
+
form.append(details);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Refresh the placeholders of a step already drawn, keeping whatever was typed.
|
|
321
|
+
export function placehold(form, step, units, setup) {
|
|
322
|
+
for (const field of step.fields) {
|
|
323
|
+
const input = form.elements[field.at];
|
|
324
|
+
if (input?.tagName === 'INPUT') {
|
|
325
|
+
input.placeholder = placeholderFor(field, units, setup);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Open the advanced section when it holds an entry the browser would refuse, so the
|
|
331
|
+
// browser can point at it.
|
|
332
|
+
export function reveal(form) {
|
|
333
|
+
for (const details of form.querySelectorAll('details')) {
|
|
334
|
+
if (details.querySelector(':invalid')) {
|
|
335
|
+
details.open = true;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
175
338
|
}
|
|
176
339
|
|
|
177
340
|
// Copy what the form shows back into `values`.
|