summaryrefslogtreecommitdiffstats
path: root/webapp/src/app/config/config.component.ts
blob: 0df707b9c7de9beadd207425f182adbe90ba6160 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { Component, OnInit } from "@angular/core";
import { Observable } from 'rxjs/Observable';
import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';

// Import RxJs required methods
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/debounceTime';

import { ConfigService, IConfig, IProject, ProjectType, ProjectTypes,
    IxdsAgentPackage } from "../services/config.service";
import { XDSServerService, IServerStatus, IXDSAgentInfo } from "../services/xdsserver.service";
import { XDSAgentService, IAgentStatus } from "../services/xdsagent.service";
import { SyncthingService, ISyncThingStatus } from "../services/syncthing.service";
import { AlertService } from "../services/alert.service";
import { ISdk, SdkService } from "../services/sdk.service";

@Component({
    templateUrl: './app/config/config.component.html',
    styleUrls: ['./app/config/config.component.css']
})

// Inspired from https://embed.plnkr.co/jgDTXknPzAaqcg9XA9zq/
// and from http://plnkr.co/edit/vCdjZM?p=preview

export class ConfigComponent implements OnInit {

    config$: Observable<IConfig>;
    sdks$: Observable<ISdk[]>;
    serverStatus$: Observable<IServerStatus>;
    agentStatus$: Observable<IAgentStatus>;
    localSTStatus$: Observable<ISyncThingStatus>;

    curProj: number;
    userEditedLabel: boolean = false;
    xdsAgentPackages: IxdsAgentPackage[] = [];
    projectTypes = ProjectTypes;

    // TODO replace by reactive FormControl + add validation
    syncToolUrl: string;
    xdsAgentUrl: string;
    xdsAgentRetry: string;
    projectsRootDir: string;
    showApplyBtn = {    // Used to show/hide Apply buttons
        "retry": false,
        "rootDir": false,
    };

    addProjectForm: FormGroup;
    pathCliCtrl = new FormControl("", Validators.required);
    pathSvrCtrl = new FormControl("", Validators.required);

    constructor(
        private configSvr: ConfigService,
        private xdsServerSvr: XDSServerService,
        private xdsAgentSvr: XDSAgentService,
        private stSvr: SyncthingService,
        private sdkSvr: SdkService,
        private alert: AlertService,
        private fb: FormBuilder
    ) {
        // Define types (first one is special/placeholder)
        this.projectTypes.unshift({value: -1, display: "--Select a type--"});
        let selectedType = this.projectTypes[0].value;

        this.curProj = 0;
        this.addProjectForm = fb.group({
            pathCli: this.pathCliCtrl,
            pathSvr: this.pathSvrCtrl,
            label: ["", Validators.nullValidator],
            type: [selectedType, Validators.pattern("[0-9]+")],
        });
    }

    ngOnInit() {
        this.config$ = this.configSvr.conf;
        this.sdks$ = this.sdkSvr.Sdks$;
        this.serverStatus$ = this.xdsServerSvr.Status$;
        this.agentStatus$ = this.xdsAgentSvr.Status$;
        this.localSTStatus$ = this.stSvr.Status$;

        // Bind xdsAgentUrl to baseURL
        this.config$.subscribe(cfg => {
            this.syncToolUrl = cfg.localSThg.URL;
            this.xdsAgentUrl = cfg.xdsAgent.URL;
            this.xdsAgentRetry = String(cfg.xdsAgent.retry);
            this.projectsRootDir = cfg.projectsRootDir;
            this.xdsAgentPackages = cfg.xdsAgentPackages;
        });

        // Auto create label name
        this.pathCliCtrl.valueChanges
            .debounceTime(100)
            .filter(n => n)
            .map(n => "Project_" + n.split('/')[0])
            .subscribe(value => {
                if (value && !this.userEditedLabel) {
                    this.addProjectForm.patchValue({ label: value });
                }
            });

        // Select 1 first type by default
        // SEB this.typeCtrl.setValue({type: ProjectTypes[0].value});
    }

    onKeyLabel(event: any) {
        this.userEditedLabel = (this.addProjectForm.value.label !== "");
    }

    submitGlobConf(field: string) {
        switch (field) {
            case "retry":
                let re = new RegExp('^[0-9]+$');
                let rr = parseInt(this.xdsAgentRetry, 10);
                if (re.test(this.xdsAgentRetry) && rr >= 0) {
                    this.configSvr.xdsAgentRetry = rr;
                } else {
                    this.alert.warning("Not a valid number", true);
                }
                break;
            case "rootDir":
                this.configSvr.projectsRootDir = this.projectsRootDir;
                break;
            default:
                return;
        }
        this.showApplyBtn[field] = false;
    }

    xdsAgentRestartConn() {
        let aUrl = this.xdsAgentUrl;
        this.configSvr.syncToolURL = this.syncToolUrl;
        this.configSvr.xdsAgentUrl = aUrl;
        this.configSvr.loadProjects();
    }

    onSubmit() {
        let formVal = this.addProjectForm.value;

        let type = formVal['type'].value;
        let numType = Number(formVal['type']);
        this.configSvr.addProject({
            label: formVal['label'],
            pathClient: formVal['pathCli'],
            pathServer: formVal['pathSvr'],
            type: numType,
            // FIXME: allow to set defaultSdkID from New Project config panel
        });
    }

}