Skip to content
Snippets Groups Projects
instance-form.component.ts 4.97 KiB
/**
 * 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, EventEmitter, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

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

@Component({
    selector: 'app-instance-form',
    templateUrl: 'instance-form.component.html'
})
export class InstanceFormComponent implements OnInit {
    @Input() instance: Instance;
    @Input() files: FileInfo[];
    @Input() directoryIsLoading: boolean;
    @Input() directoryIsLoaded: boolean;
    @Output() loadRootDirectory: EventEmitter<string> = new EventEmitter();
    @Output() onSubmit: EventEmitter<Instance> = new EventEmitter();

    public form = new FormGroup({
        name: new FormControl('', [Validators.required]),
        label: new FormControl('', [Validators.required]),
        description: new FormControl('', [Validators.required]),
        display: new FormControl('', [Validators.required]),
        data_path: new FormControl(''),
        public: new FormControl(true, [Validators.required]),
        portal_logo: new FormControl(''),
        design_color: new FormControl('#7AC29A', [Validators.required]),
        design_background_color: new FormControl('#7AC29A', [Validators.required]),
        design_logo: new FormControl(''),
        design_favicon: new FormControl(''),
        home_component: new FormControl('WelcomeComponent', [Validators.required]),
        home_component_config: new FormGroup({
            home_component_text: new FormControl(`AstroNomical Information System is a generic web tool aimed 
at facilitating and homogenizing the implementation of astronomical data. It allows 
the fast implementation of a project data exchange platform in a dedicated information system.`, [Validators.required]),
            home_component_logo: new FormControl('home_component_logo.png', [Validators.required])
        }),
        samp_enabled: new FormControl(true),
        search_by_criteria_allowed: new FormControl(true),
        search_by_criteria_label: new FormControl({value: 'Search', disabled: false}),
        search_multiple_allowed: new FormControl(false),
        search_multiple_label: new FormControl({value: 'Search multiple', disabled: true}),
        search_multiple_all_datasets_selected: new FormControl({value: false, disabled: true}),
        documentation_allowed: new FormControl(false),
        documentation_label: new FormControl({value: 'Documentation', disabled: true})
    });

    ngOnInit() {
        if (this.instance) {
            this.form.patchValue(this.instance);
            this.form.controls.name.disable();
            if (this.form.controls.search_multiple_allowed.value) {
                this.form.controls.search_multiple_label.enable();
                this.form.controls.search_multiple_all_datasets_selected.enable();
            }
            if (this.form.controls.search_by_criteria_allowed.value) {
                this.form.controls.search_by_criteria_label.enable();
            }
            if (this.form.controls.documentation_allowed.value) {
                this.form.controls.documentation_label.enable();
            }
        }
    }

    isDataPathEmpty() {
        return this.form.controls.data_path.value == '';
    }

    onChangeFileSelect(path: string) {
        this.loadRootDirectory.emit(`${this.form.controls.data_path.value}${path}`);
    }

    getHomeConfigFormGroup() {
        return this.form.controls.home_component_config as FormGroup;
    }

    checkDisableSearchByCriteriaAllowed() {
        if (this.form.controls.search_by_criteria_allowed.value) {
            this.form.controls.search_by_criteria_label.enable();
        } else {
            this.form.controls.search_by_criteria_label.disable();
        }
    }

    checkDisableAllDatasetsSelected() {
        if (this.form.controls.search_multiple_allowed.value) {
            this.form.controls.search_multiple_label.enable();
            this.form.controls.search_multiple_all_datasets_selected.enable();
        } else {
            this.form.controls.search_multiple_label.disable();
            this.form.controls.search_multiple_all_datasets_selected.setValue(false);
            this.form.controls.search_multiple_all_datasets_selected.disable();
        }
    }

    checkDisableDocumentationAllowed() {
        if (this.form.controls.documentation_allowed.value) {
            this.form.controls.documentation_label.enable();
        } else {
            this.form.controls.documentation_label.disable();
        }
    }

    submit() {
        if (this.instance) {
            this.onSubmit.emit({
                ...this.instance,
                ...this.form.getRawValue()
            });
        } else {
            this.onSubmit.emit(this.form.getRawValue());
        }
    }
}