Moved Dashboad webapp on Angular 5 !
[src/xds/xds-agent.git] / webapp / src / app / config / config.component.ts
1 import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { CollapseModule } from 'ngx-bootstrap/collapse';
4
5 import { ConfigService, IConfig } from '../services/config.service';
6 import { ProjectService, IProject } from '../services/project.service';
7 import { XDSAgentService, IAgentStatus, IXDSConfig } from '../services/xdsagent.service';
8 import { AlertService } from '../services/alert.service';
9 import { ProjectAddModalComponent } from '../projects/projectAddModal.component';
10 import { SdkService, ISdk } from '../services/sdk.service';
11 import { SdkAddModalComponent } from '../sdks/sdkAddModal.component';
12
13 @Component({
14   selector: 'app-config',
15   templateUrl: './config.component.html',
16   styleUrls: ['./config.component.css'],
17   encapsulation: ViewEncapsulation.None
18 })
19
20 // Inspired from https://embed.plnkr.co/jgDTXknPzAaqcg9XA9zq/
21 // and from http://plnkr.co/edit/vCdjZM?p=preview
22
23 export class ConfigComponent implements OnInit {
24     @ViewChild('childProjectModal') childProjectModal: ProjectAddModalComponent;
25     @ViewChild('childSdkModal') childSdkModal: SdkAddModalComponent;
26
27     config$: Observable<IConfig>;
28     projects$: Observable<IProject[]>;
29     sdks$: Observable<ISdk[]>;
30     agentStatus$: Observable<IAgentStatus>;
31
32     curProj: number;
33     curServer: number;
34     curServerID: string;
35     userEditedLabel = false;
36
37     gConfigIsCollapsed = true;
38     sdksIsCollapsed = true;
39     projectsIsCollapsed = false;
40
41     // TODO replace by reactive FormControl + add validation
42     xdsServerConnected = false;
43     xdsServerUrl: string;
44     xdsServerRetry: string;
45     projectsRootDir: string;    // FIXME: should be remove when projectAddModal will always return full path
46     showApplyBtn = {    // Used to show/hide Apply buttons
47         'retry': false,
48         'rootDir': false,
49     };
50
51     constructor(
52         private configSvr: ConfigService,
53         private projectSvr: ProjectService,
54         private xdsAgentSvr: XDSAgentService,
55         private sdkSvr: SdkService,
56         private alert: AlertService,
57     ) {
58     }
59
60     ngOnInit() {
61         this.config$ = this.configSvr.Conf$;
62         this.projects$ = this.projectSvr.Projects$;
63         this.sdks$ = this.sdkSvr.Sdks$;
64         this.agentStatus$ = this.xdsAgentSvr.Status$;
65
66         // FIXME support multiple servers
67         this.curServer = 0;
68
69         // Bind xdsServerUrl to baseURL
70         this.xdsAgentSvr.XdsConfig$.subscribe(cfg => {
71             if (!cfg || cfg.servers.length < 1) {
72                 return;
73             }
74             const svr = cfg.servers[this.curServer];
75             this.curServerID = svr.id;
76             this.xdsServerConnected = svr.connected;
77             this.xdsServerUrl = svr.url;
78             this.xdsServerRetry = String(svr.connRetry);
79             this.projectsRootDir = ''; // SEB FIXME: add in go config? cfg.projectsRootDir;
80         });
81     }
82
83     submitGlobConf(field: string) {
84         switch (field) {
85             case 'retry':
86                 const re = new RegExp('^[0-9]+$');
87                 const rr = parseInt(this.xdsServerRetry, 10);
88                 if (re.test(this.xdsServerRetry) && rr >= 0) {
89                     this.xdsAgentSvr.setServerRetry(this.curServerID, rr);
90                 } else {
91                     this.alert.warning('Not a valid number', true);
92                 }
93                 break;
94             case 'rootDir':
95                 this.configSvr.projectsRootDir = this.projectsRootDir;
96                 break;
97             default:
98                 return;
99         }
100         this.showApplyBtn[field] = false;
101     }
102
103     xdsAgentRestartConn() {
104         const url = this.xdsServerUrl;
105         this.xdsAgentSvr.setServerUrl(this.curServerID, url);
106     }
107
108 }