diff --git a/client/src/app/shared/models/motions/motion.ts b/client/src/app/shared/models/motions/motion.ts index f78e69542..38de71b0a 100644 --- a/client/src/app/shared/models/motions/motion.ts +++ b/client/src/app/shared/models/motions/motion.ts @@ -37,9 +37,7 @@ export class Motion extends BaseModel { agenda_item_id: number; log_messages: MotionLog[]; - // read from config - workflow_id: number; - // by the config above + // dynamic values workflow: Workflow; // for request @@ -100,31 +98,31 @@ export class Motion extends BaseModel { } /** - * sets the workflow_id and the workflow + * sets the and the workflow from either dataStore or WebSocket */ initDataStoreValues() { - const motionsWorkflowConfig = this.DS.filter(Config, config => config.key === 'motions_workflow')[0] as Config; - if (motionsWorkflowConfig) { - this.workflow_id = +motionsWorkflowConfig.value; - } else { - this.DS.getObservable().subscribe(newConfig => { - if (newConfig instanceof Config && newConfig.key === 'motions_workflow') { - this.workflow_id = +newConfig.value; - } - }); - } + // check the containing Workflows in DataStore + const allWorkflows = this.DS.get(Workflow) as Workflow[]; + allWorkflows.forEach(localWorkflow => { + if (localWorkflow.isStateContained(this.state_id)) { + this.workflow = localWorkflow as Workflow; + } + }); - this.workflow = this.DS.get(Workflow, this.workflow_id) as Workflow; - if (!this.workflow.id) { - this.DS.getObservable().subscribe(newModel => { - if (newModel instanceof Workflow && newModel.id === this.workflow_id) { - this.workflow = newModel; + // observe for new models + this.DS.getObservable().subscribe(newModel => { + if (newModel instanceof Workflow) { + if (newModel.isStateContained(this.state_id)) { + this.workflow = newModel as Workflow; } - }); - } + } + }); } - /** add a new motionSubmitter from user-object */ + /** + * add a new motionSubmitter from user-object + * @param user the user + */ addSubmitter(user: User) { const newSubmitter = new MotionSubmitter(null, user.id); this.submitters.push(newSubmitter); @@ -142,6 +140,11 @@ export class Motion extends BaseModel { } } + /** + * Patch the current version + * + * TODO: Altering the current version should be avoided. + */ set currentTitle(newTitle: string) { if (this.versions[0]) { this.versions[0].title = newTitle; @@ -221,9 +224,8 @@ export class Motion extends BaseModel { * return the workflow state */ get state(): any { - if (this.state_id && this.workflow && this.workflow.id) { - const state = this.workflow.state_by_id(this.state_id); - return state; + if (this.workflow) { + return this.workflow.state_by_id(this.state_id); } else { return ''; } @@ -232,8 +234,12 @@ export class Motion extends BaseModel { /** * returns possible states for the motion */ - get possible_states(): WorkflowState[] { - return this.workflow.states; + get nextStates(): WorkflowState[] { + if (this.workflow && this.state) { + return this.state.getNextStates(this.workflow); + } else { + return null; + } } /** diff --git a/client/src/app/shared/models/motions/workflow-state.ts b/client/src/app/shared/models/motions/workflow-state.ts index acc85b614..395ca6fda 100644 --- a/client/src/app/shared/models/motions/workflow-state.ts +++ b/client/src/app/shared/models/motions/workflow-state.ts @@ -1,4 +1,6 @@ import { Deserializable } from '../deserializable.model'; +import { Workflow } from './workflow'; +import { MotionLog } from './motion-log'; /** * Representation of a workflow state @@ -79,6 +81,20 @@ export class WorkflowState implements Deserializable { this.workflow_id = workflow_id; } + /** + * return a list of the next possible states. + * Also adds the current state. + */ + getNextStates(workflow: Workflow): WorkflowState[] { + const nextStates = []; + workflow.states.forEach(state => { + if (this.next_states_id.includes(state.id)) { + nextStates.push(state as WorkflowState); + } + }); + return nextStates; + } + deserialize(input: any): this { Object.assign(this, input); return this; diff --git a/client/src/app/shared/models/motions/workflow.ts b/client/src/app/shared/models/motions/workflow.ts index f1f8bb20f..bc83afbb5 100644 --- a/client/src/app/shared/models/motions/workflow.ts +++ b/client/src/app/shared/models/motions/workflow.ts @@ -21,6 +21,25 @@ export class Workflow extends BaseModel { this.first_state = first_state; } + /** + * Check if the containing @link{WorkflowState}s contain a given ID + * @param id The State ID + */ + isStateContained(obj: number | WorkflowState): boolean { + let id: number; + if (obj instanceof WorkflowState) { + id = obj.id; + } else { + id = obj; + } + + return this.states.some(state => { + if (state.id === id) { + return true; + } + }); + } + state_by_id(id: number): WorkflowState { let targetState; this.states.forEach(state => { diff --git a/client/src/app/site/motions/motion-detail/motion-detail.component.html b/client/src/app/site/motions/motion-detail/motion-detail.component.html index 0a8e475d1..f8ceda6f2 100644 --- a/client/src/app/site/motions/motion-detail/motion-detail.component.html +++ b/client/src/app/site/motions/motion-detail/motion-detail.component.html @@ -73,18 +73,19 @@ -