qube_rails 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +216 -0
- data/README.md +242 -69
- data/app/assets/javascripts/qube_rails/question.js +425 -38
- data/app/assets/stylesheets/qube_rails.css +1201 -907
- data/app/controllers/qube_rails/connections_controller.rb +23 -6
- data/app/controllers/qube_rails/questions_controller.rb +39 -7
- data/app/controllers/qube_rails/runs_controller.rb +4 -3
- data/app/models/qube_rails/connection.rb +22 -2
- data/app/models/qube_rails/run.rb +28 -10
- data/app/views/qube_rails/_error.html.erb +2 -2
- data/app/views/qube_rails/_question.html.erb +4 -1
- data/app/views/qube_rails/question/_choice.html.erb +36 -11
- data/app/views/qube_rails/question/_exit_button.html.erb +8 -0
- data/app/views/qube_rails/question/_field.html.erb +4 -3
- data/app/views/qube_rails/question/_footer.html.erb +19 -5
- data/app/views/qube_rails/question/_pick.html.erb +8 -3
- data/app/views/qube_rails/question/_picks.html.erb +4 -2
- data/app/views/qube_rails/question/_question.html.erb +22 -7
- data/app/views/qube_rails/question/_submit.html.erb +9 -0
- data/app/views/qube_rails/question/fields/_boolean.html.erb +2 -2
- data/app/views/qube_rails/question/fields/_number.html.erb +2 -2
- data/app/views/qube_rails/question/fields/_rows.html.erb +17 -4
- data/app/views/qube_rails/question/fields/_search.html.erb +3 -1
- data/app/views/qube_rails/question/fields/_select.html.erb +6 -5
- data/app/views/qube_rails/question/fields/_text.html.erb +2 -2
- data/app/views/qube_rails/question/rows/_segmented.html.erb +3 -1
- data/app/views/qube_rails/run_status/_detail.html.erb +5 -1
- data/app/views/qube_rails/run_status/_status.html.erb +5 -2
- data/config/locales/en.yml +23 -1
- data/db/migrate/20261031000000_add_qube_connection_type_and_run_headline.rb +12 -0
- data/lib/generators/qube_rails/templates/initializer.rb.tt +12 -4
- data/lib/qube_rails/configuration.rb +3 -2
- data/lib/qube_rails/connection_events.rb +3 -0
- data/lib/qube_rails/operation.rb +115 -32
- data/lib/qube_rails/question_view.rb +235 -29
- data/lib/qube_rails/rows_view.rb +18 -5
- data/lib/qube_rails/run_status_view.rb +12 -7
- data/lib/qube_rails/storyboard/ask_about_add.json +8 -10
- data/lib/qube_rails/storyboard/choose.json +8 -5
- data/lib/qube_rails/storyboard/enum_select.json +4 -3
- data/lib/qube_rails/storyboard/missing_record.json +10 -13
- data/lib/qube_rails/storyboard/name_clash.json +10 -18
- data/lib/qube_rails/storyboard/near_match.json +12 -14
- data/lib/qube_rails/storyboard/number_boolean.json +4 -3
- data/lib/qube_rails/storyboard/record_search.json +14 -15
- data/lib/qube_rails/storyboard/refusal.json +10 -18
- data/lib/qube_rails/storyboard/rows.json +11 -14
- data/lib/qube_rails/storyboard.rb +8 -3
- data/lib/qube_rails/sync_status.rb +4 -2
- data/lib/qube_rails/version.rb +1 -1
- data/lib/qube_rails/view_helpers.rb +4 -2
- data/lib/qube_rails.rb +2 -2
- metadata +5 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// qube_rails: the behaviour of a QuickBooks question (search boxes, a rows field
|
|
2
|
-
// storyboard's copy buttons. Listeners sit on document and key only on data-qube-* attributes, so
|
|
1
|
+
// qube_rails: the behaviour of a QuickBooks question (search boxes, a chain's branches, a rows field and
|
|
2
|
+
// its steps) and the storyboard's copy buttons. Listeners sit on document and key only on data-qube-* attributes, so
|
|
3
3
|
// any markup carrying them works, after Turbo navigation too. Plain JavaScript, no build. README,
|
|
4
4
|
// "Styling", has the attribute contract.
|
|
5
5
|
(function () {
|
|
@@ -35,6 +35,13 @@
|
|
|
35
35
|
p.input.setCustomValidity(unpicked ? words(box, "pick-a-match", "Pick one of the matches.") : "");
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
// A picked match: the box's check, and room for it in the input as an inline style, so a host
|
|
39
|
+
// class on the input (its own padding) can't put the check over the text.
|
|
40
|
+
var markPicked = function (box, input, on) {
|
|
41
|
+
box.toggleAttribute("data-qube-picked", on);
|
|
42
|
+
if (input) input.style.paddingInlineStart = on ? "34px" : "";
|
|
43
|
+
};
|
|
44
|
+
|
|
38
45
|
var searchUrl = function (box, q) {
|
|
39
46
|
var url = box.getAttribute("data-qube-search");
|
|
40
47
|
url += (url.indexOf("?") === -1 ? "?" : "&") +
|
|
@@ -121,7 +128,15 @@
|
|
|
121
128
|
if (match.detail) {
|
|
122
129
|
var detail = document.createElement("span");
|
|
123
130
|
detail.setAttribute("data-qube-search-detail", "");
|
|
124
|
-
|
|
131
|
+
// Its parts (QuBe Sync joins them with " · ") wrap between, not inside, as a pick's do.
|
|
132
|
+
var bits = String(match.detail).split(" · ");
|
|
133
|
+
bits.forEach(function (bit, i) {
|
|
134
|
+
var part = document.createElement("span");
|
|
135
|
+
part.className = "qube-detail-part";
|
|
136
|
+
part.textContent = i < bits.length - 1 ? bit + " ·" : bit;
|
|
137
|
+
detail.appendChild(part);
|
|
138
|
+
if (i < bits.length - 1) detail.appendChild(document.createTextNode(" "));
|
|
139
|
+
});
|
|
125
140
|
pick.appendChild(detail);
|
|
126
141
|
}
|
|
127
142
|
p.results.appendChild(pick);
|
|
@@ -133,12 +148,15 @@
|
|
|
133
148
|
var p = parts(box);
|
|
134
149
|
if (!p.input || !p.value || !p.results) return;
|
|
135
150
|
p.value.value = "";
|
|
151
|
+
markPicked(box, p.input, false);
|
|
136
152
|
check(box, p);
|
|
137
153
|
stopTimers(box);
|
|
138
154
|
var n = (asked.get(box) || 0) + 1;
|
|
139
155
|
asked.set(box, n);
|
|
140
156
|
var q = p.input.value.trim();
|
|
141
|
-
|
|
157
|
+
// Nothing typed in the focused box: the list's first records (QuBe Sync's browse). One
|
|
158
|
+
// letter waits for another.
|
|
159
|
+
if (q.length === 1 || (q === "" && document.activeElement !== p.input)) {
|
|
142
160
|
p.results.textContent = "";
|
|
143
161
|
setOpen(box, p, false, false);
|
|
144
162
|
return;
|
|
@@ -149,18 +167,26 @@
|
|
|
149
167
|
if (asked.get(box) === n) note(box, p, words(box, "searching", "Searching…"), "loading");
|
|
150
168
|
}, 150));
|
|
151
169
|
fetch(searchUrl(box, q), { headers: { Accept: "application/json" }, credentials: "same-origin" })
|
|
152
|
-
.then(function (res) {
|
|
170
|
+
.then(function (res) {
|
|
171
|
+
if (res.ok) return res.json();
|
|
172
|
+
if (res.status === 409) return { changed: true };
|
|
173
|
+
throw new Error("search " + res.status);
|
|
174
|
+
})
|
|
153
175
|
.then(function (matches) {
|
|
154
176
|
if (asked.get(box) !== n) return; // a later search, or a close, since
|
|
155
177
|
clearTimeout(slow.get(box));
|
|
178
|
+
if (matches && matches.changed) {
|
|
179
|
+
return note(box, p, words(box, "search-changed", "This question has changed. Reload the page to see the latest."), "changed");
|
|
180
|
+
}
|
|
156
181
|
showMatches(box, p, matches);
|
|
182
|
+
if (matches.length || q.length >= 2) declineFirst(box, !matches.length);
|
|
157
183
|
})
|
|
158
184
|
.catch(function () {
|
|
159
185
|
if (asked.get(box) !== n) return;
|
|
160
186
|
clearTimeout(slow.get(box));
|
|
161
187
|
note(box, p, words(box, "search-failed", "Search isn't available right now."), "failed");
|
|
162
188
|
});
|
|
163
|
-
}, 250));
|
|
189
|
+
}, q ? 250 : 0));
|
|
164
190
|
};
|
|
165
191
|
|
|
166
192
|
var pick = function (button) {
|
|
@@ -168,11 +194,49 @@
|
|
|
168
194
|
var p = parts(box);
|
|
169
195
|
p.value.value = button.getAttribute("data-value");
|
|
170
196
|
p.input.value = button.getAttribute("data-label");
|
|
197
|
+
markPicked(box, p.input, true);
|
|
171
198
|
close(box);
|
|
172
199
|
check(box, p);
|
|
173
200
|
p.value.dispatchEvent(new Event("change", { bubbles: true }));
|
|
174
201
|
box.dispatchEvent(new CustomEvent("qube:picked", { bubbles: true }));
|
|
175
|
-
|
|
202
|
+
picked(box, true);
|
|
203
|
+
declineFirst(box, false);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// A search choice's own form ([data-qube-choice-role="search"]), for a box not in a rows field.
|
|
207
|
+
var searchForm = function (box) {
|
|
208
|
+
var form = box.closest('form[data-qube-choice-role="search"]');
|
|
209
|
+
return form && !box.closest("[data-qube-rows]") ? form : null;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
// A search's submit ("Use this one") shows once a match is picked; changing the text hides it.
|
|
213
|
+
var picked = function (box, on) {
|
|
214
|
+
var form = searchForm(box);
|
|
215
|
+
if (!form) return;
|
|
216
|
+
each(form.querySelectorAll('button[type="submit"]'), function (button) {
|
|
217
|
+
if (button.form === form) conceal(button, !on);
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
var swap = function (el, from, to, style) {
|
|
222
|
+
(from || "").split(/\s+/).forEach(function (c) { if (c) el.classList.remove(c); });
|
|
223
|
+
["qube-choice-button"].concat((to || "").split(/\s+/)).forEach(function (c) { if (c) el.classList.add(c); });
|
|
224
|
+
el.setAttribute("data-qube-button", style);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// A search that found nothing makes the decline the primary action, its classes swapped between
|
|
228
|
+
// the question's data-qube-button-class and -secondary-class; a pick or matches undo it.
|
|
229
|
+
var declineFirst = function (box, on) {
|
|
230
|
+
var form = searchForm(box);
|
|
231
|
+
var question = form && form.closest("[data-qube-question]");
|
|
232
|
+
if (!question) return;
|
|
233
|
+
var primary = question.getAttribute("data-qube-button-class") || "";
|
|
234
|
+
var secondary = question.getAttribute("data-qube-button-secondary-class") || "";
|
|
235
|
+
var from = on ? "secondary" : "primary";
|
|
236
|
+
each(question.querySelectorAll('button[data-qube-choice-role="decline"], form[data-qube-choice-role="decline"] button[type="submit"]'), function (button) {
|
|
237
|
+
if (button.getAttribute("data-qube-button") !== from) return;
|
|
238
|
+
swap(button, on ? secondary : primary, on ? primary : secondary, on ? "primary" : "secondary");
|
|
239
|
+
});
|
|
176
240
|
};
|
|
177
241
|
|
|
178
242
|
var keys = function (event, box) {
|
|
@@ -200,26 +264,6 @@
|
|
|
200
264
|
}
|
|
201
265
|
};
|
|
202
266
|
|
|
203
|
-
// ---- A form choice whose required fields are all searches: its submit waits for a pick
|
|
204
|
-
// (disabled here, never in the markup, so it still posts without the script).
|
|
205
|
-
|
|
206
|
-
var gated = function (form) {
|
|
207
|
-
var required = form.querySelectorAll("input[required], select[required], textarea[required]");
|
|
208
|
-
if (!required.length) return false;
|
|
209
|
-
return Array.prototype.every.call(required, function (input) { return input.matches("[data-qube-search-input]"); });
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
var gate = function (form) {
|
|
213
|
-
if (!form || !form.matches('[data-qube-choice-kind="form"]') || !gated(form)) return;
|
|
214
|
-
var picked = Array.prototype.every.call(form.querySelectorAll("[data-qube-search]"), function (box) {
|
|
215
|
-
var p = parts(box);
|
|
216
|
-
return !p.input || !p.input.required || (p.value && p.value.value);
|
|
217
|
-
});
|
|
218
|
-
each(form.querySelectorAll('button[type="submit"], input[type="submit"]'), function (button) {
|
|
219
|
-
button.disabled = !picked;
|
|
220
|
-
});
|
|
221
|
-
};
|
|
222
|
-
|
|
223
267
|
// ---- Rows: [data-qube-rows] holds records ([data-qube-row][data-qube-values]), their fields
|
|
224
268
|
// ([data-qube-col], with data-qube-when, data-qube-required) and inputs ([data-qube-rows-input]),
|
|
225
269
|
// the "Set all to:" line ([data-qube-rows-set-all="<column>"] with [data-qube-set-all="<value>"]
|
|
@@ -294,7 +338,9 @@
|
|
|
294
338
|
var hidden = cell.querySelector("[data-qube-search-value]");
|
|
295
339
|
if (hidden) {
|
|
296
340
|
hidden.value = value.value;
|
|
341
|
+
var picked = hidden.closest("[data-qube-search]");
|
|
297
342
|
var text = cell.querySelector("[data-qube-search-input]");
|
|
343
|
+
if (picked) markPicked(picked, text, !!value.value);
|
|
298
344
|
if (text) {
|
|
299
345
|
text.value = value.label || value.value;
|
|
300
346
|
text.setCustomValidity("");
|
|
@@ -316,26 +362,52 @@
|
|
|
316
362
|
});
|
|
317
363
|
};
|
|
318
364
|
|
|
319
|
-
//
|
|
365
|
+
// A word with a count: data-qube-text-<name>-one for 1, else -other (or <name>).
|
|
366
|
+
var counted = function (el, name, n, fallbackOne, fallbackOther) {
|
|
367
|
+
var text = n === 1 ? words(el, name + "-one", fallbackOne)
|
|
368
|
+
: words(el, name + "-other", words(el, name, fallbackOther));
|
|
369
|
+
return text.replace("%{count}", n);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// "Use for the other N records too" under each filled field that other rows could take, unless
|
|
373
|
+
// its note says that was just done (and the value hasn't changed since).
|
|
320
374
|
var offers = function (list) {
|
|
321
|
-
|
|
375
|
+
each(list.querySelectorAll("[data-qube-used-for-all]"), function (line) {
|
|
376
|
+
var cell = line.closest("[data-qube-col]");
|
|
377
|
+
if (!cell || cell.hidden || valueOf(cell).value !== line.getAttribute("data-qube-used-for-all")) line.remove();
|
|
378
|
+
});
|
|
322
379
|
each(list.querySelectorAll("[data-qube-use-for-all]"), function (button) {
|
|
323
380
|
var cell = button.closest("[data-qube-col]");
|
|
324
381
|
var n = !cell || cell.hidden || !valueOf(cell).value ? 0 : targets(list, cell).length;
|
|
325
382
|
button.hidden = n === 0;
|
|
326
|
-
if (n)
|
|
383
|
+
if (n) {
|
|
384
|
+
button.textContent = counted(list, "use-for-all", n, "Use for the other record too",
|
|
385
|
+
"Use for the other %{count} records too");
|
|
386
|
+
}
|
|
327
387
|
});
|
|
328
388
|
};
|
|
329
389
|
|
|
390
|
+
// Fills the other records' empty field with this one's, says so where the button was, and tints
|
|
391
|
+
// each as it comes on show (a record in a later step is hidden now).
|
|
330
392
|
var useForAll = function (button) {
|
|
331
393
|
var cell = button.closest("[data-qube-col]");
|
|
332
394
|
var list = cell.closest("[data-qube-rows]");
|
|
333
395
|
var value = valueOf(cell);
|
|
334
|
-
targets(list, cell)
|
|
396
|
+
var others = targets(list, cell);
|
|
397
|
+
others.forEach(function (other) {
|
|
335
398
|
setValue(other, value);
|
|
336
|
-
|
|
337
|
-
|
|
399
|
+
var row = other.closest("[data-qube-row]");
|
|
400
|
+
refresh(row);
|
|
401
|
+
if (row.hidden) other.setAttribute("data-qube-filled-later", "");
|
|
402
|
+
else filled(other);
|
|
338
403
|
});
|
|
404
|
+
var line = document.createElement("div");
|
|
405
|
+
line.className = "qube-used-for-all";
|
|
406
|
+
line.setAttribute("data-qube-used-for-all", value.value);
|
|
407
|
+
line.setAttribute("role", "status");
|
|
408
|
+
line.textContent = counted(list, "used-for-all", others.length, "Also filled in on the other record.",
|
|
409
|
+
"Also filled in on the other %{count} records.");
|
|
410
|
+
button.parentNode.insertBefore(line, button.nextSibling);
|
|
339
411
|
offers(list);
|
|
340
412
|
};
|
|
341
413
|
|
|
@@ -368,13 +440,276 @@
|
|
|
368
440
|
|
|
369
441
|
// Each rows field as the page has it.
|
|
370
442
|
var sweep = function () {
|
|
443
|
+
// A rows field (and its form's submit) needs this script: hidden until it runs. Once, before
|
|
444
|
+
// the steps hide what they hide.
|
|
445
|
+
each(document.querySelectorAll("[data-qube-needs-script]"), function (el) {
|
|
446
|
+
el.removeAttribute("data-qube-needs-script");
|
|
447
|
+
conceal(el, false);
|
|
448
|
+
});
|
|
371
449
|
each(document.querySelectorAll("[data-qube-rows]"), function (list) {
|
|
372
450
|
each(rowsOf(list), refresh);
|
|
373
451
|
offers(list);
|
|
452
|
+
stepUp(list);
|
|
453
|
+
});
|
|
454
|
+
// A chain shows the branch the page has on show, with its buttons.
|
|
455
|
+
each(document.querySelectorAll("[data-qube-question]"), function (question) {
|
|
456
|
+
if (!question.querySelector("[data-qube-branch]")) return;
|
|
457
|
+
var offered = question.querySelector('[data-qube-branch-part="offered"]');
|
|
458
|
+
showBranch(question, offered && offered.hidden ? "search" : "offered", false);
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// ---- A chain: offered records with a search, a branch at a time. What belongs to each carries
|
|
463
|
+
// data-qube-branch-part ("offered" or "search"); a [data-qube-branch="<branch>"] button moves to
|
|
464
|
+
// one. "Not this one" sits with the offered records; the decline shows with the search only.
|
|
465
|
+
var showBranch = function (question, branch, focus) {
|
|
466
|
+
each(question.querySelectorAll("[data-qube-branch-part]"), function (part) {
|
|
467
|
+
conceal(part, part.getAttribute("data-qube-branch-part") !== branch);
|
|
374
468
|
});
|
|
375
|
-
each(
|
|
469
|
+
each(question.querySelectorAll('[data-qube-branch="search"]:not([data-qube-branch-part])'), function (button) {
|
|
470
|
+
conceal(button, branch !== "offered");
|
|
471
|
+
});
|
|
472
|
+
each(question.querySelectorAll('form[data-qube-choice-role="decline"]'), function (form) {
|
|
473
|
+
conceal(form, branch === "offered");
|
|
474
|
+
});
|
|
475
|
+
if (!focus) return;
|
|
476
|
+
// A move the person made: the stylesheet brings the branch in from its side.
|
|
477
|
+
question.setAttribute("data-qube-branch-moved", branch);
|
|
478
|
+
// The search box browses as it's focused; Back lands on the first offered record.
|
|
479
|
+
var target = branch === "search"
|
|
480
|
+
? question.querySelector('[data-qube-branch-part="search"] [data-qube-search-input]')
|
|
481
|
+
: question.querySelector('[data-qube-branch-part="offered"] [data-qube-choice-kind="pick"] button');
|
|
482
|
+
if (target) target.focus();
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// ---- Steps: a rows field with more than one row shows one row at a time, in order. The script
|
|
486
|
+
// adds the step count ([data-qube-step-line], "1 of 3", aria-live), Back and Next
|
|
487
|
+
// ([data-qube-step-back], [data-qube-step-next]) to the form's button row, and keeps the step on the field
|
|
488
|
+
// (data-qube-step, from 0). Rows not on show are hidden, never removed, so the one POST still
|
|
489
|
+
// sends every row; "Set all to:" is hidden, and the form's submit shows on the last step only.
|
|
490
|
+
|
|
491
|
+
// Shows or hides with the hidden attribute and display, so a stylesheet that sets display on
|
|
492
|
+
// the element can't show it anyway.
|
|
493
|
+
var conceal = function (el, hide) {
|
|
494
|
+
if (!el) return;
|
|
495
|
+
el.hidden = hide;
|
|
496
|
+
el.style.display = hide ? "none" : "";
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
var stepped = function (list) {
|
|
500
|
+
return list.hasAttribute("data-qube-stepped");
|
|
376
501
|
};
|
|
377
502
|
|
|
503
|
+
// A stepped list's step controls: the right end of its form's button row ([data-qube-step-nav]).
|
|
504
|
+
var navs = new WeakMap();
|
|
505
|
+
var navLists = new WeakMap();
|
|
506
|
+
// Found again in the markup after a Turbo cache restore (the maps hold the first elements).
|
|
507
|
+
var navOf = function (list) {
|
|
508
|
+
var nav = navs.get(list);
|
|
509
|
+
if (!nav) {
|
|
510
|
+
nav = (list.closest("form") || list).querySelector("[data-qube-step-nav]");
|
|
511
|
+
if (nav) { navs.set(list, nav); navLists.set(nav, list); }
|
|
512
|
+
}
|
|
513
|
+
return nav;
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
var listOf = function (nav) {
|
|
517
|
+
if (!nav) return null;
|
|
518
|
+
var list = navLists.get(nav);
|
|
519
|
+
if (!list) {
|
|
520
|
+
list = (nav.closest("form") || document).querySelector("[data-qube-rows][data-qube-stepped]");
|
|
521
|
+
if (list) { navs.set(list, nav); navLists.set(nav, list); }
|
|
522
|
+
}
|
|
523
|
+
return list;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
var stepControl = function (list, kind) {
|
|
527
|
+
var nav = navOf(list);
|
|
528
|
+
return nav ? nav.querySelector("[data-qube-step-" + kind + "]") : null;
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// Every shown input of the row valid: its choice picked, its required fields filled, a search's
|
|
532
|
+
// text matched to a pick.
|
|
533
|
+
var complete = function (row) {
|
|
534
|
+
return Array.prototype.every.call(row.querySelectorAll("input, select, textarea"), function (input) {
|
|
535
|
+
return input.disabled || input.type === "hidden" || input.validity.valid;
|
|
536
|
+
});
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
var firstIncomplete = function (list) {
|
|
540
|
+
return Array.prototype.find.call(rowsOf(list), function (row) { return !complete(row); });
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// A row's header (its aria-labelledby): its name, then its detail ("Customer"), if any.
|
|
544
|
+
var header = function (row) {
|
|
545
|
+
var id = row.getAttribute("aria-labelledby");
|
|
546
|
+
var el = id && document.getElementById(id);
|
|
547
|
+
return el ? el.children : [];
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
var rowName = function (row) {
|
|
551
|
+
var parts = header(row);
|
|
552
|
+
return parts.length ? parts[0].textContent.trim() : "";
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// What the step line calls a row: its detail, else its name, else its key.
|
|
556
|
+
var stepName = function (row) {
|
|
557
|
+
var parts = header(row);
|
|
558
|
+
var named = parts[1] || parts[0];
|
|
559
|
+
return named ? named.textContent.trim() : row.getAttribute("data-qube-row");
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
var stepButton = function (list, kind, label, primary) {
|
|
563
|
+
var button = document.createElement("button");
|
|
564
|
+
button.type = "button";
|
|
565
|
+
button.className = list.getAttribute(primary ? "data-qube-button-class" : "data-qube-button-secondary-class") ||
|
|
566
|
+
"qube-choice-button";
|
|
567
|
+
button.setAttribute("data-qube-button", primary ? "primary" : "secondary");
|
|
568
|
+
button.setAttribute("data-qube-step-" + kind, "");
|
|
569
|
+
button.textContent = label;
|
|
570
|
+
return button;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
// The form's own submits (not an exit's button in its row, which names another form).
|
|
574
|
+
var submits = function (list) {
|
|
575
|
+
var form = list.closest("form");
|
|
576
|
+
if (!form) return [];
|
|
577
|
+
return Array.prototype.filter.call(form.querySelectorAll('button[type="submit"], input[type="submit"]'),
|
|
578
|
+
function (button) { return button.form === form; });
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// The step's controls: Next before the last record, the form's submit on it. Neither is ever
|
|
582
|
+
// disabled: pressing one with something missing shows what (showProblem).
|
|
583
|
+
var updateStep = function (list) {
|
|
584
|
+
var rows = rowsOf(list);
|
|
585
|
+
var at = Number(list.getAttribute("data-qube-step")) || 0;
|
|
586
|
+
var last = at === rows.length - 1;
|
|
587
|
+
conceal(stepControl(list, "next"), last);
|
|
588
|
+
each(submits(list), function (button) { conceal(button, !last); });
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
// The first control of a row a person answers (a picked radio over the first one).
|
|
592
|
+
var firstControl = function (row) {
|
|
593
|
+
var controls = Array.prototype.filter.call(row.querySelectorAll("input, select, textarea, button"), function (el) {
|
|
594
|
+
return !el.disabled && el.type !== "hidden" && !el.closest("[hidden]");
|
|
595
|
+
});
|
|
596
|
+
var first = controls[0];
|
|
597
|
+
if (first && first.type === "radio") {
|
|
598
|
+
first = controls.find(function (el) { return el.name === first.name && el.checked; }) || first;
|
|
599
|
+
}
|
|
600
|
+
return first;
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
var reduced = function () {
|
|
604
|
+
return window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// Shows step `at`: its row in (opacity and a few px from the side it comes from, 180ms), the
|
|
608
|
+
// others hidden at once.
|
|
609
|
+
var showStep = function (list, at, focus) {
|
|
610
|
+
var rows = rowsOf(list);
|
|
611
|
+
at = Math.max(0, Math.min(at, rows.length - 1));
|
|
612
|
+
var was = Number(list.getAttribute("data-qube-step"));
|
|
613
|
+
list.setAttribute("data-qube-step", String(at));
|
|
614
|
+
each(rows, function (row, n) {
|
|
615
|
+
conceal(row, n !== at);
|
|
616
|
+
if (n === at) row.setAttribute("data-qube-step-current", "");
|
|
617
|
+
else row.removeAttribute("data-qube-step-current");
|
|
618
|
+
});
|
|
619
|
+
var line = navOf(list).querySelector("[data-qube-step-line]");
|
|
620
|
+
line.textContent = words(list, "step", "%{position} of %{count}")
|
|
621
|
+
.replace("%{name}", stepName(rows[at]))
|
|
622
|
+
.replace("%{position}", String(at + 1))
|
|
623
|
+
.replace("%{count}", String(rows.length));
|
|
624
|
+
conceal(stepControl(list, "back"), at === 0);
|
|
625
|
+
updateStep(list);
|
|
626
|
+
each(rows[at].querySelectorAll("[data-qube-filled-later]"), function (cell) {
|
|
627
|
+
cell.removeAttribute("data-qube-filled-later");
|
|
628
|
+
filled(cell);
|
|
629
|
+
});
|
|
630
|
+
if (!isNaN(was) && was !== at && rows[at].animate) {
|
|
631
|
+
var from = reduced() ? "none" : "translateX(" + (at > was ? 8 : -8) + "px)";
|
|
632
|
+
rows[at].animate([{ opacity: 0, transform: from }, { opacity: 1, transform: "none" }],
|
|
633
|
+
{ duration: 180, easing: "cubic-bezier(0.23, 1, 0.32, 1)" });
|
|
634
|
+
}
|
|
635
|
+
if (focus) {
|
|
636
|
+
var control = firstControl(rows[at]);
|
|
637
|
+
if (control) control.focus();
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// The row a refusal on the page names (its message holds the row's name), if any.
|
|
642
|
+
var namedInError = function (list) {
|
|
643
|
+
var question = list.closest("[data-qube-question]") || list.closest("form");
|
|
644
|
+
var error = question && question.querySelector('[role="alert"]');
|
|
645
|
+
var text = error ? error.textContent : "";
|
|
646
|
+
if (!text) return -1;
|
|
647
|
+
return Array.prototype.findIndex.call(rowsOf(list), function (row) {
|
|
648
|
+
var name = rowName(row);
|
|
649
|
+
return name && text.indexOf(name) !== -1;
|
|
650
|
+
});
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// Turns a rows field of two rows or more into steps (once: the markup it adds is the state).
|
|
654
|
+
var stepUp = function (list) {
|
|
655
|
+
var rows = rowsOf(list);
|
|
656
|
+
if (stepped(list) || rows.length < 2) return;
|
|
657
|
+
var table = list.querySelector("[data-qube-row]").parentNode;
|
|
658
|
+
list.setAttribute("data-qube-stepped", "");
|
|
659
|
+
var setAllLine = list.querySelector("[data-qube-rows-set-all]");
|
|
660
|
+
if (setAllLine) conceal(setAllLine, true);
|
|
661
|
+
// The step count, Back and Next go in the form's button row, before its submit (the row's
|
|
662
|
+
// right end); a form without one gets a row of its own after the records.
|
|
663
|
+
var form = list.closest("form");
|
|
664
|
+
var nav = form && form.querySelector("[data-qube-actions-end]");
|
|
665
|
+
var buttons = submits(list);
|
|
666
|
+
if (!nav) {
|
|
667
|
+
nav = document.createElement("div");
|
|
668
|
+
nav.className = "qube-choice-actions-end";
|
|
669
|
+
table.parentNode.insertBefore(nav, table.nextSibling);
|
|
670
|
+
}
|
|
671
|
+
nav.setAttribute("data-qube-step-nav", "");
|
|
672
|
+
navs.set(list, nav);
|
|
673
|
+
navLists.set(nav, list);
|
|
674
|
+
var before = buttons.length && nav.contains(buttons[0]) ? buttons[0] : null;
|
|
675
|
+
var line = document.createElement("span");
|
|
676
|
+
line.className = "qube-rows-step";
|
|
677
|
+
line.setAttribute("data-qube-step-line", "");
|
|
678
|
+
line.setAttribute("aria-live", "polite");
|
|
679
|
+
[line, stepButton(list, "back", words(list, "back", "Back"), false),
|
|
680
|
+
stepButton(list, "next", words(list, "next", "Next"), true)].forEach(function (el) {
|
|
681
|
+
nav.insertBefore(el, before);
|
|
682
|
+
});
|
|
683
|
+
var named = namedInError(list);
|
|
684
|
+
showStep(list, named === -1 ? 0 : named, false);
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
var move = function (button, by) {
|
|
688
|
+
var list = listOf(button.closest("[data-qube-step-nav]"));
|
|
689
|
+
if (!list) return;
|
|
690
|
+
var at = Number(list.getAttribute("data-qube-step")) || 0;
|
|
691
|
+
if (by > 0 && !complete(rowsOf(list)[at])) return showProblem(list);
|
|
692
|
+
showStep(list, at + by, true);
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// The browser refuses a form whose invalid field sits in a hidden row (and can't point at it):
|
|
696
|
+
// show the first row with a problem instead, and say what it is there.
|
|
697
|
+
var showProblem = function (list) {
|
|
698
|
+
var row = firstIncomplete(list);
|
|
699
|
+
if (!row) return false;
|
|
700
|
+
showStep(list, Array.prototype.indexOf.call(rowsOf(list), row), false);
|
|
701
|
+
var bad = Array.prototype.find.call(row.querySelectorAll("input, select, textarea"), function (input) {
|
|
702
|
+
return !input.disabled && input.type !== "hidden" && !input.validity.valid;
|
|
703
|
+
});
|
|
704
|
+
if (bad) {
|
|
705
|
+
bad.focus();
|
|
706
|
+
if (bad.reportValidity) bad.reportValidity();
|
|
707
|
+
}
|
|
708
|
+
return true;
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
var problemShown = new WeakSet();
|
|
712
|
+
|
|
378
713
|
// ---- The storyboard: [data-qube-copy] copies its value.
|
|
379
714
|
|
|
380
715
|
var copy = function (button) {
|
|
@@ -394,10 +729,12 @@
|
|
|
394
729
|
var box = input && input.closest("[data-qube-search]");
|
|
395
730
|
if (box) {
|
|
396
731
|
search(box);
|
|
397
|
-
|
|
732
|
+
picked(box, false);
|
|
398
733
|
var list = box.closest("[data-qube-rows]");
|
|
399
734
|
if (list) offers(list);
|
|
400
735
|
}
|
|
736
|
+
var steps = event.target.closest && event.target.closest("[data-qube-rows][data-qube-stepped]");
|
|
737
|
+
if (steps) updateStep(steps);
|
|
401
738
|
});
|
|
402
739
|
|
|
403
740
|
document.addEventListener("keydown", function (event) {
|
|
@@ -419,6 +756,16 @@
|
|
|
419
756
|
if (box) highlight(parts(box), option, false);
|
|
420
757
|
});
|
|
421
758
|
|
|
759
|
+
// An empty search box offers its list's first records as it's focused (not when the focus
|
|
760
|
+
// only moves within it, from one of its matches).
|
|
761
|
+
document.addEventListener("focusin", function (event) {
|
|
762
|
+
var input = event.target.closest && event.target.closest("[data-qube-search-input]");
|
|
763
|
+
var box = input && input.closest("[data-qube-search]");
|
|
764
|
+
if (!box || input.disabled || input.value.trim() !== "") return;
|
|
765
|
+
if (event.relatedTarget && box.contains(event.relatedTarget)) return;
|
|
766
|
+
search(box);
|
|
767
|
+
});
|
|
768
|
+
|
|
422
769
|
document.addEventListener("focusout", function (event) {
|
|
423
770
|
var box = event.target.closest && event.target.closest("[data-qube-search]");
|
|
424
771
|
if (box && box.hasAttribute("data-qube-open") && !(event.relatedTarget && box.contains(event.relatedTarget))) {
|
|
@@ -433,10 +780,21 @@
|
|
|
433
780
|
});
|
|
434
781
|
var result = event.target.closest("[data-qube-search-result]");
|
|
435
782
|
if (result) return pick(result);
|
|
783
|
+
var branch = event.target.closest("[data-qube-branch]");
|
|
784
|
+
var question = branch && branch.closest("[data-qube-question]");
|
|
785
|
+
if (question) return showBranch(question, branch.getAttribute("data-qube-branch"), true);
|
|
436
786
|
var setAllButton = event.target.closest("[data-qube-set-all]");
|
|
437
|
-
if (setAllButton)
|
|
787
|
+
if (setAllButton) {
|
|
788
|
+
return setAll(setAllButton);
|
|
789
|
+
}
|
|
438
790
|
var forAll = event.target.closest("[data-qube-use-for-all]");
|
|
439
|
-
if (forAll)
|
|
791
|
+
if (forAll) {
|
|
792
|
+
return useForAll(forAll);
|
|
793
|
+
}
|
|
794
|
+
var back = event.target.closest("[data-qube-step-back]");
|
|
795
|
+
if (back) return move(back, -1);
|
|
796
|
+
var next = event.target.closest("[data-qube-step-next]");
|
|
797
|
+
if (next) return move(next, 1);
|
|
440
798
|
var copyButton = event.target.closest("[data-qube-copy]");
|
|
441
799
|
if (copyButton) copy(copyButton);
|
|
442
800
|
});
|
|
@@ -447,9 +805,38 @@
|
|
|
447
805
|
var row = target.closest("[data-qube-rows] [data-qube-row][data-qube-values]");
|
|
448
806
|
if (!row) return;
|
|
449
807
|
refresh(row);
|
|
450
|
-
|
|
808
|
+
var list = row.closest("[data-qube-rows]");
|
|
809
|
+
offers(list);
|
|
810
|
+
if (stepped(list)) updateStep(list);
|
|
451
811
|
});
|
|
452
812
|
|
|
813
|
+
// A refused submit (an invalid field in a row not on show): its "invalid" events come one per
|
|
814
|
+
// field, in order, before the form is refused; the first shows its row.
|
|
815
|
+
document.addEventListener("invalid", function (event) {
|
|
816
|
+
var list = event.target.closest && event.target.closest("[data-qube-rows][data-qube-stepped]");
|
|
817
|
+
if (!list || problemShown.has(list)) return;
|
|
818
|
+
var current = list.querySelector("[data-qube-step-current]");
|
|
819
|
+
if (current && current.contains(event.target)) return; // the browser points at it itself
|
|
820
|
+
event.preventDefault();
|
|
821
|
+
problemShown.add(list);
|
|
822
|
+
setTimeout(function () {
|
|
823
|
+
problemShown.delete(list);
|
|
824
|
+
showProblem(list);
|
|
825
|
+
}, 0);
|
|
826
|
+
}, true);
|
|
827
|
+
|
|
828
|
+
// Enter in a step before the last: the next step, not the form's submit.
|
|
829
|
+
document.addEventListener("submit", function (event) {
|
|
830
|
+
var list = event.target.querySelector && event.target.querySelector("[data-qube-rows][data-qube-stepped]");
|
|
831
|
+
if (!list) return;
|
|
832
|
+
var rows = rowsOf(list);
|
|
833
|
+
var at = Number(list.getAttribute("data-qube-step")) || 0;
|
|
834
|
+
if (at >= rows.length - 1) return;
|
|
835
|
+
event.preventDefault();
|
|
836
|
+
if (complete(rows[at])) showStep(list, at + 1, true);
|
|
837
|
+
else showProblem(list);
|
|
838
|
+
}, true);
|
|
839
|
+
|
|
453
840
|
["DOMContentLoaded", "turbo:load", "turbo:render", "turbo:frame-load"].forEach(function (name) {
|
|
454
841
|
document.addEventListener(name, sweep);
|
|
455
842
|
});
|