1 import { Component, OnInit } from "@angular/core";
2 import { Observable } from 'rxjs/Observable';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
5 // Import RxJs required methods
6 import 'rxjs/add/operator/map';
7 import 'rxjs/add/operator/filter';
8 import 'rxjs/add/operator/debounceTime';
10 import { ConfigService, IConfig, IProject, ProjectType, ProjectTypes,
11 IxdsAgentPackage } from "../services/config.service";
12 import { XDSServerService, IServerStatus, IXDSAgentInfo } from "../services/xdsserver.service";
13 import { XDSAgentService, IAgentStatus } from "../services/xdsagent.service";
14 import { SyncthingService, ISyncThingStatus } from "../services/syncthing.service";
15 import { AlertService } from "../services/alert.service";
16 import { ISdk, SdkService } from "../services/sdk.service";
19 templateUrl: './app/config/config.component.html',
20 styleUrls: ['./app/config/config.component.css']
23 // Inspired from https://embed.plnkr.co/jgDTXknPzAaqcg9XA9zq/
24 // and from http://plnkr.co/edit/vCdjZM?p=preview
26 export class ConfigComponent implements OnInit {
28 config$: Observable<IConfig>;
29 sdks$: Observable<ISdk[]>;
30 serverStatus$: Observable<IServerStatus>;
31 agentStatus$: Observable<IAgentStatus>;
32 localSTStatus$: Observable<ISyncThingStatus>;
35 userEditedLabel: boolean = false;
36 xdsAgentPackages: IxdsAgentPackage[] = [];
37 projectTypes = ProjectTypes;
39 // TODO replace by reactive FormControl + add validation
42 xdsAgentRetry: string;
43 projectsRootDir: string;
44 showApplyBtn = { // Used to show/hide Apply buttons
49 addProjectForm: FormGroup;
50 pathCliCtrl = new FormControl("", Validators.required);
51 pathSvrCtrl = new FormControl("", Validators.required);
54 private configSvr: ConfigService,
55 private xdsServerSvr: XDSServerService,
56 private xdsAgentSvr: XDSAgentService,
57 private stSvr: SyncthingService,
58 private sdkSvr: SdkService,
59 private alert: AlertService,
60 private fb: FormBuilder
62 // Define types (first one is special/placeholder)
63 this.projectTypes.unshift({value: -1, display: "--Select a type--"});
64 let selectedType = this.projectTypes[0].value;
67 this.addProjectForm = fb.group({
68 pathCli: this.pathCliCtrl,
69 pathSvr: this.pathSvrCtrl,
70 label: ["", Validators.nullValidator],
71 type: [selectedType, Validators.pattern("[0-9]+")],
76 this.config$ = this.configSvr.conf;
77 this.sdks$ = this.sdkSvr.Sdks$;
78 this.serverStatus$ = this.xdsServerSvr.Status$;
79 this.agentStatus$ = this.xdsAgentSvr.Status$;
80 this.localSTStatus$ = this.stSvr.Status$;
82 // Bind xdsAgentUrl to baseURL
83 this.config$.subscribe(cfg => {
84 this.syncToolUrl = cfg.localSThg.URL;
85 this.xdsAgentUrl = cfg.xdsAgent.URL;
86 this.xdsAgentRetry = String(cfg.xdsAgent.retry);
87 this.projectsRootDir = cfg.projectsRootDir;
88 this.xdsAgentPackages = cfg.xdsAgentPackages;
91 // Auto create label name
92 this.pathCliCtrl.valueChanges
95 .map(n => "Project_" + n.split('/')[0])
97 if (value && !this.userEditedLabel) {
98 this.addProjectForm.patchValue({ label: value });
102 // Select 1 first type by default
103 // SEB this.typeCtrl.setValue({type: ProjectTypes[0].value});
106 onKeyLabel(event: any) {
107 this.userEditedLabel = (this.addProjectForm.value.label !== "");
110 submitGlobConf(field: string) {
113 let re = new RegExp('^[0-9]+$');
114 let rr = parseInt(this.xdsAgentRetry, 10);
115 if (re.test(this.xdsAgentRetry) && rr >= 0) {
116 this.configSvr.xdsAgentRetry = rr;
118 this.alert.warning("Not a valid number", true);
122 this.configSvr.projectsRootDir = this.projectsRootDir;
127 this.showApplyBtn[field] = false;
130 xdsAgentRestartConn() {
131 let aUrl = this.xdsAgentUrl;
132 this.configSvr.syncToolURL = this.syncToolUrl;
133 this.configSvr.xdsAgentUrl = aUrl;
134 this.configSvr.loadProjects();
138 let formVal = this.addProjectForm.value;
140 let type = formVal['type'].value;
141 let numType = Number(formVal['type']);
142 this.configSvr.addProject({
143 label: formVal['label'],
144 pathClient: formVal['pathCli'],
145 pathServer: formVal['pathSvr'],
147 // FIXME: allow to set defaultSdkID from New Project config panel