Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Generate chapters menu only when needed, and don't create orphaned event listeners #7604

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/js/control-bar/text-track-controls/chapters-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class ChaptersButton extends TextTrackButton {
*/
constructor(player, options, ready) {
super(player, options, ready);

this.selectCurrentItem_ = () => {
this.items.forEach(item => {
item.selected(this.track_.activeCues[0] === item.cue);
});
};
}

/**
Expand Down Expand Up @@ -56,10 +62,19 @@ class ChaptersButton extends TextTrackButton {
* @listens TextTrackList#change
*/
update(event) {
if (!this.track_ || (event && (event.type === 'addtrack' || event.type === 'removetrack'))) {
this.setTrack(this.findChaptersTrack());
if (event && event.track && event.track.kind !== 'chapters') {
return;
}

const track = this.findChaptersTrack();

if (track !== this.track_) {
this.setTrack(track);
super.update();
} else if (!this.items || (track && track.cues && track.cues.length !== this.items.length)) {
// Update the menu initially or if the number of cues has changed since set
super.update();
}
super.update();
}

/**
Expand All @@ -86,6 +101,8 @@ class ChaptersButton extends TextTrackButton {
remoteTextTrackEl.removeEventListener('load', this.updateHandler_);
}

this.track_.removeEventListener('cuechange', this.selectCurrentItem_);

this.track_ = null;
}

Expand All @@ -100,6 +117,8 @@ class ChaptersButton extends TextTrackButton {
if (remoteTextTrackEl) {
remoteTextTrackEl.addEventListener('load', this.updateHandler_);
}

this.track_.addEventListener('cuechange', this.selectCurrentItem_);
}
}

Expand Down Expand Up @@ -175,6 +194,7 @@ class ChaptersButton extends TextTrackButton {

return items;
}

}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/js/control-bar/text-track-controls/chapters-track-menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import MenuItem from '../../menu/menu-item.js';
import Component from '../../component.js';
import * as Fn from '../../utils/fn.js';

/**
* The chapter track menu item
Expand Down Expand Up @@ -35,7 +34,6 @@ class ChaptersTrackMenuItem extends MenuItem {

this.track = track;
this.cue = cue;
track.addEventListener('cuechange', Fn.bind(this, this.update));
}

/**
Expand All @@ -52,23 +50,6 @@ class ChaptersTrackMenuItem extends MenuItem {
handleClick(event) {
super.handleClick();
this.player_.currentTime(this.cue.startTime);
this.update(this.cue.startTime);
}

/**
* Update chapter menu item
*
* @param {EventTarget~Event} [event]
* The `cuechange` event that caused this function to run.
*
* @listens TextTrack#cuechange
*/
update(event) {
const cue = this.cue;
const currentTime = this.player_.currentTime();

// vjs.log(currentTime, cue.startTime);
this.selected(cue.startTime <= currentTime && currentTime < cue.endTime);
}

}
Expand Down
43 changes: 43 additions & 0 deletions test/unit/tracks/text-track-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,46 @@ QUnit.test('chapters should be displayed when remote track added and load event

player.dispose();
});

QUnit.test('chapters button should update selected menu item', function(assert) {
const player = TestHelpers.makePlayer();

this.clock.tick(1000);

const chaptersEl = player.addRemoteTextTrack(chaptersTrack, true);

chaptersEl.track.addCue({
startTime: 0,
endTime: 2,
text: 'Chapter 1'
});
chaptersEl.track.addCue({
startTime: 2,
endTime: 4,
text: 'Chapter 2'
});

assert.equal(chaptersEl.track.cues.length, 2);

if (player.tech_.featuresNativeTextTracks) {
TestHelpers.triggerDomEvent(chaptersEl, 'load');
} else {
chaptersEl.trigger('load');
}

const menuItems = player.controlBar.chaptersButton.items;

assert.ok(menuItems.find(i => i.isSelected_) === menuItems[0], 'item with startTime 0 selected on init');

player.currentTime(4);
player.trigger('timeupdate');

assert.ok(menuItems.find(i => i.isSelected_) === menuItems[1], 'second item selected on cuechange');

player.currentTime(1);
player.trigger('timeupdate');

assert.ok(menuItems.find(i => i.isSelected_) === menuItems[0], 'first item selected on cuechange');

player.dispose();
});