OpenSlides/client/src/app/site/base/list-view-base.ts

126 lines
3.4 KiB
TypeScript
Raw Normal View History

import { MatSnackBar } from '@angular/material';
import { Title } from '@angular/platform-browser';
import { OnDestroy } from '@angular/core';
2018-10-22 16:44:18 +02:00
import { TranslateService } from '@ngx-translate/core';
import { PblDataSource, PblColumnDefinition } from '@pebula/ngrid';
2018-11-02 11:41:03 +01:00
import { BaseViewComponent } from './base-view';
import { BaseViewModel } from './base-view-model';
import { StorageService } from 'app/core/core-services/storage.service';
import { ViewUser } from '../users/models/view-user';
export abstract class ListViewBaseComponent<V extends BaseViewModel> extends BaseViewComponent implements OnDestroy {
/**
* The source of the table data, will be filled by an event emitter
*/
public dataSource: PblDataSource<V>;
2018-11-05 17:40:32 +01:00
/**
* Toggle for enabling the multiSelect mode. Defaults to false (inactive)
*/
protected canMultiSelect = false;
/**
* Current state of the multi select mode. TODO Could be merged with edit mode?
2018-11-05 17:40:32 +01:00
*/
private _multiSelectMode = false;
2018-11-05 17:40:32 +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}.
* Filled using double binding from list-view-tables
2018-11-05 17:40:32 +01:00
*/
public selectedRows: V[];
/**
* Force children to have a tableColumnDefinition
*/
public abstract tableColumnDefinition: PblColumnDefinition[];
/**
* NGrid column width for single buttons
*/
public singleButtonWidth = '40px';
/**
* @param titleService the title service
* @param translate the translate service
* @param matSnackBar showing errors
* @param storage Access the store
*/
public constructor(
titleService: Title,
translate: TranslateService,
matSnackBar: MatSnackBar,
protected storage?: StorageService
) {
2018-11-02 11:41:03 +01:00
super(titleService, translate, matSnackBar);
2018-11-05 17:40:32 +01:00
this.selectedRows = [];
}
/**
* Detect changes to data source
*
* @param newDataSource
*/
public onDataSourceChange(newDataSource: PblDataSource<V>): void {
this.dataSource = newDataSource;
}
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) {
this._multiSelectMode = false;
this.deselectAll();
2018-11-05 17:40:32 +01:00
} else {
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 {
this.dataSource.selection.select(...this.dataSource.source);
2018-12-10 17:54:48 +01:00
}
/**
* Handler to quickly unselect all items.
*/
public deselectAll(): void {
if (this.dataSource) {
this.dataSource.selection.clear();
}
}
2018-11-05 17:40:32 +01:00
/**
* Returns the current state of the multiSelect modus
*/
public get isMultiSelect(): boolean {
return this._multiSelectMode;
2018-11-05 17:40:32 +01:00
}
/**
* Saves the scroll index in the storage
*
* @param key
* @param index
2018-11-05 17:40:32 +01:00
*/
public saveScrollIndex(key: string, index: number): void {
this.storage.set(`scroll_${key}`, index);
2018-11-05 17:40:32 +01: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';
}
}