2019-11-25 14:34:51 +01:00
|
|
|
import { Component, Input, Output, OnInit, EventEmitter, SimpleChanges, OnChanges, ViewChild } from '@angular/core';
|
2022-02-04 16:21:26 +01:00
|
|
|
import { Observable , of,merge,forkJoin } from 'rxjs';
|
|
|
|
import { debounceTime,distinctUntilChanged,tap,switchMap,catchError,map} from 'rxjs/operators';
|
2019-11-25 14:34:51 +01:00
|
|
|
import { TypeaheadService, TimespanService } from '@farmmaps/common';
|
2020-02-19 11:37:21 +01:00
|
|
|
import { IQueryState } from '@farmmaps/common';
|
2019-11-25 14:34:51 +01:00
|
|
|
import { IPeriodState } from '../../models/period.state';
|
|
|
|
import { tassign } from 'tassign';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'fm-map-map-search',
|
|
|
|
templateUrl: './map-search.component.html',
|
|
|
|
styleUrls: ['./map-search.component.scss']
|
|
|
|
})
|
|
|
|
export class MapSearchComponent {
|
|
|
|
|
|
|
|
@ViewChild('searchText', { static: true }) searchText;
|
|
|
|
@Input() clearEnabled: boolean
|
|
|
|
@Input() set collapsed(collapsed: boolean) {
|
|
|
|
this.collapsedLocal = collapsed;
|
|
|
|
if (collapsed) this.searchText.nativeElement.blur();
|
|
|
|
}
|
|
|
|
@Input() set searchMinified(minified: boolean) {
|
|
|
|
this.searchMinifiedLocal = minified;
|
|
|
|
}
|
2021-08-17 19:36:30 +02:00
|
|
|
@Input() set period(period:IPeriodState) {
|
|
|
|
this.periodLocal = tassign(this.periodLocal,{startDate: period.startDate,endDate:period.endDate});
|
|
|
|
this.startEndCaption = this.timespanService.getCaption(period.startDate, period.endDate, 4)
|
|
|
|
}
|
2022-02-04 16:21:26 +01:00
|
|
|
@Output() onSearch = new EventEmitter<IQueryState>()
|
|
|
|
@Output() onCitySearch = new EventEmitter<string>()
|
2019-11-25 14:34:51 +01:00
|
|
|
@Output() onClear = new EventEmitter<any>();
|
|
|
|
@Output() onSearchCollapse = new EventEmitter<any>();
|
|
|
|
@Output() onSearchExpand = new EventEmitter<any>();
|
|
|
|
@Output() onToggleMenu = new EventEmitter<any>();
|
|
|
|
@Output() onOpenModal = new EventEmitter<string>();
|
|
|
|
@Output() onCloseModal = new EventEmitter<any>();
|
2021-08-17 19:36:30 +02:00
|
|
|
@Output() onPeriodChange = new EventEmitter<IPeriodState>();
|
2019-11-25 14:34:51 +01:00
|
|
|
@Input() openedModalName: string;
|
|
|
|
@Input() set filterOptions(filterOptions: IQueryState) {
|
|
|
|
if (filterOptions && filterOptions.query && filterOptions.query.length > 0) {
|
|
|
|
this.disabled = false;
|
|
|
|
} else {
|
|
|
|
this.disabled = true;
|
|
|
|
}
|
|
|
|
this.filterOptionsLocal = tassign(this.filterOptionsLocal, { tags: filterOptions.tags, query: filterOptions.query,bbox:filterOptions.bbox });
|
|
|
|
if (filterOptions.tags) {
|
|
|
|
this.searchTextLocal = { name: filterOptions.tags };
|
|
|
|
} else {
|
|
|
|
this.searchTextLocal = { name: filterOptions.query };
|
2021-08-17 19:36:30 +02:00
|
|
|
}
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-06 14:04:14 +01:00
|
|
|
public collapsedLocal = true;
|
|
|
|
public searchMinifiedLocal = false;
|
2021-08-17 19:36:30 +02:00
|
|
|
public periodLocal: IPeriodState = { startDate:new Date(new Date(Date.now()).getFullYear(), new Date(Date.now()).getMonth() - 3, 1), endDate:new Date(Date.now())};
|
2019-11-25 14:34:51 +01:00
|
|
|
public filterOptionsLocal: IQueryState;
|
|
|
|
private extent: number[];
|
|
|
|
public searchTextLocal: any;
|
|
|
|
public searchTextLocalOutput: string;
|
2023-03-06 14:04:14 +01:00
|
|
|
public dateFilter = true;
|
2021-08-17 19:36:30 +02:00
|
|
|
public startEndCaption: string = this.timespanService.getCaption(this.periodLocal.startDate, this.periodLocal.endDate, 4);
|
2019-11-25 14:34:51 +01:00
|
|
|
|
|
|
|
searching = false;
|
|
|
|
searchFailed = false;
|
|
|
|
hideSearchingWhenUnsubscribed = new Observable(() => () => this.searching = false);
|
|
|
|
|
2023-03-06 14:04:14 +01:00
|
|
|
public disabled = true;
|
2019-11-25 14:34:51 +01:00
|
|
|
|
|
|
|
constructor(private typeaheadService: TypeaheadService, private timespanService: TimespanService) {
|
2020-05-25 10:04:35 +02:00
|
|
|
this.filterOptionsLocal = { query: "", tags: "", startDate: null, endDate: null, bboxFilter: false, itemType: null, itemCode:null,level:0,parentCode:null,bbox:[] };
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 16:21:26 +01:00
|
|
|
search = (text$: Observable<string>) =>
|
2019-11-25 14:34:51 +01:00
|
|
|
text$.pipe(
|
|
|
|
debounceTime(300),
|
|
|
|
distinctUntilChanged(),
|
2022-02-04 16:21:26 +01:00
|
|
|
tap(() => {this.searching = true;this.searchFailed=false;}),
|
|
|
|
switchMap(term => term.length < 1 ? of([]) :
|
|
|
|
forkJoin(
|
|
|
|
this.typeaheadService.getTagTypeaheadItems(term).pipe(
|
2019-11-25 14:34:51 +01:00
|
|
|
catchError(() => {
|
|
|
|
this.searchFailed = true;
|
|
|
|
return of([]);
|
2022-02-04 16:21:26 +01:00
|
|
|
}),map( (sa:string[]) => sa.map((s,i) => ({"name":s,"type":"tag"})))),
|
|
|
|
this.typeaheadService.getCityTypeaheadItems(term).pipe(
|
|
|
|
catchError(() => {
|
|
|
|
this.searchFailed = true;
|
|
|
|
return of([]);
|
|
|
|
}),map( (sa:string[]) => sa.map((s,i) => ({"name":s,"type":"city"})))),
|
|
|
|
).pipe(map(([a1,a2]) => [...a1, ...a2] ),map(a => a.sort((a, b) => (a.name.toUpperCase() > b.name.toUpperCase()) ? 1 : -1)))
|
|
|
|
),
|
2019-11-25 14:34:51 +01:00
|
|
|
tap(() => this.searching = false),
|
2022-02-04 16:21:26 +01:00
|
|
|
);
|
2019-11-25 14:34:51 +01:00
|
|
|
|
|
|
|
formatter = (x: { name: string }) => x.name;
|
|
|
|
|
|
|
|
handleSearch(event) {
|
|
|
|
this.filterOptionsLocal.tags = null;
|
|
|
|
this.filterOptionsLocal.itemType = null;
|
|
|
|
this.filterOptionsLocal.itemCode = null;
|
|
|
|
this.filterOptionsLocal.parentCode = null;
|
|
|
|
this.filterOptionsLocal.query = this.searchTextLocalOutput;
|
|
|
|
if (this.dateFilter) {
|
2021-08-17 19:36:30 +02:00
|
|
|
this.filterOptionsLocal.startDate = this.periodLocal.startDate;
|
|
|
|
this.filterOptionsLocal.endDate = this.periodLocal.endDate;
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
this.onSearch.emit(this.filterOptionsLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOpenSelectPeriodModal(event: MouseEvent) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.onOpenModal.emit('selectPeriodModal');
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCloseModal() {
|
|
|
|
this.onCloseModal.emit({});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelect(event) {
|
2022-02-04 16:21:26 +01:00
|
|
|
if(event.item.type == "tag") {
|
|
|
|
event.preventDefault();
|
|
|
|
this.filterOptionsLocal.query = null;
|
|
|
|
this.filterOptionsLocal.itemType = null;
|
|
|
|
this.filterOptionsLocal.itemCode = null;
|
|
|
|
this.filterOptionsLocal.parentCode = null;
|
|
|
|
this.filterOptionsLocal.tags = event.item.name;
|
|
|
|
if (this.dateFilter) {
|
|
|
|
this.filterOptionsLocal.startDate = this.periodLocal.startDate;
|
|
|
|
this.filterOptionsLocal.endDate = this.periodLocal.endDate;
|
|
|
|
}
|
|
|
|
this.onSearch.emit(this.filterOptionsLocal);
|
|
|
|
this.searchTextLocal = { name: this.filterOptionsLocal.tags };
|
|
|
|
} else if (event.item.type == "city") {
|
|
|
|
this.clearEnabled = true;
|
|
|
|
this.onCitySearch.emit(event.item.name);
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelectPeriod(event: { startDate: Date, endDate: Date }) {
|
2021-08-17 19:36:30 +02:00
|
|
|
this.periodLocal = { startDate: event.startDate, endDate: event.endDate}
|
|
|
|
this.onPeriodChange.emit(this.periodLocal);
|
2019-11-25 14:34:51 +01:00
|
|
|
this.startEndCaption = this.timespanService.getCaption(event.startDate, event.endDate, 4);
|
2021-08-17 19:36:30 +02:00
|
|
|
if(this.dateFilter) {
|
|
|
|
this.filterOptionsLocal.startDate =event.startDate;
|
|
|
|
this.filterOptionsLocal.endDate = event.endDate;
|
|
|
|
this.onSearch.emit(this.filterOptionsLocal);
|
|
|
|
}
|
|
|
|
this.handleCloseModal();
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChangeEnableDateFilter(enabled) {
|
|
|
|
this.dateFilter = enabled;
|
|
|
|
if (enabled) {
|
2021-08-17 19:36:30 +02:00
|
|
|
this.filterOptionsLocal.startDate = this.periodLocal.startDate;
|
|
|
|
this.filterOptionsLocal.endDate = this.periodLocal.endDate;
|
2019-11-25 14:34:51 +01:00
|
|
|
} else {
|
|
|
|
this.filterOptionsLocal.startDate = null;
|
|
|
|
this.filterOptionsLocal.endDate = null;
|
|
|
|
}
|
|
|
|
if(this.filterOptionsLocal.query || this.filterOptionsLocal.tags)
|
|
|
|
this.onSearch.emit(this.filterOptionsLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChangeEnableBBOXFilter(enabled) {
|
|
|
|
this.filterOptionsLocal.bboxFilter = enabled;
|
|
|
|
if (this.filterOptionsLocal.query || this.filterOptionsLocal.tags)
|
|
|
|
this.onSearch.emit(this.filterOptionsLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleToggleMenu(event) {
|
|
|
|
this.onToggleMenu.emit({});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus(event) {
|
|
|
|
this.onSearchExpand.emit({});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(event: string) {
|
|
|
|
this.searchTextLocalOutput = event;
|
|
|
|
if (event && event.length == 1) {
|
|
|
|
this.onSearchExpand.emit({});
|
|
|
|
}
|
|
|
|
if (event && event.length > 0)
|
|
|
|
this.disabled = false;
|
|
|
|
else
|
|
|
|
this.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClearClick(event) {
|
|
|
|
this.onClear.emit({});
|
|
|
|
}
|
2022-02-04 16:21:26 +01:00
|
|
|
|
|
|
|
getIcon(type):string {
|
|
|
|
if(type == "tag")
|
|
|
|
return "fal fa-tag";
|
|
|
|
else if(type == "city")
|
|
|
|
return "fal fa-map-marker-alt";
|
|
|
|
else if(type == "search")
|
|
|
|
return "fal fa-search";
|
|
|
|
|
|
|
|
}
|
2019-11-25 14:34:51 +01:00
|
|
|
}
|
|
|
|
|