b107e811fd732502417d58ef3c7d062b414867d6
[src/xds/xds-server.git] / webapp / src / app / config / config.component.ts
1 import { Component, ViewChild, OnInit } from "@angular/core";
2 import { Observable } from 'rxjs/Observable';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
4 import { CollapseModule } from 'ngx-bootstrap/collapse';
5
6 import { ConfigService, IConfig, IxdsAgentPackage } from "../services/config.service";
7 import { XDSServerService, IServerStatus, IXDSAgentInfo } from "../services/xdsserver.service";
8 import { XDSAgentService, IAgentStatus } from "../services/xdsagent.service";
9 import { SyncthingService, ISyncThingStatus } from "../services/syncthing.service";
10 import { AlertService } from "../services/alert.service";
11 import { ISdk, SdkService } from "../services/sdk.service";
12 import { ProjectAddModalComponent } from "../projects/projectAddModal.component";
13 import { SdkAddModalComponent } from "../sdks/sdkAddModal.component";
14
15 @Component({
16     templateUrl: './app/config/config.component.html',
17     styleUrls: ['./app/config/config.component.css']
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     sdks$: Observable<ISdk[]>;
29     serverStatus$: Observable<IServerStatus>;
30     agentStatus$: Observable<IAgentStatus>;
31     localSTStatus$: Observable<ISyncThingStatus>;
32
33     curProj: number;
34     userEditedLabel: boolean = false;
35     xdsAgentPackages: IxdsAgentPackage[] = [];
36
37     gConfigIsCollapsed: boolean = true;
38     sdksIsCollapsed: boolean = true;
39     projectsIsCollapsed: boolean = false;
40
41     // TODO replace by reactive FormControl + add validation
42     syncToolUrl: string;
43     xdsAgentUrl: string;
44     xdsAgentRetry: 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 xdsServerSvr: XDSServerService,
54         private xdsAgentSvr: XDSAgentService,
55         private stSvr: SyncthingService,
56         private sdkSvr: SdkService,
57         private alert: AlertService,
58     ) {
59     }
60
61     ngOnInit() {
62         this.config$ = this.configSvr.conf;
63         this.sdks$ = this.sdkSvr.Sdks$;
64         this.serverStatus$ = this.xdsServerSvr.Status$;
65         this.agentStatus$ = this.xdsAgentSvr.Status$;
66         this.localSTStatus$ = this.stSvr.Status$;
67
68         // Bind xdsAgentUrl to baseURL
69         this.config$.subscribe(cfg => {
70             this.syncToolUrl = cfg.localSThg.URL;
71             this.xdsAgentUrl = cfg.xdsAgent.URL;
72             this.xdsAgentRetry = String(cfg.xdsAgent.retry);
73             this.projectsRootDir = cfg.projectsRootDir;
74             this.xdsAgentPackages = cfg.xdsAgentPackages;
75         });
76
77     }
78
79     submitGlobConf(field: string) {
80         switch (field) {
81             case "retry":
82                 let re = new RegExp('^[0-9]+$');
83                 let rr = parseInt(this.xdsAgentRetry, 10);
84                 if (re.test(this.xdsAgentRetry) && rr >= 0) {
85                     this.configSvr.xdsAgentRetry = rr;
86                 } else {
87                     this.alert.warning("Not a valid number", true);
88                 }
89                 break;
90             case "rootDir":
91                 this.configSvr.projectsRootDir = this.projectsRootDir;
92                 break;
93             default:
94                 return;
95         }
96         this.showApplyBtn[field] = false;
97     }
98
99     xdsAgentRestartConn() {
100         let aUrl = this.xdsAgentUrl;
101         this.configSvr.syncToolURL = this.syncToolUrl;
102         this.configSvr.xdsAgentUrl = aUrl;
103         this.configSvr.loadProjects();
104     }
105
106 }