UNPKG

23.8 kBJavaScriptView Raw
1import { __assign, __decorate, __metadata, __spread } from 'tslib';
2import { Injectable, ViewChild, ElementRef, Input, Output, EventEmitter, HostListener, Component, NgZone, 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';
8import { DomEvent } from 'leaflet';
9
10var GeocoderService = /** @class */ (function () {
11 function GeocoderService(http) {
12 this.http = http;
13 this.geocoderBaseUrl = 'https://geodata.nationaalgeoregister.nl/locatieserver/v3';
14 }
15 GeocoderService.prototype.suggest = function (query, options) {
16 var _this = this;
17 var params = {
18 q: query,
19 fq: '*',
20 start: 0,
21 rows: 10,
22 };
23 if (options) {
24 params = Object.assign(params, options);
25 }
26 return this.http.get(this.geocoderBaseUrl + "/suggest?", { params: params })
27 .toPromise().then(function (suggestResultObject) {
28 var collations = _this.formatCollations(suggestResultObject.spellcheck.collations);
29 var places = _this.formatPlaces(suggestResultObject);
30 return { collations: collations, places: places };
31 });
32 };
33 GeocoderService.prototype.suggest$ = function (query, options) {
34 var _this = this;
35 var params = {
36 q: query,
37 fq: '*',
38 start: 0,
39 };
40 if (options) {
41 params = Object.assign(params, options);
42 }
43 return this.http.get(this.geocoderBaseUrl + "/suggest?", { params: params })
44 .pipe(map(function (suggestResultObject) {
45 var collations = _this.formatCollations(suggestResultObject.spellcheck.collations);
46 var places = _this.formatPlaces(suggestResultObject);
47 return { collations: collations, places: places };
48 }));
49 };
50 GeocoderService.prototype.lookup = function (id, options) {
51 var _this = this;
52 var params = {
53 id: id,
54 fl: '*'
55 };
56 if (options) {
57 params = Object.assign(params, options);
58 }
59 return this.http.get(this.geocoderBaseUrl + "/lookup?", { params: params }).toPromise().then(function (lookupResponse) {
60 return _this.formatLookupResponse(lookupResponse);
61 });
62 };
63 GeocoderService.prototype.lookup$ = function (id, options) {
64 var _this = this;
65 var params = {
66 id: id,
67 fl: '*'
68 };
69 if (options) {
70 params = Object.assign(params, options);
71 }
72 return this.http.get(this.geocoderBaseUrl + "/lookup?", { params: params }).pipe(map(function (lookupResponse) { return _this.formatLookupResponse(lookupResponse); }));
73 };
74 GeocoderService.prototype.free = function (searchTerm, options) {
75 var _this = this;
76 var params = {
77 q: searchTerm,
78 fl: '*',
79 fq: '*',
80 rows: 10,
81 start: 0,
82 };
83 if (options) {
84 params = Object.assign(params, options);
85 }
86 return this.http.get(this.geocoderBaseUrl + "/free?", { params: params })
87 .toPromise().then(function (freeResponse) {
88 return _this.formatReverseResponse(freeResponse);
89 });
90 };
91 GeocoderService.prototype.free$ = function (searchTerm, options) {
92 var _this = this;
93 var params = {
94 q: searchTerm,
95 fl: '*',
96 fq: '*',
97 rows: 10,
98 start: 0,
99 };
100 if (options) {
101 params = Object.assign(params, options);
102 }
103 return this.http.get(this.geocoderBaseUrl + "/free?", { params: params }).pipe(map(function (freeResponse) { return _this.formatReverseResponse(freeResponse); }));
104 };
105 GeocoderService.prototype.reverse = function (location, options) {
106 var _this = this;
107 var params = {
108 type: 'adres',
109 fq: '*',
110 fl: '*',
111 rows: 10,
112 distance: 200 // meter,
113 };
114 if (options) {
115 params = Object.assign(params, options, location);
116 }
117 var reverseUrl = 'http://test.geodata.nationaalgeoregister.nl/locatieserver/revgeo?';
118 return this.http.get(reverseUrl, { params: params }).toPromise().then(function (reverseResponse) {
119 return _this.formatReverseResponse(reverseResponse);
120 });
121 };
122 GeocoderService.prototype.reverse$ = function (location, options) {
123 var _this = this;
124 var params = {
125 type: 'adres',
126 fq: '*',
127 fl: '*',
128 rows: 10,
129 distance: 200 // meter,
130 };
131 if (options) {
132 params = Object.assign(params, options, location);
133 }
134 var reverseUrl = 'http://test.geodata.nationaalgeoregister.nl/locatieserver/revgeo?';
135 return this.http.get(reverseUrl, { params: params }).pipe(map(function (reverseResponse) { return _this.formatReverseResponse(reverseResponse); }));
136 };
137 GeocoderService.prototype.formatCollations = function (collations) {
138 var parsedCollations = [];
139 for (var i = 0; i < collations.length; i += 2) {
140 var collation = {
141 id: i,
142 naam: collations[i + 1].misspellingsAndCorrections[1],
143 weergavenaam: collations[i + 1].hits + " resultaten gevonden voor <strong> " + collations[i + 1].misspellingsAndCorrections[1] + " </strong>",
144 hits: collations[i + 1].hits,
145 };
146 parsedCollations.push(collation);
147 }
148 return parsedCollations;
149 };
150 GeocoderService.prototype.formatPlaces = function (suggestResponse) {
151 var places = suggestResponse.response.docs.map(function (place) {
152 return __assign({}, place, { highlight: suggestResponse.highlighting[place.id].suggest[0] });
153 });
154 return places;
155 };
156 /**
157 * Parse WKT in lookup response.
158 */
159 GeocoderService.prototype.formatLookupResponse = function (lookupResponse) {
160 var formatted = lookupResponse.response.docs.map(function (lookupResult) {
161 var formattedLookupResult = lookupResult;
162 if (lookupResult.centroide_ll) {
163 formattedLookupResult.centroide_ll = parse(lookupResult.centroide_ll);
164 }
165 if (lookupResult.centroide_rd) {
166 formattedLookupResult.centroide_rd = parse(lookupResult.centroide_rd);
167 }
168 if (lookupResult.geometrie_rd) {
169 formattedLookupResult.geometrie_rd = parse(lookupResult.geometrie_rd);
170 }
171 if (lookupResult.geometrie_ll) {
172 formattedLookupResult.geometrie_ll = parse(lookupResult.geometrie_ll);
173 }
174 return formattedLookupResult;
175 });
176 return formatted[0] || {};
177 };
178 GeocoderService.prototype.formatReverseResponse = function (lookupResultObject) {
179 var formatted = lookupResultObject.response.docs.map(function (reverseResult) {
180 var formattedLookupResult = reverseResult;
181 if (reverseResult.centroide_ll) {
182 formattedLookupResult.centroide_ll = parse(reverseResult.centroide_ll);
183 }
184 if (reverseResult.centroide_rd) {
185 formattedLookupResult.centroide_rd = parse(reverseResult.centroide_rd);
186 }
187 if (reverseResult.geometrie_rd) {
188 formattedLookupResult.geometrie_rd = parse(reverseResult.geometrie_rd);
189 }
190 if (reverseResult.geometrie_ll) {
191 formattedLookupResult.geometrie_ll = parse(reverseResult.geometrie_ll);
192 }
193 formattedLookupResult.highlight = reverseResult.weergavenaam;
194 return formattedLookupResult;
195 });
196 return formatted;
197 };
198 GeocoderService = __decorate([
199 Injectable(),
200 __metadata("design:paramtypes", [HttpClient])
201 ], GeocoderService);
202 return GeocoderService;
203}());
204
205// Leaflet requires the old school way for importing
206var GeocoderComponent = /** @class */ (function () {
207 function GeocoderComponent(geocoderService, zone) {
208 this.geocoderService = geocoderService;
209 this.zone = zone;
210 this.resultCountLimit = 10;
211 this.resultFieldsToShow = [];
212 this.placeFound = new EventEmitter();
213 this.formattedPlaceholder = '';
214 this.resultsVisible = true;
215 this.places = [];
216 this.collations = [];
217 this.searchThreshold = 2;
218 this.foundPlace = null;
219 this.selectedItem = [];
220 this.selectedIndex = -1;
221 this.subscriptions = [];
222 this.searchField = new FormControl();
223 this.searchForm = new FormGroup({ search: this.searchField });
224 this.subscriptions.push(this.subscribeToSearchFieldValueChanges());
225 }
226 GeocoderComponent.prototype.clickout = function (event) {
227 if (this.geocoderRef.nativeElement.contains(event.target)) {
228 this.showResults();
229 }
230 else {
231 this.hideResults();
232 }
233 };
234 GeocoderComponent.prototype.ngOnInit = function () {
235 this.formattedPlaceholder = this.formatPlaceholder(this.placeholder);
236 };
237 GeocoderComponent.prototype.ngAfterViewInit = function () {
238 var _this = this;
239 this.zone.runOutsideAngular(function () {
240 DomEvent.disableClickPropagation(_this.geocoderRef.nativeElement);
241 DomEvent.disableScrollPropagation(_this.geocoderRef.nativeElement);
242 });
243 };
244 GeocoderComponent.prototype.ngOnChanges = function (changes) {
245 if (changes.searchInput) {
246 this.searchField.setValue(changes.searchInput.currentValue);
247 }
248 };
249 GeocoderComponent.prototype.ngOnDestroy = function () {
250 this.subscriptions.forEach(function (s) { return s.unsubscribe(); });
251 };
252 GeocoderComponent.prototype.subscribeToSearchFieldValueChanges = function () {
253 var _this = this;
254 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) {
255 _this.places = suggestResponse.places;
256 _this.collations = suggestResponse.collations;
257 }); // Actual call is fired.
258 };
259 GeocoderComponent.prototype.suggest = function (searchTerm) {
260 var flArray = __spread(['id', 'score', 'type', 'weergavenaam'], this.resultFieldsToShow);
261 var params = {
262 fq: '*:*',
263 fl: flArray.toString(),
264 rows: this.resultCountLimit
265 };
266 if (this.type) {
267 var replace = ',';
268 var regex = new RegExp(replace, 'g');
269 var query = this.type.replace(regex, ' OR ');
270 params.fq = "type:(" + query + ")";
271 }
272 return this.geocoderService.suggest$(searchTerm, params);
273 };
274 GeocoderComponent.prototype.free = function () {
275 var _this = this;
276 var searchTerm = this.searchField.value;
277 var params = { fq: '*:*' };
278 if (this.type) {
279 var replace = ',';
280 var regex = new RegExp(replace, 'g');
281 var query = this.type.replace(regex, ' OR ');
282 params.fq = "type:(" + query + ")";
283 }
284 return this.geocoderService.free$(searchTerm, params).subscribe(function (places) {
285 _this.places = places;
286 });
287 };
288 GeocoderComponent.prototype.lookup = function (id) {
289 var _this = this;
290 this.geocoderService.lookup$(id).subscribe(function (lookupObj) {
291 _this.foundPlace = lookupObj;
292 if (_this.resultFieldsToShow.length > 0) {
293 var input = _this.resultFieldsToShow.map(function (resultFieldToShow) {
294 return _this.foundPlace[resultFieldToShow];
295 }).join(', ');
296 _this.fillInput(input);
297 }
298 else {
299 _this.fillInput(_this.foundPlace.weergavenaam);
300 }
301 _this.placeFound.emit(_this.foundPlace);
302 _this.clearPlaces();
303 });
304 };
305 GeocoderComponent.prototype.clear = function () {
306 this.foundPlace = null;
307 this.searchField.setValue('');
308 this.clearPlaces();
309 };
310 GeocoderComponent.prototype.clearPlaces = function () {
311 this.resetIndex();
312 this.places = [];
313 this.collations = [];
314 };
315 GeocoderComponent.prototype.handleEnter = function () {
316 if (this.selectedIndex === -1) {
317 return;
318 }
319 var selectedPlace = this.places[this.selectedIndex];
320 this.lookup(selectedPlace.id);
321 };
322 GeocoderComponent.prototype.isNoResultsFound = function () {
323 var reachedThreshold = (this.searchField.value && this.searchField.value.length) > this.searchThreshold;
324 var noSuggestions = this.places.length === 0;
325 var noResult = (this.foundPlace == null);
326 return (reachedThreshold) && (noSuggestions) && (noResult);
327 };
328 GeocoderComponent.prototype.showCollations = function () {
329 return this.collations.length > 0 && this.places.length === 0;
330 };
331 GeocoderComponent.prototype.fillInput = function (content, emitEvent) {
332 if (emitEvent === void 0) { emitEvent = false; }
333 this.searchField.setValue(content, {
334 emitEvent: emitEvent
335 });
336 };
337 GeocoderComponent.prototype.isHighlighted = function (i) {
338 if (i === this.selectedIndex) {
339 return true;
340 }
341 };
342 GeocoderComponent.prototype.moveUp = function () {
343 if (this.selectedIndex > 0) {
344 this.selectedIndex--;
345 }
346 };
347 GeocoderComponent.prototype.moveDown = function () {
348 if (this.selectedIndex < this.places.length) {
349 this.selectedIndex++;
350 }
351 };
352 GeocoderComponent.prototype.resetIndex = function () {
353 this.selectedIndex = -1;
354 };
355 GeocoderComponent.prototype.canQuery = function () {
356 var searchInput = this.searchField.value;
357 return searchInput && searchInput.length > this.searchThreshold;
358 };
359 GeocoderComponent.prototype.canClear = function () {
360 var searchInput = this.searchField.value;
361 return searchInput && searchInput.length > 0;
362 };
363 GeocoderComponent.prototype.formatPlaceholder = function (placeholder) {
364 var placeholderText = 'Zoeken op de kaart...';
365 if (placeholder) {
366 placeholderText = placeholder;
367 }
368 return placeholderText;
369 };
370 GeocoderComponent.prototype.onCollationClick = function (name) {
371 this.fillInput(name, true);
372 };
373 GeocoderComponent.prototype.showResults = function () {
374 this.resultsVisible = true;
375 };
376 GeocoderComponent.prototype.hideResults = function () {
377 this.resultsVisible = false;
378 };
379 __decorate([
380 ViewChild('geocoder', { static: false }),
381 __metadata("design:type", ElementRef)
382 ], GeocoderComponent.prototype, "geocoderRef", void 0);
383 __decorate([
384 Input(),
385 __metadata("design:type", String)
386 ], GeocoderComponent.prototype, "type", void 0);
387 __decorate([
388 Input(),
389 __metadata("design:type", String)
390 ], GeocoderComponent.prototype, "searchInput", void 0);
391 __decorate([
392 Input(),
393 __metadata("design:type", String)
394 ], GeocoderComponent.prototype, "placeholder", void 0);
395 __decorate([
396 Input(),
397 __metadata("design:type", Object)
398 ], GeocoderComponent.prototype, "resultCountLimit", void 0);
399 __decorate([
400 Input(),
401 __metadata("design:type", Array)
402 ], GeocoderComponent.prototype, "resultFieldsToShow", void 0);
403 __decorate([
404 Output(),
405 __metadata("design:type", EventEmitter)
406 ], GeocoderComponent.prototype, "placeFound", void 0);
407 __decorate([
408 HostListener('document:click', ['$event']),
409 __metadata("design:type", Function),
410 __metadata("design:paramtypes", [Object]),
411 __metadata("design:returntype", void 0)
412 ], GeocoderComponent.prototype, "clickout", null);
413 GeocoderComponent = __decorate([
414 Component({
415 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",
416 selector: 'geocoder',
417 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}"]
418 }),
419 __metadata("design:paramtypes", [GeocoderService,
420 NgZone])
421 ], GeocoderComponent);
422 return GeocoderComponent;
423}());
424
425var GeocoderModule = /** @class */ (function () {
426 function GeocoderModule() {
427 }
428 GeocoderModule = __decorate([
429 NgModule({
430 imports: [HttpClientModule, FormsModule, CommonModule, ReactiveFormsModule],
431 exports: [GeocoderComponent],
432 declarations: [GeocoderComponent],
433 providers: [GeocoderService]
434 })
435 ], GeocoderModule);
436 return GeocoderModule;
437}());
438
439export { GeocoderModule, GeocoderService, GeocoderComponent as ɵa };
440//# sourceMappingURL=angular-geocoder.js.map