Skip to content
Snippets Groups Projects
datatable.component.ts 5.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * This file is part of Anis Client.
     *
     * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
    
    
    import {
        Instance,
        Attribute,
        Dataset,
        DetailRendererConfig,
        DownloadRendererConfig,
        ImageRendererConfig,
        LinkRendererConfig,
        RendererConfig } from 'src/app/metamodel/models';
    
    import { Pagination, PaginationOrder, SearchQueryParams } from 'src/app/instance/store/models';
    
    
    /**
     * @class
     * @classdesc Datatable component.
     *
     * @implements OnInit
     */
    
    @Component({
        selector: 'app-datatable',
        templateUrl: 'datatable.component.html',
        styleUrls: ['datatable.component.scss'],
    })
    
    export class DatatableComponent implements OnInit {
    
    François Agneray's avatar
    François Agneray committed
        @Input() dataset: Dataset;
    
        @Input() instance: Instance;
    
        @Input() attributeList: Attribute[];
        @Input() outputList: number[];
    
        @Input() queryParams: SearchQueryParams;
    
        @Input() dataLength: number;
    
    François Agneray's avatar
    François Agneray committed
        @Input() data: any[];
        @Input() dataIsLoading: boolean;
        @Input() dataIsLoaded: boolean;
    
        @Input() selectedData: any[] = [];
    
    François Agneray's avatar
    François Agneray committed
        @Output() retrieveData: EventEmitter<Pagination> = new EventEmitter();
    
        @Output() addSelectedData: EventEmitter<number | string> = new EventEmitter();
        @Output() deleteSelectedData: EventEmitter<number | string> = new EventEmitter();
    
    François Agneray's avatar
    François Agneray committed
    
        public page = 1;
        public nbItems = 10;
        public sortedCol: number = null;
        public sortedOrder: PaginationOrder = PaginationOrder.a;
    
        ngOnInit(): void {
    
            this.sortedCol = this.attributeList.find(a => a.order_by).id;
    
    François Agneray's avatar
    François Agneray committed
            Promise.resolve(null).then(() => this.retrieveData.emit({
                dname: this.dataset.name,
                page: this.page,
                nbItems: this.nbItems,
                sortedCol: this.sortedCol,
                order: this.sortedOrder
            }));
    
        /**
         * Returns renderer configuration for the given attribute.
         *
         * @param  {Attribute} attribute - The attribute.
         *
         * @return DetailRendererConfig | LinkRendererConfig | DownloadRendererConfig | ImageRendererConfig | RendererConfig | null
         */
        getRendererConfig(attribute: Attribute): DetailRendererConfig | LinkRendererConfig | DownloadRendererConfig | ImageRendererConfig | RendererConfig | null {
    
    François Agneray's avatar
    François Agneray committed
            switch(attribute.renderer) {
                case 'detail':
    
                    return  attribute.renderer_config as DetailRendererConfig;
    
    François Agneray's avatar
    François Agneray committed
                case 'link':
    
                    return attribute.renderer_config as LinkRendererConfig;
    
    François Agneray's avatar
    François Agneray committed
                case 'download':
    
                    return attribute.renderer_config as DownloadRendererConfig;
    
    François Agneray's avatar
    François Agneray committed
                case 'image':
    
                    return attribute.renderer_config as ImageRendererConfig;
    
    François Agneray's avatar
    François Agneray committed
                case 'json':
    
                    return attribute.renderer_config as RendererConfig;
    
    François Agneray's avatar
    François Agneray committed
                default:
    
                    return null;
    
    François Agneray's avatar
    François Agneray committed
            }
    
        }
    
        /**
         * Returns output list from attribute list.
         *
         * @return Attribute[]
         */
        getOutputList(): Attribute[] {
            return this.attributeList
    
                .filter(a => this.outputList.includes(a.id));
    
        }
    
        /**
         * Emits events to select or unselect data.
         *
         * @param  {any} datum - The data to select or unselect.
         *
         * @fires EventEmitter<number | string>
         */
        toggleSelection(datum: any): void {
            const attribute = this.attributeList.find(a => a.search_flag === 'ID');
            const index = this.selectedData.indexOf(datum[attribute.label]);
            if (index > -1) {
                this.deleteSelectedData.emit(datum[attribute.label]);
            } else {
                this.addSelectedData.emit(datum[attribute.label]);
            }
        }
    
        /**
         * Checks if data is selected.
         *
         * @param  {any} datum - The data.
         *
         * @return boolean
         */
        isSelected(datum: any): boolean {
            const attribute = this.attributeList.find(a => a.search_flag === 'ID');
    
            if (this.selectedData.indexOf(datum[attribute.label]) > -1) {
                return true;
            }
            return false;
        }
    
        /**
         * Emits event to change datatable page.
         *
         * @param  {number} nb - The page number to access.
         *
         * @fires EventEmitter<Pagination>
         */
        changePage(nb: number): void {
            this.page = nb;
            const pagination: Pagination = {
    
    François Agneray's avatar
    François Agneray committed
                dname: this.dataset.name,
    
                page: this.page,
                nbItems: this.nbItems,
                sortedCol: this.sortedCol,
                order: this.sortedOrder
            };
    
    François Agneray's avatar
    François Agneray committed
            this.retrieveData.emit(pagination);
    
        }
    
        /**
         * Emits event to change datatable displayed items.
         *
         * @param  {number} nb - The number of items to display.
         *
         * @fires EventEmitter<Pagination>
         */
        changeNbItems(nb: number): void {
            this.nbItems = nb;
            this.changePage(1);
        }
    
        /**
         * Emits event to change the sorted order and the sorted column of the datatable.
         *
         * @param  {number} id - The id of the column to sort.
         *
         * @fires EventEmitter<Pagination>
         */
        sort(id: number): void {
            if (id === this.sortedCol) {
                this.sortedOrder = this.sortedOrder === PaginationOrder.a ? PaginationOrder.d : PaginationOrder.a;
            } else {
                this.sortedCol = id;
                this.sortedOrder = PaginationOrder.a;
            }
            this.changePage(1);
        }
    }