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
66 let last = n.split('/');
68 if (last.length > 0) {
70 if (nm === "" && last.length > 0) {
74 return "Project_" + nm;
77 if (value && !this.userEditedLabel) {
78 this.addProjectForm.patchValue({ label: value });
82 // Handle disabling of Server path
83 this.typeCtrl.valueChanges
85 .subscribe(valType => {
86 let dis = (valType === String(ProjectType.SYNCTHING));
87 this.pathSvrCtrl.reset({ value: "", disabled: dis });
92 this.cancelAction = false;
93 this.childProjectModal.show();
97 this.childProjectModal.hide();
100 onKeyLabel(event: any) {
101 this.userEditedLabel = (this.addProjectForm.value.label !== "");
104 /* FIXME: change input to file type
105 <td><input type="file" id="select-local-path" webkitdirectory
106 formControlName="pathCli" placeholder="myProject" (change)="onChangeLocalProject($event)"></td>
108 onChangeLocalProject(e) {
109 if e.target.files.length < 1 {
110 console.log('NO files');
112 let dir = e.target.files[0].webkitRelativePath;
113 console.log("files: " + dir);
114 let u = URL.createObjectURL(e.target.files[0]);
117 onChangeLocalProject(e) {
121 if (this.cancelAction) {
125 let formVal = this.addProjectForm.value;
127 let type = formVal['type'].value;
128 let numType = Number(formVal['type']);
129 this.configSvr.addProject({
130 label: formVal['label'],
131 pathClient: formVal['pathCli'],
132 pathServer: formVal['pathSvr'],
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 let selectedType = this.projectTypes[0].value;
143 this.addProjectForm.patchValue({ type: selectedType });
147 this.alert.error("Configuration ERROR: " + err, 60);