qube_rails 0.2.0 → 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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +321 -0
  3. data/README.md +406 -76
  4. data/app/assets/javascripts/qube_rails/question.js +469 -43
  5. data/app/assets/stylesheets/qube_rails.css +1349 -912
  6. data/app/controllers/qube_rails/connections_controller.rb +42 -9
  7. data/app/controllers/qube_rails/questions_controller.rb +39 -7
  8. data/app/controllers/qube_rails/runs_controller.rb +4 -3
  9. data/app/controllers/qube_rails/storyboard_controller.rb +1 -1
  10. data/app/models/qube_rails/connection.rb +131 -9
  11. data/app/models/qube_rails/quickbooks_record.rb +22 -0
  12. data/app/models/qube_rails/run.rb +28 -10
  13. data/app/views/qube_rails/_error.html.erb +2 -2
  14. data/app/views/qube_rails/_manage_connection.html.erb +19 -0
  15. data/app/views/qube_rails/_question.html.erb +4 -1
  16. data/app/views/qube_rails/question/_choice.html.erb +34 -11
  17. data/app/views/qube_rails/question/_disclosure.html.erb +7 -0
  18. data/app/views/qube_rails/question/_exit_button.html.erb +8 -0
  19. data/app/views/qube_rails/question/_field.html.erb +4 -3
  20. data/app/views/qube_rails/question/_footer.html.erb +12 -4
  21. data/app/views/qube_rails/question/_inline_search.html.erb +12 -0
  22. data/app/views/qube_rails/question/_pick.html.erb +29 -6
  23. data/app/views/qube_rails/question/_picks.html.erb +2 -1
  24. data/app/views/qube_rails/question/_question.html.erb +31 -7
  25. data/app/views/qube_rails/question/_submit.html.erb +9 -0
  26. data/app/views/qube_rails/question/fields/_boolean.html.erb +2 -2
  27. data/app/views/qube_rails/question/fields/_number.html.erb +2 -2
  28. data/app/views/qube_rails/question/fields/_rows.html.erb +17 -4
  29. data/app/views/qube_rails/question/fields/_search.html.erb +6 -3
  30. data/app/views/qube_rails/question/fields/_select.html.erb +6 -5
  31. data/app/views/qube_rails/question/fields/_text.html.erb +2 -2
  32. data/app/views/qube_rails/question/rows/_segmented.html.erb +3 -1
  33. data/app/views/qube_rails/run_status/_detail.html.erb +5 -1
  34. data/app/views/qube_rails/run_status/_status.html.erb +5 -2
  35. data/app/views/qube_rails/storyboard/show.html.erb +4 -3
  36. data/config/locales/en.yml +30 -1
  37. data/config/routes.rb +2 -0
  38. data/db/migrate/20261031000000_add_qube_connection_type_and_run_headline.rb +12 -0
  39. data/db/migrate/20261124000000_add_qube_company_file_change.rb +17 -0
  40. data/lib/generators/qube_rails/templates/connection_events.rb.tt +17 -1
  41. data/lib/generators/qube_rails/templates/initializer.rb.tt +12 -4
  42. data/lib/qube_rails/configuration.rb +5 -2
  43. data/lib/qube_rails/connection_events.rb +70 -7
  44. data/lib/qube_rails/engine.rb +1 -0
  45. data/lib/qube_rails/link_check.rb +274 -0
  46. data/lib/qube_rails/operation.rb +116 -32
  47. data/lib/qube_rails/question_view.rb +293 -35
  48. data/lib/qube_rails/request.rb +2 -0
  49. data/lib/qube_rails/rows_view.rb +18 -5
  50. data/lib/qube_rails/run_status_view.rb +12 -7
  51. data/lib/qube_rails/storyboard/ask_about_add.json +9 -11
  52. data/lib/qube_rails/storyboard/choose.json +10 -7
  53. data/lib/qube_rails/storyboard/enum_select.json +5 -4
  54. data/lib/qube_rails/storyboard/missing_record.json +11 -14
  55. data/lib/qube_rails/storyboard/name_clash.json +51 -21
  56. data/lib/qube_rails/storyboard/near_match.json +18 -15
  57. data/lib/qube_rails/storyboard/number_boolean.json +5 -4
  58. data/lib/qube_rails/storyboard/record_search.json +15 -16
  59. data/lib/qube_rails/storyboard/refusal.json +15 -18
  60. data/lib/qube_rails/storyboard/rows.json +45 -31
  61. data/lib/qube_rails/storyboard.rb +28 -7
  62. data/lib/qube_rails/sync_status.rb +16 -3
  63. data/lib/qube_rails/version.rb +1 -1
  64. data/lib/qube_rails/view_helpers.rb +4 -2
  65. data/lib/qube_rails.rb +11 -2
  66. metadata +10 -2
