mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Intelligent graphic sets implemented
This commit is contained in:
50
tilorswift/js/menu/Switch.js
Normal file
50
tilorswift/js/menu/Switch.js
Normal file
@@ -0,0 +1,50 @@
|
||||
export class Switch
|
||||
{
|
||||
constructor(status = false) {
|
||||
this.htmlElement = document.createElement('div');
|
||||
this.htmlElement.classList.add('switch');
|
||||
this.slider = document.createElement('div');
|
||||
this.slider.classList.add('switch-slider');
|
||||
this.htmlElement.appendChild(this.slider);
|
||||
this.status = status;
|
||||
this.isEnabled = true;
|
||||
|
||||
this.updateSlider();
|
||||
|
||||
this.onToggle = () => {}
|
||||
|
||||
this.htmlElement.addEventListener(
|
||||
'click',
|
||||
() => {
|
||||
if (!this.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.toggle();
|
||||
this.onToggle(this.status);
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
toggle()
|
||||
{
|
||||
this.status = !this.status;
|
||||
this.updateSlider();
|
||||
}
|
||||
|
||||
updateSlider()
|
||||
{
|
||||
this.slider.classList.add(this.status ? 'switch-slider-on' : 'switch-slider-off');
|
||||
this.slider.classList.remove(this.status ? 'switch-slider-off' : 'switch-slider-on');
|
||||
}
|
||||
|
||||
enable()
|
||||
{
|
||||
this.isEnabled = true;
|
||||
}
|
||||
|
||||
disable()
|
||||
{
|
||||
this.isEnabled = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user