Skip to content

Commit

Permalink
feat(time_indicator): updates position time when occurs drag interact…
Browse files Browse the repository at this point in the history
…ion on seek bar element
  • Loading branch information
joaopaulovieira committed Apr 21, 2021
1 parent 786fbf0 commit 109c3df
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/time_indicator/time_indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export default class TimeIndicatorPlugin extends MediaControlComponentPlugin {
get defaultTime() { return '00:00' }

bindEvents() {
const coreEventListenerData = [{ object: this.core, event: Events.CORE_ACTIVE_CONTAINER_CHANGED, callback: this.onContainerChanged }]
const coreEventListenerData = [
{ object: this.core, event: Events.CORE_ACTIVE_CONTAINER_CHANGED, callback: this.onContainerChanged },
{ object: this.core, event: Events.Custom.MEDIA_CONTROL_SEEK_BAR_START_DRAGGING, callback: this.onStartDraggingSeekBar },
{ object: this.core, event: Events.Custom.MEDIA_CONTROL_SEEK_BAR_STOP_DRAGGING, callback: this.onStopDraggingSeekBar },
]
coreEventListenerData.forEach(item => this.stopListening(item.object, item.event, item.callback))
coreEventListenerData.forEach(item => this.listenTo(item.object, item.event, item.callback))
}
Expand All @@ -41,7 +45,7 @@ export default class TimeIndicatorPlugin extends MediaControlComponentPlugin {
}

onTimeUpdate(time) {
if (time.current === null || time.total === null) return
if (time.current === null || time.total === null || this._isDragging) return

const position = Utils.formatTime(Math.floor(time.current))
const duration = Utils.formatTime(Math.floor(time.total))
Expand All @@ -63,6 +67,16 @@ export default class TimeIndicatorPlugin extends MediaControlComponentPlugin {
this.setDuration(this.defaultTime)
}

onStartDraggingSeekBar(data) {
this._isDragging = true
const position = Utils.formatTime(Math.floor(data.event.target.value))
position !== this.$position.textContent && this.setPosition(position)
}

onStopDraggingSeekBar() {
this._isDragging = false
}

render() {
if (this.isRendered) return
this.el.innerHTML = ''
Expand Down
76 changes: 76 additions & 0 deletions src/components/time_indicator/time_indicator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import TimeIndicatorPlugin from './time_indicator'
import templateHTML from './public/template.html'
import MediaControlComponentPlugin from '../../base/media_control_component/media_control_component'

Events.register('MEDIA_CONTROL_SEEK_BAR_START_DRAGGING')
Events.register('MEDIA_CONTROL_SEEK_BAR_STOP_DRAGGING')

const setupTest = (options = {}, fullSetup = false) => {
const core = new Core(options)
const plugin = new TimeIndicatorPlugin(core)
Expand Down Expand Up @@ -82,6 +85,22 @@ describe('TimeIndicatorPlugin', function() {

expect(plugin.onContainerChanged).toHaveBeenCalledTimes(1)
})

test('register onStartDraggingSeekBar method as callback for MEDIA_CONTROL_SEEK_BAR_START_DRAGGING custom event', () => {
jest.spyOn(TimeIndicatorPlugin.prototype, 'onStartDraggingSeekBar')
const { core, plugin } = setupTest()
core.trigger(Events.Custom.MEDIA_CONTROL_SEEK_BAR_START_DRAGGING)

expect(plugin.onStartDraggingSeekBar).toHaveBeenCalledTimes(1)
})

test('register onStopDraggingSeekBar method as callback for MEDIA_CONTROL_SEEK_BAR_STOP_DRAGGING custom event', () => {
jest.spyOn(TimeIndicatorPlugin.prototype, 'onStopDraggingSeekBar')
const { core, plugin } = setupTest()
core.trigger(Events.Custom.MEDIA_CONTROL_SEEK_BAR_STOP_DRAGGING)

expect(plugin.onStopDraggingSeekBar).toHaveBeenCalledTimes(1)
})
})

describe('onContainerChanged method', () => {
Expand Down Expand Up @@ -163,6 +182,16 @@ describe('TimeIndicatorPlugin', function() {
expect(this.plugin.setDuration).not.toHaveBeenCalled()
})

test('avoids update any data if _isDragging flag is truthy', () => {
this.plugin._isDragging = true
this.plugin.onTimeUpdate({ current: null, total: 100 })

expect(this.plugin.setPosition).not.toHaveBeenCalled()
expect(this.plugin.setDuration).not.toHaveBeenCalled()

this.plugin._isDragging = false
})

test('generates time text based on callback response to add on DOM elements', () => {
this.plugin.onTimeUpdate({ current: 1, total: 100 })

Expand Down Expand Up @@ -221,6 +250,53 @@ describe('TimeIndicatorPlugin', function() {
})
})

describe('onStartDraggingSeekBar method', () => {
beforeEach(() => jest.spyOn(this.plugin, 'setPosition'))

test('sets _isDragging flag to true', () => {
expect(this.plugin._isDragging).toBeFalsy()

this.plugin.onStartDraggingSeekBar({ event: { target: { value: 0 } } })

expect(this.plugin._isDragging).toBeTruthy()
})

test('generates time text based on callback response to add on DOM elements', () => {
this.plugin.onStartDraggingSeekBar({ event: { target: { value: 10 } } })

expect(this.plugin.setPosition).toHaveBeenCalledWith('00:10')
})

test('uses Math.floor to only set integer values', () => {
this.plugin.onStartDraggingSeekBar({ event: { target: { value: 10.234234235 } } })

expect(this.plugin.setPosition).toHaveBeenCalledWith('00:10')
})

test('only updates position time text if differs from current DOM element text value', () => {
this.plugin.onStartDraggingSeekBar({ event: { target: { value: 0 } } })

expect(this.plugin.setPosition).not.toHaveBeenCalled()

this.plugin.onStartDraggingSeekBar({ event: { target: { value: 10 } } })

expect(this.plugin.setPosition).toHaveBeenCalledWith('00:10')
expect(this.plugin.setPosition).toHaveBeenCalledTimes(1)
})
})

describe('onStopDraggingSeekBar method', () => {
test('sets _isDragging flag to false', () => {
this.plugin.onStartDraggingSeekBar({ event: { target: { value: 0 } } })

expect(this.plugin._isDragging).toBeTruthy()

this.plugin.onStopDraggingSeekBar()

expect(this.plugin._isDragging).toBeFalsy()
})
})

describe('render method', () => {
beforeEach(() => {
jest.spyOn(this.plugin, 'render')
Expand Down

0 comments on commit 109c3df

Please sign in to comment.