Skip to content
Snippets Groups Projects
data-path-form-control.component.ts 2.03 KiB
Newer Older
/**
 * This file is part of Anis Client.
 *
 * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import { Component, Input, Output, TemplateRef, EventEmitter } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

import { FileInfo } from 'src/app/metamodel/models';

@Component({
    selector: 'app-data-path-form-control',
    templateUrl: 'data-path-form-control.component.html'
})
export class DataPathFormControlComponent {
    @Input() form: FormGroup;
    @Input() rootDirectory: FileInfo[];
    @Input() rootDirectoryIsLoading: boolean;
    @Input() rootDirectoryIsLoaded: boolean;
    @Output() loadRootDirectory: EventEmitter<string> = new EventEmitter();

    modalRef: BsModalRef;
    fileExplorerPath = '';
    fileExplorerPristine = true;

    constructor(private modalService: BsModalService) { }

    openModal(template: TemplateRef<any>) {
        this.fileExplorerPristine = true;
        this.fileExplorerPath = this.form.controls.data_path.value;
        if (!this.fileExplorerPath) {
            this.fileExplorerPath = '';
        }
        this.modalRef = this.modalService.show(template);
        this.loadRootDirectory.emit(this.fileExplorerPath);
    }

    dataPathAction(fileInfo: FileInfo): void {
        if (fileInfo.name === '.' || fileInfo.type !== 'dir') {
            return;
        }

        if (fileInfo.name === '..') {
            this.fileExplorerPath = this.fileExplorerPath.substr(0, this.fileExplorerPath.lastIndexOf("/"));
        } else {
            this.fileExplorerPath += '/' + fileInfo.name;
        }
        this.fileExplorerPristine = false;
        this.loadRootDirectory.emit(this.fileExplorerPath);
    }

    selectDirectory() {
        this.form.controls.data_path.setValue(this.fileExplorerPath);
        this.modalRef.hide();
    }
}