koel/resources/assets/js/services/volumeManager.ts

50 lines
939 B
TypeScript
Raw Normal View History

import { ref } from 'vue'
export class VolumeManager {
2024-04-23 21:01:27 +00:00
public volume = ref(0)
private input!: HTMLInputElement
2024-03-18 17:51:51 +00:00
private originalVolume = 0
2024-03-18 17:51:51 +00:00
public init (input: HTMLInputElement, initialVolume: number) {
this.input = input
2024-03-18 17:51:51 +00:00
this.originalVolume = initialVolume
this.set(initialVolume)
}
public get () {
return this.volume.value
}
public set (volume: number) {
this.volume.value = volume
this.input.value = String(volume)
}
public mute () {
2024-03-18 17:51:51 +00:00
this.originalVolume = this.get()
this.set(0)
}
public unmute () {
2024-03-18 17:51:51 +00:00
this.set(this.originalVolume)
}
public toggleMute () {
if (this.get() === 0) {
this.unmute()
} else {
this.mute()
}
}
public increase (amount = 1) {
this.set(Math.min(10, this.get() + amount))
}
public decrease (amount = 1) {
this.set(Math.max(this.get() - amount, 0))
}
}
export const volumeManager = new VolumeManager()