UNPKG

ui.leaflet.webpack

Version:

Webpack version of ui-leaflet (https://github.com/angular-ui/ui-leaflet)

179 lines (153 loc) 8.81 kB
//angular.module('ui-leaflet').directive('paths', /** @ngInject */ module.exports = [ 'leafletLogger', '$q', 'leafletData', 'leafletMapDefaults', 'leafletHelpers', 'leafletPathsHelpers', 'leafletPathEvents', 'leafletWatchHelpers', 'L' ,function (leafletLogger, $q, leafletData, leafletMapDefaults, leafletHelpers, leafletPathsHelpers, leafletPathEvents, leafletWatchHelpers, L ) { var $log = leafletLogger; return { restrict: "A", scope: false, replace: false, require: ['leaflet', '?layers'], link: function(scope, element, attrs, controller) { var mapController = controller[0], isDefined = leafletHelpers.isDefined, isString = leafletHelpers.isString, leafletScope = mapController.getLeafletScope(), paths = leafletScope.paths, createPath = leafletPathsHelpers.createPath, bindPathEvents = leafletPathEvents.bindPathEvents, setPathOptions = leafletPathsHelpers.setPathOptions, maybeWatch = leafletWatchHelpers.maybeWatch; mapController.getMap().then(function(map) { var defaults = leafletMapDefaults.getDefaults(attrs.id), 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; } //legacy behaviour does a watch collection on the paths var _legacyWatchOptions = { type: 'watchCollection', individual: { type: 'watchDeep' } }; var watchOptions; if(leafletScope.watchOptions && leafletScope.watchOptions.paths) { watchOptions = leafletScope.watchOptions.paths; } else { watchOptions = _legacyWatchOptions; } getLayers().then(function(layers) { var leafletPaths = {}; leafletData.setPaths(leafletPaths, attrs.id); // Function for listening every single path once created var watchPathFn = function(leafletPath, name, watchOptions) { var pathWatchPath = "paths[\""+name+"\"]"; maybeWatch(leafletScope, pathWatchPath, watchOptions, function(pathData, old, clearWatch){ 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); }); }; var _clean = 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]; } } }; var _create = function(newPaths){ _clean(newPaths); // 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 (watchOptions.individual.type !== null) { watchPathFn(newPath, newName, watchOptions.individual); } else { setPathOptions(newPath, pathData.type, pathData); } } else if (isDefined(newPath)) { // Listen for changes on the new path leafletPaths[newName] = newPath; map.addLayer(newPath); if (watchOptions.individual.type !== null) { watchPathFn(newPath, newName, watchOptions.individual); } else { setPathOptions(newPath, pathData.type, pathData); } } bindPathEvents(attrs.id, newPath, newName, pathData, leafletScope); } } }; maybeWatch(leafletScope,'paths', watchOptions, function(newPaths){ _create(newPaths); }); }); }); } }; }];