Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Commit 4391e3b

Browse files
committed
feat: add leaflet-show option to help with hidden maps
The idea is to help with hiddden place like with ng-show/ng-hide div. What you have to know is if your map has been initialized in an hidden place it will be broken once you display the container. You must fix it and this option help you for that. Put an expression that is evaluated to true when the container is visible.
1 parent 772314c commit 4391e3b

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ export default angular.module('angular-leaflet', [])
77
/**
88
* This component provide a default leaflet map initialized with id='map'
99
* if no id has been provided.
10+
*
11+
* If you want to initialize you map in an hidden place (for example a tab)
12+
* you can use leaflet-show option and put the display condition so the component
13+
* will fix the leaflet map object. leaflet-show is not required.
1014
* @usage
11-
<leaflet on-map-initialized="$ctrl.onMapInitialized(map)"></leaflet>
15+
<leaflet leaflet-show="displayMap" on-map-initialized="$ctrl.onMapInitialized(map)"></leaflet>
1216
*/
1317
.component('leaflet', {
1418
template: '<div></div>',
1519
controller: controller,
1620
bindings: {
17-
onMapInitialized: '&'
21+
onMapInitialized: '&',
22+
leafletShow: '<'
1823
}
1924
})
2025

src/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/*global ngDescribe:false */
33
/*global it:false */
44
/*global expect:false */
5+
/*global spyOn:false */
56

67
ngDescribe({
78
modules: 'angular-leaflet',
@@ -58,6 +59,15 @@ ngDescribe({
5859
var maxBounds = map.options.maxBounds.toBBoxString();
5960
expect(maxBounds).toBe('-1.652756,47.143496,-1.461868,47.296462');
6061
});
62+
it('should fix if initialized in hidden stuff', function () {
63+
spyOn(deps.leafletService, 'fixHiddenLeaflet');
64+
expect(deps.leafletService.fixHiddenLeaflet).not.toHaveBeenCalled();
65+
var controller = deps.element.isolateScope().$ctrl;
66+
controller.$onChanges({leafletShow: {currentValue: false}});
67+
expect(deps.leafletService.fixHiddenLeaflet).not.toHaveBeenCalled();
68+
controller.$onChanges({leafletShow: {currentValue: true}});
69+
expect(deps.leafletService.fixHiddenLeaflet).toHaveBeenCalled();
70+
});
6171
}
6272

6373
});

src/leaflet.controller.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class LeafletCtrl {
3131
this.leafletService.data[this.mapid] = map;
3232
this.leafletService.updateMapFromSettings(map);
3333
this.onMapInitialized({map: map});
34+
this.map = map;
3435
}
36+
$onChanges(changesObj) {
37+
if (changesObj.leafletShow.currentValue && this.map) {
38+
this.leafletService.fixHiddenLeaflet(this.map);
39+
}
40+
}
41+
3542
}
3643

3744
LeafletCtrl.$inject = ['$element', 'leafletService'];

src/leaflet.service.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ class LeafletService {
102102
L.control.layers(baselayers, overlays).addTo(map);
103103
}
104104
}
105+
/**
106+
* If your leaflet map object is initialized in an hidden place
107+
* like in a tab, you can just call this function once the container
108+
* is visible.
109+
* This is used by the directive with it's leaflet-show condition
110+
* Found on stackoverflow: http://stackoverflow.com/questions/10762984/leaflet-map-not-displayed-properly-inside-tabbed-panel
111+
*/
112+
fixHiddenLeaflet(map) {
113+
L.Util.requestAnimFrame(
114+
map.invalidateSize,
115+
map, !1,
116+
map._container
117+
);
118+
}
105119
}
106120
LeafletService.$inject = ['$compile'];
107121
export default LeafletService;

src/leaflet.service.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/*global ngDescribe:false */
33
/*global it:false */
44
/*global expect:false */
5+
/*global spyOn:false */
56

67

78
ngDescribe({
@@ -69,6 +70,13 @@ ngDescribe({
6970
expect(isApplyed).toBe(false);
7071

7172
});
73+
it('should fixHiddenLeaflet call L.utils stuff', function () {
74+
var map = {};
75+
spyOn(L.Util, 'requestAnimFrame');
76+
deps.leafletService.fixHiddenLeaflet(map);
77+
expect(L.Util.requestAnimFrame).toHaveBeenCalled();
78+
});
79+
7280
}
7381

7482
});

0 commit comments

Comments
 (0)