Project

General

Profile

1
import { Component, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
2
import { FormBuilder, FormGroup } from '@angular/forms';
3
import { Repository, RepositoryInterface } from '../../domain/typeScriptClasses';
4
import { RepositoryService } from '../../services/repository.service';
5
import { ActivatedRoute, Router } from '@angular/router';
6
import { formInfoLoading, loadingRepoError } from '../../domain/shared-messages';
7
import { DatasourceUpdateFormComponent } from './sources-forms/datasource-update-form.component';
8
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
9
import { AuthenticationService } from '../../services/authentication.service';
10
import { DatasourceNewInterfaceFormComponent } from './sources-forms/datasource-new-interface-form.component';
11

    
12
@Component ({
13
  selector: 'sources-update-repo',
14
  templateUrl: 'sources-update-repo.component.html',
15
})
16

    
17
export class SourcesUpdateRepoComponent implements OnInit {
18
  loadingMessage: string;
19
  errorMessage: string;
20

    
21
  repoId: string;
22
  logoURL: string;
23
  repo: Repository;
24
  repoInterfaces: RepositoryInterface[] = [];
25

    
26
  @ViewChild('datasourceUpdateForm') datasourceUpdateForm: DatasourceUpdateFormComponent;
27

    
28
  group: FormGroup;
29

    
30
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
31
  dataForInterfaceComp: any[] = [];
32

    
33
  isModalShown: boolean;
34
  @ViewChild('updateLogoUrlModal')
35
  public updateLogoUrlModal: ConfirmationDialogComponent;
36

    
37
  constructor ( private fb: FormBuilder,
38
                private repoService: RepositoryService,
39
                private authService: AuthenticationService,
40
                private route: ActivatedRoute,
41
                private router: Router) { }
42

    
43

    
44
  ngOnInit() {
45
    this.readRepoId();
46
    let body = document.getElementsByTagName('body')[0];
47
    body.classList.remove("top_bar_active");   //remove the class
48
    body.classList.remove("page_heading_active");
49
    body.classList.remove("landing");
50
    body.classList.add("dashboard");
51
  }
52

    
53
  readRepoId() {
54
    this.repoId = this.route.snapshot.paramMap.get('id');
55
    console.log(`repoId is ${this.repoId}`);
56
    this.getRepo();
57
  }
58

    
59
  getRepo() {
60
    if (this.repoId) {
61
      this.loadingMessage = formInfoLoading;
62
      this.repoService.getRepositoryById(this.repoId).subscribe(
63
        repo => {
64
          this.repo = repo;
65
        },
66
        error => {
67
          console.log(error);
68
          this.loadingMessage = '';
69
          this.errorMessage = loadingRepoError;
70
        },
71
        () => {
72
            this.logoURL = this.repo.logoUrl;
73
            this.getRepoInterfaces();
74
        }
75
      );
76
    }
77
  }
78

    
79
  getRepoInterfaces() {
80
    this.group = this.fb.group({});
81
    this.repoService.getRepositoryInterface(this.repoId).subscribe(
82
      interfaces => {
83
        this.repoInterfaces = interfaces.sort( function(a, b) {
84
          if (a.id < b.id) {
85
            return -1;
86
          } else if (a.id > b.id) {
87
            return 1;
88
          } else {
89
            return 0;
90
          }
91
        });
92
        console.log(`the number of interfaces for ${this.repoId} is ${this.repoInterfaces.length}`);
93
      },
94
      error => {
95
        console.log(error);
96
        this.loadingMessage = '';
97
        this.errorMessage = loadingRepoError;
98
      },
99
      () => {
100
        this.loadingMessage = '';
101
        this.fillInterfacesForms();
102
      }
103
    );
104
  }
105

    
106

    
107
  fillInterfacesForms() {
108
    this.dataForInterfaceComp = [];
109
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
110
      for (let i = 0; i < this.repoInterfaces.length; i++) {
111
        this.dataForInterfaceComp.push([
112
          false, i,
113
          { id: this.repo.id,
114
            datasourceType: this.repo.datasourceType,
115
            datasourceClass: this.repo.datasourceClass,
116
            registeredBy: this.repo.registeredBy
117
          },
118
          this.repoInterfaces[i]
119
        ]);
120
      }
121
    } else {
122
      this.dataForInterfaceComp.push([
123
        false, 0,
124
        { id: this.repo.id,
125
          datasourceType: this.repo.datasourceType,
126
          datasourceClass: this.repo.datasourceClass,
127
          registeredBy: this.repo.registeredBy
128
        }
129
      ]);
130
    }
131
  }
132

    
133
  addInterfaceToList(intrf?: RepositoryInterface) {
134
    const curIndex = this.dataForInterfaceComp.length;
135
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
136
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy };
137
    if (intrf) {
138
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo, intrf]);
139
    } else {
140
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo]);
141
    }
142
  }
143

    
144
  removeInterfaceFromList(i: number) {
145
    const tempArray = this.dataForInterfaceComp;
146
    this.dataForInterfaceComp = [];
147
    tempArray.splice(i, 1);
148
    this.dataForInterfaceComp = tempArray;
149
    console.log(JSON.stringify(this.dataForInterfaceComp));
150
  }
151

    
152
  getInterfaces() {
153
    this.repoInterfaces = [];
154
    for (const el of this.interfacesArray.toArray()) {
155
      const intrf = el.getInterface();
156
      if (intrf) {
157
        this.repoInterfaces.push(intrf);
158
        console.log(JSON.stringify(intrf));
159
      }
160
    }
161
    console.log('new interfaces is ', this.repoInterfaces);
162
  }
163

    
164
  updateLogoUrl(logoUrl: string) {
165
    this.updateLogoUrlModal.ids = [logoUrl];
166
    this.updateLogoUrlModal.showModal();
167
  }
168

    
169
  updatedLogoUrl(event: any) {
170
    this.repo.logoUrl = this.logoURL;
171
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
172
    this.datasourceUpdateForm.updateRepo();
173

    
174
  }
175

    
176
  getNewLogoUrl( event: any ) {
177
    this.logoURL = event.target.value;
178

    
179
  }
180

    
181
}
(4-4/10)