rsanchez
2017-03-20 470182be4f955a1c11d912743ce9e683ac4902a5
securis/src/main/webapp/src/app/forms/base.ts
....@@ -1,5 +1,5 @@
11 import { Http } from '@angular/http';
2
-import { ActivatedRoute } from '@angular/router';
2
+import { ActivatedRoute, Router } from '@angular/router';
33 import { TdDialogService } from '@covalent/core';
44 import { ToastsManager } from 'ng2-toastr/ng2-toastr';
55
....@@ -7,7 +7,8 @@
77 import { BasicService } from '../common/utils';
88 import { SeCurisResourceServices } from '../resources/base';
99
10
-import { AfterViewInit, Component, Input } from '@angular/core';
10
+import { ElementRef, ViewChild, AfterViewInit, Component, Input } from '@angular/core';
11
+import {FormGroup } from '@angular/forms';
1112
1213
1314 export interface IComboOption {
....@@ -16,13 +17,17 @@
1617 }
1718
1819
19
-export class FormBase extends BasicService {
20
+export abstract class FormBase extends BasicService {
21
+ @ViewChild('firstField') firstField: ElementRef;
22
+ @ViewChild('form') form: FormGroup;
23
+
2024 protected form_title: string = '';
2125 protected form_subtitle: string = '';
2226 protected data: any = {};
2327 protected isNew : boolean = true;
2428
2529 constructor($L: LocaleService,
30
+ protected router: Router,
2631 protected route: ActivatedRoute,
2732 protected toaster: ToastsManager,
2833 protected resourceServices: SeCurisResourceServices,
....@@ -35,12 +40,31 @@
3540 return this.$L.get(`field.${fieldId}`);
3641 }
3742
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
+
3855 protected loadCombos(): void {}
56
+ protected abstract init(): void;
57
+ protected abstract goBack(): void;
3958
4059 save() {
4160 var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data.id, this.data);
4261 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
+ },
4468 err => this.toaster.error(err.message, this.$L.get('Error saving {}', this.resourceName))
4569 );
4670 }
....@@ -56,7 +80,10 @@
5680 if (accept) {
5781 this.resourceServices.remove(eleId)
5882 .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
+ },
6087 err => this.toaster.success(err.message, this.$L.get('Error deleting the {}', this.resourceName))
6188 );
6289 }