2015-12-10 09:40:54 -08:00
|
|
|
// #docplaster
|
|
|
|
// #docregion
|
2016-05-02 16:53:25 -07:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
import { Crisis, CrisisService } from './crisis.service';
|
|
|
|
import { Router, OnActivate, CanDeactivate, RouteSegment } from '@angular/router';
|
|
|
|
import { DialogService } from '../dialog.service';
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `
|
|
|
|
<div *ngIf="crisis">
|
|
|
|
<h3>"{{editName}}"</h3>
|
|
|
|
<div>
|
|
|
|
<label>Id: </label>{{crisis.id}}</div>
|
|
|
|
<div>
|
|
|
|
<label>Name: </label>
|
|
|
|
<input [(ngModel)]="editName" placeholder="name"/>
|
|
|
|
</div>
|
2016-02-28 17:59:57 -08:00
|
|
|
<p>
|
|
|
|
<button (click)="save()">Save</button>
|
|
|
|
<button (click)="cancel()">Cancel</button>
|
|
|
|
</p>
|
2015-12-10 09:40:54 -08:00
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
styles: ['input {width: 20em}']
|
|
|
|
})
|
2015-12-31 20:04:22 -08:00
|
|
|
|
2016-05-02 16:53:25 -07:00
|
|
|
export class CrisisDetailComponent implements OnActivate, CanDeactivate {
|
2015-12-31 16:55:53 -08:00
|
|
|
crisis: Crisis;
|
|
|
|
editName: string;
|
2016-05-02 16:53:25 -07:00
|
|
|
private curSegment: RouteSegment;
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
constructor(
|
2016-05-02 16:53:25 -07:00
|
|
|
private service: CrisisService,
|
|
|
|
private router: Router,
|
|
|
|
private dialog: DialogService
|
2015-12-10 09:40:54 -08:00
|
|
|
) { }
|
|
|
|
|
2016-05-02 16:53:25 -07:00
|
|
|
routerOnActivate(curr: RouteSegment) {
|
|
|
|
this.curSegment = curr;
|
|
|
|
|
|
|
|
let id = +curr.getParam('id');
|
|
|
|
this.service.getCrisis(id).then(crisis => {
|
2015-12-10 09:40:54 -08:00
|
|
|
if (crisis) {
|
|
|
|
this.editName = crisis.name;
|
|
|
|
this.crisis = crisis;
|
|
|
|
} else { // id not found
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-02 16:53:25 -07:00
|
|
|
routerCanDeactivate(): any {
|
2015-12-31 16:55:53 -08:00
|
|
|
// 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
|
2016-05-02 16:53:25 -07:00
|
|
|
return this.dialog.confirm('Discard changes?');
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.crisis.name = this.editName;
|
|
|
|
this.gotoCrises();
|
|
|
|
}
|
|
|
|
|
|
|
|
// #docregion gotoCrises
|
|
|
|
gotoCrises() {
|
2015-12-31 20:04:22 -08:00
|
|
|
let crisisId = this.crisis ? this.crisis.id : null;
|
|
|
|
// Pass along the hero id if available
|
|
|
|
// so that the CrisisListComponent can select that hero.
|
|
|
|
// Add a totally useless `foo` parameter for kicks.
|
|
|
|
// #docregion gotoCrises-navigate
|
2016-05-02 16:53:25 -07:00
|
|
|
// Absolute link
|
|
|
|
this.router.navigate(['/crisis-center', {id: crisisId, foo: 'foo'}]);
|
|
|
|
|
|
|
|
// Relative link
|
|
|
|
// this.router.navigate(['../', {id: crisisId, foo: 'foo'}], this.curSegment);
|
2015-12-31 20:04:22 -08:00
|
|
|
// #enddocregion gotoCrises-navigate
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
|
|
|
// #enddocregion gotoCrises
|
|
|
|
}
|