Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { Http } from '@angular/http';
import { ActivatedRoute, Router } 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 { ElementRef, ViewChild, AfterViewInit, Component, Input } from '@angular/core';
import {FormGroup } from '@angular/forms';
export interface IComboOption {
   id: number,
   label: string
}
export abstract class FormBase extends BasicService {
   @ViewChild('firstField') firstField: ElementRef;
   @ViewChild('form') form: FormGroup;
   protected form_title: string = '';
   protected form_subtitle: string = '';
   protected data: any = {};
   protected isNew : boolean = true;
   constructor($L: LocaleService, 
               protected router: Router,
               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 setFirstFocus() :void {
       if (this.firstField) {
           this.firstField.nativeElement.focus();
       }
   }
   protected reset() :void {
       if (this.form) {
           this.form.reset();
       }
   }
   
      protected loadCombos(): void {}
      protected abstract init(): void;
      protected abstract goBack(): void;
   save(fieldId : string = 'id') {
       var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data[fieldId], this.data);
       command.subscribe(
           data => {
               this.toaster.success(this.$L.get('{} saved sucessfully', this.resourceName.capitalize()));
               if (this.isNew) {
                   this.init();
               }
           },
           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()));
                           this.goBack();
                       },
                       err => this.toaster.success(err.message, this.$L.get('Error deleting the {}', this.resourceName))
               );
           }
       });
   }
   private applyDefaultValues(default_values: any, target_data: any = {}) : any {
       Object.keys(default_values).forEach((k : string) => (target_data[k] === undefined) && (target_data[k] = default_values[k]));
       return target_data;
   }
   protected prepareInitialData(idparam: string, default_values: any = {}, callback?: (data: any) => void) : void {
       super.setViewData(() => {
           this.form_title = this.$L.get('{} data', this.resourceName.capitalize());
           this.isNew = true;            
       });
       !!this.route && this.route.params.subscribe(params => {
           var eleId = params[idparam];
           if (!eleId) {
               super.setViewData(() => {
                   this.data = this.applyDefaultValues(default_values, {});
                   this.form_subtitle = this.$L.get('Create a new {}', this.resourceName) ;
               });
           } else {
               super.setViewData(() => {
                   this.isNew = false;
                   this.resourceServices.get(eleId).subscribe(eleData => {
                       super.setViewData(() => {
                           this.data = this.applyDefaultValues(default_values, eleData);
                           callback && callback(this.data);
                       });
                   });
                   this.form_subtitle = this.$L.get('Modify the {} data', this.resourceName) ;
               });
           }
       });
   }
}
@Component({
  selector: 'error-checker',
  template: `
    <div *ngIf="formField?.touched && formField.invalid" layout="column">
      <span class="error-msg" *ngFor="let err of getFieldErrors()" align="end">{{err}}</span>
    </div>`,
   styles: [
       ':host {margin-top: -10px;}',
       `.error-msg {
           color: darkred;
           font-size: 0.8em;
       }`
   ]
})
export class ErrorCheckerComponent {
  @Input() formField: any;
  @Input() fieldName: string;
  constructor(private $L: LocaleService) {
  }
  
