All files / owid-grapher/grapher/slideshowController SlideShowController.tsx

100% Statements 46/46
100% Branches 5/5
100% Functions 5/5
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x
import { action } from "mobx"
 
export interface SlideShowManager<SlideData> {
    setSlide: (slide: SlideData) => void
}
 
// A "slide" is just a query string.
export class SlideShowController<SlideData> {
    constructor(
        slides: SlideData[] = [],
        currentIndex = 0,
        manager?: SlideShowManager<SlideData>
    ) {
        this.currentIndex = currentIndex
        this.slides = slides
        this.manager = manager
    }
    private slides: SlideData[]
    private currentIndex: number
    private manager?: SlideShowManager<SlideData>
 
    get isEmpty() {
        return this.slides.length === 0
    }
 
    @action.bound private playIndexCommand(index: number) {
        const slides = this.slides
        index = index >= slides.length ? index - slides.length : index
        index = index < 0 ? slides.length + index : index
        const slide = slides[index]
 
        if (this.manager) this.manager.setSlide(slide)
    }
 
    @action.bound playNext() {
        this.playIndexCommand(++this.currentIndex)
    }
 
    @action.bound playPrevious() {
        this.playIndexCommand(--this.currentIndex)
    }
}