Skip to content

Commit

Permalink
Edit section via Lesson#show player
Browse files Browse the repository at this point in the history
- Make the player controller manage the state of the edition mode
  instead of relying on the starting player element's dataset
- Listen to the turbo events to change the player mode to edit when the
  sections section is replaced by the section edit
- Listen to the turbo events to reset the player (and the edit mode)
  when the section form is cancelled or submitted (shotgun for now)
  • Loading branch information
mauriciofierrom committed Nov 12, 2023
1 parent 1c0f82a commit e0762be
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/javascript/controllers/player_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class extends Controller {
startPending;
endPending;
settingCount = 3;
edit = false;

static targets = [ "source", "duration" ]

Expand All @@ -29,6 +30,7 @@ export default class extends Controller {
this.loop = this.element.dataset.loop === "true" ? true : false
this.start = parseFloat(this.element.dataset.start) || 0.00
this.end = parseFloat(this.element.dataset.end)
this.edit = this.element.dataset.edit === "true" ? true : false

window.onYouTubeIframeAPIReady = () => {
this.loaded = true
Expand All @@ -37,7 +39,7 @@ export default class extends Controller {
height: '390',
videoId: this.videoId || '',
playerVars: {
'autoplay': this.element.dataset.edit === "true" ? 1 : 0,
'autoplay': this.edit,
'disablekb': 1,
'controls': 0,
'start': this.start || 0
Expand Down Expand Up @@ -70,6 +72,20 @@ export default class extends Controller {
}
})
}

document.documentElement.addEventListener('turbo:before-fetch-request', (e) => {
if (e.srcElement.id === "sections") {
this.edit = !this.edit
if (!this.edit) {
this.#resetPlayer()
}
}
})

document.documentElement.addEventListener('turbo:submit-end', (e) => {
this.edit = !this.edit
this.#resetPlayer()
})
}

load() {
Expand Down Expand Up @@ -116,7 +132,7 @@ export default class extends Controller {
console.log(`Play from: ${this.start} to: ${this.end}`)
this.#resetPlayback()

if (this.element.dataset.edit === "true" ? true : false)
if (this.edit)
{
if (this.start !== start) {
console.log(`Start changed: from ${this.start} to ${start}`)
Expand Down Expand Up @@ -180,4 +196,10 @@ export default class extends Controller {
this.settingEnd = false
}
}

#resetPlayer() {
this.player.stopVideo()
this.start = parseFloat(this.element.dataset.start) || 0.00
this.end = parseFloat(this.element.dataset.end)
}
}

0 comments on commit e0762be

Please sign in to comment.