@@ -1,5 +1,5 @@
1
- // qube_rails: the behaviour of a QuickBooks question (search boxes, a rows field) and the
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 picked match's row, a form behind
2
+ // "or …", a rows field and 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 () {
@@ -29,12 +29,35 @@
29
29
  };
30
30
  };
31
31
 
32
- // Until a match is picked, the form won't send what was typed.
32
+ // A search box under offered records: its search's choice has a row among them for its pick
33
+ // (a pick form of role search, QuestionView#picks).
34
+ var slotOf = function (box) {
35
+ var form = box.closest('form[data-qube-choice-role="search"]');
36
+ var question = form && form.closest("[data-qube-question]");
37
+ if (!question || box.closest("[data-qube-rows]")) return null;
38
+ var id = form.getAttribute("data-qube-choice");
39
+ return Array.prototype.find.call(question.querySelectorAll('form[data-qube-choice-kind="pick"][data-qube-choice-role="search"]'),
40
+ function (row) { return row.getAttribute("data-qube-choice") === id; }) || null;
41
+ };
42
+
43
+ var inline = function (box) {
44
+ return !!slotOf(box);
45
+ };
46
+
47
+ // Until a match is picked, the form won't send what was typed (a box under offered records
48
+ // sends nothing itself, so it never says so).
33
49
  var check = function (box, p) {
34
- var unpicked = p.input.value && !p.value.value;
50
+ var unpicked = p.input.value && !p.value.value && !inline(box);
35
51
  p.input.setCustomValidity(unpicked ? words(box, "pick-a-match", "Pick one of the matches.") : "");
36
52
  };
37
53
 
54
+ // A picked match: the box's check, and room for it in the input as an inline style, so a host
55
+ // class on the input (its own padding) can't put the check over the text.
56
+ var markPicked = function (box, input, on) {
57
+ box.toggleAttribute("data-qube-picked", on);
58
+ if (input) input.style.paddingInlineStart = on ? "34px" : "";
59
+ };
60
+
38
61
  var searchUrl = function (box, q) {
39
62
  var url = box.getAttribute("data-qube-search");
40
63
  url += (url.indexOf("?") === -1 ? "?" : "&") +
@@ -121,7 +144,15 @@
121
144
  if (match.detail) {
122
145
  var detail = document.createElement("span");
123
146
  detail.setAttribute("data-qube-search-detail", "");
124
- detail.textContent = match.detail;
147
+ // Its parts (QuBe Sync joins them with " · ") wrap between, not inside, as a pick's do.
148
+ var bits = String(match.detail).split(" · ");
149
+ bits.forEach(function (bit, i) {
150
+ var part = document.createElement("span");
151
+ part.className = "qube-detail-part";
152
+ part.textContent = i < bits.length - 1 ? bit + " ·" : bit;
153
+ detail.appendChild(part);
154
+ if (i < bits.length - 1) detail.appendChild(document.createTextNode(" "));
155
+ });
125
156
  pick.appendChild(detail);
126
157
  }
127
158
  p.results.appendChild(pick);
@@ -133,12 +164,15 @@
133
164
  var p = parts(box);
134
165
  if (!p.input || !p.value || !p.results) return;
135
166
  p.value.value = "";
167
+ markPicked(box, p.input, false);
136
168
  check(box, p);
137
169
  stopTimers(box);
138
170
  var n = (asked.get(box) || 0) + 1;
139
171
  asked.set(box, n);
140
172
  var q = p.input.value.trim();
141
- if (q.length < 2) {
173
+ // Nothing typed in the focused box: the list's first records (QuBe Sync's browse). One
174
+ // letter waits for another.
175
+ if (q.length === 1 || (q === "" && document.activeElement !== p.input)) {
142
176
  p.results.textContent = "";
143
177
  setOpen(box, p, false, false);
144
178
  return;
@@ -149,30 +183,115 @@
149
183
  if (asked.get(box) === n) note(box, p, words(box, "searching", "Searching…"), "loading");
150
184
  }, 150));
151
185
  fetch(searchUrl(box, q), { headers: { Accept: "application/json" }, credentials: "same-origin" })
