form-dataset.component.ts 8.17 KB
Newer Older
1
import { Component, Input, Output, EventEmitter, ViewChild, TemplateRef, OnChanges, SimpleChanges } from '@angular/core';
François Agneray's avatar
François Agneray committed
2
3
import { NgForm } from '@angular/forms';

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

7
import { Dataset, Survey, Attribute, DatasetFamily, FileInfo, ImageLimit } from '../../store/model';
François Agneray's avatar
François Agneray committed
8
9
10

@Component({
    selector: 'app-form-dataset',
François Agneray's avatar
François Agneray committed
11
12
    templateUrl: 'form-dataset.component.html',
    styleUrls: ['form-dataset.component.css']
François Agneray's avatar
François Agneray committed
13
})
14
export class FormDatasetComponent implements OnChanges {
François Agneray's avatar
François Agneray committed
15
16
    @ViewChild(NgForm, {static: true}) ngForm: NgForm;
    @Input() model: Dataset = new Dataset();
17
    @Input() surveyList: Survey[];
François Agneray's avatar
François Agneray committed
18
    @Input() attributeList: Attribute[];
François Agneray's avatar
François Agneray committed
19
20
    @Input() tableList: string[];
    @Input() datasetFamilyList: DatasetFamily[];
21
    @Input() idDatasetFamilyQueryParam: number = 0;
22
23
24
25
26
27
    @Input() rootDirectoryInfo: FileInfo[];
    @Input() rootDirectoryInfoIsLoading: boolean;
    @Input() rootDirectoryInfoIsLoaded: boolean;
    @Input() datasetDirectoryInfo: FileInfo[];
    @Input() datasetDirectoryInfoIsLoading: boolean;
    @Input() datasetDirectoryInfoIsLoaded: boolean;
28
29
30
    @Input() imageLimitIsLoading: boolean;
    @Input() imageLimitIsLoaded: boolean;
    @Input() imageLimit: ImageLimit;
31
    @Output() changeSurvey: EventEmitter<number> = new EventEmitter();
32
33
    @Output() loadRootDirectoryInfo: EventEmitter<string> = new EventEmitter();
    @Output() loadDatasetDirectoryInfo: EventEmitter<string> = new EventEmitter();
34
    @Output() loadFitsImageLimits: EventEmitter<string> = new EventEmitter();
François Agneray's avatar
François Agneray committed
35
36
    @Output() submitted: EventEmitter<Dataset> = new EventEmitter();

37
    modalRef: BsModalRef;
38
    dataPathFileExplorerPristine = true;
39
    fileExplorerPath = '';
40
41

    datasetFileExplorerPath = '';
42
43
    imageSelected = null;
    datasetImages = [];
44
45
46

    constructor(private modalService: BsModalService) { }

47
48
    ngOnChanges(changes: SimpleChanges) {
        this.datasetImages = changes.model.currentValue.config.images.map(i => ({...i}));
49
50
    }

51
52
    dataPathOpenModal(template: TemplateRef<any>) {
        this.dataPathFileExplorerPristine = true;
53
        this.fileExplorerPath = this.ngForm.controls['data_path'].value;
François Agneray's avatar
François Agneray committed
54
55
56
        if (!this.fileExplorerPath) {
            this.fileExplorerPath = '';
        }
57
        this.modalRef = this.modalService.show(template);
58
        this.loadRootDirectoryInfo.emit(this.fileExplorerPath);
59
60
    }
    
61
    dataPathAction(fileInfo: FileInfo): void {
62
63
64
65
66
67
68
69
70
        if (fileInfo.name === '.' || fileInfo.type !== 'dir') {
            return;
        }

        if (fileInfo.name === '..') {
            this.fileExplorerPath = this.fileExplorerPath.substr(0, this.fileExplorerPath.lastIndexOf("/"));
        } else {
            this.fileExplorerPath += '/' + fileInfo.name;
        }
71
        this.dataPathFileExplorerPristine = false;
72
73
74
75
76
77
78
        this.loadRootDirectoryInfo.emit(this.fileExplorerPath);
    }
    
    selectDirectory() {
        this.ngForm.controls['data_path'].setValue(this.fileExplorerPath);
        this.ngForm.controls['data_path'].markAsDirty();
        this.modalRef.hide();
79
80
81
    }

    newImageOpenModal(template: TemplateRef<any>): void {
82
        this.imageSelected = null;
83
        this.modalRef = this.modalService.show(template);
84
        this.loadDatasetDirectoryInfo.emit(this.datasetFileExplorerPath);
85
86
87
88
89
90
91
    }

    newImageAction(fileInfo: FileInfo): void {
        if (fileInfo.name === '.') {
            return;
        }

92
        if (fileInfo.type === 'file' && fileInfo.mimetype === 'image/fits') {
93
            this.loadFitsImageLimits.emit(this.datasetFileExplorerPath + '/' + fileInfo.name);
94
            this.imageSelected = fileInfo;
95
96
        } else {
            if (fileInfo.name === '..') {
97
                this.datasetFileExplorerPath = this.datasetFileExplorerPath.substr(0, this.datasetFileExplorerPath.lastIndexOf("/"));
98
            } else {
99
                this.datasetFileExplorerPath += '/' + fileInfo.name;
100
            }
101
            this.imageSelected = null;
102
            this.loadDatasetDirectoryInfo.emit(this.datasetFileExplorerPath);
103
        }
104
105
106
    }

