import Vue, { CreateElement, VNode } from 'vue'; import { Component, Emit, Inject, Model, Prop, Provide, Watch, } from 'vue-property-decorator'; import { ClassName, Style, AroundPosition, LinkLikeComponent } from '../../base'; /** * Component: Carousel */ @Component export class VdCarousel extends Vue { @Prop({ type: String, default: 'div' }) public readonly containerTag!: string; @Prop({ type: String, default: 'inside' }) public readonly indicatorPosition!: AroundPosition; @Prop({ type: String, default: 'inside' }) public readonly switchButtonPosition!: AroundPosition; private switching: boolean = false; private readonly items: VdCarouselItem[] = []; public add(item: VdCarouselItem): void { const length = this.items.push(item); if (length === 1) { this.selectedItem = item; } } public remove(item: VdCarouselItem): void { const index = this.items.indexOf(item); if (index > -1) { this.items.splice(index, 1); } } private timeoutHandler?: number; private selectedItem: VdCarouselItem | null = null; public get selectedIndex(): number { return this.selectedItem ? this.items.indexOf(this.selectedItem) : -1; } public set selectedIndex(value: number) { let index = value; const selectedIndex = this.selectedIndex; const size = this.items.length; if (window === undefined || this.switching || size < 2 || index === selectedIndex) { return; } if (index < 0) { index = (size + (index % size)) % size; } else if (index > size - 1) { index = index % size; } this.switching = true; this.selectedItem = this.items[index]; this.$emit('change', index); window.setTimeout(() => (this.switching = false), 320); window.clearTimeout(this.timeoutHandler); this.timeoutHandler = window.setTimeout(() => this.selectedIndex++, 5000); } public get classes(): ClassName { return [ { 'is-switching': this.switching, }, ]; } public get wrapperStyle(): Style { return { transform: `translateX(-${this.selectedIndex * 100}%)`, }; } private mounted(): void { window.setTimeout(() => this.selectedIndex++, 5000); } private render(h: CreateElement): VNode { return (