Skip to content

Commit f12f5a6

Browse files
committed
Refactor setDecorationOptions so that it calls setPaths and setPatterns. Add unit tests for decorations directive.
1 parent 9b264f5 commit f12f5a6

File tree

8 files changed

+157
-25
lines changed

8 files changed

+157
-25
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ module.exports = function(grunt) {
252252
'src/directives/controls.js',
253253
'src/directives/eventBroadcast.js',
254254
'src/directives/maxbounds.js',
255+
'src/directives/decorations.js',
255256
'src/services/leafletData.js',
256257
'src/services/leafletMapDefaults.js',
257258
'src/services/leafletEvents.js',

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"esri-leaflet": "0.0.1-beta.4",
3131
"proj4": "~2.1.2",
3232
"proj4leaflet": "~0.7.0",
33-
"Leaflet.MakiMarkers": "~1.0.1"
33+
"Leaflet.MakiMarkers": "~1.0.1",
34+
"Leaflet.PolylineDecorator": "bbecquet/Leaflet.PolylineDecorator"
3435
},
3536
"ignore": [
3637
"**/.*",

dist/angular-leaflet-directive.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,58 @@
10601060
};
10611061
}
10621062
]);
1063+
angular.module('leaflet-directive').directive('decorations', [
1064+
'$log',
1065+
'leafletHelpers',
1066+
function ($log, leafletHelpers) {
1067+
return {
1068+
restrict: 'A',
1069+
scope: false,
1070+
replace: false,
1071+
require: 'leaflet',
1072+
link: function (scope, element, attrs, controller) {
1073+
var leafletScope = controller.getLeafletScope(), PolylineDecoratorPlugin = leafletHelpers.PolylineDecoratorPlugin, isDefined = leafletHelpers.isDefined, leafletDecorations = {};
1074+
/* Creates an "empty" decoration with a set of coordinates, but no pattern. */
1075+
function createDecoration(options) {
1076+
if (isDefined(options) && isDefined(options.coordinates)) {
1077+
if (!PolylineDecoratorPlugin.isLoaded()) {
1078+
$log.error('[AngularJS - Leaflet] The PolylineDecorator Plugin is not loaded.');
1079+
}
1080+
}
1081+
return L.polylineDecorator(options.coordinates);
1082+
}
1083+
/* Updates the path and the patterns for the provided decoration, and returns the decoration. */
1084+
function setDecorationOptions(decoration, options) {
1085+
if (isDefined(decoration) && isDefined(options)) {
1086+
if (isDefined(options.coordinates) && isDefined(options.patterns)) {
1087+
decoration.setPaths(options.coordinates);
1088+
decoration.setPatterns(options.patterns);
1089+
return decoration;
1090+
}
1091+
}
1092+
}
1093+
controller.getMap().then(function (map) {
1094+
leafletScope.$watch('decorations', function (newDecorations) {
1095+
for (var name in leafletDecorations) {
1096+
if (!isDefined(newDecorations) || !isDefined(newDecorations[name])) {
1097+
delete leafletDecorations[name];
1098+
}
1099+
map.removeLayer(leafletDecorations[name]);
1100+
}
1101+
for (var newName in newDecorations) {
1102+
var decorationData = newDecorations[newName], newDecoration = createDecoration(decorationData);
1103+
if (isDefined(newDecoration)) {
1104+
leafletDecorations[newName] = newDecoration;
1105+
map.addLayer(newDecoration);
1106+
setDecorationOptions(newDecoration, decorationData);
1107+
}
1108+
}
1109+
}, true);
1110+
});
1111+
}
1112+
};
1113+
}
1114+
]);
10631115
angular.module('leaflet-directive').service('leafletData', [
10641116
'$log',
10651117
'$q',
@@ -1072,6 +1124,7 @@
10721124
var paths = {};
10731125
var markers = {};
10741126
var geoJSON = {};
1127+
var decorations = {};
10751128
this.setMap = function (leafletMap, scopeId) {
10761129
var defer = getUnresolvedDefer(maps, scopeId);
10771130
defer.resolve(leafletMap);
@@ -1130,6 +1183,15 @@
11301183
var defer = getDefer(geoJSON, scopeId);
11311184
return defer.promise;
11321185
};
1186+
this.setDecorations = function (leafletDecorations, scopeId) {
1187+
var defer = getUnresolvedDefer(decorations, scopeId);
1188+
defer.resolve(leafletDecorations);
1189+
setResolvedDefer(decorations, scopeId);
1190+
};
1191+
this.getDecorations = function (scopeId) {
1192+
var defer = getDefer(decorations, scopeId);
1193+
return defer.promise;
1194+
};
11331195
}
11341196
]);
11351197
angular.module('leaflet-directive').factory('leafletMapDefaults', [

dist/angular-leaflet-directive.min.js

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

src/directives/decorations.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@ angular.module("leaflet-directive").directive("decorations", function($log, leaf
77

88
link: function(scope, element, attrs, controller) {
99
var leafletScope = controller.getLeafletScope(),
10+
PolylineDecoratorPlugin = leafletHelpers.PolylineDecoratorPlugin,
1011
isDefined = leafletHelpers.isDefined,
1112
leafletDecorations = {};
1213

14+
/* Creates an "empty" decoration with a set of coordinates, but no pattern. */
1315
function createDecoration(options) {
14-
var decoration = L.polylineDecorator(options.coordinates);
15-
return decoration;
16+
if (isDefined(options) && isDefined(options.coordinates)) {
17+
if (!PolylineDecoratorPlugin.isLoaded()) {
18+
$log.error('[AngularJS - Leaflet] The PolylineDecorator Plugin is not loaded.');
19+
}
20+
}
21+
22+
return L.polylineDecorator(options.coordinates);
1623
}
1724

25+
/* Updates the path and the patterns for the provided decoration, and returns the decoration. */
1826
function setDecorationOptions(decoration, options) {
19-
decoration.setPaths(options.coordinates);
20-
return options.setAnimatedPatterns(decoration);
27+
if (isDefined(decoration) && isDefined(options)) {
28+
if (isDefined(options.coordinates) && isDefined(options.patterns)) {
29+
decoration.setPaths(options.coordinates);
30+
decoration.setPatterns(options.patterns);
31+
return decoration;
32+
}
33+
}
2134
}
2235

2336
controller.getMap().then(function(map) {
24-
// var watchDecoration = function(leafletDecoration, name) {
25-
// var animationId;
26-
// var deregisterWatch = leafletScope.$watch("decorations." + name, function(decorationData) {
27-
// clearInterval(animationId);
28-
// if (!isDefined(decorationData)) {
29-
// map.removeLayer(leafletDecoration);
30-
// deregisterWatch();
31-
// return;
32-
// }
33-
// animationId = setDecorationOptions(leafletDecoration, decorationData);
34-
// }, true);
35-
// };
36-
37-
var animationId;
3837
leafletScope.$watch("decorations", function(newDecorations) {
3938
for (var name in leafletDecorations) {
4039
if (!isDefined(newDecorations) || !isDefined(newDecorations[name])) {
@@ -50,9 +49,8 @@ angular.module("leaflet-directive").directive("decorations", function($log, leaf
5049
if (isDefined(newDecoration)) {
5150
leafletDecorations[newName] = newDecoration;
5251
map.addLayer(newDecoration);
52+
setDecorationOptions(newDecoration, decorationData);
5353
}
54-
clearInterval(animationId);
55-
animationId = setDecorationOptions(newDecoration, decorationData);
5654
}
5755
}, true);
5856
});

src/services/leafletData.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ angular.module("leaflet-directive").service('leafletData', function ($log, $q, l
99
var paths = {};
1010
var markers = {};
1111
var geoJSON = {};
12+
var decorations = {};
1213

1314
this.setMap = function(leafletMap, scopeId) {
1415
var defer = getUnresolvedDefer(maps, scopeId);
@@ -80,4 +81,15 @@ angular.module("leaflet-directive").service('leafletData', function ($log, $q, l
8081
var defer = getDefer(geoJSON, scopeId);
8182
return defer.promise;
8283
};
84+
85+
this.setDecorations = function(leafletDecorations, scopeId) {
86+
var defer = getUnresolvedDefer(decorations, scopeId);
87+
defer.resolve(leafletDecorations);
88+
setResolvedDefer(decorations, scopeId);
89+
};
90+
91+
this.getDecorations = function(scopeId) {
92+
var defer = getDefer(decorations, scopeId);
93+
return defer.promise;
94+
};
8395
});

test/karma-unit.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = function(karma) {
1111
'bower_components/angular-mocks/angular-mocks.js',
1212
'dist/angular-leaflet-directive.js',
1313
//'src/**/*.js',
14-
'test/unit/*.js'
14+
'test/unit/*.js',
15+
'bower_components/Leaflet.PolylineDecorator/leaflet.polylineDecorator.js'
1516
],
1617

1718
// Frameworks
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
/*jshint -W117 */
4+
/*jshint globalstrict: true*/
5+
/* jasmine specs for directives go here */
6+
7+
describe('Directive: decorations', function() {
8+
var $compile, $rootScope, leafletData, leafletHelpers, mainCoordinates, mainDecorations;
9+
10+
beforeEach(module('leaflet-directive'));
11+
beforeEach(inject(function(_$compile_, _$rootScope_, _leafletData_, _leafletHelpers_) {
12+
$compile = _$compile_;
13+
$rootScope = _$rootScope_;
14+
leafletData = _leafletData_;
15+
leafletHelpers = _leafletHelpers_;
16+
}));
17+
beforeEach(function() {
18+
mainCoordinates = [
19+
[ 0.966, 2.02 ],
20+
[ 2.02, 4.04 ]
21+
];
22+
mainDecorations = {
23+
arrow: {
24+
coordinates: mainCoordinates,
25+
patterns: [{offset: '10%', repeat: 0, symbol: L.Symbol.arrowHead({pixelSize: 10, polygon: false, pathOptions: {stroke: true}})}]
26+
},
27+
markers: {
28+
coordinates: mainCoordinates,
29+
patterns: { offset: '5%', repeat: '10%', symbol: L.Symbol.marker()}
30+
}
31+
};
32+
});
33+
34+
afterEach(inject(function($rootScope) {
35+
$rootScope.$apply();
36+
}));
37+
38+
it("should create a decoration on the map", function() {
39+
angular.extend($rootScope, {
40+
decorations: {
41+
arrow: mainDecorations.arrow
42+
}
43+
});
44+
45+
var element = angular.element('<leaflet decorations="decorations"></leaflet>');
46+
element = $compile(element)($rootScope);
47+
$rootScope.$digest();
48+
leafletData.getDecorations().then(function(leafletDecorations) {
49+
var leafletArrow = leafletDecorations.arrow;
50+
51+
// Unfortunately, the L.PolylineDecorator class does not currently expose any value accessors.
52+
expect(leafletArrow.options.patterns.offset).toBe('10%');
53+
expect(leafletArrow.options.patterns.repeat).toBe(0);
54+
expect(leafletArrow.options.symbol).toEqual(L.Symbol.arrowHead({pixelSize: 10, polygon: false, pathOptions: {stroke: true}}));
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)