From 280daa7f3f858ecfef9c91ffd5dea1007f021048 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Sun, 19 Mar 2017 11:29:37 +0000
Subject: [PATCH] #3527 fix - Added applications and some minor refactoring

---
 securis/src/main/webapp/src/app/forms/base.ts |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/securis/src/main/webapp/src/app/forms/base.ts b/securis/src/main/webapp/src/app/forms/base.ts
index 915ad53..708c83e 100644
--- a/securis/src/main/webapp/src/app/forms/base.ts
+++ b/securis/src/main/webapp/src/app/forms/base.ts
@@ -1,5 +1,64 @@
+import { Http } from '@angular/http';
+import { ActivatedRoute } from '@angular/router';
+import { ToastsManager } from 'ng2-toastr/ng2-toastr';
+
+import { LocaleService } from '../common/i18n';
+import { SeCurisResourceServices } from '../resources/base';
+
+import { AfterViewInit } from '@angular/core';
+
 
 export interface IComboOption {
 	id: number,
 	label: string
+}
+
+
+export class FormBase {
+	protected form_title: string = '';
+	protected form_subtitle: string = '';
+	protected data: any = {};
+	protected isNew : boolean = true;
+
+	constructor(protected $L: LocaleService, 
+	            protected route: ActivatedRoute,
+				protected toaster: ToastsManager,
+				protected resourceServices: SeCurisResourceServices, 
+				protected resourceName: string ) {
+	}
+
+	public getFieldName(fieldId: string) : string {
+		return this.$L.get(`field.${fieldId}`);
+	}
+
+  	protected loadCombos(): void {}
+
+	save() {
+		var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data.id, this.data);
+		command.subscribe(
+			data => this.toaster.success(this.$L.get('{} saved sucessfully', this.$L.get(this.resourceName).capitalize())),
+			err => {
+				console.error(err);
+				this.toaster.success(this.$L.get('Error saving {}', this.$L.get(this.resourceName)));
+			}
+			);
+	}
+
+	protected prepareData(idparam: string, default_values: any = {}) : void {
+		this.form_title = this.$L.get('{} data', this.resourceName.capitalize());
+		this.isNew = true;
+		!!this.route && this.route.params.subscribe(params => {
+			var eleId = +params[idparam]; // (+) converts string 'id' to a number
+			if (!eleId) {
+				this.data = {};
+				Object.keys(default_values).forEach((k : string) => this.data[k] = default_values[k]);
+				this.form_subtitle = this.$L.get('Create a new {}', this.resourceName) ;
+			} else {
+				this.isNew = false;
+				this.resourceServices.get(eleId).subscribe(eleData => this.data = eleData);
+				this.form_subtitle = this.$L.get('Modify the {} data', this.resourceName) ;
+			}
+		});
+	}
+
 }
\ No newline at end of file

--
Gitblit v1.3.2