rsanchez
2017-03-21 a6e1ace2b6bdba8c08a4acfa42433f3ac073b747
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
import { Http } from '@angular/http';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { ApplicationsService } from '../resources/applications';
import { LicenseTypesService } from '../resources/license_types';
import { LocaleService } from '../common/i18n';
import { TdDialogService } from '@covalent/core';
import { Component, AfterViewInit, ViewChild } from '@angular/core';
import { TdMediaService } from '@covalent/core';
import { FormBase, IComboOption } from './base';
import { ActivatedRoute, Router } from '@angular/router';
var lt_example = { 
  id: 1,
  code: 'CI01',
  name: 'CI ConfigServer ext',
  application_id: 1,
  application_name: 'CurisIntegrity',
  creation_timestamp: 1418384535000,
  description: 'CI ConfigServer with extended mode and 50 lines',
  metadata: [ { key: 'max_docs', value: '250000', mandatory: true } ] 
}
@Component({
  selector: 'licensetype-form',
  templateUrl: 'src/app/forms/licensetype.form.html'
})
export class LicenseTypeFormComponent extends FormBase {
  
  allApplications: IComboOption[];
  constructor(private http: Http,
              private licenseTypes: LicenseTypesService,
              private applications: ApplicationsService,
              router: Router,
              toaster: ToastsManager,
              route: ActivatedRoute,              
              $L: LocaleService,
              dialogs: TdDialogService) {
    super($L, router, route, toaster, licenseTypes, $L.get('license type'), dialogs);
  }
  private _prepareMetadata(metadata : any[]) : any[] {
    metadata.forEach((md : any) => {
        if (md.value !== null && md.value !== '') {
          md.readonly = true;
        }
      });
    return metadata;
  }
  changeApplication(event: any) : void{
    console.log(event);
    this.applications.get(this.data.application_id)
        .map(app_data => this._prepareMetadata(app_data.metadata))
        .subscribe(
          metadata => this.data.metadata = metadata,
          err => console.error('Error loading application metadata')
        );
  }
  loadCombos() : void {
      this.applications.get()
        .map(list => list.map((app : any) => <IComboOption>{id: app.id, label: `(${app.code}) ${app.name}`}))
        .subscribe(
          data => this.allApplications = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)),
          err => console.error('Error loading applications')
        );
  }
  goBack(): void {
    this.router.navigate([`licensetypes`]);
  }
  init() : void {
    this.loadCombos();
    super.setFirstFocus();
    super.reset();
    super.prepareInitialData('licensetypeId', {
      metadata: []
    }, (data) => this._prepareMetadata(data.metadata));
  }
 
  ngAfterViewInit(): void {
    this.init();
  }
}