Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit e6b7790

Browse files
committed
fix(events): fix #235 errors when id is interpolated
Fix errors and close PR - fix #235 Using a computed map id causes some misnamed events - closes #278 Fix id attr value not being interpolated for leafletDirectiveMap events PR
1 parent 9acabbd commit e6b7790

File tree

10 files changed

+74
-52
lines changed

10 files changed

+74
-52
lines changed

dist/ui-leaflet.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* ui-leaflet 1.0.0 2016-08-19
2+
* ui-leaflet 1.0.0 2016-08-22
33
* ui-leaflet - An AngularJS directive to easily interact with Leaflet maps
44
* git: https://github.com/angular-ui/ui-leaflet
55
*/
@@ -26,7 +26,8 @@ angular.module('ui-leaflet', ['nemLogging']).directive('leaflet', ["$q", "leafle
2626
controls: '=',
2727
decorations: '=',
2828
eventBroadcast: '=',
29-
watchOptions: '='
29+
watchOptions: '=',
30+
id: '@'
3031
},
3132
transclude: true,
3233
template: '<div class="angular-leaflet-map"><div ng-transclude></div></div>',
@@ -124,7 +125,7 @@ angular.module('ui-leaflet', ['nemLogging']).directive('leaflet', ["$q", "leafle
124125
// if no event-broadcast attribute, all events are broadcasted
125126
if (!isDefined(attrs.eventBroadcast)) {
126127
var logic = "broadcast";
127-
addEvents(map, mapEvents, "eventName", scope, logic);
128+
addEvents(map, attrs.id, mapEvents, "eventName", scope, logic);
128129
}
129130

130131
// Resolve the map object to the promises
@@ -3648,7 +3649,7 @@ angular.module('ui-leaflet').directive('eventBroadcast', ["leafletLogger", "$roo
36483649
}
36493650
// as long as the map is removed in the root leaflet directive we
36503651
// do not need ot clean up the events as leaflet does it itself
3651-
addEvents(map, mapEvents, "eventName", leafletScope, logic);
3652+
addEvents(map, attrs.id, mapEvents, "eventName", leafletScope, logic);
36523653
});
36533654
}
36543655
};
@@ -5197,7 +5198,9 @@ angular.module('ui-leaflet').factory('leafletMapEvents', ["$rootScope", "$q", "l
51975198
};
51985199

51995200
var _genDispatchMapEvent = function _genDispatchMapEvent(scope, eventName, logic, maybeMapId) {
5200-
if (maybeMapId) maybeMapId = maybeMapId + '.';
5201+
if (maybeMapId) {
5202+
maybeMapId = maybeMapId + '.';
5203+
}
52015204
return function (e) {
52025205
// Put together broadcast name
52035206
var broadcastName = 'leafletDirectiveMap.' + maybeMapId + eventName;
@@ -5223,11 +5226,15 @@ angular.module('ui-leaflet').factory('leafletMapEvents', ["$rootScope", "$q", "l
52235226
}
52245227
};
52255228

5226-
var _addEvents = function _addEvents(map, mapEvents, contextName, scope, logic) {
5229+
var _addEvents = function _addEvents(map, mapId, mapEvents, contextName, scope, logic) {
52275230
leafletIterators.each(mapEvents, function (eventName) {
52285231
var context = {};
52295232
context[contextName] = eventName;
5230-
map.on(eventName, _genDispatchMapEvent(scope, eventName, logic, map._container.id || ''), context);
5233+
if (!mapId) {
5234+
mapId = map._container.id || '';
5235+
}
5236+
5237+
map.on(eventName, _genDispatchMapEvent(scope, eventName, logic, mapId), context);
52315238
});
52325239
};
52335240

dist/ui-leaflet.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-leaflet.min.no-header.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-leaflet_dev_mapped.js

Lines changed: 14 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-leaflet_dev_mapped.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/eventBroadcast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ angular.module('ui-leaflet').directive('eventBroadcast', function (leafletLogger
5151
}
5252
// as long as the map is removed in the root leaflet directive we
5353
// do not need ot clean up the events as leaflet does it itself
54-
addEvents(map, mapEvents, "eventName", leafletScope, logic);
54+
addEvents(map, attrs.id, mapEvents, "eventName", leafletScope, logic);
5555
});
5656
}
5757
};

src/directives/leaflet.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ angular.module('ui-leaflet', ['nemLogging']).directive('leaflet',
1818
controls : '=',
1919
decorations : '=',
2020
eventBroadcast : '=',
21-
watchOptions : '='
21+
watchOptions : '=',
22+
id : '@'
2223
},
2324
transclude: true,
2425
template: '<div class="angular-leaflet-map"><div ng-transclude></div></div>',
@@ -125,7 +126,7 @@ angular.module('ui-leaflet', ['nemLogging']).directive('leaflet',
125126
// if no event-broadcast attribute, all events are broadcasted
126127
if (!isDefined(attrs.eventBroadcast)) {
127128
var logic = "broadcast";
128-
addEvents(map, mapEvents, "eventName", scope, logic);
129+
addEvents(map, attrs.id, mapEvents, "eventName", scope, logic);
129130
}
130131

131132
// Resolve the map object to the promises

src/services/events/leafletMapEvents.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ angular.module('ui-leaflet')
5353
};
5454

5555
var _genDispatchMapEvent = function(scope, eventName, logic, maybeMapId) {
56-
if (maybeMapId)
56+
if (maybeMapId) {
5757
maybeMapId = maybeMapId + '.';
58+
}
5859
return function(e) {
5960
// Put together broadcast name
6061
var broadcastName = 'leafletDirectiveMap.' + maybeMapId + eventName;
@@ -80,11 +81,15 @@ angular.module('ui-leaflet')
8081
}
8182
};
8283

83-
var _addEvents = function(map, mapEvents, contextName, scope, logic){
84+
var _addEvents = function(map, mapId, mapEvents, contextName, scope, logic){
8485
leafletIterators.each(mapEvents, function(eventName) {
8586
var context = {};
8687
context[contextName] = eventName;
87-
map.on(eventName, _genDispatchMapEvent(scope, eventName, logic, map._container.id || ''), context);
88+
if(!mapId) {
89+
mapId = map._container.id || '';
90+
}
91+
92+
map.on(eventName, _genDispatchMapEvent(scope, eventName, logic, mapId), context);
8893
});
8994
};
9095

src/services/leafletHelpers.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ angular.module('ui-leaflet').service('leafletHelpers', function ($q, $log, $time
5858
function _obtainEffectiveMapId(d, mapId) {
5959
var id, i;
6060
if (!angular.isDefined(mapId)) {
61-
if (Object.keys(d).length === 0) {
62-
id = "main";
63-
} else if (Object.keys(d).length >= 1) {
64-
for (i in d) {
65-
if (d.hasOwnProperty(i)) {
66-
id = i;
61+
if (Object.keys(d).length === 0) {
62+
id = "main";
63+
} else if (Object.keys(d).length >= 1) {
64+
for (i in d) {
65+
if (d.hasOwnProperty(i)) {
66+
id = i;
67+
}
6768
}
68-
}
69-
} else {
69+
} else {
7070
$log.error(_errorHeader + "- You have more than 1 map on the DOM, you must provide the map ID to the leafletData.getXXX call");
7171
}
7272
} else {

test/unit/eventsDirectiveSpec.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ describe('Directive: leaflet', function() {
2121
$rootScope.$apply();
2222
}));
2323

24-
['', '1'].forEach(function(mapId){
24+
['', '1', '{{aMapId}}'].forEach(function(mapId){
2525
it( mapId + ' should broadcast events from the rootscope when triggered leaflet events',function(){
2626
var element = mapId? angular.element('<leaflet id="' + mapId + '" events="events"></leaflet>') :angular.element('<leaflet events="events"></leaflet>');
27+
$rootScope.aMapId = 'a-map-id';
2728
element = $compile(element)($rootScope);
2829
var scope = element.scope();
2930
var check = {};
@@ -69,20 +70,21 @@ describe('Directive: leaflet', function() {
6970
check[origEventName] = true;
7071
}
7172

72-
if(mapId)
73-
mapId = mapId + '.';
74-
75-
leafletData.getMap().then(function(map) {
76-
mapEvents.forEach(function(origEventName){
77-
var eventName = 'leafletDirectiveMap.' + mapId + origEventName;
78-
// leafletLogger.log(eventName); // Inspect
79-
scope.$on(eventName, function(){
80-
setEventTrue(origEventName, eventName);
81-
});
82-
map.fireEvent([origEventName]);
83-
expect(check[origEventName]).toEqual(true);
84-
check[origEventName] = undefined;
85-
});
73+
74+
leafletData.getMap(mapId).then(function(map) {
75+
if(mapId) {
76+
mapId = mapId + '.';
77+
}
78+
mapEvents.forEach(function(origEventName) {
79+
var eventName = 'leafletDirectiveMap.' + mapId + origEventName;
80+
// console.log('Call: ' + eventName); // Inspect
81+
scope.$on(eventName, function(){
82+
setEventTrue(origEventName, eventName);
83+
});
84+
map.fireEvent([origEventName]);
85+
expect(check[origEventName]).toEqual(true);
86+
check[origEventName] = undefined;
87+
});
8688
});
8789

8890
});

0 commit comments

Comments
 (0)