rsanchez
2017-03-19 280daa7f3f858ecfef9c91ffd5dea1007f021048
securis/src/main/webapp/src/app/forms/base.ts
....@@ -1,5 +1,64 @@
1
+import { Http } from '@angular/http';
2
+import { ActivatedRoute } from '@angular/router';
3
+import { ToastsManager } from 'ng2-toastr/ng2-toastr';
4
+
5
+import { LocaleService } from '../common/i18n';
6
+import { SeCurisResourceServices } from '../resources/base';
7
+
8
+import { AfterViewInit } from '@angular/core';
9
+
110
211 export interface IComboOption {
312 id: number,
413 label: string
14
+}
15
+
16
+
17
+export class FormBase {
18
+ protected form_title: string = '';
19
+ protected form_subtitle: string = '';
20
+ protected data: any = {};
21
+ protected isNew : boolean = true;
22
+
23
+ constructor(protected $L: LocaleService,
24
+ protected route: ActivatedRoute,
25
+ protected toaster: ToastsManager,
26
+ protected resourceServices: SeCurisResourceServices,
27
+ protected resourceName: string ) {
28
+ }
29
+
30
+ public getFieldName(fieldId: string) : string {
31
+ return this.$L.get(`field.${fieldId}`);
32
+ }
33
+
34
+ protected loadCombos(): void {}
35
+
36
+ save() {
37
+ var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data.id, this.data);
38
+ command.subscribe(
39
+ data => this.toaster.success(this.$L.get('{} saved sucessfully', this.$L.get(this.resourceName).capitalize())),
40
+ err => {
41
+ console.error(err);
42
+ this.toaster.success(this.$L.get('Error saving {}', this.$L.get(this.resourceName)));
43
+ }
44
+ );
45
+ }
46
+
47
+ protected prepareData(idparam: string, default_values: any = {}) : void {
48
+ this.form_title = this.$L.get('{} data', this.resourceName.capitalize());
49
+ this.isNew = true;
50
+ !!this.route && this.route.params.subscribe(params => {
51
+ var eleId = +params[idparam]; // (+) converts string 'id' to a number
52
+ if (!eleId) {
53
+ this.data = {};
54
+ Object.keys(default_values).forEach((k : string) => this.data[k] = default_values[k]);
55
+ this.form_subtitle = this.$L.get('Create a new {}', this.resourceName) ;
56
+ } else {
57
+ this.isNew = false;
58
+ this.resourceServices.get(eleId).subscribe(eleData => this.data = eleData);
59
+ this.form_subtitle = this.$L.get('Modify the {} data', this.resourceName) ;
60
+ }
61
+ });
62
+ }
63
+
564 }