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 transcript sidebar for multiple media objects #2268

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion app/components/embed/media_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</div>
</div>
</div>
<div class="left-drawer-content transcript" data-action="media-loaded@window->transcript#persistPlayer media-data-loaded@window->transcript#load time-update@window->transcript#highlightCue">
<div class="left-drawer-content transcript" data-action="media-loaded@window->transcript#persistPlayer switch-transcript@window->transcript#switchTranscript media-data-loaded@window->transcript#load time-update@window->transcript#highlightCue">
<div data-transcript-target="outlet"></div>
</div>
</section>
Expand Down
4 changes: 3 additions & 1 deletion app/components/embed/media_tag_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def media_tag # rubocop:disable Metrics/MethodLength
id: "sul-embed-media-#{@resource_iteration.index}",
data: {
auth_url: authentication_url,
index: @resource_iteration.index,
media_tag_target: 'authorizeableResource',
controller: 'media-player',
action: 'media-seek@window->media-player#seek ' \
Expand All @@ -82,8 +83,9 @@ def media_tag # rubocop:disable Metrics/MethodLength
end

def streaming_source
type = Rails.env.development? ? file.mimetype : 'application/x-mpegURL'
stacks_media_stream = Embed::StacksMediaStream.new(druid:, file:)
tag.source(src: stacks_media_stream.to_playlist_url, type: 'application/x-mpegURL')
tag.source(src: stacks_media_stream.to_playlist_url, type:)
end

# Generate the video caption elements
Expand Down
18 changes: 15 additions & 3 deletions app/javascript/controllers/media_player_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ export default class extends Controller {
const event = new CustomEvent('time-update', { detail: timestamp })
window.dispatchEvent(event)
})

this.player.index = this.element.dataset.index;

this.player.on('loadedmetadata', (evt) => {
// only load media for the first player
if (evt.target.player.index == 0){
const event = new CustomEvent('media-loaded', { detail: this.player });
window.dispatchEvent(event);
}
})

// The loadeddata event occurs when the first frame of the video is available, and
// happens after loadedmetadata
this.player.on('loadeddata', () => {
const event = new CustomEvent('media-data-loaded')
window.dispatchEvent(event)
this.player.on('loadeddata', (evt) => {
if (evt.target.player.index == 0){
const event = new CustomEvent('media-data-loaded');
window.dispatchEvent(event);
}
})
}

Expand Down
11 changes: 8 additions & 3 deletions app/javascript/controllers/media_wrapper_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ export default class extends Controller {
toggleVisibility(event) {
const index = event.detail.index
this.element.hidden = this.indexValue !== index
this.pauseAllMedia()
this.pauseAllMedia(index)
}

pauseAllMedia() {
// switch transcript if media object index is the same as the visible index.
pauseAllMedia(index) {
dnoneill marked this conversation as resolved.
Show resolved Hide resolved
const mediaObject = this.element.querySelector('.video-js')
if (mediaObject) {
videojs(mediaObject.id).pause()
const playerObject = videojs(mediaObject.id);
playerObject.pause()
if (mediaObject.dataset.index == index){
window.dispatchEvent(new CustomEvent('switch-transcript', { detail: playerObject }))
}
}
}
}
15 changes: 9 additions & 6 deletions app/javascript/controllers/transcript_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export default class extends Controller {
this.loaded = true
}

// event called when switch-transcript event is fired.
// This really only happens when there are more than one media item with captions.
switchTranscript(evt) {
this.player = evt.detail;
this.setupTranscriptLanguageSwitching();
this.renderCues();
}

// Safari cues require special handling.
// We want the track cues to be available so we can properly generate the transcript sidebar language dropdown
// if there is more than one language track.
Expand Down Expand Up @@ -136,12 +144,7 @@ export default class extends Controller {
}

setupTranscriptLanguageSwitching() {
this.captionTracks.forEach(track => {
this.captionLanguageSelectTarget.insertAdjacentHTML(
'beforeend',
`<option value="${track.language}">${track.label}</option>`
)
})
this.captionLanguageSelectTarget.innerHTML = this.captionTracks.map(track => `<option value="${track.language}">${track.label}</option>`).join("");
}

buildCue(cue) {
Expand Down
Loading