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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
| import { ChangeDetectorRef, Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
import { AbstractControl, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { FilterTrafficName } from '@shared-app/enums';
import { FiltersLayer, FiltersLayerWithMode, FilterTrafficItem } from '@shared-app/model/filter-traffic.model';
import { FiltersTrafficService } from '@shared-app/store/filters/traffic/filters-traffic.service';
import { Subscription } from 'rxjs';
/**
* Filter component.
*/
@Component({
selector: 'app-filter',
templateUrl: './filter.component.html',
styleUrls: ['./filter.component.css'],
})
export class FilterComponent implements OnInit, OnDestroy {
/**
* true if submit button has been clicked.
*/
submitted = false;
/**
* Event emitter to submit the child form.
*/
public submit = new EventEmitter();
checkValidDeleteFilter = false;
/**
* Error message if the number of days between dates is more than one.
*/
errorMsgNbDaysBetweenDates = "Vous ne devez entrer qu'une seule date";
/**
* Error message if the date is not fill.
*/
errorMsgDateRequired = 'La date doit être renseignée';
/**
* Error message if the number of filters is upper.
*/
errorMsgTooMuchFilters = 'Vous devez supprimer les autres critères pour "Afficher tout le trafic"';
/**
* Error message if the number of filter is equals to zero.
*/
errorMsgZeroFilter = 'Vous devez avoir au moins un critère dans la liste';
/**
* Ref. to the form.
*/
applyFilterForm = new UntypedFormGroup({});
/**
* Ref. of the filter service subscription.
*/
private filterServiceSub: Subscription;
/**
* Ref. of the search submit button subscription.
*/
private submitSub: Subscription;
/**
* The traffic layers.
*/
trafficLayers: FiltersLayer[] = [];
/**
* The current active filters.
*/
activesFilters: FilterTrafficItem[];
/**
* The current layer.
*/
currentLayer: FiltersLayerWithMode;
/**
* Indicate if we need to reinit.
*/
reinit = false;
/**
* Reinit calendar.
*/
reinitCalendar = false;
@Output() onSetCurrentLayer: EventEmitter<FiltersLayerWithMode> = new EventEmitter();
/**
* Constructor.
*
* @param filterService service use to manage filters
* @param fb the form builder
*/
constructor(public filtersService: FiltersTrafficService, private fb: UntypedFormBuilder, private cd: ChangeDetectorRef) {}
/**
* Acitvate the delete button for filters.
*/
checkDeleteFilters() {
this.checkValidDeleteFilter = true;
}
/**
* Cancel the delete of filters.
*/
cancelDeleteFilters() {
this.checkValidDeleteFilter = false;
}
/**
* On init comp.
*/
ngOnInit() {
this.applyFilterForm = this.fb.group(
{
layerControl: [null, Validators.required],
calendarControl: [null], //calendarControl: [null, Validators.required],
fullTrafficWithFlightPlanControl: [],
fullTrafficWithoutFlightPlanControl: [],
},
/*
{
validator: fullTrafficCheckedValidator(this.filtersService),
},*/
);
this.filtersService.filtersLayers$.subscribe(layers => {
console.log('SUBSCRIBE OF FILTERS LAYERS INTO FILTERS COMP');
if (!layers) {
return;
}
this.trafficLayers = layers;
this.cd.detectChanges();
});
this.filterServiceSub = this.filtersService.currentLayer$.subscribe(current => {
this.currentLayer = current;
if (!current) {
return;
}
/*
if (current.filtersLayer.getListOfFilters().length === 0 && this.f.fullTrafficWithFlightPlanControl.value === true) {
this.applyFilterForm.get('fullTrafficWithFlightPlanControl').setValue(false);
}
if (current.filtersLayer.getListOfFilters().length === 0 && this.f.fullTrafficWithoutFlightPlanControl.value === true) {
this.applyFilterForm.get('fullTrafficWithFlightPlanControl').setValue(false);
}*/
/*
if (current.mode === ModeTrafficLayer.UPDATE) {
const fullTrafficWithFlightPlanFilter: boolean =
current.filtersLayer.getListOfFilters().filter(f => f.key === FilterTrafficName.FULL_TRAFFIC_WITH_FLIGHT_PLAN).length > 0;
if (fullTrafficWithFlightPlanFilter) {
this.f.fullTrafficWithFlightPlanControl.setValue(true);
}
}
if (current.mode === ModeTrafficLayer.UPDATE) {
const fullTrafficWithoutFlightPlanFilter: boolean =
current.filtersLayer.getListOfFilters().filter(f => f.key === FilterTrafficName.FULL_TRAFFIC_WITHOUT_FLIGHT_PLAN).length > 0;
if (fullTrafficWithoutFlightPlanFilter) {
this.f.fullTrafficWithoutFlightPlanControl.setValue(true);
}
} */
this.calendarControlValidator(); // complete form builder
this.applyFilterForm.updateValueAndValidity();
this.activesFilters = current.filtersLayer.getListOfFilters().filter(f => f.key !== FilterTrafficName.DATE);
this.cd.detectChanges();
});
}
/**
* On destroy comp.
*/
ngOnDestroy(): void {
if (this.filterServiceSub) {
this.filterServiceSub.unsubscribe();
}
}
/**
* Handler when the validate button is clicked.
*/
onSubmit() {
this.submitted = true;
this.reinit = false;
if (this.applyFilterForm.invalid) {
console.log('Errors=>', this.applyFilterForm.errors);
return;
}
this.resetAndApplyFilters();
this.submit.emit('');
}
/**
* apply calendar validator requirement
*/
private calendarControlValidator() {
let isNumeroDeVolExist = this.filtersService.isNumeroDeVolExist();
if (!isNumeroDeVolExist) {
this.applyFilterForm.get('calendarControl').setValidators([Validators.required]);
} else {
this.applyFilterForm.get('calendarControl').clearValidators();
}
this.applyFilterForm.get('calendarControl').updateValueAndValidity();
}
/**
* clean calendar input field
* @param event
*/
reinitCalendarFunction(event){
if(event){
this.reinitCalendar = event;
}
}
/**
* Update the name of the current layer.
*/
private updateNameCurrentLayer() {
const newFiltersLayer: FiltersLayer = new FiltersLayer(
this.currentLayer.filtersLayer.getId(),
this.f.layerControl.value,
this.currentLayer.filtersLayer.getListOfFilters(),
);
const newCurrentLayer: FiltersLayerWithMode = {
filtersLayer: newFiltersLayer,
mode: this.currentLayer.mode,
};
this.filtersService.updateCurrentLayer(newCurrentLayer);
}
/**
* Reset and apply filters.
*/
private resetAndApplyFilters() {
const prevNameCurrentLayer = this.currentLayer.filtersLayer.getNameLayer();
const prevIdCurrentLayer = this.currentLayer.filtersLayer.getId();
if (this.f.layerControl.value !== prevIdCurrentLayer) {
this.updateNameCurrentLayer();
}
this.reinit = true;
this.activesFilters = [];
//this.f.fullTrafficWithFlightPlanControl.setValue(false);
//this.f.fullTrafficWithoutFlightPlanControl.setValue(false);
this.filtersService.addOrUpdateLayer(prevIdCurrentLayer, prevNameCurrentLayer);
this.filtersService.applyFiltersFromCurrentLayer();
this.reinitCalendar = true;
}
/**
* Handle the cancellation, reinit.
*/
layerCancelled() {
this.reinitCalendar = true;
this.filtersService.resetCurrentLayer();
}
/**
* Return true when the full trafic with flight plan is checked.
*
* @returns true when the full trafic is checked, otherwise false
*/
isFullTrafficWithFlightPlanChecked(): boolean {
return this.f.fullTrafficWithFlightPlanControl.value === true;
}
/**
* Return true when the full trafic without flight plan is checked.
*
* @returns true when the full trafic without flight plan is checked, otherwise false
*/
isFullTrafficWithoutFlightPlanChecked(): boolean {
return this.f.fullTrafficWithoutFlightPlanControl.value === true;
}
/**
* Return true when the full trafic with flight plan is unchecked.
*
* @returns true when the full trafic with flight plan is unchecked, otherwise false
*/
isNotFullTrafficWithFlightPlanChecked(): boolean {
return this.f.fullTrafficWithFlightPlanControl.value !== true;
}
/**
* Return true when the full trafic without flight plan is unchecked.
*
* @returns true when the full trafic without flight plan is unchecked, otherwise false
*/
isNotFullTrafficWithoutFlightPlanChecked(): boolean {
return this.f.fullTrafficWithoutFlightPlanControl.value !== true;
}
/**
* Return true if there is one filter at least.
*
* @returns true if there is one filter at least, otherwise false
*/
haveFilters(): boolean {
return this.filtersService.haveFilters();
}
/**
* Return true if there is filter none.
*
* @returns true if there is filter none, otherwise false
*/
notHaveFilters(): boolean {
return this.filtersService.notHaveFilters();
}
/**
* Remove all filters.
*/
removeAllFilters() {
this.filtersService.removeAllfiltersIntoCurrentLayer();
this.checkValidDeleteFilter = false;
}
/**
* Returns an object reference of the controls of form.
* Each control is accessibe by its name as key and the value returned as a FormControl.
*
* @returns an object of each control of form
*/
get f(): { [key: string]: AbstractControl } {
return this.applyFilterForm.controls;
}
} |