2015-12-10 09:40:54 -08:00
|
|
|
// #docplaster
|
|
|
|
|
|
|
|
// #docregion
|
|
|
|
import {Component, OnInit} from 'angular2/core';
|
|
|
|
import {Crisis, CrisisService} from './crisis.service';
|
|
|
|
import {RouteParams, Router} from 'angular2/router';
|
2015-12-19 14:16:27 -08:00
|
|
|
// #docregion routerCanDeactivate
|
2015-12-10 09:40:54 -08:00
|
|
|
import {CanDeactivate, ComponentInstruction} from 'angular2/router';
|
|
|
|
import {DialogService} from '../dialog.service';
|
|
|
|
|
2015-12-19 14:16:27 -08:00
|
|
|
// #enddocregion routerCanDeactivate
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
// #docregion template
|
|
|
|
template: `
|
|
|
|
<div *ngIf="crisis">
|
|
|
|
<h3>"{{editName}}"</h3>
|
|
|
|
<div>
|
|
|
|
<label>Id: </label>{{crisis.id}}</div>
|
|
|
|
<div>
|
|
|
|
<label>Name: </label>
|
|
|
|
<input [(ngModel)]="editName" placeholder="name"/>
|
|
|
|
</div>
|
|
|
|
<button (click)="save()">Save</button>
|
|
|
|
<button (click)="cancel()">Cancel</button>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
// #enddocregion template
|
|
|
|
styles: ['input {width: 20em}']
|
|
|
|
})
|
2015-12-19 14:16:27 -08:00
|
|
|
// #docregion routerCanDeactivate, cancel-save
|
2015-12-10 09:40:54 -08:00
|
|
|
export class CrisisDetailComponent implements OnInit, CanDeactivate {
|
|
|
|
|
2015-12-31 16:55:53 -08:00
|
|
|
crisis: Crisis;
|
|
|
|
editName: string;
|
2015-12-10 09:40:54 -08:00
|
|
|
|
2015-12-19 14:16:27 -08:00
|
|
|
// #enddocregion routerCanDeactivate, cancel-save
|
2015-12-10 09:40:54 -08:00
|
|
|
constructor(
|
|
|
|
private _service: CrisisService,
|
|
|
|
private _router: Router,
|
|
|
|
private _routeParams: RouteParams,
|
|
|
|
private _dialog: DialogService
|
|
|
|
) { }
|
|
|
|
|
|
|
|
// #docregion ngOnInit
|
|
|
|
ngOnInit() {
|
|
|
|
let id = +this._routeParams.get('id');
|
|
|
|
this._service.getCrisis(id).then(crisis => {
|
|
|
|
if (crisis) {
|
|
|
|
this.editName = crisis.name;
|
|
|
|
this.crisis = crisis;
|
|
|
|
} else { // id not found
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// #enddocregion ngOnInit
|
|
|
|
|
2015-12-31 16:55:53 -08:00
|
|
|
// #docregion routerCanDeactivate
|
|
|
|
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
|
|
|
|
// Allow synchronous navigation (`true`) if no crisis or the crisis is unchanged.
|
|
|
|
if (!this.crisis || this.crisis.name === this.editName) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-14 18:19:17 -08:00
|
|
|
// Otherwise ask the user with the dialog service and return its
|
2015-12-31 16:55:53 -08:00
|
|
|
// promise which resolves to true or false when the user decides
|
|
|
|
return this._dialog.confirm('Discard changes?');
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
2015-12-31 16:55:53 -08:00
|
|
|
// #enddocregion routerCanDeactivate
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
// #docregion cancel-save
|
|
|
|
cancel() {
|
|
|
|
this.editName = this.crisis.name;
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.crisis.name = this.editName;
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
// #enddocregion cancel-save
|
|
|
|
|
|
|
|
// #docregion gotoCrises
|
|
|
|
gotoCrises() {
|
|
|
|
let route =
|
2015-12-31 16:55:53 -08:00
|
|
|
// pass along the crisis id if available
|
|
|
|
// so that the CrisisList component can select that crisis
|
|
|
|
['CrisisCenter', {id: this.crisis ? this.crisis.id : null} ]
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
this._router.navigate(route);
|
|
|
|
}
|
|
|
|
// #enddocregion gotoCrises
|
2015-12-19 14:16:27 -08:00
|
|
|
// #docregion routerCanDeactivate, cancel-save
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
2015-12-19 14:16:27 -08:00
|
|
|
// #enddocregion routerCanDeactivate, cancel-save
|
2015-12-10 09:40:54 -08:00
|
|
|
// #enddocregion
|