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

77 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Displayable } from './displayable';
import { Identifiable } from '../../shared/models/base/identifiable';
2019-02-08 16:02:46 +01:00
import { Collection } from 'app/shared/models/base/collection';
import { BaseModel } from 'app/shared/models/base/base-model';
export type TitleInformation = object;
2019-02-12 09:25:56 +01:00
export interface ViewModelConstructor<T extends BaseViewModel> {
COLLECTIONSTRING: string;
new (...args: any[]): T;
}
2018-09-10 15:53:11 +02:00
/**
* Base class for view models. alls view models should have titles.
*/
2019-07-17 16:13:49 +02:00
export abstract class BaseViewModel<M extends BaseModel = any> implements Displayable, Identifiable, Collection {
protected _model: M;
public get id(): number {
return this._model.id;
}
/**
2019-02-08 16:02:46 +01:00
* force children of BaseModel to have a collectionString.
*
* Has a getter but no setter.
*/
2019-02-08 16:02:46 +01:00
protected _collectionString: string;
2019-02-08 16:02:46 +01:00
/**
* returns the collectionString.
*
* The server and the dataStore use it to identify the collection.
*/
public get collectionString(): string {
return this._collectionString;
}
2019-06-17 13:45:16 +02:00
/**
* @returns the element id of the model
*/
public get elementId(): string {
return `${this.collectionString}:${this.id}`;
}
public getTitle: () => string;
public getListTitle: () => string;
2019-02-08 16:02:46 +01:00
/**
2019-02-08 16:02:46 +01:00
* Returns the verbose name.
*
* @param plural If the name should be plural
* @returns the verbose name of the model
*/
public getVerboseName: (plural?: boolean) => string;
2018-09-10 15:53:11 +02:00
2019-02-08 16:02:46 +01:00
/**
* @param collectionString
* @param model
2019-02-08 16:02:46 +01:00
*/
public constructor(collectionString: string, model: M) {
2019-02-08 16:02:46 +01:00
this._collectionString = collectionString;
this._model = model;
2019-02-08 16:02:46 +01:00
}
2018-09-10 15:53:11 +02:00
/**
* @returns the main underlying model of the view model
*/
public getModel(): M {
return this._model;
}
public toString(): string {
return this.getTitle();
2018-09-10 15:53:11 +02:00
}
}