2019-06-14 11:18:54 +02:00
|
|
|
import { MatSnackBar } from '@angular/material';
|
2018-09-11 16:38:23 +02:00
|
|
|
import { Title } from '@angular/platform-browser';
|
2019-06-14 11:18:54 +02:00
|
|
|
import { OnDestroy } from '@angular/core';
|
2018-10-22 16:44:18 +02:00
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-06-14 11:18:54 +02:00
|
|
|
import { PblDataSource, PblColumnDefinition } from '@pebula/ngrid';
|
2019-04-23 16:57:35 +02:00
|
|
|
|
2018-11-02 11:41:03 +01:00
|
|
|
import { BaseViewComponent } from './base-view';
|
2019-06-14 11:18:54 +02:00
|
|
|
import { BaseViewModel } from './base-view-model';
|
2019-03-21 16:16:19 +01:00
|
|
|
import { StorageService } from 'app/core/core-services/storage.service';
|
2019-06-17 12:50:53 +02:00
|
|
|
import { ViewUser } from '../users/models/view-user';
|
2019-05-07 16:25:39 +02:00
|
|
|
|
2019-06-14 11:18:54 +02:00
|
|
|
export abstract class ListViewBaseComponent<V extends BaseViewModel> extends BaseViewComponent implements OnDestroy {
|
2018-09-11 16:38:23 +02:00
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* The source of the table data, will be filled by an event emitter
|
2018-09-11 16:38:23 +02:00
|
|
|
*/
|
2019-06-14 11:18:54 +02:00
|
|
|
public dataSource: PblDataSource<V>;
|
2018-09-11 16:38:23 +02:00
|
|
|
|
2018-11-05 17:40:32 +01:00
|
|
|
/**
|
|
|
|
* Toggle for enabling the multiSelect mode. Defaults to false (inactive)
|
|
|
|
*/
|
|
|
|
protected canMultiSelect = false;
|
|
|
|
|
|
|
|
/**
|
2018-12-12 18:06:06 +01:00
|
|
|
* Current state of the multi select mode. TODO Could be merged with edit mode?
|
2018-11-05 17:40:32 +01:00
|
|
|
*/
|
2018-12-12 18:06:06 +01:00
|
|
|
private _multiSelectMode = false;
|
2018-11-05 17:40:32 +01:00
|
|
|
|
|
|
|
/**
|
2018-12-12 18:06:06 +01:00
|
|
|
* An array of currently selected items, upon which multi select actions can be performed
|
2018-11-05 17:40:32 +01:00
|
|
|
* see {@link selectItem}.
|
2019-06-14 11:18:54 +02:00
|
|
|
* Filled using double binding from list-view-tables
|
2018-11-05 17:40:32 +01:00
|
|
|
*/
|
|
|
|
public selectedRows: V[];
|
|
|
|
|
2019-03-21 16:16:19 +01:00
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* Force children to have a tableColumnDefinition
|
2019-03-21 16:16:19 +01:00
|
|
|
*/
|
2019-06-14 11:18:54 +02:00
|
|
|
public abstract tableColumnDefinition: PblColumnDefinition[];
|
2019-03-21 16:16:19 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* NGrid column width for single buttons
|
2019-03-21 16:16:19 +01:00
|
|
|
*/
|
2019-06-14 11:18:54 +02:00
|
|
|
public singleButtonWidth = '40px';
|
2019-03-21 16:16:19 +01:00
|
|
|
|
2019-04-04 11:46:30 +02:00
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* @param titleService the title service
|
2018-09-11 16:38:23 +02:00
|
|
|
* @param translate the translate service
|
2019-02-26 15:30:16 +01:00
|
|
|
* @param matSnackBar showing errors
|
2019-05-07 16:25:39 +02:00
|
|
|
* @param storage Access the store
|
2018-09-11 16:38:23 +02:00
|
|
|
*/
|
2019-02-18 13:31:15 +01:00
|
|
|
public constructor(
|
|
|
|
titleService: Title,
|
|
|
|
translate: TranslateService,
|
|
|
|
matSnackBar: MatSnackBar,
|
2019-06-14 11:18:54 +02:00
|
|
|
protected storage?: StorageService
|
2019-02-18 13:31:15 +01:00
|
|
|
) {
|
2018-11-02 11:41:03 +01:00
|
|
|
super(titleService, translate, matSnackBar);
|
2018-11-05 17:40:32 +01:00
|
|
|
this.selectedRows = [];
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:45:34 +01:00
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* Detect changes to data source
|
2019-03-20 12:45:34 +01:00
|
|
|
*
|
2019-06-14 11:18:54 +02:00
|
|
|
* @param newDataSource
|
2019-03-20 12:45:34 +01:00
|
|
|
*/
|
2019-06-14 11:18:54 +02:00
|
|
|
public onDataSourceChange(newDataSource: PblDataSource<V>): void {
|
|
|
|
this.dataSource = newDataSource;
|
2019-03-20 12:45:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 17:40:32 +01:00
|
|
|
/**
|
|
|
|
* enables/disables the multiSelect Mode
|
|
|
|
*/
|
2018-12-10 17:54:48 +01:00
|
|
|
public toggleMultiSelect(): void {
|
2018-11-05 17:40:32 +01:00
|
|
|
if (!this.canMultiSelect || this.isMultiSelect) {
|
2018-12-12 18:06:06 +01:00
|
|
|
this._multiSelectMode = false;
|
2019-06-14 11:18:54 +02:00
|
|
|
this.deselectAll();
|
2018-11-05 17:40:32 +01:00
|
|
|
} else {
|
2018-12-12 18:06:06 +01:00
|
|
|
this._multiSelectMode = true;
|
2018-11-05 17:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:54:48 +01:00
|
|
|
/**
|
|
|
|
* Select all files in the current data source
|
|
|
|
*/
|
|
|
|
public selectAll(): void {
|
2019-06-14 11:18:54 +02:00
|
|
|
this.dataSource.selection.select(...this.dataSource.source);
|
2018-12-10 17:54:48 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 11:18:54 +02:00
|
|
|
/**
|
|
|
|
* Handler to quickly unselect all items.
|
|
|
|
*/
|
2018-12-12 16:14:21 +01:00
|
|
|
public deselectAll(): void {
|
2019-06-14 11:18:54 +02:00
|
|
|
if (this.dataSource) {
|
|
|
|
this.dataSource.selection.clear();
|
|
|
|
}
|
2018-12-12 16:14:21 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 17:40:32 +01:00
|
|
|
/**
|
|
|
|
* Returns the current state of the multiSelect modus
|
|
|
|
*/
|
|
|
|
public get isMultiSelect(): boolean {
|
2018-12-12 18:06:06 +01:00
|
|
|
return this._multiSelectMode;
|
2018-11-05 17:40:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-14 11:18:54 +02:00
|
|
|
* Saves the scroll index in the storage
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* @param index
|
2018-11-05 17:40:32 +01:00
|
|
|
*/
|
2019-06-14 11:18:54 +02:00
|
|
|
public saveScrollIndex(key: string, index: number): void {
|
|
|
|
this.storage.set(`scroll_${key}`, index);
|
2018-11-05 17:40:32 +01:00
|
|
|
}
|
2019-06-17 12:50:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the user is not active, formulate a tooltip accordingly
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* @returns "Inactive" as (untranslated) string if the given user is not active
|
|
|
|
*/
|
|
|
|
public getUserTooltip(user: ViewUser): string {
|
|
|
|
return user.is_active ? '' : 'Inactive';
|
|
|
|
}
|
2018-09-11 16:38:23 +02:00
|
|
|
}
|