|
1 |
| -angular.module("leaflet-directive").directive('bounds', function ($log, $timeout, $http, leafletHelpers, nominatimService, leafletBoundsHelpers) { |
| 1 | +angular.module('leaflet-directive').directive('bounds', function($log, $timeout, $http, leafletHelpers, nominatimService, leafletBoundsHelpers) { |
2 | 2 |
|
3 |
| - return { |
4 |
| - restrict: "A", |
5 |
| - scope: false, |
6 |
| - replace: false, |
7 |
| - require: [ 'leaflet' ], |
| 3 | + return { |
| 4 | + restrict: 'A', |
| 5 | + scope: false, |
| 6 | + replace: false, |
| 7 | + require: ['leaflet'], |
8 | 8 |
|
9 |
| - link: function(scope, element, attrs, controller) { |
10 |
| - var isDefined = leafletHelpers.isDefined; |
11 |
| - var createLeafletBounds = leafletBoundsHelpers.createLeafletBounds; |
12 |
| - var leafletScope = controller[0].getLeafletScope(); |
13 |
| - var mapController = controller[0]; |
14 |
| - var errorHeader = leafletHelpers.errorHeader + ' [Bounds] '; |
| 9 | + link: function(scope, element, attrs, controller) { |
| 10 | + var isDefined = leafletHelpers.isDefined; |
| 11 | + var createLeafletBounds = leafletBoundsHelpers.createLeafletBounds; |
| 12 | + var leafletScope = controller[0].getLeafletScope(); |
| 13 | + var mapController = controller[0]; |
| 14 | + var errorHeader = leafletHelpers.errorHeader + ' [Bounds] '; |
15 | 15 |
|
16 |
| - var emptyBounds = function(bounds) { |
17 |
| - return (bounds._southWest.lat === 0 && bounds._southWest.lng === 0 && |
18 |
| - bounds._northEast.lat === 0 && bounds._northEast.lng === 0); |
19 |
| - }; |
| 16 | + var emptyBounds = function(bounds) { |
| 17 | + return (bounds._southWest.lat === 0 && bounds._southWest.lng === 0 && |
| 18 | + bounds._northEast.lat === 0 && bounds._northEast.lng === 0); |
| 19 | + }; |
20 | 20 |
|
21 |
| - mapController.getMap().then(function (map) { |
22 |
| - leafletScope.$on('boundsChanged', function (event) { |
23 |
| - var scope = event.currentScope; |
24 |
| - var bounds = map.getBounds(); |
| 21 | + mapController.getMap().then(function(map) { |
| 22 | + leafletScope.$on('boundsChanged', function(event) { |
| 23 | + var scope = event.currentScope; |
| 24 | + var bounds = map.getBounds(); |
25 | 25 |
|
26 |
| - if (emptyBounds(bounds) || scope.settingBoundsFromScope) { |
27 |
| - return; |
28 |
| - } |
29 |
| - scope.settingBoundsFromLeaflet = true; |
30 |
| - var newScopeBounds = { |
31 |
| - northEast: { |
32 |
| - lat: bounds._northEast.lat, |
33 |
| - lng: bounds._northEast.lng |
34 |
| - }, |
35 |
| - southWest: { |
36 |
| - lat: bounds._southWest.lat, |
37 |
| - lng: bounds._southWest.lng |
38 |
| - }, |
39 |
| - options: bounds.options |
40 |
| - }; |
41 |
| - if (!angular.equals(scope.bounds, newScopeBounds)) { |
42 |
| - scope.bounds = newScopeBounds; |
43 |
| - } |
44 |
| - $timeout( function() { |
45 |
| - scope.settingBoundsFromLeaflet = false; |
46 |
| - }); |
47 |
| - }); |
| 26 | + if (emptyBounds(bounds) || scope.settingBoundsFromScope) { |
| 27 | + return; |
| 28 | + } |
48 | 29 |
|
49 |
| - var lastNominatimQuery; |
50 |
| - leafletScope.$watch('bounds', function (bounds) { |
51 |
| - if (scope.settingBoundsFromLeaflet) |
52 |
| - return; |
53 |
| - if (isDefined(bounds.address) && bounds.address !== lastNominatimQuery) { |
54 |
| - scope.settingBoundsFromScope = true; |
55 |
| - nominatimService.query(bounds.address, attrs.id).then(function(data) { |
56 |
| - var b = data.boundingbox; |
57 |
| - var newBounds = [ [ b[0], b[2]], [ b[1], b[3]] ]; |
58 |
| - map.fitBounds(newBounds); |
59 |
| - }, function(errMsg) { |
60 |
| - $log.error(errorHeader + ' ' + errMsg + '.'); |
61 |
| - }); |
62 |
| - lastNominatimQuery = bounds.address; |
63 |
| - $timeout( function() { |
64 |
| - scope.settingBoundsFromScope = false; |
65 |
| - }); |
66 |
| - return; |
67 |
| - } |
| 30 | + scope.settingBoundsFromLeaflet = true; |
| 31 | + var newScopeBounds = { |
| 32 | + northEast: { |
| 33 | + lat: bounds._northEast.lat, |
| 34 | + lng: bounds._northEast.lng, |
| 35 | + }, |
| 36 | + southWest: { |
| 37 | + lat: bounds._southWest.lat, |
| 38 | + lng: bounds._southWest.lng, |
| 39 | + }, |
| 40 | + options: bounds.options, |
| 41 | + }; |
| 42 | + if (!angular.equals(scope.bounds, newScopeBounds)) { |
| 43 | + scope.bounds = newScopeBounds; |
| 44 | + } |
68 | 45 |
|
69 |
| - var leafletBounds = createLeafletBounds(bounds); |
70 |
| - if (leafletBounds && !map.getBounds().equals(leafletBounds)) { |
71 |
| - scope.settingBoundsFromScope = true; |
72 |
| - map.fitBounds(leafletBounds, bounds.options); |
73 |
| - $timeout( function() { |
74 |
| - scope.settingBoundsFromScope = false; |
75 |
| - }); |
76 |
| - } |
77 |
| - }, true); |
| 46 | + $timeout(function() { |
| 47 | + scope.settingBoundsFromLeaflet = false; |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + var lastNominatimQuery; |
| 52 | + leafletScope.$watch('bounds', function(bounds) { |
| 53 | + if (scope.settingBoundsFromLeaflet) |
| 54 | + return; |
| 55 | + if (isDefined(bounds.address) && bounds.address !== lastNominatimQuery) { |
| 56 | + scope.settingBoundsFromScope = true; |
| 57 | + nominatimService.query(bounds.address, attrs.id).then(function(data) { |
| 58 | + var b = data.boundingbox; |
| 59 | + var newBounds = [[b[0], b[2]], [b[1], b[3]]]; |
| 60 | + map.fitBounds(newBounds); |
| 61 | + }, function(errMsg) { |
| 62 | + |
| 63 | + $log.error(errorHeader + ' ' + errMsg + '.'); |
| 64 | + }); |
| 65 | + |
| 66 | + lastNominatimQuery = bounds.address; |
| 67 | + $timeout(function() { |
| 68 | + scope.settingBoundsFromScope = false; |
| 69 | + }); |
| 70 | + |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + var leafletBounds = createLeafletBounds(bounds); |
| 75 | + if (leafletBounds && !map.getBounds().equals(leafletBounds)) { |
| 76 | + scope.settingBoundsFromScope = true; |
| 77 | + map.fitBounds(leafletBounds, bounds.options); |
| 78 | + $timeout(function() { |
| 79 | + scope.settingBoundsFromScope = false; |
78 | 80 | });
|
79 |
| - } |
80 |
| - }; |
| 81 | + } |
| 82 | + }, true); |
| 83 | + }); |
| 84 | + }, |
| 85 | + }; |
81 | 86 | });
|
0 commit comments