turbo_material 0.3.3 → 0.3.5

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +55 -14
  3. data/Rakefile +5 -2
  4. data/app/assets/javascripts/turbo_material/drawer_button_controller.js +2 -1
  5. data/app/assets/javascripts/turbo_material/material_checkbox_controller.js +5 -3
  6. data/app/assets/javascripts/turbo_material/material_chips_input_controller.js +26 -16
  7. data/app/assets/javascripts/turbo_material/material_chips_select_controller.js +35 -21
  8. data/app/assets/javascripts/turbo_material/material_data_table_controller.js +1 -3
  9. data/app/assets/javascripts/turbo_material/material_dialog_controller.js +4 -1
  10. data/app/assets/javascripts/turbo_material/material_drawer_controller.js +2 -11
  11. data/app/assets/javascripts/turbo_material/material_input_controller.js +2 -1
  12. data/app/assets/javascripts/turbo_material/material_list_controller.js +2 -1
  13. data/app/assets/javascripts/turbo_material/material_live_form_controller.js +81 -0
  14. data/app/assets/javascripts/turbo_material/material_menu_button_controller.js +14 -2
  15. data/app/assets/javascripts/turbo_material/material_menu_surface_controller.js +2 -1
  16. data/app/assets/javascripts/turbo_material/material_radio_controller.js +2 -1
  17. data/app/assets/javascripts/turbo_material/material_ripple_controller.js +1 -0
  18. data/app/assets/javascripts/turbo_material/material_select_controller.js +6 -4
  19. data/app/assets/javascripts/turbo_material/material_switch_controller.js +4 -3
  20. data/app/assets/javascripts/turbo_material/material_tooltip_controller.js +2 -1
  21. data/app/helpers/turbo_material/application_helper.rb +1 -1
  22. data/app/helpers/turbo_material/tooltip_helper.rb +2 -2
  23. data/app/views/common/_menu_contents.html.erb +1 -1
  24. data/app/views/components/_chip_set.html.erb +1 -1
  25. data/app/views/components/_chips_input.html.erb +9 -8
  26. data/app/views/components/_data_table.html.erb +5 -5
  27. data/app/views/components/_input.html.erb +3 -4
  28. data/app/views/components/_menu_button.html.erb +5 -4
  29. data/app/views/components/_modal.html.erb +10 -9
  30. data/app/views/components/_radio.html.erb +16 -20
  31. data/app/views/components/_select.html.erb +2 -3
  32. data/app/views/components/_switch.html.erb +2 -2
  33. data/app/views/components/_textarea.html.erb +4 -4
  34. data/app/views/layouts/turbo_material/lookbook.html.erb +2 -2
  35. data/app/views/lookbook/_data_table_headers.html.erb +1 -0
  36. data/app/views/lookbook/_data_table_rows.html.erb +6 -0
  37. data/app/views/lookbook/_drawer.html.erb +16 -0
  38. data/app/views/lookbook/_tooltip.html.erb +7 -0
  39. data/config/importmap.rb +1 -1
  40. data/config/routes.rb +1 -0
  41. data/lib/generators/turbo_material/install_generator.rb +5 -7
  42. data/lib/lookbook/chip_set_preview.rb +7 -0
  43. data/lib/lookbook/chips_input_preview.rb +2 -3
  44. data/lib/lookbook/data_table_preview.rb +20 -0
  45. data/lib/lookbook/drawer_preview.rb +9 -0
  46. data/lib/lookbook/menu_button_preview.rb +2 -1
  47. data/lib/lookbook/modal_preview.rb +2 -1
  48. data/lib/lookbook/tooltip_preview.rb +8 -0
  49. data/lib/turbo_material/engine.rb +7 -25
  50. data/lib/turbo_material/version.rb +6 -1
  51. data/lib/turbo_material.rb +0 -15
  52. metadata +14 -6
  53. data/lib/turbo_material/configuration.rb +0 -7
  54. data/lib/turbo_material/post_install.rb +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25991511168858057501aff4d89fde69598f6e1d86ee3d78e92250caef93bb77
4
- data.tar.gz: 6cdad636bf4e421a50ce8e8c0290c1e1ef1c306c6fc58ed096d9d190fdcee68f
3
+ metadata.gz: d849c4052043a8f97f28b63570bf575b416400c6546d7c58d15b956926a852c5
4
+ data.tar.gz: fececa0b9159f4b469d39f7006b28d3d33acce302a3c9b610e70d13b014a8747
5
5
  SHA512:
