UNPKG

23.3 kBJavaScriptView Raw
1import { __assign, __decorate, __metadata, __spread } from 'tslib';
2import { Injectable, ViewChild, ElementRef, Input, Output, EventEmitter, HostListener, Component, NgModule } from '@angular/core';
3import { HttpClient, HttpClientModule } from '@angular/common/http';
4import { CommonModule } from '@angular/common';
5import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
6import { map, filter, debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
7import { parse } from 'terraformer-wkt-parser';
8
9var GeocoderService = /** @class */ (function () {
10 function GeocoderService(http) {
11 this.http = http;
12 this.geocoderBaseUrl = 'https://geodata.nationaalgeoregister.nl/locatieserver/v3';
13 }
14 GeocoderService.prototype.suggest = function (query, options) {
15 var _this = this;
16 var params = {
17 q: query,
18 fq: '*',
19 start: 0,
20 rows: 10,
21 };
22 if (options) {
23 params = Object.assign(params, options);
24 }
25 return this.http.get(this.geocoderBaseUrl + "/suggest?", { params: params })
26 .toPromise().then(function (suggestResultObject) {
27 var collations = _this.formatCollations(suggestResultObject.spellcheck.collations);
28 var places = _this.formatPlaces(suggestResultObject);
29 return { collations: collations, places: places };
30 });
31 };
32 GeocoderService.prototype.suggest$ = function (query, options) {
33 var _this = this;
34 var params = {
35 q: query,
36 fq: '*',
37 start: 0,
38 };
39 if (options) {
40 params = Object.assign(params, options);
41 }
42 return this.http.get(this.geocoderBaseUrl + "/suggest?", { params: params })
43 .pipe(map(function (suggestResultObject) {
44 var collations = _this.formatCollations(suggestResultObject.spellcheck.collations);
45 var places = _this.formatPlaces(suggestResultObject);
46 return { collations: collations, places: places };
47 }));
48 };
49 GeocoderService.prototype.lookup = function (id, options) {
50 var _this = this;
51 var params = {
52 id: id,
53 fl: '*'
54 };
55 if (options) {
56 params = Object.assign(params, options);
57 }
58 return this.http.get(this.geocoderBaseUrl + "/lookup?", { params: params }).toPromise().then(function (lookupResponse) {
59 return _this.formatLookupResponse(lookupResponse);
60 });
61 };
62 GeocoderService.prototype.lookup$ = function (id, options) {
63 var _this = this;
64 var params = {
65 id: id,
66 fl: '*'
67 };
68 if (options) {
69 params = Object.assign(params, options);
70 }
71 return this.http.get(this.geocoderBaseUrl + "/lookup?", { params: params }).pipe(map(function (lookupResponse) { return _this.formatLookupResponse(lookupResponse); }));
72 };
73 GeocoderService.prototype.free = function (searchTerm, options) {
74 var _this = this;
75 var params = {
76 q: searchTerm,
77 fl: '*',
78 fq: '*',
79 rows: 10,
80 start: 0,
81 };
82 if (options) {
83 params = Object.assign(params, options);
84 }
85 return this.http.get(this.geocoderBaseUrl + "/free?", { params: params })
86 .toPromise().then(function (freeResponse) {
87 return _this.formatReverseResponse(freeResponse);
88 });
89 };
90 GeocoderService.prototype.free$ = function (searchTerm, options) {
91 var _this = this;
92 var params = {
93 q: searchTerm,
94 fl: '*',
95 fq: '*',
96 rows: 10,
97 start: 0,
98 };
99 if (options) {
100 params = Object.assign(params, options);
101 }
102 return this.http.get(this.geocoderBaseUrl + "/free?", { params: params }).pipe(map(function (freeResponse) { return _this.formatReverseResponse(freeResponse); }));
103 };
104 GeocoderService.prototype.reverse = function (location, options) {
105 var _this = this;
106 var params = {
107 type: 'adres',
108 fq: '*',
109 fl: '*',
110 rows: 10,
111 distance: 200 // meter,
112 };
113 if (options) {
114 params = Object.assign(params, options, location);
115 }
116 var reverseUrl = 'https://geodata.nationaalgeoregister.nl/locatieserver/revgeo?';
117 return this.http.get(reverseUrl, { params: params }).toPromise().then(function (reverseResponse) {
118 return _this.formatReverseResponse(reverseResponse);
119 });
120 };
121 GeocoderService.prototype.reverse$ = function (location, options) {
122 var _this = this;
123 var params = {
124 type: 'adres',
125 fq: '*',
126 fl: '*',
127 rows: 10,
128 distance: 200 // meter,
129 };
130 if (options) {
131 params = Object.assign(params, options, location);
132 }
133 var reverseUrl = 'https://geodata.nationaalgeoregister.nl/locatieserver/revgeo?';
134 return this.http.get(reverseUrl, { params: params }).pipe(map(function (reverseResponse) { return _this.formatReverseResponse(reverseResponse); }));
135 };
136 GeocoderService.prototype.formatCollations = function (collations) {
137 var parsedCollations = [];
138 for (var i = 0; i < collations.length; i += 2) {
139 var collation = {
140 id: i,
141 naam: collations[i + 1].misspellingsAndCorrections[1],
142 weergavenaam: collations[i + 1].hits + " resultaten gevonden voor <strong> " + collations[i + 1].misspellingsAndCorrections[1] + " </strong>",
143 hits: collations[i + 1].hits,
144 };
145 parsedCollations.push(collation);
146 }
147 return parsedCollations;
148 };
149 GeocoderService.prototype.formatPlaces = function (suggestResponse) {
150 var places = suggestResponse.response.docs.map(function (place) {
151 return __assign({}, place, { highlight: suggestResponse.highlighting[place.id].suggest[0] });
152 });
153 return places;
154 };
155 /**
156 * Parse WKT in lookup response.
157 */
158 GeocoderService.prototype.formatLookupResponse = function (lookupResponse) {
159 var formatted = lookupResponse.response.docs.map(function (lookupResult) {
160 var formattedLookupResult = lookupResult;
161 if (lookupResult.centroide_ll) {
162 formattedLookupResult.centroide_ll = parse(lookupResult.centroide_ll);
163 }
164 if (lookupResult.centroide_rd) {
165 formattedLookupResult.centroide_rd = parse(lookupResult.centroide_rd);
166 }
167 if (lookupResult.geometrie_rd) {
168 formattedLookupResult.geometrie_rd = parse(lookupResult.geometrie_rd);
169 }
170 if (lookupResult.geometrie_ll) {
171 formattedLookupResult.geometrie_ll = parse(lookupResult.geometrie_ll);
172 }
173 return formattedLookupResult;
174 });
175 return formatted[0] || {};
176 };
177 GeocoderService.prototype.formatReverseResponse = function (lookupResultObject) {
178 var formatted = lookupResultObject.response.docs.map(function (reverseResult) {
179 var formattedLookupResult = reverseResult;
180 if (reverseResult.centroide_ll) {
181 formattedLookupResult.centroide_ll = parse(reverseResult.centroide_ll);
182 }
183 if (reverseResult.centroide_rd) {
184 formattedLookupResult.centroide_rd = parse(reverseResult.centroide_rd);
185 }
186 if (reverseResult.geometrie_rd) {
187 formattedLookupResult.geometrie_rd = parse(reverseResult.geometrie_rd);
188 }
189 if (reverseResult.geometrie_ll) {
190 formattedLookupResult.geometrie_ll = parse(reverseResult.geometrie_ll);
191 }
192 formattedLookupResult.highlight = reverseResult.weergavenaam;
193 return formattedLookupResult;
194 });
195 return formatted;
196 };
197 GeocoderService = __decorate([
198 Injectable(),
199 __metadata("design:paramtypes", [HttpClient])
200 ], GeocoderService);
201 return GeocoderService;
202}());
203
204// Leaflet requires the old school way for importing
205var GeocoderComponent = /** @class */ (function () {
206 function GeocoderComponent(geocoderService) {
207 this.geocoderService = geocoderService;
208 this.resultCountLimit = 10;
209 this.resultFieldsToShow = [];
210 this.placeFound = new EventEmitter();
211 this.formattedPlaceholder = '';
212 this.resultsVisible = true;
213 this.places = [];
214 this.collations = [];
215 this.searchThreshold = 2;
216 this.foundPlace = null;
217 this.selectedItem = [];
218 this.selectedIndex = -1;
219 this.subscriptions = [];
220 this.searchField = new FormControl();
221 this.searchForm = new FormGroup({ search: this.searchField });
222 this.subscriptions.push(this.subscribeToSearchFieldValueChanges());
223 }
224 GeocoderComponent.prototype.clickout = function (event) {
225 if (this.geocoderRef.nativeElement.contains(event.target)) {
226 this.showResults();
227 }
228 else {
229 this.hideResults();
230 }
231 };
232 GeocoderComponent.prototype.ngOnInit = function () {
233 this.formattedPlaceholder = this.formatPlaceholder(this.placeholder);
234 };
235 GeocoderComponent.prototype.ngOnChanges = function (changes) {
236 if (changes.searchInput) {
237 this.searchField.setValue(changes.searchInput.currentValue);
238 }
239 };
240 GeocoderComponent.prototype.ngOnDestroy = function () {
241 this.subscriptions.forEach(function (s) { return s.unsubscribe(); });
242 };
243 GeocoderComponent.prototype.subscribeToSearchFieldValueChanges = function () {
244 var _this = this;
245 return this.searchField.valueChanges.pipe(filter(function (searchInput) { return searchInput.length > _this.searchThreshold; }), debounceTime(100), distinctUntilChanged(), switchMap(function (searchInput) { return _this.suggest(searchInput); })).subscribe(function (suggestResponse) {
246 _this.places = suggestResponse.places;
247 _this.collations = suggestResponse.collations;
248 }); // Actual call is fired.
249 };
250 GeocoderComponent.prototype.suggest = function (searchTerm) {
251 var flArray = __spread(['id', 'score', 'type', 'weergavenaam'], this.resultFieldsToShow);
252 var params = {
253 fq: '*:*',
254 fl: flArray.toString(),
255 rows: this.resultCountLimit
256 };
257 if (this.type) {
258 var replace = ',';
259 var regex = new RegExp(replace, 'g');
260 var query = this.type.replace(regex, ' OR ');
261 params.fq = "type:(" + query + ")";
262 }
263 return this.geocoderService.suggest$(searchTerm, params);
264 };
265 GeocoderComponent.prototype.free = function () {
266 var _this = this;
267 var searchTerm = this.searchField.value;
268 var params = { fq: '*:*' };
269 if (this.type) {
270 var replace = ',';
271 var regex = new RegExp(replace, 'g');
272 var query = this.type.replace(regex, ' OR ');
273 params.fq = "type:(" + query + ")";
274 }
275 return this.geocoderService.free$(searchTerm, params).subscribe(function (places) {
276 _this.places = places;
277 });
278 };
279 GeocoderComponent.prototype.lookup = function (id) {
280 var _this = this;
281 this.geocoderService.lookup$(id).subscribe(function (lookupObj) {
282 _this.foundPlace = lookupObj;
283 if (_this.resultFieldsToShow.length > 0) {
284 var input = _this.resultFieldsToShow.map(function (resultFieldToShow) {
285 return _this.foundPlace[resultFieldToShow];
286 }).join(', ');
287 _this.fillInput(input);
288 }
289 else {
290 _this.fillInput(_this.foundPlace.weergavenaam);
291 }
292 _this.placeFound.emit(_this.foundPlace);
293 _this.clearPlaces();
294 });
295 };
296 GeocoderComponent.prototype.clear = function () {
297 this.foundPlace = null;
298 this.searchField.setValue('');
299 this.clearPlaces();
300 };
301 GeocoderComponent.prototype.clearPlaces = function () {
302 this.resetIndex();
303 this.places = [];
304 this.collations = [];
305 };
306 GeocoderComponent.prototype.handleEnter = function () {
307 if (this.selectedIndex === -1) {
308 return;
309 }
310 var selectedPlace = this.places[this.selectedIndex];
311 this.lookup(selectedPlace.id);
312 };
313 GeocoderComponent.prototype.isNoResultsFound = function () {
314 var reachedThreshold = (this.searchField.value && this.searchField.value.length) > this.searchThreshold;
315 var noSuggestions = this.places.length === 0;
316 var noResult = (this.foundPlace == null);
317 return (reachedThreshold) && (noSuggestions) && (noResult);
318 };
319 GeocoderComponent.prototype.showCollations = function () {
320 return this.collations.length > 0 && this.places.length === 0;
321 };
322 GeocoderComponent.prototype.fillInput = function (content, emitEvent) {
323 if (emitEvent === void 0) { emitEvent = false; }
324 this.searchField.setValue(content, {
325 emitEvent: emitEvent
326 });
327 };
328 GeocoderComponent.prototype.isHighlighted = function (i) {
329 if (i === this.selectedIndex) {
330 return true;
331 }
332 };
333 GeocoderComponent.prototype.moveUp = function () {
334 if (this.selectedIndex > 0) {
335 this.selectedIndex--;
336 }
337 };
338 GeocoderComponent.prototype.moveDown = function () {
339 if (this.selectedIndex < this.places.length) {
340 this.selectedIndex++;
341 }
342 };
343 GeocoderComponent.prototype.resetIndex = function () {
344 this.selectedIndex = -1;
345 };
346 GeocoderComponent.prototype.canQuery = function () {
347 var searchInput = this.searchField.value;
348 return searchInput && searchInput.length > this.searchThreshold;
349 };
350 GeocoderComponent.prototype.canClear = function () {
351 var searchInput = this.searchField.value;
352 return searchInput && searchInput.length > 0;
353 };
354 GeocoderComponent.prototype.formatPlaceholder = function (placeholder) {
355 var placeholderText = 'Zoeken op de kaart...';
356 if (placeholder) {
357 placeholderText = placeholder;
358 }
359 return placeholderText;
360 };
361 GeocoderComponent.prototype.onCollationClick = function (name) {
362 this.fillInput(name, true);
363 };
364 GeocoderComponent.prototype.showResults = function () {
365 this.resultsVisible = true;
366 };
367 GeocoderComponent.prototype.hideResults = function () {
368 this.resultsVisible = false;
369 };
370 __decorate([
371 ViewChild('geocoder', { static: false }),
372 __metadata("design:type", ElementRef)
373 ], GeocoderComponent.prototype, "geocoderRef", void 0);
374 __decorate([
375 Input(),
376 __metadata("design:type", String)
377 ], GeocoderComponent.prototype, "type", void 0);
378 __decorate([
379 Input(),
380 __metadata("design:type", String)
381 ], GeocoderComponent.prototype, "searchInput", void 0);
382 __decorate([
383 Input(),
384 __metadata("design:type", String)
385 ], GeocoderComponent.prototype, "placeholder", void 0);
386 __decorate([
387 Input(),
388 __metadata("design:type", Object)
389 ], GeocoderComponent.prototype, "resultCountLimit", void 0);
390 __decorate([
391 Input(),
392 __metadata("design:type", Array)
393 ], GeocoderComponent.prototype, "resultFieldsToShow", void 0);
394 __decorate([
395 Output(),
396 __metadata("design:type", EventEmitter)
397 ], GeocoderComponent.prototype, "placeFound", void 0);
398 __decorate([
399 HostListener('document:click', ['$event']),
400 __metadata("design:type", Function),
401 __metadata("design:paramtypes", [Object]),
402 __metadata("design:returntype", void 0)
403 ], GeocoderComponent.prototype, "clickout", null);
404 GeocoderComponent = __decorate([
405 Component({
406 template: "<div #geocoder (keydown.ArrowUp)=\"moveUp()\" (keydown.ArrowDown)=\"moveDown()\" (keydown.Enter)=\"handleEnter()\"\r\n class=\"geocoder-container\">\r\n <!-- Input -->\r\n <div class=\"geocoder-search-container\">\r\n <form [formGroup]=\"searchForm\">\r\n <input (focus)=\"showResults()\" formControlName=\"search\" class=\"geocoder-search-input\" [placeholder]=\"formattedPlaceholder\" type=\"text\"\r\n aria-label=\"Zoeken\" maxlength=\"100\">\r\n </form>\r\n <button class=\"searchButton\" aria-label=\"zoeken\" (click)=\"free()\" [ngClass]=\"{'highlight': canQuery()}\"> <svg\r\n aria-hidden=\"true\" data-fa-processed=\"\" data-prefix=\"fal\" data-icon=\"search\" role=\"img\"\r\n xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-search fa-w-16 fa-9x\">\r\n <path fill=\"currentColor\"\r\n d=\"M508.5 481.6l-129-129c-2.3-2.3-5.3-3.5-8.5-3.5h-10.3C395 312 416 262.5 416 208 416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c54.5 0 104-21 141.1-55.2V371c0 3.2 1.3 6.2 3.5 8.5l129 129c4.7 4.7 12.3 4.7 17 0l9.9-9.9c4.7-4.7 4.7-12.3 0-17zM208 384c-97.3 0-176-78.7-176-176S110.7 32 208 32s176 78.7 176 176-78.7 176-176 176z\"\r\n class=\"\"></path>\r\n </svg></button>\r\n <button class=\"closeButton\" aria-label=\"zoekopdracht verwijderen\" (click)=\"clear()\"\r\n [ngClass]=\"{'highlight': canClear()}\"> <svg aria-hidden=\"true\" data-fa-processed=\"\" data-prefix=\"fal\"\r\n data-icon=\"times\" role=\"img\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 384 512\"\r\n class=\"svg-inline--fa fa-times fa-w-12 fa-7x\">\r\n <path fill=\"currentColor\"\r\n d=\"M217.5 256l137.2-137.2c4.7-4.7 4.7-12.3 0-17l-8.5-8.5c-4.7-4.7-12.3-4.7-17 0L192 230.5 54.8 93.4c-4.7-4.7-12.3-4.7-17 0l-8.5 8.5c-4.7 4.7-4.7 12.3 0 17L166.5 256 29.4 393.2c-4.7 4.7-4.7 12.3 0 17l8.5 8.5c4.7 4.7 12.3 4.7 17 0L192 281.5l137.2 137.2c4.7 4.7 12.3 4.7 17 0l8.5-8.5c4.7-4.7 4.7-12.3 0-17L217.5 256z\"\r\n class=\"\"></path>\r\n </svg></button>\r\n </div>\r\n <!-- Search results-->\r\n <div class=\"geocoder-suggestions-container\" *ngIf=\"resultsVisible === true\">\r\n <div class=\"geocoder-suggestions-results\" *ngIf=\"places.length > 0\">\r\n <ul class=\"geocoder-suggestions-list\">\r\n <li class=\"geocoder-suggestion\" tabindex={{i}} *ngFor=\"let place of places; let i = index;\"\r\n (click)=\"lookup(place.id)\" [ngClass]=\"{'focus-background': isHighlighted(i)}\">\r\n <p *ngIf=\"resultFieldsToShow.length === 0\" class=\"geocoder-suggestion-title\" [innerHTML]=\"place.highlight\">\r\n </p>\r\n <p *ngIf=\"resultFieldsToShow.length > 0\" class=\"geocoder-suggestion-title\">\r\n <ng-container *ngFor=\"let field of resultFieldsToShow\">\r\n {{place[field]}}\r\n </ng-container>\r\n </p>\r\n <p class=\"geocoder-suggestion-type\"> {{place.type}} </p>\r\n </li>\r\n </ul>\r\n </div>\r\n <div class=\"geocoder-no-results\" *ngIf=\"isNoResultsFound()\">\r\n <p>Geen zoekresultaten gevonden voor <strong> {{searchField.value}}</strong>.</p>\r\n </div>\r\n <div class=\"geocoder-collation-container\" *ngIf=\"showCollations()\">\r\n <ul class=\"geocoder-collations-list\">\r\n <li class=\"geocoder-collation\" tabindex={{i}} (click)=\"onCollationClick(collation.naam)\"\r\n *ngFor=\"let collation of collations; let i = index;\" [innerHTML]=\"collation.weergavenaam\">\r\n </li>\r\n </ul>\r\n </div>\r\n </div>\r\n</div>\r\n",
407 selector: 'geocoder',
408 styles: ["::-webkit-input-placeholder{font-size:14px;color:#797979}:-moz-placeholder{font-size:14px;color:#797979}::-moz-placeholder{font-size:14px;color:#797979}:-ms-input-placeholder{font-size:14px;color:#797979}::-ms-input-placeholder{font-size:14px;color:#797979}:placeholder-shown{font-size:14px;color:#797979}:focus{outline:0}input,li,p,span{margin:0;font-size:14px;color:#797979}.geocoder-container{width:100%;position:relative;max-width:100%}.geocoder-search-container{width:100%;border:none;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);background:#fff}.geocoder-search-input{padding:15px;border:none;width:95%;width:calc(100% - 80px);overflow:hidden;max-width:100%}button{position:absolute;cursor:pointer}.searchButton{top:13px;right:50px;padding-right:15px;background:#fff;border:none;border-right:1px solid #797979;height:20px}.closeButton{top:10px;right:10px;margin-left:16px;background:#fff;border:none;height:26px}svg{width:18px;color:#797979}.closeButton:hover svg,.highlight svg,.searchButton:hover svg{color:#79abff;transition:.1s cubic-bezier(.075,.82,.165,1)}.geocoder-suggestions-results{top:53px;position:absolute;background:#fff;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);width:100%;z-index:1;max-height:400px;overflow-y:auto}.geocoder-collations-list,.geocoder-suggestions-list{list-style-type:none;padding:0;margin:0}.geocoder-suggestion{padding:15px;position:relative;border-bottom:1px solid #f1f1f1}.geocoder-suggestion-title{max-width:243px}.geocoder-suggestion-type{text-transform:uppercase;font-weight:700;font-size:11px;position:absolute;right:15px;top:36%;color:#bfbfbf}.geocoder-collation:hover,.geocoder-suggestion:hover{cursor:pointer;background-color:#f7f7f7}.focus-background{background-color:#f7f7f7}.geocoder-no-results{top:53px;position:absolute;background:#fff;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);width:100%;padding:15px;z-index:1;box-sizing:border-box}.geocoder-collation{z-index:1}.geocoder-collation-container{top:108px;position:absolute;background:#fff;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);width:100%;z-index:1}.geocoder-collation-container li{padding:15px;border-bottom:1px solid #f1f1f1}"]
409 }),
410 __metadata("design:paramtypes", [GeocoderService])
411 ], GeocoderComponent);
412 return GeocoderComponent;
413}());
414
415var GeocoderModule = /** @class */ (function () {
416 function GeocoderModule() {
417 }
418 GeocoderModule = __decorate([
419 NgModule({
420 imports: [HttpClientModule, FormsModule, CommonModule, ReactiveFormsModule],
421 exports: [GeocoderComponent],
422 declarations: [GeocoderComponent],
423 providers: [GeocoderService]
424 })
425 ], GeocoderModule);
426 return GeocoderModule;
427}());
428
429export { GeocoderModule, GeocoderService, GeocoderComponent as ɵa };
430//# sourceMappingURL=angular-geocoder.js.map