152
- .then(function (res) { return res.ok ? res.json() : []; })
186
+ .then(function (res) {
187
+ if (res.ok) return res.json();
188
+ if (res.status === 409) return { changed: true };
189
+ throw new Error("search " + res.status);
190
+ })
153
191
  .then(function (matches) {
154
192
  if (asked.get(box) !== n) return; // a later search, or a close, since
155
193
  clearTimeout(slow.get(box));
194
+ if (document.activeElement !== p.input) return close(box); // only the box in use opens
195
+ if (matches && matches.changed) {
196
+ return note(box, p, words(box, "search-changed", "This question has changed. Reload the page to see the latest."), "changed");
197
+ }
156
198
  showMatches(box, p, matches);
199
+ if (matches.length || q.length >= 2) declineFirst(box, !matches.length && !pickedRow(box));
157
200
  })
158
201
  .catch(function () {
159
202
  if (asked.get(box) !== n) return;
160
203
  clearTimeout(slow.get(box));
204
+ if (document.activeElement !== p.input) return close(box);
161
205
  note(box, p, words(box, "search-failed", "Search isn't available right now."), "failed");
162
206
  });
163
- }, 250));
207
+ }, q ? 250 : 0));
208
+ };
209
+
210
+ var pickedRow = function (box) {
211
+ var row = slotOf(box);
212
+ return row && !row.hidden ? row : null;
213
+ };
214
+
215
+ // A pick in a box under offered records answers nothing: the match shows in its row (its name,
216
+ // its detail, "Use this one", focused), replacing an earlier one, and the box empties.
217
+ var pickInline = function (box, button) {
218
+ var p = parts(box);
219
+ var row = slotOf(box);
220
+ close(box);
221
+ p.input.value = "";
222
+ p.value.value = "";
223
+ check(box, p);
224
+ if (!row) return;
225
+ var value = row.querySelector('input[type="hidden"][name^="data["]');
226
+ if (value) value.value = button.getAttribute("data-value");
227
+ // The row's text: its name, then its detail (the slot renders both, empty).
228
+ var submit = row.querySelector('button[type="submit"]');
229
+ var text = submit && submit.parentElement.firstElementChild; // the row's text, beside its button
230
+ if (!text || !text.children[0]) return;
231
+ text.children[0].textContent = button.getAttribute("data-label");
232
+ var detail = text.children[1];
233
+ var source = button.querySelector("[data-qube-search-detail]");
234
+ if (detail) {
235
+ detail.textContent = "";
236
+ if (source) each(source.childNodes, function (node) { detail.appendChild(node.cloneNode(true)); });
237
+ conceal(detail, !source);
238
+ }
239
+ conceal(row, false);
240
+ submit.focus();
241
+ declineFirst(box, false);
164
242
  };
165
243
 
166
244
  var pick = function (button) {
167
245
  var box = button.closest("[data-qube-search]");
246
+ if (inline(box)) return pickInline(box, button);
168
247
  var p = parts(box);
169
248
  p.value.value = button.getAttribute("data-value");
170
249
  p.input.value = button.getAttribute("data-label");
250
+ markPicked(box, p.input, true);
171
251
  close(box);
172
252
  check(box, p);
173
253
  p.value.dispatchEvent(new Event("change", { bubbles: true }));
174
254
  box.dispatchEvent(new CustomEvent("qube:picked", { bubbles: true }));
175
- gate(box.closest("form"));
255
+ picked(box, true);
256
+ declineFirst(box, false);
257
+ };
258
+
259
+ // A search choice's own form ([data-qube-choice-role="search"]), for a box not in a rows field.
260
+ var searchForm = function (box) {
261
+ var form = box.closest('form[data-qube-choice-role="search"]');
262
+ return form && !box.closest("[data-qube-rows]") ? form : null;
263
+ };
264
+
265
+ // A search's submit ("Use this one") shows once a match is picked; changing the text hides it.
266
+ var picked = function (box, on) {
267
+ var form = searchForm(box);
268
+ if (!form) return;
269
+ each(form.querySelectorAll('button[type="submit"]'), function (button) {
270
+ if (button.form === form) conceal(button, !on);
271
+ });
272
+ };
273
+
274
+ var swap = function (el, from, to, style) {
275
+ (from || "").split(/\s+/).forEach(function (c) { if (c) el.classList.remove(c); });
276
+ ["qube-choice-button"].concat((to || "").split(/\s+/)).forEach(function (c) { if (c) el.classList.add(c); });
277
+ el.setAttribute("data-qube-button", style);
278
+ };
279
+
280
+ // A search that found nothing makes the decline the primary action, its classes swapped between
281
+ // the question's data-qube-button-class and -secondary-class; a pick or matches undo it.
282
+ var declineFirst = function (box, on) {
283
+ var form = searchForm(box);
284
+ var question = form && form.closest("[data-qube-question]");
285
+ if (!question) return;
286
+ var primary = question.getAttribute("data-qube-button-class") || "";
287
+ var secondary = question.getAttribute("data-qube-button-secondary-class") || "";
288
+ var from = on ? "secondary" : "primary";
289
+ // A decline's button, its exit button in another row, or its option row's action.
290
+ each(question.querySelectorAll('button[data-qube-choice-role="decline"], form[data-qube-choice-role="decline"] button[type="submit"], form[data-qube-choice-role="decline"] [data-qube-offered]'), function (button) {
291
+ if (button.getAttribute("data-qube-button") !== from) return;
292
+ swap(button, on ? secondary : primary, on ? primary : secondary, on ? "primary" : "secondary");
293
+ if (button.hasAttribute("data-qube-offered")) button.setAttribute("data-qube-offered", on ? "primary" : "secondary");
294
+ });
176
295
  };