6
- metadata.gz: 57f3067c460730c00d7e8db584825d11d83fc944d1a4f3577f839489b72768a488e1e85fe8a989c405c3203cc6f0660c23f441603f5a71f76d4650824bce50d9
7
- data.tar.gz: 4fc62a90926c6a638bffd19f149be00416d16ae3319cbfa3e3357d087ac61c828c4d0f4ad15413cb599db6005619c06cb272d6ed3b1ba1a4f134d1fc170a113c
6
+ metadata.gz: c4c079e3d67ce542b61b0eb49d220003659bb0e9f857dac5a86b2471b9b5d191edcf1e6d6d9b00fe05b4ebe9af25a05afeb307d00ccc4052f24c601f5f5b9629
7
+ data.tar.gz: 771df3933b837efc574c93aa1df9f250e1b96794b2854fd115b349720df8c1186ec26531c82cb995412be5ae083ed54100c0f2fed67d0590c6a4daf59185276b
data/README.md CHANGED
@@ -30,11 +30,13 @@ eagerLoadControllersFrom("turbo_material", application)
30
30
  Add following to your `app/view/layouts/application.html.erb` in `<head>` section:
31
31
 
32
32
  ```erb
33
- <link href="//cdn.jsdelivr.net/npm/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
34
- <script src="//cdn.jsdelivr.net/npm/material-components-web@latest/dist/material-components-web.min.js"></script>
33
+ <link href="https://cdn.jsdelivr.net/npm/material-components-web@14.0.0/dist/material-components-web.min.css" rel="stylesheet" integrity="sha256-YPguGDqg55HB8+tbrJBbWuiF9J+XCK7sjscaiwFMTxI=" crossorigin="anonymous">
34
+ <script src="https://cdn.jsdelivr.net/npm/material-components-web@14.0.0/dist/material-components-web.min.js" integrity="sha256-8DHMt+TYs1kVcO+R+oZYTrHYIYwHUOChiQsqKb2BT3g=" crossorigin="anonymous"></script>
35
35
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
36
36
  ```
37
37
 
38
+ The `integrity` hashes match MDC 14.0.0 (`TurboMaterial::MDC_VERSION`); they are also available as `TurboMaterial::MDC_CSS_SRI` and `TurboMaterial::MDC_JS_SRI`.
39
+
38
40
  Follow `test/dummy` in this project for examples.
39
41
 
40
42
  ### Material Components for Web customizations
