3 * Copyright (C) 2017 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import { Component, Input, ViewChild, 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';
24 // Import RxJs required methods
25 import 'rxjs/add/operator/map';
26 import 'rxjs/add/operator/filter';
27 import 'rxjs/add/operator/debounceTime';
29 import { AlertService, IAlert } from '../../../@core-xds/services/alert.service';
30 import { ProjectService, IProject, ProjectType, ProjectTypes } from '../../../@core-xds/services/project.service';
31 import { XDSConfigService } from '../../../@core-xds/services/xds-config.service';
35 selector: 'xds-project-add-modal',
36 templateUrl: 'project-add-modal.component.html',
38 export class ProjectAddModalComponent implements OnInit {
39 // @Input('server-id') serverID: string;
40 private serverID: string;
43 userEditedLabel = false;
44 projectTypes = Object.assign([], ProjectTypes);
46 addProjectForm: FormGroup;
47 typeCtrl: FormControl;
48 pathCliCtrl: FormControl;
49 pathSvrCtrl: FormControl;
52 private alert: AlertService,
53 private projectSvr: ProjectService,
54 private XdsConfigSvr: XDSConfigService,
55 private fb: FormBuilder,
56 private activeModal: NgbActiveModal,
58 // Define types (first one is special/placeholder)
59 this.projectTypes.unshift({ value: ProjectType.UNSET, display: '--Select a type--' });
61 this.typeCtrl = new FormControl(this.projectTypes[0].value, this.validatorProjType);
62 this.pathCliCtrl = new FormControl('', this.validatorProjPath);
63 this.pathSvrCtrl = new FormControl({ value: '', disabled: true }, this.validatorProjPath);
65 this.addProjectForm = fb.group({
67 pathCli: this.pathCliCtrl,
68 pathSvr: this.pathSvrCtrl,
69 label: ['', Validators.nullValidator],
76 this.serverID = this.XdsConfigSvr.getCurServer().id;
77 this.XdsConfigSvr.onCurServer().subscribe(svr => this.serverID = svr.id);
79 // Auto create label name
80 this.pathCliCtrl.valueChanges
84 const last = n.split('/');
86 if (last.length > 0) {
88 if (nm === '' && last.length > 0) {
92 return 'Project_' + nm;
95 if (value && !this.userEditedLabel) {
96 this.addProjectForm.patchValue({ label: value });
100 // Handle disabling of Server path
101 this.typeCtrl.valueChanges
103 .subscribe(valType => {
104 const dis = (valType === String(ProjectType.SYNCTHING));
105 this.pathSvrCtrl.reset({ value: '', disabled: dis });
110 this.activeModal.close();
113 onKeyLabel(event: any) {
114 this.userEditedLabel = (this.addProjectForm.value.label !== '');
117 onChangeLocalProject(e) {
121 if (this.cancelAction) {
125 const formVal = this.addProjectForm.value;
127 const type = formVal['type'].value;
128 this.projectSvr.add({
129 serverId: this.serverID,
130 label: formVal['label'],
131 pathClient: formVal['pathCli'],
132 pathServer: formVal['pathSvr'],
133 type: formVal['type'],
134 // FIXME: allow to set defaultSdkID from New Project config panel
137 this.alert.info('Project ' + prj.label + ' successfully created.');
140 // Reset Value for the next creation
141 this.addProjectForm.reset();
142 const selectedType = this.projectTypes[0].value;
143 this.addProjectForm.patchValue({ type: selectedType });
147 this.alert.error(err, 60);
152 private validatorProjType(g: FormGroup): ValidationErrors | null {
153 return (g.value !== ProjectType.UNSET) ? null : { validatorProjType: { valid: false } };
156 private validatorProjPath(g: FormGroup): ValidationErrors | null {
157 return (g.disabled || g.value !== '') ? null : { validatorProjPath: { valid: false } };