Parcourir la source

feat(sub): subtitles module

yuusora il y a 3 ans
Parent
commit
e7798e8d15
1 fichiers modifiés avec 38 ajouts et 0 suppressions
  1. 38 0
      src/js/subtitles.js

+ 38 - 0
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 = `<p></p>`;
+                // 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;