2018-09-04 14:55:07 +02:00
|
|
|
import { Motion } from '../../../shared/models/motions/motion';
|
|
|
|
import { Category } from '../../../shared/models/motions/category';
|
|
|
|
import { User } from '../../../shared/models/users/user';
|
|
|
|
import { Workflow } from '../../../shared/models/motions/workflow';
|
|
|
|
import { WorkflowState } from '../../../shared/models/motions/workflow-state';
|
2018-09-13 09:23:57 +02:00
|
|
|
import { BaseModel } from '../../../shared/models/base/base-model';
|
2018-09-11 16:38:23 +02:00
|
|
|
import { BaseViewModel } from '../../base/base-view-model';
|
2018-09-10 15:53:11 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-09-04 14:55:07 +02:00
|
|
|
/**
|
|
|
|
* Motion class for the View
|
|
|
|
*
|
|
|
|
* Stores a motion including all (implicit) references
|
|
|
|
* Provides "safe" access to variables and functions in {@link Motion}
|
|
|
|
* @ignore
|
|
|
|
*/
|
2018-09-10 15:53:11 +02:00
|
|
|
export class ViewMotion extends BaseViewModel {
|
2018-09-04 14:55:07 +02:00
|
|
|
private _motion: Motion;
|
|
|
|
private _category: Category;
|
|
|
|
private _submitters: User[];
|
|
|
|
private _supporters: User[];
|
|
|
|
private _workflow: Workflow;
|
|
|
|
private _state: WorkflowState;
|
|
|
|
|
|
|
|
public get motion(): Motion {
|
|
|
|
return this._motion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get id(): number {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.id : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get identifier(): string {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.identifier : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get title(): string {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.title : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get text(): string {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.text : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get reason(): string {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.reason : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get category(): Category {
|
|
|
|
return this._category;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get categoryId(): number {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion && this.category ? this.motion.category_id : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get submitters(): User[] {
|
|
|
|
return this._submitters;
|
|
|
|
}
|
|
|
|
|
2018-09-20 12:25:37 +02:00
|
|
|
public get submitterIds(): number[] {
|
|
|
|
return this.motion ? this.motion.submitters_id : null;
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
public get supporters(): User[] {
|
|
|
|
return this._supporters;
|
|
|
|
}
|
|
|
|
|
2018-09-20 12:25:37 +02:00
|
|
|
public get supporterIds(): number[] {
|
|
|
|
return this.motion ? this.motion.supporters_id : null;
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
public get workflow(): Workflow {
|
|
|
|
return this._workflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get state(): WorkflowState {
|
|
|
|
return this._state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get stateId(): number {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion && this.motion.state_id ? this.motion.state_id : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get recommendationId(): number {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion && this.motion.recommendation_id ? this.motion.recommendation_id : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FIXME:
|
|
|
|
* name of recommender exist in a config
|
|
|
|
* previously solved using `this.DS.filter<Config>(Config)`
|
|
|
|
* and checking: motionsRecommendationsByConfig.value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public get recommender(): string {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get recommendation(): WorkflowState {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.recommendationId && this.workflow ? this.workflow.getStateById(this.recommendationId) : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get origin(): string {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.motion ? this.motion.origin : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get nextStates(): WorkflowState[] {
|
2018-09-11 16:38:23 +02:00
|
|
|
return this.state && this.workflow ? this.state.getNextStates(this.workflow) : null;
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 12:25:37 +02:00
|
|
|
public set supporters(users: User[]) {
|
|
|
|
const userIDArr: number[] = [];
|
|
|
|
users.forEach(user => {
|
|
|
|
userIDArr.push(user.id);
|
|
|
|
});
|
|
|
|
this._supporters = users;
|
|
|
|
this._motion.supporters_id = userIDArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set submitters(users: User[]) {
|
|
|
|
// For the newer backend with weight:
|
|
|
|
// const submitterArr: MotionSubmitter[] = []
|
|
|
|
// users.forEach(user => {
|
|
|
|
// const motionSub = new MotionSubmitter();
|
|
|
|
// submitterArr.push(motionSub);
|
|
|
|
// });
|
|
|
|
// this._motion.submitters = submitterArr;
|
|
|
|
this._submitters = users;
|
|
|
|
const submitterIDArr: number[] = [];
|
|
|
|
// for the older backend:
|
|
|
|
users.forEach(user => {
|
|
|
|
submitterIDArr.push(user.id);
|
|
|
|
});
|
|
|
|
this._motion.submitters_id = submitterIDArr;
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
public constructor(
|
|
|
|
motion?: Motion,
|
|
|
|
category?: Category,
|
|
|
|
submitters?: User[],
|
|
|
|
supporters?: User[],
|
|
|
|
workflow?: Workflow,
|
|
|
|
state?: WorkflowState
|
|
|
|
) {
|
2018-09-10 15:53:11 +02:00
|
|
|
super();
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
this._motion = motion;
|
|
|
|
this._category = category;
|
|
|
|
this._submitters = submitters;
|
|
|
|
this._supporters = supporters;
|
|
|
|
this._workflow = workflow;
|
|
|
|
this._state = state;
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:53:11 +02:00
|
|
|
public getTitle(translate?: TranslateService): string {
|
|
|
|
return this.title;
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
/**
|
|
|
|
* Updates the local objects if required
|
|
|
|
* @param update
|
|
|
|
*/
|
|
|
|
public updateValues(update: BaseModel): void {
|
|
|
|
if (update instanceof Workflow) {
|
2018-09-20 12:25:37 +02:00
|
|
|
this.updateWorkflow(update as Workflow);
|
2018-09-04 14:55:07 +02:00
|
|
|
} else if (update instanceof Category) {
|
2018-09-20 12:25:37 +02:00
|
|
|
this.updateCategory(update as Category);
|
2018-09-04 14:55:07 +02:00
|
|
|
}
|
|
|
|
// TODO: There is no way (yet) to add Submitters to a motion
|
|
|
|
// Thus, this feature could not be tested
|
|
|
|
}
|
|
|
|
|
2018-09-20 12:25:37 +02:00
|
|
|
/**
|
|
|
|
* Updates the Category
|
|
|
|
*/
|
|
|
|
public updateCategory(update: Category): void {
|
|
|
|
if (this.motion && update.id === this.motion.category_id) {
|
|
|
|
this._category = update as Category;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* updates the Workflow
|
|
|
|
*/
|
|
|
|
public updateWorkflow(update: Workflow): void {
|
|
|
|
if (this.motion && update.id === this.motion.workflow_id) {
|
|
|
|
this._workflow = update as Workflow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
public hasSupporters(): boolean {
|
|
|
|
return !!(this.supporters && this.supporters.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplicate this motion into a copy of itself
|
|
|
|
*/
|
|
|
|
public copy(): ViewMotion {
|
|
|
|
return new ViewMotion(
|
|
|
|
this._motion,
|
|
|
|
this._category,
|
|
|
|
this._submitters,
|
|
|
|
this._supporters,
|
|
|
|
this._workflow,
|
|
|
|
this._state
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|