-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tec: Provide a Stimulus "checkboxes" controller
- Loading branch information
1 parent
b370ad0
commit 14828fb
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
export default class extends Controller { | ||
static get targets () { | ||
return ['control']; | ||
} | ||
|
||
connect () { | ||
this.refreshControls(); | ||
} | ||
|
||
checkAll (event) { | ||
const checkboxes = this.element.querySelectorAll('input[type="checkbox"]'); | ||
|
||
checkboxes.forEach((checkbox) => { | ||
checkbox.checked = true; | ||
}); | ||
|
||
this.refreshControls(); | ||
} | ||
|
||
uncheckAll (event) { | ||
const checkboxes = this.element.querySelectorAll('input[type="checkbox"]'); | ||
|
||
checkboxes.forEach((checkbox) => { | ||
checkbox.checked = false; | ||
}); | ||
|
||
this.refreshControls(); | ||
} | ||
|
||
refreshControls () { | ||
this.controlTargets.forEach((node) => { | ||
this.switchDisabledForControl(node); | ||
}); | ||
} | ||
|
||
switchDisabled (event) { | ||
this.switchDisabledForControl(event.target); | ||
} | ||
|
||
switchDisabledForControl (control) { | ||
const selector = control.dataset.checkboxesControl; | ||
const controlledNodes = this.element.querySelectorAll(selector); | ||
|
||
controlledNodes.forEach((node) => { | ||
node.disabled = control.checked; | ||
|
||
if (node.disabled) { | ||
// Deselect the node value(s) | ||
if ( | ||
node.nodeName === 'INPUT' && | ||
(node.type === 'checkbox' || node.type === 'radio') | ||
) { | ||
node.checked = false; | ||
} else if (node.nodeName === 'INPUT' || node.nodeName === 'TEXTAREA') { | ||
node.value = ''; | ||
} else if (node.nodeName === 'SELECT') { | ||
node.selectedIndex = -1; | ||
} | ||
} | ||
|
||
// Send a "change" event to the node element. | ||
// It is especially useful to refresh the "assignee" multiselect_actors | ||
// select so it can refresh the list. | ||
const event = new Event('change'); | ||
node.dispatchEvent(event); | ||
}); | ||
} | ||
} |