diff --git a/CHANGELOG.md b/CHANGELOG.md index 51e1ede01..26f6282af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * @m14t removed unused loadEvent property in ControlBar options ([view](https://github.com/videojs/video.js/pull/3363)) * @bklava updated pt-BR language file ([view](https://github.com/videojs/video.js/pull/3373)) * @mister-ben updated menus to use default videojs font-family ([view](https://github.com/videojs/video.js/pull/3384)) +* @vdeshpande fixed chapters getting duplicated each time a track is loaded ([view](https://github.com/videojs/video.js/pull/3354)) -------------------- diff --git a/src/js/control-bar/text-track-controls/chapters-button.js b/src/js/control-bar/text-track-controls/chapters-button.js index a885c751e..0cc8606ea 100644 --- a/src/js/control-bar/text-track-controls/chapters-button.js +++ b/src/js/control-bar/text-track-controls/chapters-button.js @@ -75,9 +75,11 @@ class ChaptersButton extends TextTrackButton { createMenu() { let tracks = this.player_.textTracks() || []; let chaptersTrack; - let items = this.items = []; + let items = this.items || []; - for (let i = 0, length = tracks.length; i < length; i++) { + for (let i = tracks.length - 1; i >= 0; i--) { + + // We will always choose the last track as our chaptersTrack let track = tracks[i]; if (track['kind'] === this.kind_) { @@ -97,6 +99,12 @@ class ChaptersButton extends TextTrackButton { }); menu.children_.unshift(title); Dom.insertElFirst(title, menu.contentEl()); + } else { + // We will empty out the menu children each time because we want a + // fresh new menu child list each time + items.forEach(item => menu.removeChild(item)); + // Empty out the ChaptersButton menu items because we no longer need them + items = []; } if (chaptersTrack && chaptersTrack.cues == null) { @@ -124,17 +132,15 @@ class ChaptersButton extends TextTrackButton { menu.addChild(mi); } - - this.addChild(menu); } - if (this.items.length > 0) { + if (items.length > 0) { this.show(); } - + // Assigning the value of items back to this.items for next iteration + this.items = items; return menu; } - } ChaptersButton.prototype.kind_ = 'chapters';