1 import { Component, Input, ViewChild, OnInit } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { ModalDirective } from 'ngx-bootstrap/modal';
4 import { FormControl, FormGroup, Validators, FormBuilder, ValidatorFn, AbstractControl } from '@angular/forms';
6 // Import RxJs required methods
7 import 'rxjs/add/operator/map';
8 import 'rxjs/add/operator/filter';
9 import 'rxjs/add/operator/debounceTime';
11 import { AlertService, IAlert } from "../services/alert.service";
13 ConfigService, IConfig, IProject, ProjectType, ProjectTypes,
15 } from "../services/config.service";
19 selector: 'project-add-modal',
20 templateUrl: './app/projects/projectAddModal.component.html',
21 styleUrls: ['./app/projects/projectAddModal.component.css']
23 export class ProjectAddModalComponent {
24 @ViewChild('childProjectModal') public childProjectModal: ModalDirective;
25 @Input() title?: string;
27 config$: Observable<IConfig>;
29 cancelAction: boolean = false;
30 userEditedLabel: boolean = false;
31 projectTypes = ProjectTypes;
33 addProjectForm: FormGroup;
34 typeCtrl: FormControl;
35 pathCliCtrl: FormControl;
36 pathSvrCtrl: FormControl;
39 private alert: AlertService,
40 private configSvr: ConfigService,
41 private fb: FormBuilder
43 // Define types (first one is special/placeholder)
44 this.projectTypes.unshift({ value: -1, display: "--Select a type--" });
46 this.typeCtrl = new FormControl(this.projectTypes[0].value, Validators.pattern("[0-9]+"));
47 this.pathCliCtrl = new FormControl("", Validators.required);
48 this.pathSvrCtrl = new FormControl({ value: "", disabled: true }, [Validators.required, Validators.minLength(1)]);
50 this.addProjectForm = fb.group({
52 pathCli: this.pathCliCtrl,
53 pathSvr: this.pathSvrCtrl,
54 label: ["", Validators.nullValidator],
59 this.config$ = this.configSvr.conf;
61 // Auto create label name
62 this.pathCliCtrl.valueChanges
65 .map(n => "Project_" + n.split('/')[0])
67 if (value && !this.userEditedLabel) {
68 this.addProjectForm.patchValue({ label: value });
72 // Handle disabling of Server path
73 this.typeCtrl.valueChanges
75 .subscribe(valType => {
76 let dis = (valType === String(ProjectType.SYNCTHING));
77 this.pathSvrCtrl.reset({ value: "", disabled: dis });
82 this.cancelAction = false;
83 this.childProjectModal.show();
87 this.childProjectModal.hide();
90 onKeyLabel(event: any) {
91 this.userEditedLabel = (this.addProjectForm.value.label !== "");
94 /* FIXME: change input to file type
95 <td><input type="file" id="select-local-path" webkitdirectory
96 formControlName="pathCli" placeholder="myProject" (change)="onChangeLocalProject($event)"></td>
98 onChangeLocalProject(e) {
99 if e.target.files.length < 1 {
100 console.log('SEB NO files');
102 let dir = e.target.files[0].webkitRelativePath;
103 console.log("SEB files: " + dir);
104 let u = URL.createObjectURL(e.target.files[0]);
107 onChangeLocalProject(e) {
111 if (this.cancelAction) {
115 let formVal = this.addProjectForm.value;
117 let type = formVal['type'].value;
118 let numType = Number(formVal['type']);
119 this.configSvr.addProject({
120 label: formVal['label'],
121 pathClient: formVal['pathCli'],
122 pathServer: formVal['pathSvr'],
124 // FIXME: allow to set defaultSdkID from New Project config panel
127 this.alert.info("Project " + prj.label + " successfully created.");
130 // Reset Value for the next creation
131 this.addProjectForm.reset();
132 let selectedType = this.projectTypes[0].value;
133 this.addProjectForm.patchValue({ type: selectedType });
137 this.alert.error("Configuration ERROR: " + err, 60);