| .. | .. |
|---|
| 1 | 1 | import { Http } from '@angular/http'; |
|---|
| 2 | | -import { ActivatedRoute } from '@angular/router'; |
|---|
| 2 | +import { ActivatedRoute, Router } from '@angular/router'; |
|---|
| 3 | 3 | import { TdDialogService } from '@covalent/core'; |
|---|
| 4 | 4 | import { ToastsManager } from 'ng2-toastr/ng2-toastr'; |
|---|
| 5 | 5 | |
|---|
| .. | .. |
|---|
| 7 | 7 | import { BasicService } from '../common/utils'; |
|---|
| 8 | 8 | import { SeCurisResourceServices } from '../resources/base'; |
|---|
| 9 | 9 | |
|---|
| 10 | | -import { AfterViewInit, Component, Input } from '@angular/core'; |
|---|
| 10 | +import { ElementRef, ViewChild, AfterViewInit, Component, Input } from '@angular/core'; |
|---|
| 11 | +import {FormGroup } from '@angular/forms'; |
|---|
| 11 | 12 | |
|---|
| 12 | 13 | |
|---|
| 13 | 14 | export interface IComboOption { |
|---|
| .. | .. |
|---|
| 16 | 17 | } |
|---|
| 17 | 18 | |
|---|
| 18 | 19 | |
|---|
| 19 | | -export class FormBase extends BasicService { |
|---|
| 20 | +export abstract class FormBase extends BasicService { |
|---|
| 21 | + @ViewChild('firstField') firstField: ElementRef; |
|---|
| 22 | + @ViewChild('form') form: FormGroup; |
|---|
| 23 | + |
|---|
| 20 | 24 | protected form_title: string = ''; |
|---|
| 21 | 25 | protected form_subtitle: string = ''; |
|---|
| 22 | 26 | protected data: any = {}; |
|---|
| 23 | 27 | protected isNew : boolean = true; |
|---|
| 24 | 28 | |
|---|
| 25 | 29 | constructor($L: LocaleService, |
|---|
| 30 | + protected router: Router, |
|---|
| 26 | 31 | protected route: ActivatedRoute, |
|---|
| 27 | 32 | protected toaster: ToastsManager, |
|---|
| 28 | 33 | protected resourceServices: SeCurisResourceServices, |
|---|
| .. | .. |
|---|
| 35 | 40 | return this.$L.get(`field.${fieldId}`); |
|---|
| 36 | 41 | } |
|---|
| 37 | 42 | |
|---|
| 43 | + protected setFirstFocus() :void { |
|---|
| 44 | + if (this.firstField) { |
|---|
| 45 | + this.firstField.nativeElement.focus(); |
|---|
| 46 | + } |
|---|
| 47 | + } |
|---|
| 48 | + |
|---|
| 49 | + protected reset() :void { |
|---|
| 50 | + if (this.form) { |
|---|
| 51 | + this.form.reset(); |
|---|
| 52 | + } |
|---|
| 53 | + } |
|---|
| 54 | + |
|---|
| 38 | 55 | protected loadCombos(): void {} |
|---|
| 56 | + protected abstract init(): void; |
|---|
| 57 | + protected abstract goBack(): void; |
|---|
| 39 | 58 | |
|---|
| 40 | 59 | save() { |
|---|
| 41 | 60 | var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data.id, this.data); |
|---|
| 42 | 61 | command.subscribe( |
|---|
| 43 | | - data => this.toaster.success(this.$L.get('{} saved sucessfully', this.resourceName.capitalize())), |
|---|
| 62 | + data => { |
|---|
| 63 | + this.toaster.success(this.$L.get('{} saved sucessfully', this.resourceName.capitalize())); |
|---|
| 64 | + if (this.isNew) { |
|---|
| 65 | + this.init(); |
|---|
| 66 | + } |
|---|
| 67 | + }, |
|---|
| 44 | 68 | err => this.toaster.error(err.message, this.$L.get('Error saving {}', this.resourceName)) |
|---|
| 45 | 69 | ); |
|---|
| 46 | 70 | } |
|---|
| .. | .. |
|---|
| 56 | 80 | if (accept) { |
|---|
| 57 | 81 | this.resourceServices.remove(eleId) |
|---|
| 58 | 82 | .subscribe( |
|---|
| 59 | | - responseData => this.toaster.success(this.$L.get('{} was sucessfully deleted', this.resourceName.capitalize())), |
|---|
| 83 | + responseData => { |
|---|
| 84 | + this.toaster.success(this.$L.get('{} was sucessfully deleted', this.resourceName.capitalize())); |
|---|
| 85 | + this.goBack(); |
|---|
| 86 | + }, |
|---|
| 60 | 87 | err => this.toaster.success(err.message, this.$L.get('Error deleting the {}', this.resourceName)) |
|---|
| 61 | 88 | ); |
|---|
| 62 | 89 | } |
|---|