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',
37 styleUrls: ['project-add-modal.component.scss'],
39 export class ProjectAddModalComponent implements OnInit {
40 // @Input('server-id') serverID: string;
41 private serverID: string;
44 userEditedLabel = false;
45 projectTypes = Object.assign([], ProjectTypes);
47 addProjectForm: FormGroup;
48 typeCtrl: FormControl;
49 pathCliCtrl: FormControl;
50 pathSvrCtrl: FormControl;
53 private alert: AlertService,
54 private projectSvr: ProjectService,
55 private XdsConfigSvr: XDSConfigService,
56 private fb: FormBuilder,
57 private activeModal: NgbActiveModal,
59 // Define types (first one is special/placeholder)
60 this.projectTypes.unshift({ value: ProjectType.UNSET, display: '--Select a type--' });
62 this.typeCtrl = new FormControl(this.projectTypes[0].value, this.validatorProjType);
63 this.pathCliCtrl = new FormControl('', this.validatorProjPath);
64 this.pathSvrCtrl = new FormControl({ value: '', disabled: true }, this.validatorProjPath);
66 this.addProjectForm = fb.group({
68 pathCli: this.pathCliCtrl,
69 pathSvr: this.pathSvrCtrl,
70 label: ['', Validators.nullValidator],
77 this.serverID = this.XdsConfigSvr.getCurServer().id;
78 this.XdsConfigSvr.onCurServer().subscribe(svr => this.serverID = svr.id);
80 // Auto create label name
81 this.pathCliCtrl.valueChanges
85 const last = n.split('/');
87 if (last.length > 0) {
89 if (nm === '' && last.length > 0) {
93 return 'Project_' + nm;
96 if (value && !this.userEditedLabel) {
97 this.addProjectForm.patchValue({ label: value });
101 // Handle disabling of Server path
102 this.typeCtrl.valueChanges
104 .subscribe(valType => {
105 const dis = (valType === String(ProjectType.SYNCTHING));
106 this.pathSvrCtrl.reset({ value: '', disabled: dis });
111 this.activeModal.close();
114 onKeyLabel(event: any) {
115 this.userEditedLabel = (this.addProjectForm.value.label !== '');
118 onChangeLocalProject(e) {
122 if (this.cancelAction) {
126 const formVal = this.addProjectForm.value;
128 const type = formVal['type'].value;
129 this.projectSvr.add({
130 serverId: this.serverID,
131 label: formVal['label'],
132 pathClient: formVal['pathCli'],
133 pathServer: formVal['pathSvr'],
134 type: formVal['type'],
135 // FIXME: allow to set defaultSdkID from New Project config panel
138 this.alert.info('Project ' + prj.label + ' successfully created.');
141 // Reset Value for the next creation
142 this.addProjectForm.reset();
143 const selectedType = this.projectTypes[0].value;
144 this.addProjectForm.patchValue({ type: selectedType });
148 this.alert.error(err, 60);
153 private validatorProjType(g: FormGroup): ValidationErrors | null {
154 return (g.value !== ProjectType.UNSET) ? null : { validatorProjType: { valid: false } };
157 private validatorProjPath(g: FormGroup): ValidationErrors | null {
158 return (g.disabled || g.value !== '') ? null : { validatorProjPath: { valid: false } };