2018-08-16 17:03:39 +02:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
2018-08-22 11:26:53 +02:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
2018-08-09 16:03:24 +02:00
|
|
|
import { BaseComponent } from '../../../base.component';
|
|
|
|
import { Motion } from '../../../shared/models/motions/motion';
|
2018-08-16 17:03:39 +02:00
|
|
|
import { Category } from '../../../shared/models/motions/category';
|
|
|
|
import { FormGroup, FormBuilder } from '@angular/forms';
|
|
|
|
import { MatExpansionPanel } from '@angular/material';
|
2018-08-22 11:26:53 +02:00
|
|
|
import { DataStoreService } from '../../../core/services/dataStore.service';
|
|
|
|
import { OperatorService } from '../../../core/services/operator.service';
|
2018-08-09 16:03:24 +02:00
|
|
|
|
2018-08-22 11:26:53 +02:00
|
|
|
/**
|
|
|
|
* Component for the motion detail view
|
|
|
|
*/
|
2018-08-09 16:03:24 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-motion-detail',
|
|
|
|
templateUrl: './motion-detail.component.html',
|
|
|
|
styleUrls: ['./motion-detail.component.scss']
|
|
|
|
})
|
2018-08-22 11:26:53 +02:00
|
|
|
// export class MotionDetailComponent extends BaseComponent implements OnInit {
|
|
|
|
export class MotionDetailComponent implements OnInit {
|
|
|
|
/**
|
|
|
|
* MatExpansionPanel for the meta info
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
@ViewChild('metaInfoPanel') metaInfoPanel: MatExpansionPanel;
|
2018-08-22 11:26:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MatExpansionPanel for the content panel
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
@ViewChild('contentPanel') contentPanel: MatExpansionPanel;
|
|
|
|
|
2018-08-22 11:26:53 +02:00
|
|
|
/**
|
|
|
|
* Target motion. Might be new or old
|
|
|
|
*/
|
2018-08-09 16:03:24 +02:00
|
|
|
motion: Motion;
|
2018-08-22 11:26:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Motions meta-info
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
metaInfoForm: FormGroup;
|
2018-08-22 11:26:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Motion content. Can be a new version
|
|
|
|
*/
|
2018-08-20 18:13:28 +02:00
|
|
|
contentForm: FormGroup;
|
2018-08-22 11:26:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the motion is edited
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
editMotion = false;
|
2018-08-22 11:26:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the motion is new
|
|
|
|
*/
|
2018-08-21 14:56:26 +02:00
|
|
|
newMotion = false;
|
2018-08-16 17:03:39 +02:00
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
2018-08-22 11:26:53 +02:00
|
|
|
* Constuct the detail view.
|
|
|
|
*
|
|
|
|
* TODO: DataStore needs removed and added via the parent.
|
|
|
|
* Own service for put and post required
|
2018-08-21 14:56:26 +02:00
|
|
|
*
|
|
|
|
* @param route determine if this is a new or an existing motion
|
|
|
|
* @param formBuilder For reactive forms. Form Group and Form Control
|
|
|
|
*/
|
2018-08-22 11:26:53 +02:00
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private operator: OperatorService,
|
|
|
|
private myDataStore: DataStoreService
|
|
|
|
) {
|
|
|
|
// TODO: Add super again
|
|
|
|
// super();
|
2018-08-16 17:03:39 +02:00
|
|
|
this.createForm();
|
2018-08-21 14:56:26 +02:00
|
|
|
|
|
|
|
if (route.snapshot.url[0].path === 'new') {
|
|
|
|
this.newMotion = true;
|
|
|
|
this.editMotion = true;
|
|
|
|
this.motion = new Motion();
|
|
|
|
} else {
|
|
|
|
// load existing motion
|
|
|
|
this.route.params.subscribe(params => {
|
|
|
|
console.log('params ', params);
|
|
|
|
|
|
|
|
// has the motion of the DataStore was initialized before.
|
2018-08-22 11:26:53 +02:00
|
|
|
this.motion = this.myDataStore.get(Motion, params.id) as Motion;
|
2018-08-21 14:56:26 +02:00
|
|
|
|
|
|
|
// Observe motion to get the motion in the parameter and also get the changes
|
2018-08-22 11:26:53 +02:00
|
|
|
this.myDataStore.getObservable().subscribe(newModel => {
|
2018-08-21 14:56:26 +02:00
|
|
|
if (newModel instanceof Motion) {
|
|
|
|
if (newModel.id === +params.id) {
|
|
|
|
this.motion = newModel as Motion;
|
|
|
|
}
|
2018-08-09 16:03:24 +02:00
|
|
|
}
|
2018-08-21 14:56:26 +02:00
|
|
|
});
|
2018-08-09 16:03:24 +02:00
|
|
|
});
|
2018-08-21 14:56:26 +02:00
|
|
|
}
|
2018-08-09 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Async load the values of the motion in the Form.
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
patchForm() {
|
2018-08-20 18:13:28 +02:00
|
|
|
this.metaInfoForm.patchValue({
|
|
|
|
category_id: this.motion.category.id,
|
|
|
|
state_id: this.motion.state.id,
|
|
|
|
recommendation_id: this.motion.recommendation.id,
|
|
|
|
identifier: this.motion.identifier,
|
|
|
|
origin: this.motion.origin
|
|
|
|
});
|
|
|
|
this.contentForm.patchValue({
|
|
|
|
currentTitle: this.motion.currentTitle,
|
|
|
|
currentText: this.motion.currentText,
|
|
|
|
currentReason: this.motion.currentReason
|
|
|
|
});
|
2018-08-16 17:03:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Creates the forms for the Motion and the MotionVersion
|
|
|
|
*
|
|
|
|
* TODO: Build a custom form validator
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
createForm() {
|
|
|
|
this.metaInfoForm = this.formBuilder.group({
|
2018-08-20 18:13:28 +02:00
|
|
|
identifier: [''],
|
|
|
|
category_id: [''],
|
|
|
|
state_id: [''],
|
|
|
|
recommendation_id: [''],
|
|
|
|
origin: ['']
|
|
|
|
});
|
|
|
|
this.contentForm = this.formBuilder.group({
|
|
|
|
currentTitle: [''],
|
|
|
|
currentText: [''],
|
|
|
|
currentReason: ['']
|
2018-08-16 17:03:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Save a motion. Calls the "patchValues" function in the MotionObject
|
|
|
|
*
|
|
|
|
* http:post the motion to the server.
|
|
|
|
* The AutoUpdate-Service should see a change once it arrives and show it
|
|
|
|
* in the list view automatically
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
saveMotion() {
|
2018-08-20 18:13:28 +02:00
|
|
|
const newMotionValues = { ...this.metaInfoForm.value, ...this.contentForm.value };
|
|
|
|
this.motion.patchValues(newMotionValues);
|
2018-08-21 14:56:26 +02:00
|
|
|
|
2018-08-22 11:26:53 +02:00
|
|
|
// TODO: This is DRAFT. Reads out Motion version directly. Potentially insecure.
|
|
|
|
this.motion.title = this.motion.currentTitle;
|
|
|
|
this.motion.text = this.motion.currentText;
|
2018-08-21 14:56:26 +02:00
|
|
|
|
2018-08-22 11:26:53 +02:00
|
|
|
this.myDataStore.save(this.motion).subscribe(answer => {
|
|
|
|
console.log('answer, ', answer);
|
|
|
|
if (answer && answer.id && this.newMotion) {
|
|
|
|
this.router.navigate(['./motions/' + answer.id]);
|
|
|
|
}
|
2018-08-21 14:56:26 +02:00
|
|
|
});
|
2018-08-16 17:03:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* return all Categories.
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
getMotionCategories(): Category[] {
|
2018-08-22 11:26:53 +02:00
|
|
|
const categories = this.myDataStore.get(Category);
|
2018-08-16 17:03:39 +02:00
|
|
|
return categories as Category[];
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Click on the edit button (pen-symbol)
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
editMotionButton() {
|
|
|
|
this.editMotion ? (this.editMotion = false) : (this.editMotion = true);
|
2018-08-20 12:28:43 +02:00
|
|
|
|
2018-08-16 17:03:39 +02:00
|
|
|
if (this.editMotion) {
|
2018-08-20 12:28:43 +02:00
|
|
|
this.patchForm();
|
2018-08-16 17:03:39 +02:00
|
|
|
this.metaInfoPanel.open();
|
|
|
|
this.contentPanel.open();
|
2018-08-20 12:28:43 +02:00
|
|
|
} else {
|
|
|
|
this.saveMotion();
|
2018-08-16 17:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Init. Does nothing here.
|
|
|
|
*/
|
|
|
|
ngOnInit() {}
|
2018-08-09 16:03:24 +02:00
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Function to download a motion.
|
|
|
|
*
|
|
|
|
* TODO: does nothing yet.
|
|
|
|
*/
|
2018-08-09 16:03:24 +02:00
|
|
|
downloadSingleMotionButton() {
|
|
|
|
console.log('Download this motion');
|
|
|
|
}
|
|
|
|
}
|