import { Http } from '@angular/http'; import { ActivatedRoute } from '@angular/router'; import { TdDialogService } from '@covalent/core'; import { ToastsManager } from 'ng2-toastr/ng2-toastr'; import { LocaleService } from '../common/i18n'; import { BasicService } from '../common/utils'; import { SeCurisResourceServices } from '../resources/base'; import { AfterViewInit, Component, Input } from '@angular/core'; export interface IComboOption { id: number, label: string } export class FormBase extends BasicService { protected form_title: string = ''; protected form_subtitle: string = ''; protected data: any = {}; protected isNew : boolean = true; constructor($L: LocaleService, protected route: ActivatedRoute, protected toaster: ToastsManager, protected resourceServices: SeCurisResourceServices, protected resourceName: string, protected dialogs : TdDialogService ) { super($L); } 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.resourceName.capitalize())), err => this.toaster.error(err.message, this.$L.get('Error saving {}', this.resourceName)) ); } delete(eleId: number | string) : void { this.dialogs.openConfirm({ message: this.$L.get('The {} with ID {} will be deleted. Do you want to continue ?', this.resourceName, eleId), disableClose: false, // defaults to false title: this.$L.get('Delete {}', this.resourceName), cancelButton: this.$L.get('Cancel'), acceptButton: this.$L.get('Yes, delete') }).afterClosed().subscribe((accept: boolean) => { if (accept) { this.resourceServices.remove(eleId) .subscribe( responseData => this.toaster.success(this.$L.get('{} was sucessfully deleted', this.resourceName.capitalize())), err => this.toaster.success(err.message, this.$L.get('Error deleting the {}', this.resourceName)) ); } }); } protected prepareInitialData(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) ; } }); } } @Component({ selector: 'error-checker', template: `