Skip to content

Commit

Permalink
feat(controls): Reworked and cleaned up "controls" code. Now is possi…
Browse files Browse the repository at this point in the history
…ble to add/remove controls dinamically, and it's very easy to extend controls with new ones.
  • Loading branch information
tombatossals committed Jul 1, 2015
1 parent e7591a5 commit 2d008cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
51 changes: 40 additions & 11 deletions src/directives/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,49 @@ angular.module("leaflet-directive").directive('controls', function ($log, leafle
return;
}

var createControl = leafletControlHelpers.createControl,
leafletScope = controller.getLeafletScope(),
controls = leafletScope.controls;
var createControl = leafletControlHelpers.createControl;
var isValidControlType = leafletControlHelpers.isValidControlType;
var leafletScope = controller.getLeafletScope();
var isDefined = leafletHelpers.isDefined;
var leafletControls = {};
var errorHeader = leafletHelpers.errorHeader + ' [Controls] ';

controller.getMap().then(function(map) {
for (var controlType in controls) {
var control;
if (controlType !== 'custom') {
control = createControl(controlType, controls[controlType]);
} else {
control = controls[controlType];

leafletScope.$watchCollection('controls', function(newControls) {

// Delete controls from the array
for (var name in leafletControls) {
if (!isDefined(newControls[name])) {
if (map.hasControl(leafletControls[name])) {
map.removeControl(leafletControls[name]);
}
delete leafletControls[name];
}
}

for (var newName in newControls) {
var control;

var controlType = isDefined(newControls[newName].type) ? newControls[newName].type : newName;

if (!isValidControlType(controlType)) {
$log.error(errorHeader + ' Invalid control type: ' + controlType + '.');
return;
}

if (controlType !== 'custom') {

This comment has been minimized.

Copy link
@eczajk1

eczajk1 Jul 30, 2015

Contributor

@tombatossals Seems like controlType will never === 'custom' because of the previous if statement. As a result, "custom" controls no long work.

control = createControl(controlType, newControls[newName]);
} else {
control = newControls[newName];
}
map.addControl(control);

leafletControls[newName] = control;
}
map.addControl(control);
}

});

});
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/directives/tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module("leaflet-directive").directive('tiles', function ($log, leafletDa
leafletScope = controller.getLeafletScope(),
tiles = leafletScope.tiles;

if (!isDefined(tiles) && !isDefined(tiles.url)) {
if (!isDefined(tiles) || !isDefined(tiles.url)) {
$log.warn("[AngularJS - Leaflet] The 'tiles' definition doesn't have the 'url' property.");
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/leafletControlHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ angular.module("leaflet-directive").factory('leafletControlHelpers', function ($
checkValidParams: function(params) {
if(!isDefined(params.layer)) {
$log.warn(errorHeader +' minimap "layer" option should be defined.');
return;
return false;
}
return true;
},
createControl: function(params) {
var layer = createLayer(params.layer);
Expand All @@ -142,6 +143,10 @@ angular.module("leaflet-directive").factory('leafletControlHelpers', function ($
return {
layersControlMustBeVisible: _controlLayersMustBeVisible,

isValidControlType: function(type) {
return Object.keys(controlTypes).indexOf(type) !== -1;
},

createControl: function (type, params) {
if (!controlTypes[type].checkValidParams(params)) {
return;
Expand Down

0 comments on commit 2d008cc

Please sign in to comment.