Skip to content

Commit

Permalink
MBS-9811: Extend toggle component to be more reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Jan 2, 2025
1 parent a0152a5 commit 7508bbd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion amd/build/toggle_handler.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/toggle_handler.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions amd/src/toggle_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,33 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

export const init = (inputid) => {
const toggleContainer = document.getElementById(inputid);
export const init = (inputSelector) => {
const toggleContainer = document.querySelector(inputSelector);
if (toggleContainer) {
const toggle = toggleContainer.querySelector('input');

toggleContainer.addEventListener('click', () => {
// Click event will fire before status is being updated, so we have to invert 0 and 1 here.
toggleContainer.dataset.checked = toggle.checked ? '0' : '1';
toggle.checked = !toggle.checked;
toggle.dispatchEvent(new Event('change'));
});

toggle.addEventListener('change', () => {
// New state incoming.
if (!toggle.checked) {
window.location.replace(toggle.dataset.targetwhenchecked);
} else {
window.location.replace(toggle.dataset.targetwhennotchecked);
}
return false;
});
// To make the toggle also usable without directly loading a page on changing the state
// we only add the listener here if both target attributes are set and not empty.
const useUrlsOnChange = toggle.dataset.targetwhenchecked && toggle.dataset.targetwhenchecked.length > 0
&& toggle.dataset.targetwhennotchecked && toggle.dataset.targetwhennotchecked.length > 0;
if (useUrlsOnChange) {
toggle.addEventListener('change', () => {
// New state incoming.
if (!toggle.checked) {
window.location.replace(toggle.dataset.targetwhenchecked);
} else {
window.location.replace(toggle.dataset.targetwhennotchecked);
}
return false;
});
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions templates/toggle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"text": "Toggle this to enable/disable what you want"
}
}}
<div id="local_ai_manager_toggle-{{uniqid}}" class="custom-control custom-switch">
<div class="custom-control custom-switch" data-toggle-identifier="{{identifier}}" {{#checked}} data-checked="1" {{/checked}}{{^checked}} data-checked="0" {{/checked}}>
<input type="checkbox" class="custom-control-input"{{!
}} {{#checked}}checked{{/checked}}{{!
}} data-targetwhenchecked="{{{targetwhenchecked}}}"{{!
Expand All @@ -35,6 +35,6 @@
</div>
{{#js}}
require(['local_ai_manager/toggle_handler'], function(ToggleHandler) {
ToggleHandler.init('local_ai_manager_toggle-{{uniqid}}');
ToggleHandler.init('[data-toggle-identifier="{{identifier}}"]');
});
{{/js}}

0 comments on commit 7508bbd

Please sign in to comment.