add_module.js
1 year ago
checkbox_group.js
1 year ago
module.js
1 year ago
module_editor.js
1 year ago
module_list.js
1 year ago
module_picker.js
1 year ago
reorder_modules.js
1 year ago
checkbox_group.js
56 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | |
| 3 | export default class extends Controller { |
| 4 | static targets = ['groupTab', 'group'] |
| 5 | |
| 6 | static values = { |
| 7 | max: { type: Number, default: -1 } |
| 8 | } |
| 9 | |
| 10 | connect() { |
| 11 | this.updateCheckboxStates() |
| 12 | this.element.addEventListener('change', (event) => { |
| 13 | this.updateCheckboxStates() |
| 14 | }) |
| 15 | } |
| 16 | |
| 17 | updateCheckboxStates() { |
| 18 | const checkboxes = Array.from( |
| 19 | this.element.querySelectorAll('input[type=checkbox]') |
| 20 | ) |
| 21 | const totalChecked = checkboxes.filter(checkbox => checkbox.checked).length |
| 22 | |
| 23 | if (totalChecked === 1) { |
| 24 | // Disable checked boxes so they can't select 0 |
| 25 | checkboxes.forEach(checkbox => { |
| 26 | if (checkbox.checked) { |
| 27 | checkbox.disabled = true |
| 28 | } |
| 29 | }) |
| 30 | } else if (totalChecked === this.maxValue) { |
| 31 | // Disable unchecked boxes so they can't select more than 4 |
| 32 | checkboxes.forEach(checkbox => { |
| 33 | if (!checkbox.checked) { |
| 34 | checkbox.disabled = true |
| 35 | } |
| 36 | }) |
| 37 | } else { |
| 38 | // Ensure everything is enabled |
| 39 | checkboxes.forEach(checkbox => { |
| 40 | checkbox.disabled = false |
| 41 | }) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | changeTab(event) { |
| 46 | const selectedGroup = event.currentTarget.dataset.groupName |
| 47 | |
| 48 | this.groupTabTargets.forEach(groupTab => { |
| 49 | groupTab.classList.toggle('selected', groupTab.dataset.groupName === selectedGroup) |
| 50 | }) |
| 51 | |
| 52 | this.groupTargets.forEach(group => { |
| 53 | group.classList.toggle('selected', group.dataset.groupName === selectedGroup) |
| 54 | }) |
| 55 | } |
| 56 | } |