Newer
Older
/**
* 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 {
@Input() instance: Instance;
@Input() attributeList: Attribute[];
@Input() outputList: number[];
@Input() queryParams: SearchQueryParams;
@Input() data: any[];
@Input() dataIsLoading: boolean;
@Input() dataIsLoaded: boolean;
@Output() retrieveData: EventEmitter<Pagination> = new EventEmitter();
@Output() addSelectedData: EventEmitter<number | string> = new EventEmitter();
@Output() deleteSelectedData: EventEmitter<number | string> = new EventEmitter();
public page = 1;
public nbItems = 10;
public sortedCol: number = null;
public sortedOrder: PaginationOrder = PaginationOrder.a;
this.sortedCol = this.attributeList.find(a => a.order_by).id;
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 {
return attribute.renderer_config as DetailRendererConfig;
return attribute.renderer_config as LinkRendererConfig;
return attribute.renderer_config as DownloadRendererConfig;
return attribute.renderer_config as ImageRendererConfig;
return attribute.renderer_config as RendererConfig;
}
/**
* Returns output list from attribute list.
*
* @return Attribute[]
*/
getOutputList(): Attribute[] {
return this.attributeList
.filter(a => this.outputList.includes(a.id));
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
}
/**
* 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 = {
page: this.page,
nbItems: this.nbItems,
sortedCol: this.sortedCol,
order: this.sortedOrder
};
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
}
/**
* 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);
}
}