  getFieldErrors() : string[] {
    if (this.formField.valid) {
      return []
    } else {
      return (<string[]>Object.keys(this.formField.errors)).map((err:string) => this.getErrorMsg(err));
    }
  }
  private updateFieldErrors() {
  }
  private getErrorMsg(err: string) : string{
    switch(err) { 
      case 'required': { 
        return this.fieldName + ' '+ this.$L.get('is required');
      } 
      case 'number': { 
        return this.fieldName + ' '+ this.$L.get('should be a number');
      } 
      default: { 
        return this.fieldName + ' '+ this.$L.get('unknown error') + ' ' + err;
      } 
    } 
  }
  log(obj: any) {
    console.log(obj)
  }
  
}
@Component({
  selector: 'field-readonly',
  template: `
  <div layout="column" class="mat-input-container readonly" >
       <div class="mat-input-wrapper">
           <div class="mat-input-table">
               <div class="mat-input-prefix"></div>
               <div class="mat-input-infix">
                   <div class="label-value mat-input-element">{{value}}</div>
                   <span class="mat-input-placeholder-wrapper" >
                       <label class="mat-input-placeholder mat-float">
                           <span class="placeholder">{{label}}</span>
                       </label>
                   </span>
               </div>
               <div class="mat-input-suffix"></div>
           </div>
           <div class="mat-input-underline"></div>
       </div>
   </div>`,
   styles: [`.readonly .mat-input-element {
               margin-top: 0px !important;
               color: rgba(0, 0, 0, 0.50);
           }`,
           `.readonly .mat-input-element {
               margin-top: 0px;
               color: rgba(0, 0, 0, 0.50);
           }`,
           `.readonly.mat-input-container {
               width: 100%;
           }`]
})
export class FieldReadonlyComponent {
   @Input('value') value: any;
   private _label : string;
    @Input('label')
   set label(txt: string) {
       this._label = this.$L.get(txt);
   }
   get label(): string { return this._label; }
   constructor(private $L : LocaleService) {
   }
}
interface MetadataPair {
   key: string,
   value: string,
   readonly: boolean,
   mandatory: boolean
}
@Component({
  selector: 'metadata-manager',
  template: `
  <div layout="column" layout-fill flex>
       <div layout="row" layout-align="start center">
           <span class="md-title">{{title}}</span>
           <span flex></span>
           <button *ngIf="addOrDelete" type="button"  md-icon-button color="primary" (click)="newMetadata()"><md-icon>add</md-icon></button>
       </div>
       <div layout="row" layout-align="start center" *ngFor="let pair of metadata" class="values">
           <md-input-container flex="35" >
               <input mdInput type="text" [ngModelOptions]="{standalone: true}" [(ngModel)]="pair.key" [readonly]="readonly || !editKeys" [mdTooltip]="pair.key" />
               <md-placeholder>
                   <span i18n="field.key"></span>
               </md-placeholder>
           </md-input-container>
           <md-input-container flex >
               <input mdInput type="text" [ngModelOptions]="{standalone: true}" [(ngModel)]="pair.value" [readonly]="readonly || pair.readonly" [required]="pair.mandatory"
                [mdTooltip]="pair.value" />
               <md-placeholder>
                   <span i18n="field.value"></span>
               </md-placeholder>
           </md-input-container>
           <md-checkbox *ngIf="!readonly && addOrDelete" [ngModelOptions]="{standalone: true}" [(ngModel)]="pair.mandatory" name="mandatory" [mdTooltip]="$L.get('field.mandatory')">
           </md-checkbox>
           <button *ngIf="addOrDelete" type="button" md-icon-button color="warn" (click)="deleteMetadata(pair)"><md-icon>delete</md-icon></button>
       </div>
   </div>`,
   styles: [
       ':host { width:100%; }',
       `.values > * {
           margin-left: 5px;
           margin-right: 5px;
       }`
   ]
})
export class MetadataManagerComponent {
   @Input('metadata') metadata: MetadataPair[];
   @Input('addOrDelete') addOrDelete: boolean = false;
   @Input('editKeys') editKeys: boolean = false;
   @Input('readonly') readonly: boolean = false;
   @Input('title') title: string;
   constructor(private $L : LocaleService) {
       this.title = $L.get('License metadata');
   }
   newMetadata() : void{
       this.metadata.push(<MetadataPair>{
           mandatory: false,
           readonly: false
       });
   }
   deleteMetadata(pair: MetadataPair) : void {
       let indexToRemove = this.metadata.findIndex(ele => ele.key === pair.key);
       if (indexToRemove !== -1) {
           this.metadata.splice(indexToRemove, 1);
       }
   }
}