@@ -310,19 +312,24 @@ Implements [Material Design Checkbox](https://github.com/material-components/mat
310
312
  Implements [Material Design Radio button](https://github.com/material-components/material-components-web/tree/master/packages/mdc-radio) component.
311
313
 
312
314
  ```erb
313
- <%= material_radio name: 'option', id: 'option-1', form: form, label: 'Option 1' %>
315
+ <%= material_radio name: 'size', id: 'size', form: form, label: 'Size', value: 'm',
316
+ options: [{ value: 's', label: 'Small' }, { value: 'm', label: 'Medium' }] %>
314
317
  ```
315
318
 
319
+ Renders one radio per option. Without `options` it renders Yes/No (`'true'`/`'false'`), and a blank `value` selects No.
320
+
316
321
  #### Options
317
322
 
318
323
  | Option | Type | Description |
319
324
  | --- | --- | --- |
320
- | `name` | String | Radio button name |
321
- | `id` | String | Radio button ID |
325
+ | `name` | String | Field name |
326
+ | `id` | String | ID prefix; each radio gets `<id>-1`, `<id>-2`, … |
322
327
  | `form` | Object | Form object |
323
- | `label` | String | Radio button label |
324
- | `disabled` | Boolean | Whether the radio button is disabled |
325
- | `value` | String | Radio button value |
328
+ | `label` | String | Group label |
329
+ | `options` | Array | `[{ value:, label: }]`, defaults to Yes/No |
330
+ | `value` | String | Currently selected value |
331
+ | `disabled` | Boolean | Whether the radios are disabled |
332
+ | `required` | Boolean | Whether a choice is required |
326
333
  | `parent` | Object | Radio button parent |
327
334
 
328
335
  ### Switch button
@@ -330,16 +337,19 @@ Implements [Material Design Radio button](https://github.com/material-components
330
337
  Implements [Material Design Switch button](https://github.com/material-components/material-components-web/tree/master/packages/mdc-switch) component.
331
338
 
332
339
  ```erb
333
- <%= material_switch label: 'Switch', true_label: 'On', false_label: 'Off' %>
340
+ <%= material_switch form: form, name: 'notify', id: 'notify', label: 'Switch', true_label: 'On', false_label: 'Off' %>
334
341
  ```
335
342
 
336
343
  #### Options
337
344
 
338
345
  | Option | Type | Description |
339
346
  | --- | --- | --- |
347
+ | `form` | Object | Form object |
348
+ | `name` | String | Field name (submitted through a hidden field) |
349
+ | `id` | String | Switch button ID, also used by its label |
350
+ | `value` | Boolean | Current state |
340
351
  | `label` | String | Switch button label text |
341
352
  | `disabled` | Boolean | Whether the switch is disabled |
342
- | `required` | Boolean | Whether the switch is required |
343
353
  | `true_label` | String | Text that displays when switch is on |
344
354
  | `false_label` | String | Text that displays when switch is off |
345
355
 
@@ -400,9 +410,12 @@ Implements [Material Design Chip](https://github.com/material-components/materia
400
410
  Implements [Material Design Chips Input](https://github.com/material-components/material-components-web/tree/master/packages/mdc-chips) component.
401
411
 
402
412
  ```erb
403
- <%= material_chips_input form: form, name: 'tags', label: 'Tags', selected: [{label: 'Tag 1'}, {label: 'Tag 2'}], options: [{label: 'Option 1'}, {label: 'Option 2'}] %>
413
+ <%= material_chips_input form: form, name: 'tags', id: 'tags', label: 'Tags', url: tags_path,
414
+ selected: [{ id: 1, label: 'Tag 1' }, { id: 2, label: 'Tag 2' }] %>
404
415
  ```
405
416
 
417
+ The hidden `name` field holds the comma-separated `id`s of the chips. Each `selected` and `options` entry needs an `id` and a `label`. The `url` endpoint receives `query`, `exclude` (current ids), `suffix`, `name` and `opened`, and responds with a Turbo Stream that re-renders `material_chips_input_options`.
418
+
406
419
  #### Options
407
420
 
408
421
  | Option | Type | Description |
@@ -417,9 +430,9 @@ Implements [Material Design Chips Input](https://github.com/material-components/
417
430
  | `suffix` | String | Suffix of the input |
418
431
  | `type` | String | Type of the input |
419
432
  | `url` | String | URL for fetching options |
420
- | `selected` | Array | Array of selected chips |
421
- | `options` | Array | Array of available options |
422
- | `value` | String | Value of the input |
433
+ | `selected` | Array | Selected chips, `[{ id:, label: }]` |
434
+ | `options` | Array | Available options, `[{ id:, label: }]` |
435
+ | `value` | String | Initial hidden value; defaults to the `selected` ids |
423
436
  | `fixed` | Boolean | Whether the input is fixed |
424
437
  | `prefetch` | Boolean | Whether to prefetch options |
425
438
  | `additional_query_params` | Hash | Additional query parameters |
@@ -475,6 +488,9 @@ Implements [Material Design Data Table](https://github.com/material-components/m
475
488
  | `pagy` | Object | Pagy object for pagination |
476
489
  | `table_headers_partial` | String | Partial name for table headers |
477
490
  | `table_contents_partial` | String | Partial name for table contents |
491
+ | `per_page_options` | Array | Rows-per-page choices, `[{ value:, label: }]`; default 10/25/50/100 |
492
+
493
+ The rows-per-page form uses the gem's `material-live-form` Stimulus controller: it submits on change (debounced 2s for text input, configurable with `data-material-live-form-delay-value`) and keeps focus and caret position across the Turbo re-render. Rows need `data-row-id`. Selecting rows sends `PUT <url>/<row id>/select` and `PUT <url>/select_all`; sorting visits `<url>?order=<column>&reverse=<bool>`.
478
494
 
479
495
  ### Data Table Row Checkbox
480
496
 
@@ -598,6 +614,31 @@ Implements [Material Design Tooltip](https://github.com/material-components/mate
598
614
  | --- | --- |--------------------------------------|
599
615
  | `id` | String | DOM ID of the element we displaying the tooltip for |
600
616
 
617
+ ### Drawer
618
+
619
+ Stimulus controllers for the [Material Design Drawer](https://github.com/material-components/material-components-web/tree/master/packages/mdc-drawer); the markup is yours.
620
+
621
+ ```erb
622
+ <button data-controller="drawer-button" data-drawer-button-material-drawer-outlet="#drawer"
623
+ data-action="click->drawer-button#click">menu</button>
624
+ <aside id="drawer" class="mdc-drawer mdc-drawer--dismissible mdc-drawer--right"
625
+ data-controller="material-drawer"
626
+ data-material-drawer-opened-value="true"
627
+ data-material-drawer-name-value="sidebar"
628
+ data-material-drawer-open-url-value="<%= drawer_open_path %>"
629
+ data-material-drawer-close-url-value="<%= drawer_close_path %>">
630
+ <div class="mdc-drawer__content">...</div>
631
+ </aside>
632
+ ```
633
+
634
+ | Value | Description |
635
+ | --- | --- |
636
+ | `opened` | Initial state. When `true`, the drawer also opens at ≥1024px and closes below it as the viewport changes. Leave it out to keep MDC's default and send no request on load |
637
+ | `open-url`, `close-url` | Optional; receive `PUT` with `{ name }` whenever the drawer opens or closes, e.g. to remember the state in the session |
638
+ | `name` | Sent with those requests |
639
+
640
+ `drawer-button` hides itself unless the page has a `.mdc-drawer--dismissible.mdc-drawer--right` drawer.
641
+
601
642
  ## Lookbook documentation for components
602
643
 
603
644
  Gem implements [Lookbook](https://lookbook.build) documentation for all components. To use it in the application, add `gem 'lookbook'` to your Gemfile and run `bundle install`. Then add following to your `config/application.rb`:
data/Rakefile CHANGED
@@ -5,6 +5,9 @@ require 'bundler/setup'
5
5
  APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
6
6
  load 'rails/tasks/engine.rake'
7
7
 
8
- load 'rails/tasks/statistics.rake'
9
-
10
8
  require 'bundler/gem_tasks'
9
+
10
+ # engine.rake only defines app:test, which runs the dummy app's own test folder.
11
+ desc 'Run the engine tests'
12
+ task(:test) { sh 'bin/rails test' }
13
+ task default: :test
@@ -7,7 +7,7 @@ export default class extends Controller {
7
7
  if(!document.querySelector('.mdc-drawer--dismissible.mdc-drawer--right')) {
8
8
  this.element.style.display = 'none';
9
9
  } else {
10
- mdc.ripple.MDCRipple.attachTo(this.element);
10
+ this.ripple = mdc.ripple.MDCRipple.attachTo(this.element);
11
11
  }
12
12
  }
13
13
 
@@ -16,5 +16,6 @@ export default class extends Controller {
16
16
  }
17
17
 
18
18
  disconnect() {
19
+ this.ripple?.destroy();
19
20
  }
20
21
  }
@@ -2,11 +2,13 @@ import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
4
  connect() {
5
- const checkbox = mdc.checkbox.MDCCheckbox.attachTo(this.element.querySelector('.mdc-checkbox'));
6
- const formField = mdc.formField.MDCFormField.attachTo(this.element);
7
- formField.input = checkbox;
5
+ this.checkbox = mdc.checkbox.MDCCheckbox.attachTo(this.element.querySelector('.mdc-checkbox'));
6
+ this.formField = mdc.formField.MDCFormField.attachTo(this.element);
7
+ this.formField.input = this.checkbox;
8
8
  }
9
9
 
10
10
  disconnect() {
11
+ this.formField?.destroy();
12
+ this.checkbox?.destroy();
11
13
  }
12
14
  }
@@ -26,37 +26,45 @@ export default class extends Controller {
26
26
  useClickOutside(this);
27
27
  }
28
28
 
29
- chipTemplate(label, value) {
30
- return `<div class="mdc-chip__text">${label}</div><i data-action="click->material-chips-input#removeChip" data-value="${value}" class="material-icons mdc-chip__icon mdc-chip__icon--trailing" tabindex="0" role="button">cancel</i>`;
31
- }
32
-
33
- addChip(content, value) {
29
+ addChip(label, value) {
34
30
  const chipEl = document.createElement('div');
35
31
  chipEl.classList.add('mdc-chip');
36
- if (this.chipCssValue) {
37
- chipEl.classList.add(this.chipCssValue);
38
- }
39
32
  chipEl.dataset.value = value;
40
- chipEl.innerHTML = content;
33
+ const text = document.createElement('div');
34
+ text.className = 'mdc-chip__text';
35
+ text.textContent = label;
36
+ const icon = document.createElement('i');
37
+ icon.className = 'material-icons mdc-chip__icon mdc-chip__icon--trailing';
38
+ icon.tabIndex = 0;
39
+ icon.setAttribute('role', 'button');
40
+ icon.dataset.action = 'click->material-chips-input#removeChip';
41
+ icon.dataset.value = value;
42
+ icon.textContent = 'cancel';
43
+ chipEl.append(text, icon);
41
44
  this.chipsetEl.insertBefore(chipEl, this.hiddenTarget);
42
45
  this.chips.addChip(chipEl);
43
- this.hiddenTarget.value = this.chips.chipsList.map((chip) => chip.root.dataset.value).join(',');
46
+ this.syncHidden();
44
47
  this.search();
45
48
  }
46
49
 
47
50
  removeChip(event) {
48
51
  event.stopPropagation();
49
52
  event.preventDefault();
50
- const chip = this.element.querySelector(`.mdc-chip[data-value="${event.target.dataset.value}"]`);
51
- const chipIndex = this.chips.foundation.adapter.getIndexOfChipById(chip.id);
52
- this.chips.foundation.adapter.removeChipAtIndex(chipIndex);
53
- if (this.chips.foundation.adapter.getChipListCount() === 0) {
53
+ const chip = this.element.querySelector(`.mdc-chip[data-value="${CSS.escape(event.target.dataset.value)}"]`);
54
+ // ponytail: chips have no ids, so MDC's getIndexOfChipById("") would hit the first chip
55
+ chip.remove();
56
+ this.syncHidden();
57
+ if (!this.hiddenTarget.value) {
54
58
  this.input.foundation.adapter.floatLabel(false);
55
59
  }
56
- this.hiddenTarget.value = this.chips.chipsList.map((chip) => chip.root.dataset.value).join(',');
57
60
  this.search();
58
61
  }
59
62
 
63
+ // Read chips from the DOM so server-rendered `selected` chips count too.
64
+ syncHidden() {
65
+ this.hiddenTarget.value = Array.from(this.chipsetEl.querySelectorAll('.mdc-chip[data-value]'), (chip) => chip.dataset.value).join(',');
66
+ }
67
+
60
68
  open() {
61
69
  if ((this.opened && this.panelTarget.classList.contains('mdc-menu-surface--open')) || (parseInt(this.panelTarget.dataset.size) === 0)) return;
62
70
 
@@ -95,7 +103,7 @@ export default class extends Controller {
95
103
  select(user) {
96
104
  this.close();
97
105
  this.inputTarget.value = "";
98
- this.addChip(this.chipTemplate(user.dataset.name, user.dataset.value), user.dataset.value);
106
+ this.addChip(user.dataset.name, user.dataset.value);
99
107
  }
100
108
 
101
109
  focusNext(event) {
@@ -129,6 +137,8 @@ export default class extends Controller {
129
137
  }
130
138
 
131
139
  disconnect() {
140
+ this.chips?.destroy();
141
+ this.input?.destroy();
132
142
  }
133
143
 
134
144
  search() {
@@ -21,19 +21,24 @@ export default class extends Controller {
21
21
  if (this.selectedValue.length > 0) {
22
22
  this.select.foundation.adapter.floatLabel(true);
23
23
  this.selectedValue.forEach(item => {
24
- let listItem = this.element.querySelector(`.mdc-deprecated-list-item[data-value="${item}"]`);
25
- this.addChip(this.chipTemplate(listItem.dataset.label, listItem.dataset.value, listItem.dataset.confirmed), listItem.dataset.value);
24
+ let listItem = this.listItem(item);
25
+ if (!listItem) return; // selected value not among options
26
+ this.addChip(listItem.dataset.label, listItem.dataset.value, listItem.dataset.confirmed);
26
27
  listItem.classList.add('!hidden');
27
28
  });
28
29
  }
29
30
  this.select.listen('MDCSelect:change', (event) => {
30
- const selected = this.element.querySelector(`.mdc-deprecated-list-item[data-value="${event.detail.value}"]`);
31
+ const selected = this.listItem(event.detail.value);
31
32
  selected.classList.add('!hidden');
32
- this.addChip(this.chipTemplate(selected.dataset.label, selected.dataset.value, selected.dataset.confirmed), selected.dataset.value);
33
+ this.addChip(selected.dataset.label, selected.dataset.value, selected.dataset.confirmed);
33
34
  });
34
35
  }
35
36
 
36
- addChip(content, value) {
37
+ listItem(value) {
38
+ return this.element.querySelector(`.mdc-deprecated-list-item[data-value="${CSS.escape(value)}"]`);
39
+ }
40
+
41
+ addChip(label, value, confirmed) {
37
42
  const chipEl = document.createElement('div');
38
43
  chipEl.role = 'row';
39
44
  chipEl.classList.add('mdc-chip');
@@ -41,12 +46,21 @@ export default class extends Controller {
41
46
  chipEl.classList.add(this.chipCssValue);
42
47
  }
43
48
  chipEl.dataset.value = value;
44
- chipEl.innerHTML = content;
49
+ const text = document.createElement('div');
50
+ text.className = 'mdc-chip__text';
51
+ text.dataset.action = 'click->material-chips-select#toggle';
52
+ text.textContent = (confirmed === 'false' && this.confirmableValue) ? `${label} (unconfirmed)` : label;
53
+ const icon = document.createElement('i');
54
+ icon.className = 'material-icons mdc-chip__icon mdc-chip__icon--trailing';
55
+ icon.tabIndex = 0;
56
+ icon.setAttribute('role', 'button');
57
+ icon.dataset.action = 'click->material-chips-select#removeChip';
58
+ icon.dataset.value = value;
59
+ icon.textContent = 'cancel';
60
+ chipEl.append(text, icon);
45
61
  this.chipsetEl.appendChild(chipEl);
46
62
  this.materialChipset.addChip(chipEl);
47
- if (this.hasHiddenTarget) {
48
- this.hiddenTarget.value = this.materialChipset.chipsList.map((chip) => chip.root.dataset.value).join(',');
49
- }
63
+ this.syncHidden();
50
64
  if (this.urlValue) {
51
65
  post(this.urlValue, {
52
66
  body: {
@@ -60,13 +74,10 @@ export default class extends Controller {
60
74
  removeChip(event) {
61
75
  event.stopPropagation();
62
76
  event.preventDefault();
63
- const chip = this.element.querySelector(`.mdc-chip[data-value="${event.target.dataset.value}"]`);
77
+ const chip = this.element.querySelector(`.mdc-chip[data-value="${CSS.escape(event.target.dataset.value)}"]`);
64
78
  this.chipsetEl.removeChild(chip);
65
- if (this.hasHiddenTarget) {
66
- this.hiddenTarget.value = this.materialChipset.chipsList.map((chip) => chip.root.dataset.value).join(',');
67
- }
68
- const selected = this.element.querySelector(`.mdc-deprecated-list-item[data-value="${event.target.dataset.value}"]`);
69
- selected.classList.remove('!hidden');
79
+ this.syncHidden();
80
+ this.listItem(event.target.dataset.value)?.classList.remove('!hidden');
70
81
  if (this.urlValue) {
71
82
  destroy(`${this.urlValue}/${event.target.dataset.value}`, {
72
83
  responseKind: "turbo-stream",
@@ -77,11 +88,17 @@ export default class extends Controller {
77
88
  }
78
89
  }
79
90
 
91
+ // Read chips from the DOM: MDCChipSet.chipsList keeps chips removed with removeChild.
92
+ syncHidden() {
93
+ if (!this.hasHiddenTarget) return;
94
+ this.hiddenTarget.value = Array.from(this.chipsetEl.querySelectorAll('.mdc-chip[data-value]'), (chip) => chip.dataset.value).join(',');
95
+ }
96
+
80
97
  toggle(event) {
81
98
  if (this.confirmableValue) {
82
99
  event.stopPropagation();
83
100
  event.preventDefault();
84
- const item = this.element.querySelector(`.mdc-deprecated-list-item[data-value="${event.target.parentElement.dataset.value}"]`);
101
+ const item = this.listItem(event.target.parentElement.dataset.value);
85
102
  item.dataset.confirmed = item.dataset.confirmed === 'true' ? 'false' : 'true';
86
103
  if (item.dataset.confirmed === 'true') {
87
104
  let params = new URLSearchParams(this.queryStringValue);
@@ -102,11 +119,8 @@ export default class extends Controller {
102
119
  }
103
120
  }
104
121
 
105
- chipTemplate(label, value, confirmed) {
106
- const confirm_label = (confirmed === 'false' && this.confirmableValue) ? '&nbsp;(unconfirmed)' : '';
107
- return `<div class="mdc-chip__text" data-action="click->material-chips-select#toggle">${label}${confirm_label}</div><i data-action="click->material-chips-select#removeChip" data-value="${value}" class="material-icons mdc-chip__icon mdc-chip__icon--trailing" tabindex="0" role="button">cancel</i>`;
108
- }
109
-
110
122
  disconnect() {
123
+ this.materialChipset?.destroy();
124
+ this.select?.destroy();
111
125
  }
112
126
  }
@@ -20,10 +20,7 @@ export default class extends Controller {
20
20
  params.delete("reverse");
21
21
  params.append("order", event.detail.columnId);
22
22
  params.append("reverse", event.detail.sortValue === "descending" ? "true" : "false");
23
- let frame = this.element.querySelector('turbo-frame#pupils-table-data');
24
23
  Turbo.visit(`${this.urlValue}?${params.toString()}`);
25
- // , { action: 'advance', frame: this.bodyValue }
26
- // FIXME: get this back as soon as https://github.com/hotwired/turbo/issues/489 is fixed
27
24
  }
28
25
 
29
26
  select(event) {
@@ -52,5 +49,6 @@ export default class extends Controller {
52
49
  }
53
50
 
54
51
  disconnect() {
52
+ this.dataTable?.destroy();
55
53
  }
56
54
  }
@@ -2,6 +2,7 @@ import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
4
  dialog = undefined;
5
+ ripples = [];
5
6
 
6
7
  static values = {
7
8
  opened: Boolean,
@@ -12,7 +13,7 @@ export default class extends Controller {
12
13
  this.dialog = mdc.dialog.MDCDialog.attachTo(this.element);
13
14
  this.dialog.listen('MDCDialog:opened', () => {
14
15
  this.element.querySelectorAll('.mdc-icon-button').forEach((button) => {
15
- mdc.ripple.MDCRipple.attachTo(button);
16
+ this.ripples.push(mdc.ripple.MDCRipple.attachTo(button));
16
17
  });
17
18
  });
18
19
  if (this.openedValue) {
@@ -32,5 +33,7 @@ export default class extends Controller {
32
33
  }
33
34
 
34
35
  disconnect() {
36
+ this.ripples.forEach((ripple) => ripple.destroy());
37
+ this.dialog?.destroy();
35
38
  }
36
39
  }
@@ -11,7 +11,7 @@ export default class extends Controller {
11
11
 
12
12
  connect() {
13
13
  this.drawer = mdc.drawer.MDCDrawer.attachTo(this.element);
14
- if (this.openedValue !== undefined) {
14
+ if (this.hasOpenedValue) {
15
15
  if (this.openedValue) {
16
16
  this.open();
17
17
  } else {
@@ -35,16 +35,6 @@ export default class extends Controller {
35
35
  }
36
36
  }
37
37
 
38
- isLg({ name, media, matches, event }) {
39
- if (this.openedValue) {
40
- if(matches) {
41
- this.open();
42
- } else {
43
- this.close();
44
- }
45
- }
46
- }
47
-
48
38
  open() {
49
39
  this.drawer.open = true;
50
40
  if (this.openUrlValue) {
@@ -76,5 +66,6 @@ export default class extends Controller {
76
66
  }
77
67
 
78
68
  disconnect() {
69
+ this.drawer?.destroy();
79
70
  }
80
71
  }
@@ -2,9 +2,10 @@ import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
4
  connect() {
5
- mdc.textField.MDCTextField.attachTo(this.element);
5
+ this.textField = mdc.textField.MDCTextField.attachTo(this.element);
6
6
  }
7
7
 
8
8
  disconnect() {
9
+ this.textField?.destroy();
9
10
  }
10
11
  }
@@ -5,7 +5,7 @@ export default class extends Controller {
5
5
  this.list = mdc.list.MDCList.attachTo(this.element);
6
6
  this.list.listen('MDCList:action', (event) => {
7
7
  this.focusElement(event.detail.index);
8
- this.element.dispatchEvent(new CustomEvent('country-select-focus', {
8
+ this.element.dispatchEvent(new CustomEvent('material-list:action', {
9
9
  bubbles: true,
10
10
  cancelable: true,
11
11
  detail: {
@@ -53,5 +53,6 @@ export default class extends Controller {
53
53
  }
54
54
 
55
55
  disconnect() {
56
+ this.list?.destroy();
56
57
  }
57
58
  }
@@ -0,0 +1,81 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ // Auto-submits a form: debounced on `submit`, immediately on `submitNow`.
4
+ // Keeps focus and caret position across the Turbo re-render.
5
+ // Extracted from itu-online-admissions' live_form_controller (lodash debounce → setTimeout).
6
+ const FOCUSABLE_INPUTS = [
7
+ "text", "textarea", "number", "email", "password", "search", "tel", "url", "date", "time",
8
+ "datetime-local", "color", "range"
9
+ ];
10
+
11
+ export default class extends Controller {
12
+ static values = { delay: { type: Number, default: 2000 } }
13
+
14
+ initialize() {
15
+ this.submitStart = this.submitStart.bind(this)
16
+ this.submitEnd = this.submitEnd.bind(this)
17
+ }
18
+
19
+ connect() {
20
+ if (window.liveForm === undefined) {
21
+ window.liveForm = {}
22
+ document.addEventListener('turbo:submit-start', this.submitStart)
23
+ document.addEventListener('turbo:submit-end', this.submitEnd)
24
+ } else if (window.liveForm.focused) {
25
+ this.restoreFocus(window.liveForm.focused, window.liveForm.selectionStart, window.liveForm.selectionEnd)
26
+ window.liveForm = {}
27
+ }
28
+ this.element.classList.add('live-form');
29
+ }
30
+
31
+ disconnect() {
32
+ clearTimeout(this.timer)
33
+ document.removeEventListener('turbo:submit-start', this.submitStart)
34
+ document.removeEventListener('turbo:submit-end', this.submitEnd)
35
+ }
36
+
37
+ submitStart() {
38
+ document.body.classList.add('turbo-loading')
39
+ }
40
+
41
+ submitEnd() {
42
+ document.body.classList.remove('turbo-loading')
43
+ const active = document.activeElement || document.body
44
+ window.liveForm.selectionStart = active.selectionStart
45
+ window.liveForm.selectionEnd = active.selectionEnd
46
+ window.liveForm.focused = active.id
47
+ }
48
+
49
+ restoreFocus(focusedId, selectionStart, selectionEnd) {
50
+ const focused = document.getElementById(focusedId)
51
+ if (!focused || !FOCUSABLE_INPUTS.includes(focused.type)) return
52
+ if (focused.readOnly) focused.blur()
53
+ if (!focused.matches(":focus")) focused.focus()
54
+ if (focused.setSelectionRange && (focused.type === "text" || focused.type === "textarea")) {
55
+ focused.setSelectionRange(selectionStart, selectionEnd)
56
+ }
57
+ }
58
+
59
+ submit(event) {
60
+ if (event.target.dataset.autosave === "off") return
61
+ clearTimeout(this.timer)
62
+ this.timer = setTimeout(() => this.submitNow(event), this.delayValue)
63
+ }
64
+
65
+ submitNow(event) {
66
+ clearTimeout(this.timer)
67
+ const frame = event.target.dataset.frame
68
+ if (frame !== undefined) {
69
+ let input = this.element.querySelector("input[type='hidden'][name='frame']")
70
+ if (!input) {
71
+ input = document.createElement("input")
72
+ input.type = "hidden"
73
+ input.name = "frame"
74
+ this.element.appendChild(input)
75
+ }
76
+ input.value = frame
77
+ }
78
+ this.element.requestSubmit()
79
+ setTimeout(() => this.element.querySelector("input[type='hidden'][name='frame']")?.remove(), 1000)
80
+ }
81
+ }
@@ -6,19 +6,31 @@ export default class extends Controller {
6
6
  static values = { logoutPath: String };
7
7
 
8
8
  connect() {
9
- mdc.ripple.MDCRipple.attachTo(this.element);
9
+ this.ripple = mdc.ripple.MDCRipple.attachTo(this.element);
10
10
  this.menu = mdc.menu.MDCMenu.attachTo(this.element.querySelector('.mdc-menu-surface'));
11
11
  const button = this.element.querySelector('.menu-button');
12
12
  this.menu.setAnchorElement(button);
13
13
  this.menu.setAnchorCorner(mdc.menu.Corner.BOTTOM_START);
14
14
  this.menu.setFixedPosition(true);
15
+ // MDC's outside-click handler closes the menu before our click handler runs,
16
+ // so remember the state from before the click starts.
17
+ this.rememberOpen = () => { this.wasOpen = this.menu.open; };
18
+ button.addEventListener('pointerdown', this.rememberOpen);
19
+ button.addEventListener('keydown', this.rememberOpen);
20
+ this.button = button;
15
21
  }
16
22
 
17
23
  disconnect() {
24
+ this.button?.removeEventListener('pointerdown', this.rememberOpen);
25
+ this.button?.removeEventListener('keydown', this.rememberOpen);
26
+ this.menu?.destroy();
27
+ this.ripple?.destroy();
18
28
  }
19
29
 
20
30
  toggleMenu(event) {
21
- if (this.menu.open) {
31
+ const wasOpen = this.wasOpen ?? this.menu.open;
32
+ this.wasOpen = undefined;
33
+ if (wasOpen) {
22
34
  this.close();
23
35
  } else {
24
36
  this.open();
@@ -3,9 +3,10 @@ import { Controller } from "@hotwired/stimulus";
3
3
  export default class extends Controller {
4
4
 
5
5
  connect() {
6
- this.menuSurface = new mdc.menuSurface.MDCMenuSurface.attachTo(this.element);
6
+ this.menuSurface = mdc.menuSurface.MDCMenuSurface.attachTo(this.element);
7
7
  }
8
8
 
9
9
  disconnect() {
10
+ this.menuSurface?.destroy();
10
11
  }
11
12
  }
@@ -2,9 +2,10 @@ import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
4
  connect() {
5
- const select = mdc.radio.MDCRadio.attachTo(this.element);
5
+ this.radio = mdc.radio.MDCRadio.attachTo(this.element);
6
6
  }
7
7
 
8
8
  disconnect() {
9
+ this.radio?.destroy();
9
10
  }
10
11
  }
@@ -7,5 +7,6 @@ export default class extends Controller {
7
7
  }
8
8
 
9
9
  disconnect() {
10
+ this.ripple?.destroy();
10
11
  }
11
12
  }