Skip to content
Snippets Groups Projects
instance-form.component.ts 1.95 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/store/models';

@Component({
    selector: 'app-instance-form',
    templateUrl: 'instance-form.component.html'
})
export class InstanceFormComponent implements OnInit {
    @Input() instance: Instance;
    @Output() onSubmit: EventEmitter<Instance> = new EventEmitter();

    public searchFormGroup = new FormGroup({
        search_by_criteria_allowed: new FormControl(true),
        search_multiple_allowed: new FormControl(false),
        search_multiple_all_datasets_selected: new FormControl({value: false, disabled: true})
    });

    public documentationFormGroup = new FormGroup({
        documentation_allowed: new FormControl(false)
    });

    public form = new FormGroup({
        name: new FormControl('', [Validators.required]),
        label: new FormControl('', [Validators.required]),
        client_url: new FormControl('', [Validators.required]),
        config: new FormGroup({
            search: this.searchFormGroup,
            documentation: this.documentationFormGroup
        })
    });

    ngOnInit() {
        if (this.instance) {
            this.form.patchValue(this.instance);
            if (this.searchFormGroup.controls.search_multiple_allowed.value) {
                this.searchFormGroup.controls.search_multiple_all_datasets_selected.enable();
            }
        }
    }

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