|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + angular |
| 5 | + .module('openSenseMapApp') |
| 6 | + .directive('osemGeocoder', osemGeocoder); |
| 7 | + |
| 8 | + osemGeocoder.$inject = []; |
| 9 | + |
| 10 | + function osemGeocoder () { |
| 11 | + var directive = { |
| 12 | + templateUrl: 'components/geocoder.directive.html', |
| 13 | + link: link, |
| 14 | + // eslint-disable-next-line angular/directive-restrict |
| 15 | + restrict: 'E', |
| 16 | + transclude: true, |
| 17 | + controller: GeocoderController, |
| 18 | + controllerAs: 'geocoder', |
| 19 | + bindToController: true, // because the scope is isolated |
| 20 | + scope: { |
| 21 | + selectBox: '=' |
| 22 | + }, |
| 23 | + }; |
| 24 | + |
| 25 | + return directive; |
| 26 | + |
| 27 | + //// |
| 28 | + |
| 29 | + function link (scope, element, attrs) {} |
| 30 | + } |
| 31 | + |
| 32 | + GeocoderController.$inject = ['$scope', '$http']; |
| 33 | + |
| 34 | + function GeocoderController ($scope, $http) { |
| 35 | + var vm = this; |
| 36 | + |
| 37 | + vm.$onInit = onInit; |
| 38 | + vm.$onDestroy = onDestroy; |
| 39 | + |
| 40 | + vm.searchString = ''; |
| 41 | + vm.showClearSearch = false; |
| 42 | + vm.modelOptions = { |
| 43 | + debounce: { |
| 44 | + default: 300, |
| 45 | + blur: 250, |
| 46 | + }, |
| 47 | + getterSetter: true, |
| 48 | + }; |
| 49 | + |
| 50 | + vm.searchStringChanged = searchStringChanged; |
| 51 | + vm.getLocations = getLocations; |
| 52 | + vm.selectResult = selectResult; |
| 53 | + vm.clearSearch = clearSearch; |
| 54 | + |
| 55 | + //// |
| 56 | + |
| 57 | + function onInit () { |
| 58 | + activate(); |
| 59 | + } |
| 60 | + |
| 61 | + function onDestroy () {} |
| 62 | + |
| 63 | + function activate () {} |
| 64 | + |
| 65 | + function searchStringChanged () { |
| 66 | + if (vm.searchString !== '') { |
| 67 | + vm.showClearSearch = true; |
| 68 | + } else { |
| 69 | + vm.showClearSearch = false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function getLocations (searchstring) { |
| 74 | + var results = []; |
| 75 | + |
| 76 | + return $http |
| 77 | + .get('//locationiq.org/v1/search.php', { |
| 78 | + params: { |
| 79 | + format: 'json', |
| 80 | + key: '23e12b10d8c3aad04e8e', |
| 81 | + addressdetails: 1, |
| 82 | + limit: 4, |
| 83 | + q: searchstring, |
| 84 | + }, |
| 85 | + }) |
| 86 | + .then(function (response) { |
| 87 | + return results.concat(response.data); |
| 88 | + }) |
| 89 | + .catch(function () { |
| 90 | + return results; |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + function selectResult (item) { |
| 95 | + $scope.geocoder.selectBox(item); |
| 96 | + } |
| 97 | + |
| 98 | + function clearSearch () { |
| 99 | + vm.searchString = ''; |
| 100 | + vm.searchStringChanged(); |
| 101 | + } |
| 102 | + |
| 103 | + //// |
| 104 | + } |
| 105 | +})(); |
0 commit comments