Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* 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, ChangeDetectionStrategy } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { nanValidator, rangeValidator } from '../../validators';
import { ConeSearch, Resolver } from 'src/app/instance/store/models';
@Component({
selector: 'app-dec',
templateUrl: 'dec.component.html',
styleUrls: ['input-group.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
/**
* @class
* @classdesc DEC component.
*/
export class DecComponent {
/**
* Disables DEC fields.
*
* @param {boolean} disabled - If the field has to be disabled.
*/
@Input()
set disabled(disabled: boolean) {
this.isDisabled = disabled;
this.initFields();
}
/**
* Sets RA, DEC and radius from cone search.
*
* @param {ConeSearch} coneSearch - The cone search.
*/
@Input()
set coneSearch(coneSearch: ConeSearch) {
this.ra = coneSearch.ra;
this.radius = coneSearch.radius;
if (coneSearch.dec) {
this.decDegree.setValue(coneSearch.dec);
if(this.decDegree.valid && !this.decHFocused && !this.decMFocused && !this.decSFocused) {
this.decDegree2HMS(coneSearch.dec);
}
} else {
this.decDegree.reset();
this.decH.reset();
this.decM.reset();
this.decS.reset();
}
this.initFields();
}
/**
* Sets RA from resolver.
*
* @param {Resolver} resolver - The resolver.
*/
@Input()
set resolver(resolver: Resolver) {
this.resolvedDec = null;
if (resolver) {
this.resolvedDec = resolver.dec;
this.decDegree.setValue(resolver.dec);
this.decDegree2HMS(resolver.dec);
}
}
/**
* Sets isDegree.
*
* @param {string} unit - The unit.
*/
@Input()
set unit(unit: string) {
unit === 'degree' ? this.isDegree = true : this.isDegree = false;
this.initFields();
}
@Output() updateConeSearch: EventEmitter<ConeSearch> = new EventEmitter();
@Output() deleteResolver: EventEmitter<null> = new EventEmitter();
ra: number;
radius: number;
isDisabled = false;
isDegree = true;
resolvedDec: number;
decHFocused: boolean = false;
decMFocused: boolean = false;
decSFocused: boolean = false;
decDegree = new FormControl('', [Validators.required, nanValidator, rangeValidator(-90, 90, 'DEC')]);
decH = new FormControl('', [nanValidator, rangeValidator(-90, 90, 'Degree')]);
decM = new FormControl('', [nanValidator, rangeValidator(0, 60, 'Minutes')]);
decS = new FormControl('', [nanValidator, rangeValidator(0, 60, 'Seconds')]);
/**
* Sets DEC fields.
*/
initFields(): void {
if (this.isDisabled) {
this.decDegree.disable();
this.decH.disable();
this.decM.disable();
this.decS.disable();
} else if (this.isDegree) {
this.decDegree.enable();
this.decH.disable();
this.decM.disable();
this.decS.disable();
} else {
this.decDegree.disable();
this.decH.enable();
this.decM.enable();
this.decS.enable();
}
}
/**
* Converts DEC hour minute second from degree and sets DEC HMS fields.
*
* @param {number} value - The degree value.
*/
decDegree2HMS(value: number): void {
const hh = Math.trunc(value);
let tmp = (Math.abs(value - hh)) * 60;
const mm = Math.trunc(tmp);
tmp = (tmp - mm) * 60;
const ss = tmp.toFixed(2);
this.decH.setValue(hh);
this.decM.setValue(mm);
this.decS.setValue(ss);
}
/**
* Sets DEC degree from hour minute second and sets DEC degree field.
*/
decHMS2Degree(): void {
const hh = +this.decH.value;
const mm = +this.decM.value;
const ss = +this.decS.value;
const tmp = ((ss / 60) + mm) / 60;
let deg = tmp + Math.abs(hh);
if (hh < 0) {
deg = -deg;
}
this.decDegree.setValue(+deg.toFixed(8));
}
/**
* Changes fields focus.
*
* @param {string} field - The field.
* @param {boolean} isFocused - Is the field is focused.
*/
changeFocus(field: string, isFocused: boolean) {
switch (field) {
case 'dech':
this.decHFocused = isFocused;
break;
case 'decm':
this.decMFocused = isFocused;
break
case 'decs':
this.decSFocused = isFocused;
break;
}
}
/**
* Manages DEC value change.
*/
decChange(): void {
if (this.isDegree) {
if (this.decDegree.valid) {
this.decDegree2HMS(this.decDegree.value);
} else {
this.decH.reset();
this.decM.reset();
this.decS.reset();
}
this.updateConeSearch.emit({ ra: this.ra, dec: this.decDegree.value, radius: this.radius } as ConeSearch);
} else {
if (this.decH.valid && this.decM.valid && this.decS.valid) {
this.setToDefaultValue();
this.decHMS2Degree();
this.updateConeSearch.emit({ ra: this.ra, dec: this.decDegree.value, radius: this.radius } as ConeSearch);
} else {
this.decDegree.reset();
}
}
this.resetResolver();
}
/**
* Sets DEC hour minute second fields to default value if not valid.
*/
setToDefaultValue(): void {
if (this.decH.value === '' || this.decH.value === null) {
this.decH.setValue(0);
}
if (this.decM.value === '' || this.decM.value === null) {
this.decM.setValue(0);
}
if (this.decS.value === '' || this.decS.value === null) {
this.decS.setValue(0);
}
}
/**
* Emits reset resolver event.
*/
resetResolver(): void {
if (this.resolvedDec && this.resolvedDec !== this.decDegree.value) {
this.deleteResolver.emit();
}
}
}