177
296
 
178
297
  var keys = function (event, box) {
@@ -200,26 +319,6 @@
200
319
  }
201
320
  };
202
321
 
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
322
  // ---- Rows: [data-qube-rows] holds records ([data-qube-row][data-qube-values]), their fields
224
323
  // ([data-qube-col], with data-qube-when, data-qube-required) and inputs ([data-qube-rows-input]),
225
324
  // the "Set all to:" line ([data-qube-rows-set-all="<column>"] with [data-qube-set-all="<value>"]
@@ -294,7 +393,9 @@
294
393
  var hidden = cell.querySelector("[data-qube-search-value]");
295
394
  if (hidden) {
296
395
  hidden.value = value.value;
396
+ var picked = hidden.closest("[data-qube-search]");
297
397
  var text = cell.querySelector("[data-qube-search-input]");
398
+ if (picked) markPicked(picked, text, !!value.value);
298
399
  if (text) {
299
400
  text.value = value.label || value.value;
300
401
  text.setCustomValidity("");
@@ -316,26 +417,52 @@
316
417
  });
317
418
  };
318
419
 
319
- // "Use for all N" under each filled field that other rows could take.
420
+ // A word with a count: data-qube-text-<name>-one for 1, else -other (or <name>).
421
+ var counted = function (el, name, n, fallbackOne, fallbackOther) {
422
+ var text = n === 1 ? words(el, name + "-one", fallbackOne)
423
+ : words(el, name + "-other", words(el, name, fallbackOther));
424
+ return text.replace("%{count}", n);
425
+ };
426
+
427
+ // "Use for the other N records too" under each filled field that other rows could take, unless
428
+ // its note says that was just done (and the value hasn't changed since).
320
429
  var offers = function (list) {
321
- var template = list.getAttribute("data-qube-text-use-for-all") || "Use for all %{count}";
430
+ each(list.querySelectorAll("[data-qube-used-for-all]"), function (line) {
431
+ var cell = line.closest("[data-qube-col]");
432
+ if (!cell || cell.hidden || valueOf(cell).value !== line.getAttribute("data-qube-used-for-all")) line.remove();
433
+ });
322
434
  each(list.querySelectorAll("[data-qube-use-for-all]"), function (button) {
323
435
  var cell = button.closest("[data-qube-col]");
324
436
  var n = !cell || cell.hidden || !valueOf(cell).value ? 0 : targets(list, cell).length;
325
437
  button.hidden = n === 0;
326
- if (n) button.textContent = template.replace("%{count}", n);
438
+ if (n) {
439
+ button.textContent = counted(list, "use-for-all", n, "Use for the other record too",
440
+ "Use for the other %{count} records too");
441
+ }
327
442
  });
328
443
  };
329
444
 
