Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / pages / build / build-settings-modal / build-settings-modal.component.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, Input, OnInit } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
22 import { FormControl, FormGroup, Validators, ValidationErrors, FormBuilder, ValidatorFn, AbstractControl } from '@angular/forms';
23
24 import { AlertService } from '../../../@core-xds/services/alert.service';
25 import { ProjectService, IProject } from '../../../@core-xds/services/project.service';
26
27
28 @Component({
29   selector: 'xds-build-settings-modal',
30   templateUrl: 'build-settings-modal.component.html',
31 })
32
33 export class BuildSettingsModalComponent implements OnInit {
34   // @Input('server-id') serverID: string;
35   private serverID: string;
36
37   closeAction = false;
38   userEditedLabel = false;
39
40   settingsProjectForm: FormGroup;
41   subpathCtrl = new FormControl('', Validators.nullValidator);
42
43   private curPrj: IProject;
44
45   constructor(
46     private alert: AlertService,
47     private projectSvr: ProjectService,
48     private fb: FormBuilder,
49     private activeModal: NgbActiveModal,
50   ) {
51     this.settingsProjectForm = fb.group({
52       subpath: this.subpathCtrl,
53       cmdClean: ['', Validators.required],
54       cmdPrebuild: ['', Validators.nullValidator],
55       cmdBuild: ['', Validators.required],
56       cmdPopulate: ['', Validators.nullValidator],
57       cmdArgs: ['', Validators.nullValidator],
58       envVars: ['', Validators.nullValidator],
59     });
60   }
61
62   ngOnInit() {
63     this.curPrj = this.projectSvr.getCurrent();
64     this.settingsProjectForm.patchValue(this.curPrj.uiSettings);
65   }
66
67   closeModal() {
68     this.activeModal.close();
69   }
70
71   resetDefault() {
72     this.settingsProjectForm.patchValue(this.projectSvr.getDefaultSettings());
73   }
74
75   onSubmit() {
76     if (!this.closeAction) {
77       return;
78     }
79
80     this.curPrj.uiSettings = this.settingsProjectForm.value;
81     this.projectSvr.setSettings(this.curPrj)
82     .subscribe(prj => {
83       this.alert.info('Settings of project "' + prj.label + '" successfully updated.');
84       this.closeModal();
85
86       // Reset Value for the next creation
87       this.settingsProjectForm.reset();
88     },
89     err => {
90       this.alert.error(err, 60);
91       this.closeModal();
92     });
93   }
94
95 }