',
link: function(scope, element, attrs, controller) {
var isDefined = leafletHelpers.isDefined;
var leafletScope = controller.getLeafletScope();
var layers = leafletScope.layers;
scope.$watch('icons', function() {
var defaultIcons = {
uncheck: 'fa fa-square-o',
check: 'fa fa-check-square-o',
radio: 'fa fa-dot-circle-o',
unradio: 'fa fa-circle-o',
up: 'fa fa-angle-up',
down: 'fa fa-angle-down',
open: 'fa fa-angle-double-down',
close: 'fa fa-angle-double-up',
toggleLegend: 'fa fa-pencil-square-o',
};
if (isDefined(scope.icons)) {
angular.extend(defaultIcons, scope.icons);
angular.extend(scope.icons, defaultIcons);
} else {
scope.icons = defaultIcons;
}
});
// Setting layer stack order.
attrs.order = (isDefined(attrs.order) && (attrs.order === 'normal' || attrs.order === 'reverse')) ? attrs.order : 'normal';
scope.order = attrs.order === 'normal';
scope.orderNumber = attrs.order === 'normal' ? -1 : 1;
scope.layers = layers;
controller.getMap().then(function(map) {
leafletScope.$watch('layers.baselayers', function(newBaseLayers) {
var baselayersArray = {};
leafletData.getLayers().then(function(leafletLayers) {
var key;
for (key in newBaseLayers) {
var layer = newBaseLayers[key];
layer.icon = scope.icons[map.hasLayer(leafletLayers.baselayers[key]) ? 'radio' : 'unradio'];
baselayersArray[key] = layer;
}
scope.baselayersArray = baselayersArray;
});
});
leafletScope.$watch('layers.overlays', function(newOverlayLayers) {
var overlaysArray = [];
var groupVisibleCount = {};
leafletData.getLayers().then(function(leafletLayers) {
var key;
for (key in newOverlayLayers) {
var layer = newOverlayLayers[key];
layer.icon = scope.icons[(layer.visible ? 'check' : 'uncheck')];
overlaysArray.push(layer);
if (!isDefined(scope.layerProperties[layer.name])) {
scope.layerProperties[layer.name] = {
opacity: isDefined(layer.layerOptions.opacity) ? layer.layerOptions.opacity * 100 : 100,
opacityControl: false,
showLegend: true,
};
}
if (isDefined(layer.group)) {
if (!isDefined(scope.groupProperties[layer.group])) {
scope.groupProperties[layer.group] = {
visible: false,
};
}
groupVisibleCount[layer.group] = isDefined(groupVisibleCount[layer.group]) ? groupVisibleCount[layer.group] : {
count: 0,
visibles: 0,
};
groupVisibleCount[layer.group].count++;
if (layer.visible) {
groupVisibleCount[layer.group].visibles++;
}
}
if (isDefined(layer.index) && leafletLayers.overlays[key].setZIndex) {
leafletLayers.overlays[key].setZIndex(newOverlayLayers[key].index);
}
}
for (key in groupVisibleCount) {
scope.groupProperties[key].visible = groupVisibleCount[key].visibles === groupVisibleCount[key].count;
}
scope.overlaysArray = overlaysArray;
});
}, true);
});
},
};
}]);
angular.module('leaflet-directive').directive('layers', ["$log", "$q", "leafletData", "leafletHelpers", "leafletLayerHelpers", "leafletControlHelpers", function($log, $q, leafletData, leafletHelpers, leafletLayerHelpers, leafletControlHelpers) {
return {
restrict: 'A',
scope: false,
replace: false,
require: 'leaflet',
controller: ["$scope", function($scope) {
$scope._leafletLayers = $q.defer();
this.getLayers = function() {
return $scope._leafletLayers.promise;
};
}],
link: function(scope, element, attrs, controller) {
var isDefined = leafletHelpers.isDefined;
var leafletLayers = {};
var leafletScope = controller.getLeafletScope();
var layers = leafletScope.layers;
var createLayer = leafletLayerHelpers.createLayer;
var safeAddLayer = leafletLayerHelpers.safeAddLayer;
var safeRemoveLayer = leafletLayerHelpers.safeRemoveLayer;
var updateLayersControl = leafletControlHelpers.updateLayersControl;
var isLayersControlVisible = false;
controller.getMap().then(function(map) {
// We have baselayers to add to the map
scope._leafletLayers.resolve(leafletLayers);
leafletData.setLayers(leafletLayers, attrs.id);
leafletLayers.baselayers = {};
leafletLayers.overlays = {};
var mapId = attrs.id;
// Setup all baselayers definitions
var oneVisibleLayer = false;
for (var layerName in layers.baselayers) {
var newBaseLayer = createLayer(layers.baselayers[layerName]);
if (!isDefined(newBaseLayer)) {
delete layers.baselayers[layerName];
continue;
}
leafletLayers.baselayers[layerName] = newBaseLayer;
// Only add the visible layer to the map, layer control manages the addition to the map
// of layers in its control
if (layers.baselayers[layerName].top === true) {
safeAddLayer(map, leafletLayers.baselayers[layerName]);
oneVisibleLayer = true;
}
}
// If there is no visible layer add first to the map
if (!oneVisibleLayer && Object.keys(leafletLayers.baselayers).length > 0) {
safeAddLayer(map, leafletLayers.baselayers[Object.keys(layers.baselayers)[0]]);
}
// Setup the Overlays
for (layerName in layers.overlays) {
//if (layers.overlays[layerName].type === 'cartodb') {
//
//}
var newOverlayLayer = createLayer(layers.overlays[layerName]);
if (!isDefined(newOverlayLayer)) {
delete layers.overlays[layerName];
continue;
}
leafletLayers.overlays[layerName] = newOverlayLayer;
// Only add the visible overlays to the map
if (layers.overlays[layerName].visible === true) {
safeAddLayer(map, leafletLayers.overlays[layerName]);
}
}
// Watch for the base layers
leafletScope.$watch('layers.baselayers', function(newBaseLayers, oldBaseLayers) {
if (angular.equals(newBaseLayers, oldBaseLayers)) {
isLayersControlVisible = updateLayersControl(map, mapId, isLayersControlVisible, newBaseLayers, layers.overlays, leafletLayers);
return true;
}
// Delete layers from the array
for (var name in leafletLayers.baselayers) {
if (!isDefined(newBaseLayers[name]) || newBaseLayers[name].doRefresh) {
// Remove from the map if it's on it
if (map.hasLayer(leafletLayers.baselayers[name])) {
map.removeLayer(leafletLayers.baselayers[name]);
}
delete leafletLayers.baselayers[name];
if (newBaseLayers[name] && newBaseLayers[name].doRefresh) {
newBaseLayers[name].doRefresh = false;
}
}
}
// add new layers
for (var newName in newBaseLayers) {
if (!isDefined(leafletLayers.baselayers[newName])) {
var testBaseLayer = createLayer(newBaseLayers[newName]);
if (isDefined(testBaseLayer)) {
leafletLayers.baselayers[newName] = testBaseLayer;
// Only add the visible layer to the map
if (newBaseLayers[newName].top === true) {
safeAddLayer(map, leafletLayers.baselayers[newName]);
}
}
} else {
if (newBaseLayers[newName].top === true && !map.hasLayer(leafletLayers.baselayers[newName])) {
safeAddLayer(map, leafletLayers.baselayers[newName]);
} else if (newBaseLayers[newName].top === false && map.hasLayer(leafletLayers.baselayers[newName])) {
map.removeLayer(leafletLayers.baselayers[newName]);
}
}
}
//we have layers, so we need to make, at least, one active
var found = false;
// search for an active layer
for (var key in leafletLayers.baselayers) {
if (map.hasLayer(leafletLayers.baselayers[key])) {
found = true;
break;
}
}
// If there is no active layer make one active
if (!found && Object.keys(leafletLayers.baselayers).length > 0) {
safeAddLayer(map, leafletLayers.baselayers[Object.keys(leafletLayers.baselayers)[0]]);
}
// Only show the layers switch selector control if we have more than one baselayer + overlay
isLayersControlVisible = updateLayersControl(map, mapId, isLayersControlVisible, newBaseLayers, layers.overlays, leafletLayers);
}, true);
// Watch for the overlay layers
leafletScope.$watch('layers.overlays', function(newOverlayLayers, oldOverlayLayers) {
if (angular.equals(newOverlayLayers, oldOverlayLayers)) {
isLayersControlVisible = updateLayersControl(map, mapId, isLayersControlVisible, layers.baselayers, newOverlayLayers, leafletLayers);
return true;
}
// Delete layers from the array
for (var name in leafletLayers.overlays) {
if (!isDefined(newOverlayLayers[name]) || newOverlayLayers[name].doRefresh) {
// Remove from the map if it's on it
if (map.hasLayer(leafletLayers.overlays[name])) {
// Safe remove when ArcGIS layers is loading.
var options = isDefined(newOverlayLayers[name]) ?
newOverlayLayers[name].layerOptions : null;
safeRemoveLayer(map, leafletLayers.overlays[name], options);
}
// TODO: Depending on the layer type we will have to delete what's included on it
delete leafletLayers.overlays[name];
if (newOverlayLayers[name] && newOverlayLayers[name].doRefresh) {
newOverlayLayers[name].doRefresh = false;
}
}
}
// add new overlays
for (var newName in newOverlayLayers) {
if (!isDefined(leafletLayers.overlays[newName])) {
var testOverlayLayer = createLayer(newOverlayLayers[newName]);
if (!isDefined(testOverlayLayer)) {
// If the layer creation fails, continue to the next overlay
continue;
}
leafletLayers.overlays[newName] = testOverlayLayer;
if (newOverlayLayers[newName].visible === true) {
safeAddLayer(map, leafletLayers.overlays[newName]);
}
} else {
// check for the .visible property to hide/show overLayers
if (newOverlayLayers[newName].visible && !map.hasLayer(leafletLayers.overlays[newName])) {
safeAddLayer(map, leafletLayers.overlays[newName]);
} else if (newOverlayLayers[newName].visible === false && map.hasLayer(leafletLayers.overlays[newName])) {
// Safe remove when ArcGIS layers is loading.
safeRemoveLayer(map, leafletLayers.overlays[newName], newOverlayLayers[newName].layerOptions);
}
}
//refresh heatmap data if present
if (newOverlayLayers[newName].visible && map._loaded && newOverlayLayers[newName].data && newOverlayLayers[newName].type === 'heatmap') {
leafletLayers.overlays[newName].setData(newOverlayLayers[newName].data);
leafletLayers.overlays[newName].update();
}
}
// Only add the layers switch selector control if we have more than one baselayer + overlay
isLayersControlVisible = updateLayersControl(map, mapId, isLayersControlVisible, layers.baselayers, newOverlayLayers, leafletLayers);
}, true);
});
},
};
}]);
angular.module('leaflet-directive').directive('legend', ["$log", "$http", "leafletHelpers", "leafletLegendHelpers", function($log, $http, leafletHelpers, leafletLegendHelpers) {
return {
restrict: 'A',
scope: false,
replace: false,
require: 'leaflet',
link: function(scope, element, attrs, controller) {
var isArray = leafletHelpers.isArray;
var isDefined = leafletHelpers.isDefined;
var isFunction = leafletHelpers.isFunction;
var leafletScope = controller.getLeafletScope();
var legend = leafletScope.legend;
var legendClass;
var position;
var leafletLegend;
var type;
leafletScope.$watch('legend', function(newLegend) {
if (isDefined(newLegend)) {
legendClass = newLegend.legendClass ? newLegend.legendClass : 'legend';
position = newLegend.position || 'bottomright';
// default to arcgis
type = newLegend.type || 'arcgis';
}
}, true);
controller.getMap().then(function(map) {
leafletScope.$watch('legend', function(newLegend) {
if (!isDefined(newLegend)) {
if (isDefined(leafletLegend)) {
leafletLegend.removeFrom(map);
leafletLegend = null;
}
return;
}
if (!isDefined(newLegend.url) && (type === 'arcgis') && (!isArray(newLegend.colors) || !isArray(newLegend.labels) || newLegend.colors.length !== newLegend.labels.length)) {
$log.warn('[AngularJS - Leaflet] legend.colors and legend.labels must be set.');
return;
}
if (isDefined(newLegend.url)) {
$log.info('[AngularJS - Leaflet] loading legend service.');
return;
}
if (isDefined(leafletLegend)) {
leafletLegend.removeFrom(map);
leafletLegend = null;
}
leafletLegend = L.control({
position: position,
});
if (type === 'arcgis') {
leafletLegend.onAdd = leafletLegendHelpers.getOnAddArrayLegend(newLegend, legendClass);
}
leafletLegend.addTo(map);
});
leafletScope.$watch('legend.url', function(newURL) {
if (!isDefined(newURL)) {
return;
}
$http.get(newURL)
.success(function(legendData) {
if (isDefined(leafletLegend)) {
leafletLegendHelpers.updateLegend(leafletLegend.getContainer(), legendData, type, newURL);
} else {
leafletLegend = L.control({
position: position,
});
leafletLegend.onAdd = leafletLegendHelpers.getOnAddLegend(legendData, legendClass, type, newURL);
leafletLegend.addTo(map);
}
if (isDefined(legend.loadedData) && isFunction(legend.loadedData)) {
legend.loadedData();
}
})
.error(function() {
$log.warn('[AngularJS - Leaflet] legend.url not loaded.');
});
});
});
},
};
}]);
angular.module('leaflet-directive').directive('markers',
["$log", "$rootScope", "$q", "leafletData", "leafletHelpers", "leafletMapDefaults", "leafletMarkersHelpers", "leafletMarkerEvents", "leafletIterators", "leafletWatchHelpers", "leafletDirectiveControlsHelpers", function($log, $rootScope, $q, leafletData, leafletHelpers, leafletMapDefaults,
leafletMarkersHelpers, leafletMarkerEvents, leafletIterators, leafletWatchHelpers,
leafletDirectiveControlsHelpers) {
//less terse vars to helpers
var isDefined = leafletHelpers.isDefined;
var errorHeader = leafletHelpers.errorHeader;
var Helpers = leafletHelpers;
var isString = leafletHelpers.isString;
var addMarkerWatcher = leafletMarkersHelpers.addMarkerWatcher;
var updateMarker = leafletMarkersHelpers.updateMarker;
var listenMarkerEvents = leafletMarkersHelpers.listenMarkerEvents;
var addMarkerToGroup = leafletMarkersHelpers.addMarkerToGroup;
var createMarker = leafletMarkersHelpers.createMarker;
var deleteMarker = leafletMarkersHelpers.deleteMarker;
var $it = leafletIterators;
var _markersWatchOptions = leafletHelpers.watchOptions;
var maybeWatch = leafletWatchHelpers.maybeWatch;
var extendDirectiveControls = leafletDirectiveControlsHelpers.extend;
var _getLMarker = function(leafletMarkers, name, maybeLayerName) {
if (!Object.keys(leafletMarkers).length) return;
if (maybeLayerName && isString(maybeLayerName)) {
if (!leafletMarkers[maybeLayerName] || !Object.keys(leafletMarkers[maybeLayerName]).length)
return;
return leafletMarkers[maybeLayerName][name];
}
return leafletMarkers[name];
};
var _setLMarker = function(lObject, leafletMarkers, name, maybeLayerName) {
if (maybeLayerName && isString(maybeLayerName)) {
if (!isDefined(leafletMarkers[maybeLayerName]))
leafletMarkers[maybeLayerName] = {};
leafletMarkers[maybeLayerName][name] = lObject;
} else
leafletMarkers[name] = lObject;
return lObject;
};
var _maybeAddMarkerToLayer = function(layerName, layers, model, marker, doIndividualWatch, map) {
if (!isString(layerName)) {
$log.error(errorHeader + ' A layername must be a string');
return false;
}
if (!isDefined(layers)) {
$log.error(errorHeader + ' You must add layers to the directive if the markers are going to use this functionality.');
return false;
}
if (!isDefined(layers.overlays) || !isDefined(layers.overlays[layerName])) {
$log.error(errorHeader + ' A marker can only be added to a layer of type "group"');
return false;
}
var layerGroup = layers.overlays[layerName];
if (!(layerGroup instanceof L.LayerGroup || layerGroup instanceof L.FeatureGroup)) {
$log.error(errorHeader + ' Adding a marker to an overlay needs a overlay of the type "group" or "featureGroup"');
return false;
}
// The marker goes to a correct layer group, so first of all we add it
layerGroup.addLayer(marker);
// The marker is automatically added to the map depending on the visibility
// of the layer, so we only have to open the popup if the marker is in the map
if (!doIndividualWatch && map.hasLayer(marker) && model.focus === true) {
marker.openPopup();
}
return true;
};
//TODO: move to leafletMarkersHelpers??? or make a new class/function file (leafletMarkersHelpers is large already)
var _addMarkers = function(mapId, markersToRender, oldModels, map, layers, leafletMarkers, leafletScope,
watchOptions, maybeLayerName, skips) {
for (var newName in markersToRender) {
if (skips[newName])
continue;
if (newName.search('-') !== -1) {
$log.error('The marker can\'t use a "-" on his key name: "' + newName + '".');
continue;
}
var model = Helpers.copy(markersToRender[newName]);
var pathToMarker = Helpers.getObjectDotPath(maybeLayerName ? [maybeLayerName, newName] : [newName]);
var maybeLMarker = _getLMarker(leafletMarkers, newName, maybeLayerName);
if (!isDefined(maybeLMarker)) {
//(nmccready) very important to not have model changes when lObject is changed
//this might be desirable in some cases but it causes two-way binding to lObject which is not ideal
//if it is left as the reference then all changes from oldModel vs newModel are ignored
//see _destroy (where modelDiff becomes meaningless if we do not copy here)
var marker = createMarker(model);
var layerName = (model ? model.layer : undefined) || maybeLayerName; //original way takes pref
if (!isDefined(marker)) {
$log.error(errorHeader + ' Received invalid data on the marker ' + newName + '.');
continue;
}
_setLMarker(marker, leafletMarkers, newName, maybeLayerName);
// Bind message
if (isDefined(model.message)) {
marker.bindPopup(model.message, model.popupOptions);
}
// Add the marker to a cluster group if needed
if (isDefined(model.group)) {
var groupOptions = isDefined(model.groupOption) ? model.groupOption : null;
addMarkerToGroup(marker, model.group, groupOptions, map);
}
// Show label if defined
if (Helpers.LabelPlugin.isLoaded() && isDefined(model.label) && isDefined(model.label.message)) {
marker.bindLabel(model.label.message, model.label.options);
}
// Check if the marker should be added to a layer
if (isDefined(model) && (isDefined(model.layer) || isDefined(maybeLayerName))) {
var pass = _maybeAddMarkerToLayer(layerName, layers, model, marker,
watchOptions.individual.doWatch, map);
if (!pass)
continue; //something went wrong move on in the loop
} else if (!isDefined(model.group)) {
// We do not have a layer attr, so the marker goes to the map layer
map.addLayer(marker);
if (!watchOptions.individual.doWatch && model.focus === true) {
marker.openPopup();
}
}
if (watchOptions.individual.doWatch) {
addMarkerWatcher(marker, pathToMarker, leafletScope, layers, map,
watchOptions.individual.isDeep);
}
listenMarkerEvents(marker, model, leafletScope, watchOptions.individual.doWatch, map);
leafletMarkerEvents.bindEvents(mapId, marker, pathToMarker, model, leafletScope, layerName);
} else {
var oldModel = isDefined(oldModel) ? oldModels[newName] : undefined;
updateMarker(model, oldModel, maybeLMarker, pathToMarker, leafletScope, layers, map);
}
}
};
var _seeWhatWeAlreadyHave = function(markerModels, oldMarkerModels, lMarkers, isEqual, cb) {
var hasLogged = false;
var equals = false;
var oldMarker;
var newMarker;
var doCheckOldModel = isDefined(oldMarkerModels);
for (var name in lMarkers) {
if (!hasLogged) {
$log.debug(errorHeader + '[markers] destroy: ');
hasLogged = true;
}
if (doCheckOldModel) {
//might want to make the option (in watch options) to disable deep checking
//ie the options to only check !== (reference check) instead of angular.equals (slow)
newMarker = markerModels[name];
oldMarker = oldMarkerModels[name];
equals = angular.equals(newMarker, oldMarker) && isEqual;
}
if (!isDefined(markerModels) ||
!Object.keys(markerModels).length ||
!isDefined(markerModels[name]) ||
!Object.keys(markerModels[name]).length ||
equals) {
if (cb && Helpers.isFunction(cb))
cb(newMarker, oldMarker, name);
}
}
};
var _destroy = function(markerModels, oldMarkerModels, lMarkers, map, layers) {
_seeWhatWeAlreadyHave(markerModels, oldMarkerModels, lMarkers, false,
function(newMarker, oldMarker, lMarkerName) {
$log.debug(errorHeader + '[marker] is deleting marker: ' + lMarkerName);
deleteMarker(lMarkers[lMarkerName], map, layers);
delete lMarkers[lMarkerName];
});
};
var _getNewModelsToSkipp = function(newModels, oldModels, lMarkers) {
var skips = {};
_seeWhatWeAlreadyHave(newModels, oldModels, lMarkers, true,
function(newMarker, oldMarker, lMarkerName) {
$log.debug(errorHeader + '[marker] is already rendered, marker: ' + lMarkerName);
skips[lMarkerName] = newMarker;
});
return skips;
};
return {
restrict: 'A',
scope: false,
replace: false,
require: ['leaflet', '?layers'],
link: function(scope, element, attrs, controller) {
var mapController = controller[0];
var leafletScope = mapController.getLeafletScope();
mapController.getMap().then(function(map) {
var leafletMarkers = {};
var getLayers;
// If the layers attribute is used, we must wait until the layers are created
if (isDefined(controller[1])) {
getLayers = controller[1].getLayers;
} else {
getLayers = function() {
var deferred = $q.defer();
deferred.resolve();
return deferred.promise;
};
}
var watchOptions = leafletScope.markersWatchOptions || _markersWatchOptions;
// backwards compat
if (isDefined(attrs.watchMarkers))
watchOptions.doWatch = watchOptions.individual.doWatch =
(!isDefined(attrs.watchMarkers) || Helpers.isTruthy(attrs.watchMarkers));
var isNested = (isDefined(attrs.markersNested) && Helpers.isTruthy(attrs.markersNested));
getLayers().then(function(layers) {
var _clean = function(models, oldModels) {
if (isNested) {
$it.each(models, function(markerToMaybeDel, layerName) {
var oldModel = isDefined(oldModel) ? oldModels[layerName] : undefined;
_destroy(markerToMaybeDel, oldModel, leafletMarkers[layerName], map, layers);
});
return;
}
_destroy(models, oldModels, leafletMarkers, map, layers);
};
var _create = function(models, oldModels) {
_clean(models, oldModels);
var skips = null;
if (isNested) {
$it.each(models, function(markersToAdd, layerName) {
var oldModel = isDefined(oldModel) ? oldModels[layerName] : undefined;
skips = _getNewModelsToSkipp(models[layerName], oldModel, leafletMarkers[layerName]);
_addMarkers(attrs.id, markersToAdd, oldModels, map, layers, leafletMarkers, leafletScope,
watchOptions, layerName, skips);
});
return;
}
skips = _getNewModelsToSkipp(models, oldModels, leafletMarkers);
_addMarkers(attrs.id, models, oldModels, map, layers, leafletMarkers, leafletScope,
watchOptions, undefined, skips);
};
extendDirectiveControls(attrs.id, 'markers', _create, _clean);
leafletData.setMarkers(leafletMarkers, attrs.id);
maybeWatch(leafletScope, 'markers', watchOptions, function(newMarkers, oldMarkers) {
_create(newMarkers, oldMarkers);
});
});
});
},
};
}]);
angular.module('leaflet-directive').directive('maxbounds', ["$log", "leafletMapDefaults", "leafletBoundsHelpers", "leafletHelpers", function($log, leafletMapDefaults, leafletBoundsHelpers, leafletHelpers) {
return {
restrict: 'A',
scope: false,
replace: false,
require: 'leaflet',
link: function(scope, element, attrs, controller) {
var leafletScope = controller.getLeafletScope();
var isValidBounds = leafletBoundsHelpers.isValidBounds;
var isNumber = leafletHelpers.isNumber;
controller.getMap().then(function(map) {
leafletScope.$watch('maxbounds', function(maxbounds) {
if (!isValidBounds(maxbounds)) {
// Unset any previous maxbounds
map.setMaxBounds();
return;
}
var leafletBounds = leafletBoundsHelpers.createLeafletBounds(maxbounds);
if (isNumber(maxbounds.pad)) {
leafletBounds = leafletBounds.pad(maxbounds.pad);
}
map.setMaxBounds(leafletBounds);
if (!attrs.center && !attrs.lfCenter) {
map.fitBounds(leafletBounds);
}
});
});
},
};
}]);
angular.module('leaflet-directive').directive('paths', ["$log", "$q", "leafletData", "leafletMapDefaults", "leafletHelpers", "leafletPathsHelpers", "leafletPathEvents", function($log, $q, leafletData, leafletMapDefaults, leafletHelpers, leafletPathsHelpers, leafletPathEvents) {
return {
restrict: 'A',
scope: false,
replace: false,
require: ['leaflet', '?layers'],
link: function(scope, element, attrs, controller) {
var mapController = controller[0];
var isDefined = leafletHelpers.isDefined;
var isString = leafletHelpers.isString;
var leafletScope = mapController.getLeafletScope();
var paths = leafletScope.paths;
var createPath = leafletPathsHelpers.createPath;
var bindPathEvents = leafletPathEvents.bindPathEvents;
var setPathOptions = leafletPathsHelpers.setPathOptions;
mapController.getMap().then(function(map) {
var defaults = leafletMapDefaults.getDefaults(attrs.id);
var getLayers;
// If the layers attribute is used, we must wait until the layers are created
if (isDefined(controller[1])) {
getLayers = controller[1].getLayers;
} else {
getLayers = function() {
var deferred = $q.defer();
deferred.resolve();
return deferred.promise;
};
}
if (!isDefined(paths)) {
return;
}
getLayers().then(function(layers) {
var leafletPaths = {};
leafletData.setPaths(leafletPaths, attrs.id);
// Should we watch for every specific marker on the map?
var shouldWatch = (!isDefined(attrs.watchPaths) || attrs.watchPaths === 'true');
// Function for listening every single path once created
var watchPathFn = function(leafletPath, name) {
var clearWatch = leafletScope.$watch('paths["' + name + '"]', function(pathData, old) {
if (!isDefined(pathData)) {
if (isDefined(old.layer)) {
for (var i in layers.overlays) {
var overlay = layers.overlays[i];
overlay.removeLayer(leafletPath);
}
}
map.removeLayer(leafletPath);
clearWatch();
return;
}
setPathOptions(leafletPath, pathData.type, pathData);
}, true);
};
leafletScope.$watchCollection('paths', function(newPaths) {
// Delete paths (by name) from the array
for (var name in leafletPaths) {
if (!isDefined(newPaths[name])) {
map.removeLayer(leafletPaths[name]);
delete leafletPaths[name];
}
}
// Create the new paths
for (var newName in newPaths) {
if (newName.search('\\$') === 0) {
continue;
}
if (newName.search('-') !== -1) {
$log.error('[AngularJS - Leaflet] The path name "' + newName + '" is not valid. It must not include "-" and a number.');
continue;
}
if (!isDefined(leafletPaths[newName])) {
var pathData = newPaths[newName];
var newPath = createPath(newName, newPaths[newName], defaults);
// bind popup if defined
if (isDefined(newPath) && isDefined(pathData.message)) {
newPath.bindPopup(pathData.message, pathData.popupOptions);
}
// Show label if defined
if (leafletHelpers.LabelPlugin.isLoaded() && isDefined(pathData.label) && isDefined(pathData.label.message)) {
newPath.bindLabel(pathData.label.message, pathData.label.options);
}
// Check if the marker should be added to a layer
if (isDefined(pathData) && isDefined(pathData.layer)) {
if (!isString(pathData.layer)) {
$log.error('[AngularJS - Leaflet] A layername must be a string');
continue;
}
if (!isDefined(layers)) {
$log.error('[AngularJS - Leaflet] You must add layers to the directive if the markers are going to use this functionality.');
continue;
}
if (!isDefined(layers.overlays) || !isDefined(layers.overlays[pathData.layer])) {
$log.error('[AngularJS - Leaflet] A path can only be added to a layer of type "group"');
continue;
}
var layerGroup = layers.overlays[pathData.layer];
if (!(layerGroup instanceof L.LayerGroup || layerGroup instanceof L.FeatureGroup)) {
$log.error('[AngularJS - Leaflet] Adding a path to an overlay needs a overlay of the type "group" or "featureGroup"');
continue;
}
// Listen for changes on the new path
leafletPaths[newName] = newPath;
// The path goes to a correct layer group, so first of all we add it
layerGroup.addLayer(newPath);
if (shouldWatch) {
watchPathFn(newPath, newName);
} else {
setPathOptions(newPath, pathData.type, pathData);
}
} else if (isDefined(newPath)) {
// Listen for changes on the new path
leafletPaths[newName] = newPath;
map.addLayer(newPath);
if (shouldWatch) {
watchPathFn(newPath, newName);
} else {
setPathOptions(newPath, pathData.type, pathData);
}
}
bindPathEvents(attrs.id, newPath, newName, pathData, leafletScope);
}
}
});
});
});
},
};
}]);
angular.module('leaflet-directive').directive('tiles', ["$log", "leafletData", "leafletMapDefaults", "leafletHelpers", function($log, leafletData, leafletMapDefaults, leafletHelpers) {
return {
restrict: 'A',
scope: false,
replace: false,
require: 'leaflet',
link: function(scope, element, attrs, controller) {
var isDefined = leafletHelpers.isDefined;
var leafletScope = controller.getLeafletScope();
var tiles = leafletScope.tiles;
if (!isDefined(tiles) || !isDefined(tiles.url)) {
$log.warn('[AngularJS - Leaflet] The \'tiles\' definition doesn\'t have the \'url\' property.');
return;
}
controller.getMap().then(function(map) {
var defaults = leafletMapDefaults.getDefaults(attrs.id);
var tileLayerObj;
leafletScope.$watch('tiles', function(tiles, oldtiles) {
var tileLayerOptions = defaults.tileLayerOptions;
var tileLayerUrl = defaults.tileLayer;
// If no valid tiles are in the scope, remove the last layer
if (!isDefined(tiles.url) && isDefined(tileLayerObj)) {
map.removeLayer(tileLayerObj);
return;
}
// No leafletTiles object defined yet
if (!isDefined(tileLayerObj)) {
if (isDefined(tiles.options)) {
angular.copy(tiles.options, tileLayerOptions);
}
if (isDefined(tiles.url)) {
tileLayerUrl = tiles.url;
}
if (tiles.type === 'wms') {
tileLayerObj = L.tileLayer.wms(tileLayerUrl, tileLayerOptions);
} else {
tileLayerObj = L.tileLayer(tileLayerUrl, tileLayerOptions);
}
tileLayerObj.addTo(map);
leafletData.setTiles(tileLayerObj, attrs.id);
return;
}
// If the options of the tilelayer is changed, we need to redraw the layer
if (isDefined(tiles.url) && isDefined(tiles.options) &&
(tiles.type !== oldtiles.type || !angular.equals(tiles.options, tileLayerOptions))) {
map.removeLayer(tileLayerObj);
tileLayerOptions = defaults.tileLayerOptions;
angular.copy(tiles.options, tileLayerOptions);
tileLayerUrl = tiles.url;
if (tiles.type === 'wms') {
tileLayerObj = L.tileLayer.wms(tileLayerUrl, tileLayerOptions);
} else {
tileLayerObj = L.tileLayer(tileLayerUrl, tileLayerOptions);
}
tileLayerObj.addTo(map);
leafletData.setTiles(tileLayerObj, attrs.id);
return;
}
// Only the URL of the layer is changed, update the tiles object
if (isDefined(tiles.url)) {
tileLayerObj.setUrl(tiles.url);
}
}, true);
});
},
};
}]);
/*
Create multiple similar directives for watchOptions to support directiveControl
instead. (when watches are disabled)
NgAnnotate does not work here due to the functional creation
*/
['markers', 'geojson'].forEach(function(name) {
angular.module('leaflet-directive').directive(name + 'WatchOptions', [
'$log', '$rootScope', '$q', 'leafletData', 'leafletHelpers',
function($log, $rootScope, $q, leafletData, leafletHelpers) {
var isDefined = leafletHelpers.isDefined,
errorHeader = leafletHelpers.errorHeader,
isObject = leafletHelpers.isObject,
_watchOptions = leafletHelpers.watchOptions;
return {
restrict: 'A',
scope: false,
replace: false,
require: ['leaflet'],
link: function(scope, element, attrs, controller) {
var mapController = controller[0],
leafletScope = mapController.getLeafletScope();
mapController.getMap().then(function() {
if (isDefined(scope[name + 'WatchOptions'])) {
if (isObject(scope[name + 'WatchOptions']))
angular.extend(_watchOptions, scope[name + 'WatchOptions']);
else
$log.error(errorHeader + '[' + name + 'WatchOptions] is not an object');
leafletScope[name + 'WatchOptions'] = _watchOptions;
}
});
},
};
},]);
});
angular.module('leaflet-directive')
.factory('LeafletEventsHelpersFactory', ["$rootScope", "$q", "$log", "leafletHelpers", function($rootScope, $q, $log, leafletHelpers) {
var safeApply = leafletHelpers.safeApply;
var isDefined = leafletHelpers.isDefined;
var isObject = leafletHelpers.isObject;
var isArray = leafletHelpers.isArray;
var errorHeader = leafletHelpers.errorHeader;
var EventsHelper = function(rootBroadcastName, lObjectType) {
this.rootBroadcastName = rootBroadcastName;
$log.debug('LeafletEventsHelpersFactory: lObjectType: ' + lObjectType + 'rootBroadcastName: ' + rootBroadcastName);
//used to path/key out certain properties based on the type , "markers", "geojson"
this.lObjectType = lObjectType;
};
EventsHelper.prototype.getAvailableEvents = function() {return [];};
/*
argument: name: Note this can be a single string or dot notation
Example:
markerModel : {
m1: { lat:_, lon: _}
}
//would yield name of
name = "m1"
If nested:
markerModel : {
cars: {
m1: { lat:_, lon: _}
}
}
//would yield name of
name = "cars.m1"
*/
EventsHelper.prototype.genDispatchEvent = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName, extra) {
var _this = this;
maybeMapId = maybeMapId || '';
if (maybeMapId)
maybeMapId = '.' + maybeMapId;
return function(e) {
var broadcastName = _this.rootBroadcastName + maybeMapId + '.' + eventName;
$log.debug(broadcastName);
_this.fire(leafletScope, broadcastName, logic, e, e.target || lObject, model, name, layerName, extra);
};
};
EventsHelper.prototype.fire = function(scope, broadcastName, logic, event, lObject, model, modelName, layerName) {
// Safely broadcast the event
safeApply(scope, function() {
var toSend = {
leafletEvent: event,
leafletObject: lObject,
modelName: modelName,
model: model,
};
if (isDefined(layerName))
angular.extend(toSend, {layerName: layerName});
if (logic === 'emit') {
scope.$emit(broadcastName, toSend);
} else {
$rootScope.$broadcast(broadcastName, toSend);
}
});
};
EventsHelper.prototype.bindEvents = function(maybeMapId, lObject, name, model, leafletScope, layerName, extra) {
var events = [];
var logic = 'emit';
var _this = this;
if (!isDefined(leafletScope.eventBroadcast)) {
// Backward compatibility, if no event-broadcast attribute, all events are broadcasted
events = this.getAvailableEvents();
} else if (!isObject(leafletScope.eventBroadcast)) {
// Not a valid object
$log.error(errorHeader + 'event-broadcast must be an object check your model.');
} else {
// We have a possible valid object
if (!isDefined(leafletScope.eventBroadcast[_this.lObjectType])) {
// We do not have events enable/disable do we do nothing (all enabled by default)
events = this.getAvailableEvents();
} else if (!isObject(leafletScope.eventBroadcast[_this.lObjectType])) {
// Not a valid object
$log.warn(errorHeader + 'event-broadcast.' + [_this.lObjectType] + ' must be an object check your model.');
} else {
// We have a possible valid map object
// Event propadation logic
if (isDefined(leafletScope.eventBroadcast[this.lObjectType].logic)) {
// We take care of possible propagation logic
if (leafletScope.eventBroadcast[_this.lObjectType].logic !== 'emit' &&
leafletScope.eventBroadcast[_this.lObjectType].logic !== 'broadcast')
$log.warn(errorHeader + 'Available event propagation logic are: \'emit\' or \'broadcast\'.');
}
// Enable / Disable
var eventsEnable = false;
var eventsDisable = false;
if (isDefined(leafletScope.eventBroadcast[_this.lObjectType].enable) &&
isArray(leafletScope.eventBroadcast[_this.lObjectType].enable))
eventsEnable = true;
if (isDefined(leafletScope.eventBroadcast[_this.lObjectType].disable) &&
isArray(leafletScope.eventBroadcast[_this.lObjectType].disable))
eventsDisable = true;
if (eventsEnable && eventsDisable) {
// Both are active, this is an error
$log.warn(errorHeader + 'can not enable and disable events at the same time');
} else if (!eventsEnable && !eventsDisable) {
// Both are inactive, this is an error
$log.warn(errorHeader + 'must enable or disable events');
} else {
// At this point the object is OK, lets enable or disable events
if (eventsEnable) {
// Enable events
leafletScope.eventBroadcast[this.lObjectType].enable.forEach(function(eventName) {
// Do we have already the event enabled?
if (events.indexOf(eventName) !== -1) {
// Repeated event, this is an error
$log.warn(errorHeader + 'This event ' + eventName + ' is already enabled');
} else {
// Does the event exists?
if (_this.getAvailableEvents().indexOf(eventName) === -1) {
// The event does not exists, this is an error
$log.warn(errorHeader + 'This event ' + eventName + ' does not exist');
} else {
// All ok enable the event
events.push(eventName);
}
}
});
} else {
// Disable events
events = this.getAvailableEvents();
leafletScope.eventBroadcast[_this.lObjectType].disable.forEach(function(eventName) {
var index = events.indexOf(eventName);
if (index === -1) {
// The event does not exist
$log.warn(errorHeader + 'This event ' + eventName + ' does not exist or has been already disabled');
} else {
events.splice(index, 1);
}
});
}
}
}
}
events.forEach(function(eventName) {
lObject.on(eventName, _this.genDispatchEvent(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName, extra));
});
return logic;
};
return EventsHelper;
}])
.service('leafletEventsHelpers', ["LeafletEventsHelpersFactory", function(LeafletEventsHelpersFactory) {
return new LeafletEventsHelpersFactory();
}]);
angular.module('leaflet-directive')
.factory('leafletGeoJsonEvents', ["$rootScope", "$q", "$log", "leafletHelpers", "LeafletEventsHelpersFactory", "leafletData", function($rootScope, $q, $log, leafletHelpers,
LeafletEventsHelpersFactory, leafletData) {
var safeApply = leafletHelpers.safeApply;
var EventsHelper = LeafletEventsHelpersFactory;
var GeoJsonEvents = function() {
EventsHelper.call(this, 'leafletDirectiveGeoJson', 'geojson');
};
GeoJsonEvents.prototype = new EventsHelper();
GeoJsonEvents.prototype.genDispatchEvent = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName, extra) {
var base = EventsHelper.prototype.genDispatchEvent.call(this, maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName);
var _this = this;
return function(e) {
if (eventName === 'mouseout') {
if (extra.resetStyleOnMouseout) {
leafletData.getGeoJSON(extra.mapId)
.then(function(leafletGeoJSON) {
//this is broken on nested needs to traverse or user layerName (nested)
var lobj = layerName ? leafletGeoJSON[layerName] : leafletGeoJSON;
lobj.resetStyle(e.target);
});
}
safeApply(leafletScope, function() {
$rootScope.$broadcast(_this.rootBroadcastName + '.mouseout', e);
});
}
base(e); //common
};
};
GeoJsonEvents.prototype.getAvailableEvents = function() { return [
'click',
'dblclick',
'mouseover',
'mouseout',
];
};
return new GeoJsonEvents();
}]);
angular.module('leaflet-directive')
.factory('leafletLabelEvents', ["$rootScope", "$q", "$log", "leafletHelpers", "LeafletEventsHelpersFactory", function($rootScope, $q, $log, leafletHelpers, LeafletEventsHelpersFactory) {
var Helpers = leafletHelpers;
var EventsHelper = LeafletEventsHelpersFactory;
var LabelEvents = function() {
EventsHelper.call(this, 'leafletDirectiveLabel', 'markers');
};
LabelEvents.prototype = new EventsHelper();
LabelEvents.prototype.genDispatchEvent = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName) {
var markerName = name.replace('markers.', '');
return EventsHelper.prototype
.genDispatchEvent.call(this, maybeMapId, eventName, logic, leafletScope, lObject, markerName, model, layerName);
};
LabelEvents.prototype.getAvailableEvents = function() {
return [
'click',
'dblclick',
'mousedown',
'mouseover',
'mouseout',
'contextmenu',
];
};
LabelEvents.prototype.genEvents = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName) {
var _this = this;
var labelEvents = this.getAvailableEvents();
var scopeWatchName = Helpers.getObjectArrayPath('markers.' + name);
labelEvents.forEach(function(eventName) {
lObject.label.on(eventName, _this.genDispatchEvent(
maybeMapId, eventName, logic, leafletScope, lObject.label, scopeWatchName, model, layerName));
});
};
LabelEvents.prototype.bindEvents = function() {};
return new LabelEvents();
}]);
angular.module('leaflet-directive')
.factory('leafletMapEvents', ["$rootScope", "$q", "$log", "leafletHelpers", "leafletEventsHelpers", "leafletIterators", function($rootScope, $q, $log, leafletHelpers, leafletEventsHelpers, leafletIterators) {
var isDefined = leafletHelpers.isDefined;
var fire = leafletEventsHelpers.fire;
var _getAvailableMapEvents = function() {
return [
'click',
'dblclick',
'mousedown',
'mouseup',
'mouseover',
'mouseout',
'mousemove',
'contextmenu',
'focus',
'blur',
'preclick',
'load',
'unload',
'viewreset',
'movestart',
'move',
'moveend',
'dragstart',
'drag',
'dragend',
'zoomstart',
'zoomanim',
'zoomend',
'zoomlevelschange',
'resize',
'autopanstart',
'layeradd',
'layerremove',
'baselayerchange',
'overlayadd',
'overlayremove',
'locationfound',
'locationerror',
'popupopen',
'popupclose',
'draw:created',
'draw:edited',
'draw:deleted',
'draw:drawstart',
'draw:drawstop',
'draw:editstart',
'draw:editstop',
'draw:deletestart',
'draw:deletestop',
];
};
var _genDispatchMapEvent = function(scope, eventName, logic, maybeMapId) {
if (maybeMapId)
maybeMapId = maybeMapId + '.';
return function(e) {
// Put together broadcast name
var broadcastName = 'leafletDirectiveMap.' + maybeMapId + eventName;
$log.debug(broadcastName);
// Safely broadcast the event
fire(scope, broadcastName, logic, e, e.target, scope);
};
};
var _notifyCenterChangedToBounds = function(scope) {
scope.$broadcast('boundsChanged');
};
var _notifyCenterUrlHashChanged = function(scope, map, attrs, search) {
if (!isDefined(attrs.urlHashCenter)) {
return;
}
var center = map.getCenter();
var centerUrlHash = (center.lat).toFixed(4) + ':' + (center.lng).toFixed(4) + ':' + map.getZoom();
if (!isDefined(search.c) || search.c !== centerUrlHash) {
//$log.debug("notified new center...");
scope.$emit('centerUrlHash', centerUrlHash);
}
};
var _addEvents = function(map, mapEvents, contextName, scope, logic) {
leafletIterators.each(mapEvents, function(eventName) {
var context = {};
context[contextName] = eventName;
map.on(eventName, _genDispatchMapEvent(scope, eventName, logic, map._container.id || ''), context);
});
};
return {
getAvailableMapEvents: _getAvailableMapEvents,
genDispatchMapEvent: _genDispatchMapEvent,
notifyCenterChangedToBounds: _notifyCenterChangedToBounds,
notifyCenterUrlHashChanged: _notifyCenterUrlHashChanged,
addEvents: _addEvents,
};
}]);
angular.module('leaflet-directive')
.factory('leafletMarkerEvents', ["$rootScope", "$q", "$log", "leafletHelpers", "LeafletEventsHelpersFactory", "leafletLabelEvents", function($rootScope, $q, $log, leafletHelpers, LeafletEventsHelpersFactory, leafletLabelEvents) {
var safeApply = leafletHelpers.safeApply;
var isDefined = leafletHelpers.isDefined;
var Helpers = leafletHelpers;
var lblHelp = leafletLabelEvents;
var EventsHelper = LeafletEventsHelpersFactory;
var MarkerEvents = function() {
EventsHelper.call(this, 'leafletDirectiveMarker', 'markers');
};
MarkerEvents.prototype = new EventsHelper();
MarkerEvents.prototype.genDispatchEvent = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName) {
var handle = EventsHelper.prototype
.genDispatchEvent.call(this, maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName);
return function(e) {
// Broadcast old marker click name for backwards compatibility
if (eventName === 'click') {
safeApply(leafletScope, function() {
$rootScope.$broadcast('leafletDirectiveMarkersClick', name);
});
} else if (eventName === 'dragend') {
safeApply(leafletScope, function() {
model.lat = lObject.getLatLng().lat;
model.lng = lObject.getLatLng().lng;
});
if (model.message && model.focus === true) {
lObject.openPopup();
}
}
handle(e); //common
};
};
MarkerEvents.prototype.getAvailableEvents = function() { return [
'click',
'dblclick',
'mousedown',
'mouseover',
'mouseout',
'contextmenu',
'dragstart',
'drag',
'dragend',
'move',
'remove',
'popupopen',
'popupclose',
'touchend',
'touchstart',
'touchmove',
'touchcancel',
'touchleave',
];
};
MarkerEvents.prototype.bindEvents = function(maybeMapId, lObject, name, model, leafletScope, layerName) {
var logic = EventsHelper.prototype.bindEvents.call(this, maybeMapId, lObject, name, model, leafletScope, layerName);
if (Helpers.LabelPlugin.isLoaded() && isDefined(lObject.label)) {
lblHelp.genEvents(maybeMapId, name, logic, leafletScope, lObject, model, layerName);
}
};
return new MarkerEvents();
}]);
angular.module('leaflet-directive')
.factory('leafletPathEvents', ["$rootScope", "$q", "$log", "leafletHelpers", "leafletLabelEvents", "leafletEventsHelpers", function($rootScope, $q, $log, leafletHelpers, leafletLabelEvents, leafletEventsHelpers) {
var isDefined = leafletHelpers.isDefined;
var isObject = leafletHelpers.isObject;
var Helpers = leafletHelpers;
var errorHeader = leafletHelpers.errorHeader;
var lblHelp = leafletLabelEvents;
var fire = leafletEventsHelpers.fire;
/*
TODO (nmccready) This EventsHelper needs to be derrived from leafletEventsHelpers to elminate copy and paste code.
*/
var _genDispatchPathEvent = function(maybeMapId, eventName, logic, leafletScope, lObject, name, model, layerName) {
maybeMapId = maybeMapId || '';
if (maybeMapId)
maybeMapId = '.' + maybeMapId;
return function(e) {
var broadcastName = 'leafletDirectivePath' + maybeMapId + '.' + eventName;
$log.debug(broadcastName);
fire(leafletScope, broadcastName, logic, e, e.target || lObject, model, name, layerName);
};
};
var _bindPathEvents = function(maybeMapId, lObject, name, model, leafletScope) {
var pathEvents = [];
var i;
var eventName;
var logic = 'broadcast';
if (!isDefined(leafletScope.eventBroadcast)) {
// Backward compatibility, if no event-broadcast attribute, all events are broadcasted
pathEvents = _getAvailablePathEvents();
} else if (!isObject(leafletScope.eventBroadcast)) {
// Not a valid object
$log.error(errorHeader + 'event-broadcast must be an object check your model.');
} else {
// We have a possible valid object
if (!isDefined(leafletScope.eventBroadcast.path)) {
// We do not have events enable/disable do we do nothing (all enabled by default)
pathEvents = _getAvailablePathEvents();
} else if (isObject(leafletScope.eventBroadcast.paths)) {
// Not a valid object
$log.warn(errorHeader + 'event-broadcast.path must be an object check your model.');
} else {
// We have a possible valid map object
// Event propadation logic
if (leafletScope.eventBroadcast.path.logic !== undefined && leafletScope.eventBroadcast.path.logic !== null) {
// We take care of possible propagation logic
if (leafletScope.eventBroadcast.path.logic !== 'emit' && leafletScope.eventBroadcast.path.logic !== 'broadcast') {
// This is an error
$log.warn(errorHeader + 'Available event propagation logic are: \'emit\' or \'broadcast\'.');
} else if (leafletScope.eventBroadcast.path.logic === 'emit') {
logic = 'emit';
}
}
// Enable / Disable
var pathEventsEnable = false;
var pathEventsDisable = false;
if (leafletScope.eventBroadcast.path.enable !== undefined && leafletScope.eventBroadcast.path.enable !== null) {
if (typeof leafletScope.eventBroadcast.path.enable === 'object') {
pathEventsEnable = true;
}
}
if (leafletScope.eventBroadcast.path.disable !== undefined && leafletScope.eventBroadcast.path.disable !== null) {
if (typeof leafletScope.eventBroadcast.path.disable === 'object') {
pathEventsDisable = true;
}
}
if (pathEventsEnable && pathEventsDisable) {
// Both are active, this is an error
$log.warn(errorHeader + 'can not enable and disable events at the same time');
} else if (!pathEventsEnable && !pathEventsDisable) {
// Both are inactive, this is an error
$log.warn(errorHeader + 'must enable or disable events');
} else {
// At this point the path object is OK, lets enable or disable events
if (pathEventsEnable) {
// Enable events
for (i = 0; i < leafletScope.eventBroadcast.path.enable.length; i++) {
eventName = leafletScope.eventBroadcast.path.enable[i];
// Do we have already the event enabled?
if (pathEvents.indexOf(eventName) !== -1) {
// Repeated event, this is an error
$log.warn(errorHeader + 'This event ' + eventName + ' is already enabled');
} else {
// Does the event exists?
if (_getAvailablePathEvents().indexOf(eventName) === -1) {
// The event does not exists, this is an error
$log.warn(errorHeader + 'This event ' + eventName + ' does not exist');
} else {
// All ok enable the event
pathEvents.push(eventName);
}
}
}
} else {
// Disable events
pathEvents = _getAvailablePathEvents();
for (i = 0; i < leafletScope.eventBroadcast.path.disable.length; i++) {
eventName = leafletScope.eventBroadcast.path.disable[i];
var index = pathEvents.indexOf(eventName);
if (index === -1) {
// The event does not exist
$log.warn(errorHeader + 'This event ' + eventName + ' does not exist or has been already disabled');
} else {
pathEvents.splice(index, 1);
}
}
}
}
}
}
for (i = 0; i < pathEvents.length; i++) {
eventName = pathEvents[i];
lObject.on(eventName, _genDispatchPathEvent(maybeMapId, eventName, logic, leafletScope, pathEvents, name));
}
if (Helpers.LabelPlugin.isLoaded() && isDefined(lObject.label)) {
lblHelp.genEvents(maybeMapId, name, logic, leafletScope, lObject, model);
}
};
var _getAvailablePathEvents = function() {
return [
'click',
'dblclick',
'mousedown',
'mouseover',
'mouseout',
'contextmenu',
'add',
'remove',
'popupopen',
'popupclose',
];
};
return {
getAvailablePathEvents: _getAvailablePathEvents,
bindPathEvents: _bindPathEvents,
};
}]);
}(angular));