445
+ // Fills the other records' empty field with this one's, says so where the button was, and tints
446
+ // each as it comes on show (a record in a later step is hidden now).
330
447
  var useForAll = function (button) {
331
448
  var cell = button.closest("[data-qube-col]");
332
449
  var list = cell.closest("[data-qube-rows]");
333
450
  var value = valueOf(cell);
334
- targets(list, cell).forEach(function (other) {
451
+ var others = targets(list, cell);
452
+ others.forEach(function (other) {
335
453
  setValue(other, value);
336
- refresh(other.closest("[data-qube-row]"));
337
- filled(other);
454
+ var row = other.closest("[data-qube-row]");
455
+ refresh(row);
456
+ if (row.hidden) other.setAttribute("data-qube-filled-later", "");
457
+ else filled(other);
338
458
  });
459
+ var line = document.createElement("div");
460
+ line.className = "qube-used-for-all";
461
+ line.setAttribute("data-qube-used-for-all", value.value);
462
+ line.setAttribute("role", "status");
463
+ line.textContent = counted(list, "used-for-all", others.length, "Also filled in on the other record.",
464
+ "Also filled in on the other %{count} records.");
465
+ button.parentNode.insertBefore(line, button.nextSibling);
339
466
  offers(list);
340
467
  };
341
468
 
@@ -368,13 +495,247 @@
368
495
 
369
496
  // Each rows field as the page has it.
370
497
  var sweep = function () {
498
+ // A rows field (and its form's submit) needs this script: hidden until it runs. Once, before
499
+ // the steps hide what they hide.
500
+ each(document.querySelectorAll("[data-qube-needs-script]"), function (el) {
501
+ el.removeAttribute("data-qube-needs-script");
502
+ conceal(el, false);
503
+ });
371
504
  each(document.querySelectorAll("[data-qube-rows]"), function (list) {
372
505
  each(rowsOf(list), refresh);
373
506
  offers(list);
507
+ stepUp(list);
374
508
  });
375
- each(document.querySelectorAll('[data-qube-choice-kind="form"]'), gate);
376
509
  };
377
510
 
511
+ // ---- Steps: a rows field with more than one row shows one row at a time, in order. The script
512
+ // adds the step count ([data-qube-step-line], "1 of 3", aria-live), Back and Next
513
+ // ([data-qube-step-back], [data-qube-step-next]) to the form's button row, and keeps the step on the field
514
+ // (data-qube-step, from 0). Rows not on show are hidden, never removed, so the one POST still
515
+ // sends every row; "Set all to:" is hidden, and the form's submit shows on the last step only.
516
+
517
+ // Shows or hides with the hidden attribute and display, so a stylesheet that sets display on
518
+ // the element can't show it anyway.
519
+ var conceal = function (el, hide) {
520
+ if (!el) return;
521
+ el.hidden = hide;
522
+ el.style.display = hide ? "none" : "";
523
+ };
524
+
525
+ var stepped = function (list) {
526
+ return list.hasAttribute("data-qube-stepped");
527
+ };
528
+
529
+ // A stepped list's step controls: the right end of its form's button row ([data-qube-step-nav]).
530
+ var navs = new WeakMap();
531
+ var navLists = new WeakMap();
532
+ // Found again in the markup after a Turbo cache restore (the maps hold the first elements).
533
+ var navOf = function (list) {
534
+ var nav = navs.get(list);
535
+ if (!nav) {
536
+ nav = (list.closest("form") || list).querySelector("[data-qube-step-nav]");
537
+ if (nav) { navs.set(list, nav); navLists.set(nav, list); }
538
+ }
539
+ return nav;
540
+ };
541
+
542
+ var listOf = function (nav) {
543
+ if (!nav) return null;
544
+ var list = navLists.get(nav);
545
+ if (!list) {
546
+ list = (nav.closest("form") || document).querySelector("[data-qube-rows][data-qube-stepped]");
547
+ if (list) { navs.set(list, nav); navLists.set(nav, list); }
548
+ }
549
+ return list;
550
+ };
551
+
552
+ var stepControl = function (list, kind) {
553
+ var nav = navOf(list);
554
+ return nav ? nav.querySelector("[data-qube-step-" + kind + "]") : null;
555
+ };
556
+
557
+ // Every shown input of the row valid: its choice picked, its required fields filled, a search's
558
+ // text matched to a pick.
559
+ var complete = function (row) {
560
+ return Array.prototype.every.call(row.querySelectorAll("input, select, textarea"), function (input) {
561
+ return input.disabled || input.type === "hidden" || input.validity.valid;
562
+ });
563
+ };
564
+
565
+ var firstIncomplete = function (list) {
566
+ return Array.prototype.find.call(rowsOf(list), function (row) { return !complete(row); });
567
+ };
568
+
569
+ // A row's header (its aria-labelledby): its name, then its detail ("Customer"), if any.
570
+ var header = function (row) {
571
+ var id = row.getAttribute("aria-labelledby");
572
+ var el = id && document.getElementById(id);
573
+ return el ? el.children : [];
574
+ };
575
+
576
+ var rowName = function (row) {
577
+ var parts = header(row);
578
+ return parts.length ? parts[0].textContent.trim() : "";
579
+ };
580
+
581
+ // What the step line calls a row: its detail, else its name, else its key.
582
+ var stepName = function (row) {
583
+ var parts = header(row);
584
+ var named = parts[1] || parts[0];
585
+ return named ? named.textContent.trim() : row.getAttribute("data-qube-row");
586
+ };
587
+
588
+ var stepButton = function (list, kind, label, primary) {
589
+ var button = document.createElement("button");
590
+ button.type = "button";
591
+ button.className = list.getAttribute(primary ? "data-qube-button-class" : "data-qube-button-secondary-class") ||
592
+ "qube-choice-button";
593
+ button.setAttribute("data-qube-button", primary ? "primary" : "secondary");
594
+ button.setAttribute("data-qube-step-" + kind, "");
595
+ button.textContent = label;
596
+ return button;
597
+ };
598
+
599
+ // The form's own submits (not an exit's button in its row, which names another form).
600
+ var submits = function (list) {
601
+ var form = list.closest("form");
602
+ if (!form) return [];
603
+ return Array.prototype.filter.call(form.querySelectorAll('button[type="submit"], input[type="submit"]'),
604
+ function (button) { return button.form === form; });
605
+ };
606
+
607
+ // The step's controls: Next before the last record, the form's submit on it. Neither is ever
608
+ // disabled: pressing one with something missing shows what (showProblem).
609
+ var updateStep = function (list) {
610
+ var rows = rowsOf(list);
611
+ var at = Number(list.getAttribute("data-qube-step")) || 0;
612
+ var last = at === rows.length - 1;
613
+ conceal(stepControl(list, "next"), last);
614
+ each(submits(list), function (button) { conceal(button, !last); });
615
+ };
616
+
617
+ // The first control of a row a person answers (a picked radio over the first one).
618
+ var firstControl = function (row) {
619
+ var controls = Array.prototype.filter.call(row.querySelectorAll("input, select, textarea, button"), function (el) {
620
+ return !el.disabled && el.type !== "hidden" && !el.closest("[hidden]");
621
+ });
622
+ var first = controls[0];
623
+ if (first && first.type === "radio") {
624
+ first = controls.find(function (el) { return el.name === first.name && el.checked; }) || first;
625
+ }
626
+ return first;
627
+ };
628
+
629
+ var reduced = function () {
630
+ return window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
631
+ };
632
+
633
+ // Shows step `at`: its row in (opacity and a few px from the side it comes from, 180ms), the
634
+ // others hidden at once.
635
+ var showStep = function (list, at, focus) {
636
+ var rows = rowsOf(list);
637
+ at = Math.max(0, Math.min(at, rows.length - 1));
638
+ var was = Number(list.getAttribute("data-qube-step"));
639
+ list.setAttribute("data-qube-step", String(at));
640
+ each(rows, function (row, n) {
641
+ conceal(row, n !== at);
642
+ if (n === at) row.setAttribute("data-qube-step-current", "");
643
+ else row.removeAttribute("data-qube-step-current");
644
+ });
645
+ var line = navOf(list).querySelector("[data-qube-step-line]");
646
+ line.textContent = words(list, "step", "%{position} of %{count}")
647
+ .replace("%{name}", stepName(rows[at]))
648
+ .replace("%{position}", String(at + 1))
649
+ .replace("%{count}", String(rows.length));
650
+ conceal(stepControl(list, "back"), at === 0);
651
+ updateStep(list);
652
+ each(rows[at].querySelectorAll("[data-qube-filled-later]"), function (cell) {
653
+ cell.removeAttribute("data-qube-filled-later");
654
+ filled(cell);
655
+ });
656
+ if (!isNaN(was) && was !== at && rows[at].animate) {
657
+ var from = reduced() ? "none" : "translateX(" + (at > was ? 8 : -8) + "px)";
658
+ rows[at].animate([{ opacity: 0, transform: from }, { opacity: 1, transform: "none" }],
659
+ { duration: 180, easing: "cubic-bezier(0.23, 1, 0.32, 1)" });
660
+ }
661
+ if (focus) {
662
+ var control = firstControl(rows[at]);
663
+ if (control) control.focus();
664
+ }
665
+ };
666
+
667
+ // The row a refusal on the page names (its message holds the row's name), if any.
668
+ var namedInError = function (list) {
669
+ var question = list.closest("[data-qube-question]") || list.closest("form");
670
+ var error = question && question.querySelector('[role="alert"]');
671
+ var text = error ? error.textContent : "";
672
+ if (!text) return -1;
673
+ return Array.prototype.findIndex.call(rowsOf(list), function (row) {
674
+ var name = rowName(row);
675
+ return name && text.indexOf(name) !== -1;
676
+ });
677
+ };
678
+
679
+ // Turns a rows field of two rows or more into steps (once: the markup it adds is the state).
680
+ var stepUp = function (list) {
681
+ var rows = rowsOf(list);
682
+ if (stepped(list) || rows.length < 2) return;
683
+ var table = list.querySelector("[data-qube-row]").parentNode;
684
+ list.setAttribute("data-qube-stepped", "");
685
+ var setAllLine = list.querySelector("[data-qube-rows-set-all]");
686
+ if (setAllLine) conceal(setAllLine, true);
687
+ // The step count, Back and Next go in the form's button row, before its submit (the row's
688
+ // right end); a form without one gets a row of its own after the records.
689
+ var form = list.closest("form");
690
+ var nav = form && form.querySelector("[data-qube-actions-end]");
691
+ var buttons = submits(list);
692
+ if (!nav) {
693
+ nav = document.createElement("div");
694
+ nav.className = "qube-choice-actions-end";
695
+ table.parentNode.insertBefore(nav, table.nextSibling);
696
+ }
697
+ nav.setAttribute("data-qube-step-nav", "");
698
+ navs.set(list, nav);
699
+ navLists.set(nav, list);
700
+ var before = buttons.length && nav.contains(buttons[0]) ? buttons[0] : null;
701
+ var line = document.createElement("span");
702
+ line.className = "qube-rows-step";
703
+ line.setAttribute("data-qube-step-line", "");
704
+ line.setAttribute("aria-live", "polite");
705
+ [line, stepButton(list, "back", words(list, "back", "Back"), false),
706
+ stepButton(list, "next", words(list, "next", "Next"), true)].forEach(function (el) {
707
+ nav.insertBefore(el, before);
708
+ });
709
+ var named = namedInError(list);
710
+ showStep(list, named === -1 ? 0 : named, false);
711
+ };
712
+
713
+ var move = function (button, by) {
714
+ var list = listOf(button.closest("[data-qube-step-nav]"));
715
+ if (!list) return;
716
+ var at = Number(list.getAttribute("data-qube-step")) || 0;
717
+ if (by > 0 && !complete(rowsOf(list)[at])) return showProblem(list);
718
+ showStep(list, at + by, true);
719
+ };
720
+
721
+ // The browser refuses a form whose invalid field sits in a hidden row (and can't point at it):
722
+ // show the first row with a problem instead, and say what it is there.
723
+ var showProblem = function (list) {
724
+ var row = firstIncomplete(list);
725
+ if (!row) return false;
726
+ showStep(list, Array.prototype.indexOf.call(rowsOf(list), row), false);
727
+ var bad = Array.prototype.find.call(row.querySelectorAll("input, select, textarea"), function (input) {
728
+ return !input.disabled && input.type !== "hidden" && !input.validity.valid;
729
+ });
730
+ if (bad) {
731
+ bad.focus();
732
+ if (bad.reportValidity) bad.reportValidity();
733
+ }
734
+ return true;
735
+ };
736
+
737
+ var problemShown = new WeakSet();
738
+
378
739
  // ---- The storyboard: [data-qube-copy] copies its value.
379
740
 
380
741
  var copy = function (button) {
@@ -394,10 +755,12 @@
394
755
  var box = input && input.closest("[data-qube-search]");
395
756
  if (box) {
396
757
  search(box);
397
- gate(box.closest("form"));
758
+ picked(box, false);
398
759
  var list = box.closest("[data-qube-rows]");
399
760
  if (list) offers(list);
400
761
  }
762
+ var steps = event.target.closest && event.target.closest("[data-qube-rows][data-qube-stepped]");
763
+ if (steps) updateStep(steps);
401
764
  });
402
765
 
403
766
  document.addEventListener("keydown", function (event) {
@@ -419,13 +782,32 @@
419
782
  if (box) highlight(parts(box), option, false);
420
783
  });
421
784
 
785
+ // An empty search box offers its list's first records as it's focused (not when the focus
786
+ // only moves within it, from one of its matches).
787
+ document.addEventListener("focusin", function (event) {
788
+ var input = event.target.closest && event.target.closest("[data-qube-search-input]");
789
+ var box = input && input.closest("[data-qube-search]");
790
+ if (!box || input.disabled || input.value.trim() !== "") return;
791
+ if (event.relatedTarget && box.contains(event.relatedTarget)) return;
792
+ search(box);
793
+ });
794
+
795
+ // Closed as the focus leaves, open or not yet: a list still on its way (the focus's browse)
796
+ // never opens for a box no longer in use.
422
797
  document.addEventListener("focusout", function (event) {
423
798
  var box = event.target.closest && event.target.closest("[data-qube-search]");
424
- if (box && box.hasAttribute("data-qube-open") && !(event.relatedTarget && box.contains(event.relatedTarget))) {
425
- close(box);
426
- }
799
+ if (box && !(event.relatedTarget && box.contains(event.relatedTarget))) close(box);
427
800
  });
428
801
 
802
+ // A form behind "or <its label>…": opening it puts the focus in its first field. (toggle
803
+ // doesn't bubble, so it's caught on the way down.)
804
+ document.addEventListener("toggle", function (event) {
805
+ var details = event.target;
806
+ if (!details.matches || !details.matches("details[data-qube-disclosure]") || !details.open) return;
807
+ var input = details.querySelector("input:not([type=hidden]), select, textarea");
808
+ if (input) input.focus();
809
+ }, true);
810
+
429
811
  document.addEventListener("click", function (event) {
430
812
  if (!event.target.closest) return;
431
813
  each(document.querySelectorAll("[data-qube-search][data-qube-open]"), function (box) {
@@ -434,9 +816,17 @@
434
816
  var result = event.target.closest("[data-qube-search-result]");
435
817
  if (result) return pick(result);
436
818
  var setAllButton = event.target.closest("[data-qube-set-all]");
437
- if (setAllButton) return setAll(setAllButton);
819
+ if (setAllButton) {
820
+ return setAll(setAllButton);
821
+ }
438
822
  var forAll = event.target.closest("[data-qube-use-for-all]");
439
- if (forAll) return useForAll(forAll);
823
+ if (forAll) {
824
+ return useForAll(forAll);
825
+ }
826
+ var back = event.target.closest("[data-qube-step-back]");
827
+ if (back) return move(back, -1);
828
+ var next = event.target.closest("[data-qube-step-next]");
829
+ if (next) return move(next, 1);
440
830
  var copyButton = event.target.closest("[data-qube-copy]");
441
831
  if (copyButton) copy(copyButton);
442
832
  });
@@ -447,9 +837,45 @@
447
837
  var row = target.closest("[data-qube-rows] [data-qube-row][data-qube-values]");
448
838
  if (!row) return;
449
839
  refresh(row);
450
- offers(row.closest("[data-qube-rows]"));
840
+ var list = row.closest("[data-qube-rows]");
841
+ offers(list);
842
+ if (stepped(list)) updateStep(list);
451
843
  });
452
844
 
845
+ // A refused submit (an invalid field in a row not on show): its "invalid" events come one per
846
+ // field, in order, before the form is refused; the first shows its row.
847
+ document.addEventListener("invalid", function (event) {
848
+ var list = event.target.closest && event.target.closest("[data-qube-rows][data-qube-stepped]");
849
+ if (!list || problemShown.has(list)) return;
850
+ var current = list.querySelector("[data-qube-step-current]");
851
+ if (current && current.contains(event.target)) return; // the browser points at it itself
852
+ event.preventDefault();
853
+ problemShown.add(list);
854
+ setTimeout(function () {
855
+ problemShown.delete(list);
856
+ showProblem(list);
857
+ }, 0);
858
+ }, true);
859
+
860
+ // A box under offered records sends nothing itself (Enter with no match highlighted): its pick's
861
+ // row answers.
862
+ document.addEventListener("submit", function (event) {
863
+ var box = event.target.querySelector && event.target.querySelector("[data-qube-search]");
864
+ if (box && inline(box) && box.closest("form") === event.target) event.preventDefault();
865
+ }, true);
866
+
867
+ // Enter in a step before the last: the next step, not the form's submit.
868
+ document.addEventListener("submit", function (event) {
869
+ var list = event.target.querySelector && event.target.querySelector("[data-qube-rows][data-qube-stepped]");
870
+ if (!list) return;
871
+ var rows = rowsOf(list);
872
+ var at = Number(list.getAttribute("data-qube-step")) || 0;
873
+ if (at >= rows.length - 1) return;
874
+ event.preventDefault();
875
+ if (complete(rows[at])) showStep(list, at + 1, true);
876
+ else showProblem(list);
877
+ }, true);
878
+
453
879
  ["DOMContentLoaded", "turbo:load", "turbo:render", "turbo:frame-load"].forEach(function (name) {
454
880
  document.addEventListener(name, sweep);
455
881
  });