mapcomplete/UI/SearchAndGo.ts

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-07-01 02:12:33 +02:00
import {UIElement} from "./UIElement";
2020-07-20 15:54:50 +02:00
import {TextField} from "./Input/TextField";
2020-07-01 02:12:33 +02:00
import {UIEventSource} from "./UIEventSource";
import {FixedUiElement} from "./Base/FixedUiElement";
import {Geocoding} from "../Logic/Geocoding";
import {Basemap} from "../Logic/Basemap";
export class SearchAndGo extends UIElement {
2020-07-01 21:21:29 +02:00
private _placeholder = new UIEventSource("Zoek naar een locatie...")
2020-07-20 15:54:50 +02:00
private _searchField = new TextField<string>({
placeholder: this._placeholder
}
);
2020-07-01 02:12:33 +02:00
private _foundEntries = new UIEventSource([]);
private _map: Basemap;
private _goButton = new FixedUiElement("<img class='search-go' src='./assets/search.svg' alt='GO'>");
constructor(map: Basemap) {
super(undefined);
this._map = map;
this.ListenTo(this._foundEntries);
const self = this;
this._searchField.enterPressed.addCallback(() => {
self.RunSearch();
});
this._goButton.onClick(function () {
self.RunSearch();
});
}
// Triggered by 'enter' or onclick
private RunSearch() {
2020-07-20 15:54:50 +02:00
const searchString = this._searchField.GetValue().data;
2020-07-01 02:12:33 +02:00
this._searchField.Clear();
this._placeholder.setData("Bezig met zoeken...");
const self = this;
2020-07-01 21:21:29 +02:00
Geocoding.Search(searchString, this._map, (result) => {
if (result.length == 0) {
this._placeholder.setData("Niets gevonden");
return;
}
const bb = result[0].boundingbox;
const bounds = [
[bb[0], bb[2]],
[bb[1], bb[3]]
]
self._map.map.fitBounds(bounds);
this._placeholder.setData("Zoek naar een locatie...");
},
() => {
this._placeholder.setData("Niets gevonden: er ging iets mis");
});
2020-07-01 02:12:33 +02:00
}
protected InnerRender(): string {
// "<img class='search' src='./assets/search.svg' alt='Search'> " +
2020-07-05 18:59:47 +02:00
return this._searchField.Render() +
this._goButton.Render();
2020-07-01 02:12:33 +02:00
}
Update() {
super.Update();
this._searchField.Update();
this._goButton.Update();
}
Activate() {
super.Activate();
this._searchField.Activate();
this._goButton.Activate();
}
}