3 * Copyright (C) 2017-2018 "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, ViewEncapsulation, 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';
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 encapsulation: ViewEncapsulation.None,
39 .modal-xxl .modal-lg {
45 export class ProjectAddModalComponent implements OnInit {
46 // @Input('server-id') serverID: string;
47 private serverID: string;
50 userEditedLabel = false;
51 projectTypes = Object.assign([], ProjectTypes);
53 addProjectForm: FormGroup;
54 typeCtrl: FormControl;
55 pathCliCtrl: FormControl;
56 pathSvrCtrl: FormControl;
59 private alert: AlertService,
60 private projectSvr: ProjectService,
61 private XdsConfigSvr: XDSConfigService,
62 private fb: FormBuilder,
63 private activeModal: NgbActiveModal,
65 // Define types (first one is special/placeholder)
66 this.projectTypes.unshift({ value: ProjectType.UNSET, display: '--Select a type--' });
68 this.typeCtrl = new FormControl(this.projectTypes[0].value, this.validatorProjType);
69 this.pathCliCtrl = new FormControl('', this.validatorProjPath);
70 this.pathSvrCtrl = new FormControl({ value: '', disabled: true }, this.validatorProjPath);
72 this.addProjectForm = fb.group({
74 pathCli: this.pathCliCtrl,
75 pathSvr: this.pathSvrCtrl,
76 label: ['', Validators.nullValidator],
83 this.serverID = this.XdsConfigSvr.getCurServer().id;
84 this.XdsConfigSvr.onCurServer().subscribe(svr => this.serverID = svr.id);
86 // Auto create label name
87 this.pathCliCtrl.valueChanges
91 const last = n.split('/');
93 if (last.length > 0) {
95 if (nm === '' && last.length > 0) {
99 return 'Project_' + nm;
101 .subscribe(value => {
102 if (value && !this.userEditedLabel) {
103 this.addProjectForm.patchValue({ label: value });
107 // Handle disabling of Server path
108 this.typeCtrl.valueChanges
110 .subscribe(valType => {
111 const dis = (valType === String(ProjectType.SYNCTHING));
112 this.pathSvrCtrl.reset({ value: '', disabled: dis });
117 this.activeModal.close();
120 onKeyLabel(event: any) {
121 this.userEditedLabel = (this.addProjectForm.value.label !== '');
124 onChangeLocalProject(e) {
128 if (this.cancelAction) {
132 const formVal = this.addProjectForm.value;
134 const type = formVal['type'].value;
135 this.projectSvr.add({
136 serverId: this.serverID,
137 label: formVal['label'],
138 pathClient: formVal['pathCli'],
139 pathServer: formVal['pathSvr'],
140 type: formVal['type'],
141 // FIXME: allow to set defaultSdkID from New Project config panel
144 this.alert.info('Project ' + prj.label + ' successfully created.');
147 // Reset Value for the next creation
148 this.addProjectForm.reset();
149 const selectedType = this.projectTypes[0].value;
150 this.addProjectForm.patchValue({ type: selectedType });
154 this.alert.error(err, 60);
159 private validatorProjType(g: FormGroup): ValidationErrors | null {
160 return (g.value !== ProjectType.UNSET) ? null : { validatorProjType: { valid: false } };
163 private validatorProjPath(g: FormGroup): ValidationErrors | null {
164 return (g.disabled || g.value !== '') ? null : { validatorProjPath: { valid: false } };