import Vue, { CreateElement, VNode } from 'vue'; import { Component, Emit, Inject, Model, Prop, Provide, Watch, } from 'vue-property-decorator'; import { Style, ClassName, Theme, ThemeComponent } from '../../base'; import { mobius } from '../../../utils/math/index'; import { BodyDestroyer } from '../../../plugins/body-render/body-render'; let $$Vue: typeof Vue | undefined; declare module 'vue/types/vue' { interface Vue { $vd_gallery(items: ReadonlyArray, selectedIndex: number): void; } } export interface GalleryItem { format?: 'image' | 'video' | 'audio'; origin: string; fullscreen?: string; preview?: string; thumbnail?: string; label?: string; } /** * Component: Gallery */ @Component export class VdGallery extends Vue implements ThemeComponent { // tslint:disable-next-line:function-name public static install($Vue: typeof Vue): void { if ($$Vue && $$Vue === $Vue) { return; } $$Vue = $Vue; // tslint:disable-next-line:no-unsafe-any Object.defineProperty($Vue.prototype, '$vd_gallery', { writable: false, value: function( this: Vue, items: ReadonlyArray, selectedIndex: number, ): void { VdGallery.create(this, items, selectedIndex); }, }); } private static create( parent: Vue, items: ReadonlyArray, selectedIndex: number, ): void { const gallery = new VdGallery({ parent, propsData: { itemsSource: items, }, }); gallery.selectedIndex = selectedIndex; gallery.switchFullscreen(); gallery.$once('close', () => gallery.$destroy()); } @Prop(String) public readonly theme?: Theme; public get themeValue(): Theme { return this.theme || this.$vd_theme.theme || 'lite'; } public get classes(): ClassName { return [`vdp-theme_${this.themeValue}`]; } @Prop(String) public readonly label?: string; @Prop({ type: Array, required: true }) public readonly itemsSource!: GalleryItem[]; private offset: Style = { transform: null }; private thumbnailOffset: Style = { transform: null }; private index: number = 0; public get selectedIndex(): number { return this.index; } public set selectedIndex(value: number) { const index = mobius(value, this.itemsSource.length); this.index = index; this.offset.transform = `translateX(${-100 * index}%)`; const thumbnailIndex = this.itemsSource.length <= 6 ? 0 : this.itemsSource.length - 6 < index ? this.itemsSource.length - 6 : index > 0 ? index - 1 : index; this.thumbnailOffset.transform = `translateX(${(-100 * thumbnailIndex) / 6}%)`; this.$emit('change', index); } private destroyBodyHandler?: BodyDestroyer; public switchFullscreen(): void { if (this.destroyBodyHandler) { this.destroyBodyHandler(); this.destroyBodyHandler = undefined; this.$emit('close'); } else { this.destroyBodyHandler = this.$vd_bodyRender(this.renderFullscreen); this.$emit('open'); } } private onSelect(index: number): (event: MouseEvent) => void { return e => (this.selectedIndex = index); } private onSelectPrev(event: MouseEvent): void { this.selectedIndex--; } private onSelectNext(event: MouseEvent): void { this.selectedIndex++; } private beforeDestroy(): void { if (this.destroyBodyHandler) { this.destroyBodyHandler(); } } private renderFullscreen(h: CreateElement): VNode { return (
    {this.itemsSource.map((item, index) => ( ))}
    {this.itemsSource.length < 6 && (
  • )} {this.itemsSource.map((item, index) => (
  • ))} {this.itemsSource.length < 6 && (
  • )}
); } private render(h: CreateElement): VNode { return (
    {this.itemsSource.map((item, index) => ( ))}
    {this.itemsSource.length < 6 && (
  • )} {this.itemsSource.map((item, index) => (
  • ))} {this.itemsSource.length < 6 && (
  • )}
); } }