    selectImage(): void {
107
        const name = this.datasetFileExplorerPath + '/' + this.imageSelected.name;
108
109
110
111
112
        let id = 1;
        if (this.datasetImages.length > 0) {
            id = Math.max(...this.datasetImages.map(i => i.id)) + 1;
        }
        this.datasetImages.push({ id, name, size: this.imageSelected.size, ...this.imageLimit });
113
114
115
116
117
        this.modalRef.hide();
        this.ngForm.controls['data_path'].markAsDirty();
    }

    deleteImage(image): void {
118
        this.datasetImages = this.datasetImages.filter(i => i.id !== image.id);
119
120
        this.ngForm.controls['data_path'].markAsDirty();
    }
121

122
    isConeSearchDisabled(): boolean {
François Agneray's avatar
François Agneray committed
123
124
125
        return (!this.model.name) ? true : false;
    }

126
    getDatasetFamilyByIdQueryParam(): DatasetFamily {
127
128
129
        return this.datasetFamilyList.find(datasetFamily => datasetFamily.id === this.idDatasetFamilyQueryParam);
    }

130
131
132
133
134
135
136
137
138
139
140
141
142
143
    getBackgroundEnabled(image) {
        const m = this.model.config.cone_search.background.find(i => i.id === image.id);
        if (m) {
            return m.enabled;
        } else {
            return false;
        }
    }

    getBackgroundDisplay(image) {
        const m = this.model.config.cone_search.background.find(i => i.id === image.id);
        if (m) {
            return m.display;
        } else {
144
            return (image.id + 1) * 10;
145
146
147
        }
    }

148
149
150
151
    getImageFilename(path: string) {
        return path.replace(/^.*[\\\/]/, '');
    }

152
153
    onChange(surveyName: string): void {
        this.changeSurvey.emit(this.surveyList.find(survey => survey.name === surveyName).id_database);
François Agneray's avatar
François Agneray committed
154
    }
François Agneray's avatar
François Agneray committed
155
    
156
    emit(dataset: Dataset): void {
François Agneray's avatar
François Agneray committed
157
158
159
160
161
162
163
164
        let datasetEmitted: Dataset;
        (this.model.name) ? datasetEmitted = {name: this.model.name, ...dataset} : datasetEmitted = dataset;
        if (!dataset.data_path) {
            datasetEmitted.data_path = '';
        }
        if (!dataset.vo) {
            datasetEmitted.vo = false;
        }
165
166
167
        if (this.idDatasetFamilyQueryParam > 0) {
            datasetEmitted.id_dataset_family = this.idDatasetFamilyQueryParam;
        }
François Agneray's avatar
François Agneray committed
168
        datasetEmitted.config = {
169
            images: this.datasetImages,
François Agneray's avatar
François Agneray committed
170
            cone_search: {
171
172
                enabled: this.ngForm.form.value.cone_search_enabled,
                opened: this.ngForm.form.value.cone_search_opened,
173
174
                column_ra: +this.ngForm.form.value.column_ra,
                column_dec: +this.ngForm.form.value.column_dec,
175
176
177
178
179
180
181
182
                plot_enabled: this.ngForm.form.value.plot_enabled,
                sdss_enabled: this.ngForm.form.value.sdss_enabled,
                sdss_display: this.ngForm.form.value.sdss_display,
                background: this.datasetImages.map((v, i) => {
                    const e = `${v.id}_enabled`;
                    const d = `${v.id}_display`;
                    return {id: v.id, enabled: this.ngForm.form.value[e], display: this.ngForm.form.value[d]}
                })
183
            },
184
185
186
            download: {
                enabled: this.ngForm.form.value.download_enabled,
                opened: this.ngForm.form.value.download_opened,
187
188
                csv: this.ngForm.form.value.download_result_csv,
                ascii: this.ngForm.form.value.download_result_ascii,
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                vo: this.ngForm.form.value.download_result_vo,
                archive: this.ngForm.form.value.download_result_archive,
            },
            summary: {
                enabled: this.ngForm.form.value.summary_enabled,
                opened: this.ngForm.form.value.summary_opened
            },
            server_link: {
                enabled: this.ngForm.form.value.server_link_enabled,
                opened: this.ngForm.form.value.server_link_opened
            },
            samp: {
                enabled: this.ngForm.form.value.samp_enabled,
                opened: this.ngForm.form.value.samp_opened
            },
            datatable: {
                enabled: this.ngForm.form.value.datatable_enabled,
                opened: this.ngForm.form.value.datatable_opened,
                selectable_row: this.ngForm.form.value.selectable_row
208
            }
François Agneray's avatar
François Agneray committed
209
        };
François Agneray's avatar
François Agneray committed
210
211
212
        this.submitted.emit(datasetEmitted);
    }
}