Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
fix(current-chapter): Enable current chapter preview
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-heimbuch committed Oct 27, 2017
1 parent af73d10 commit 9c9b91c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/components/player/progress-bar/CurrentChapter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

<script>
import get from 'lodash/get'
import { currentChapter, currentChapterIndex } from 'utils/chapters'
import { currentChapter, currentChapterIndex, currentChapterByPlaytime } from 'utils/chapters'
export default {
data () {
return {
chapters: this.$select('chapters'),
ghost: this.$select('ghost'),
theme: this.$select('theme')
}
},
Expand All @@ -27,7 +28,14 @@
},
chapterTitle () {
const current = currentChapter(this.chapters)
let current
if (!this.ghost.active) {
current = currentChapter(this.chapters)
} else {
current = currentChapterByPlaytime(this.chapters)(this.ghost.time)
}
return get(current, 'title', '')
}
},
Expand Down
15 changes: 14 additions & 1 deletion src/utils/chapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ const currentChapterIndex = findIndex({active: true})

const currentChapter = find({active: true})

const currentChapterByPlaytime = chapters => playtime => find(chapter => {
if (playtime < chapter.start) {
return false
}

if (playtime >= chapter.end) {
return false
}

return true
})(chapters)

export {
currentChapter,
currentChapterIndex
currentChapterIndex,
currentChapterByPlaytime
}
13 changes: 11 additions & 2 deletions src/utils/chapters.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import test from 'ava'
import { currentChapter, currentChapterIndex } from './chapters'
import { currentChapter, currentChapterIndex, currentChapterByPlaytime } from './chapters'

test.beforeEach(t => {
t.context.inactiveChapter = {
start: 10,
start: 0,
end: 3600,
title: 'Inactive Chapter',
active: false
}

t.context.activeChapter = {
start: 3600,
end: 7200,
title: 'Active Chapter',
active: true
}

t.context.additionalChapter = {
start: 7200,
end: 9000,
title: 'Additional Chapter',
active: false
}
Expand Down Expand Up @@ -44,3 +47,9 @@ test('currentChapterIndex should return undefined if no matches', t => {
test('currentChapterIndex should find the active chapter index in a list', t => {
t.is(currentChapterIndex([t.context.inactiveChapter, t.context.activeChapter]), 1)
})

test('currentChapterByPlaytime should find the active chapter by playtime', t => {
t.is(currentChapterByPlaytime([t.context.inactiveChapter, t.context.activeChapter, t.context.additionalChapter])(11), t.context.inactiveChapter)
t.is(currentChapterByPlaytime([t.context.inactiveChapter, t.context.activeChapter, t.context.additionalChapter])(4000), t.context.activeChapter)
t.is(currentChapterByPlaytime([t.context.activeChapter, t.context.additionalChapter, t.context.inactiveChapter])(3500), t.context.inactiveChapter)
})

0 comments on commit 9c9b91c

Please sign in to comment.