From e7798e8d15c1730b98cec4de811cc7d895c3e64d Mon Sep 17 00:00:00 2001 From: yuusora Date: Thu, 13 Jan 2022 09:25:23 +0800 Subject: [PATCH] feat(sub): subtitles module --- src/js/subtitles.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/js/subtitles.js diff --git a/src/js/subtitles.js b/src/js/subtitles.js new file mode 100644 index 0000000..ba19c98 --- /dev/null +++ b/src/js/subtitles.js @@ -0,0 +1,38 @@ +class Subtitles { + constructor(player) { + this.player = player; + + this.player.template.mask.addEventListener('click', () => { + this.hide(); + }); + this.player.template.subtitlesButton.addEventListener('click', () => { + this.show(); + }); + + for (let i = 0; i < this.player.template.subtitlesItem.length; i++) { + this.player.template.subtitlesItem[i].addEventListener('click', () => { + this.hide(); + // clear subtitle show for new subtitle don't have now duration time. If don't, will display last subtitle. + this.player.template.subtitle.innerHTML = `

`; + // update video track src + this.player.template.subtrack.src = this.player.template.subtitlesItem[i].dataset.subtitle; + // update options current subindex for reload (such as changeQuality) + this.player.options.subtitle.index = i; + }); + } + } + + hide() { + this.player.template.subtitlesBox.classList.remove('dplayer-subtitles-box-open'); + this.player.template.mask.classList.remove('dplayer-mask-show'); + this.player.controller.disableAutoHide = false; + } + + show() { + this.player.template.subtitlesBox.classList.add('dplayer-subtitles-box-open'); + this.player.template.mask.classList.add('dplayer-mask-show'); + this.player.controller.disableAutoHide = true; + } +} + +export default Subtitles;