import { readFile, updateHTMLOptions, showRelatedHTML, syncHTMLValue, insertHTMLChild, } from '@/components/helper' import '@/components/background.css' import buildConfig from '!/config.json' export default class Background { #el = document.createElement('div') #parentEl #videoEl #default = { location: `${import.meta.env.BASE_URL}assets/${buildConfig.background_folder}/`, image: buildConfig.default_background, } #config = { video: { name: null, volume: 100, }, useVideo: false, name: null, } constructor(el) { this.#parentEl = el this.#el.id = 'background-box' this.image = this.#default.location + this.#default.image this.#el.innerHTML = ` ` insertHTMLChild(this.#parentEl, this.#el) } async init() { this.#videoEl = document.getElementById('video-src') } resetImage() { document.getElementById('custom-background').value = '' document.getElementById('custom-background-clear').disabled = true this.#config.name = null this.image = this.#default.location + this.#default.image } resetVideo() { this.#config.video.name = null this.#videoEl.src = '' document.getElementById('custom-video-background').value = '' document.getElementById('custom-video-background-clear').disabled = true } reset() { this.resetImage() this.resetVideo() } get useVideo() { return this.#config.useVideo } set useVideo(v) { this.#config.useVideo = v } set image(v) { this.#el.style.backgroundImage = `url("${v}")` } set video(v) { if (!v) { this.resetVideo() return } const update = (url, v = null) => { this.#config.video.name = { isLocalFile: v !== null, value: v ? v.name : url, } this.#videoEl.src = url this.#videoEl.load() document.getElementById('custom-video-background-clear').disabled = false } if (typeof v === 'object') { readFile(v, (blobURL) => update(blobURL, v)) } else { update(v) } } get volume() { return this.#config.video.volume } set volume(v) { v = parseInt(v) this.#config.video.volume = v this.#videoEl.volume = v / 100 } get current() { return this.#config.name || this.#default.image } set default(v) { this.#default.image = v if (!this.#config.name) this.image = this.#default.location + this.#default.image } set custom(v) { if (!v) { this.resetImage() return } const update = (url, v = null) => { this.#config.name = { isLocalFile: v !== null, value: v ? v.name : url, } this.image = url document.getElementById('custom-background-clear').disabled = false } if (typeof v === 'object') { readFile(v, (blobURL) => update(blobURL, v)) } else { update(v) } } get config() { return { default: this.#default.image, ...this.#config, } } get backCompatibilityFns() { const _this = this return { currentBackground: _this.current, setBackgoundImage: (v) => (_this.image = v), setDefaultBackground: (v) => (_this.default = v), setBackground: (v) => (_this.custom = v), resetBackground: _this.resetImage, setVideo: (e) => (_this.video = e.target.files[0]), getVideoVolume: () => _this.volume, setVideoVolume: (v) => (_this.volume = v), setVideoFromWE: (url) => (_this.video = url), resetVideo: _this.resetVideo, } } get HTML() { return `