-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new service for the paths helpers
- Loading branch information
1 parent
4fbb715
commit 337abba
Showing
2 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
angular.module("leaflet-directive").factory('leafletPathHelpers', function ($rootScope, $log, leafletHelpers) { | ||
var isDefined = leafletHelpers.isDefined, | ||
isArray = leafletHelpers.isArray, | ||
isNumber = leafletHelpers.isNumber, | ||
isValidPoint = leafletHelpers.isValidPoint; | ||
|
||
function _convertToLeafletLatLngs(latlngs) { | ||
return latlngs.filter(function(latlng) { | ||
return !!latlng.lat && !!latlng.lng; | ||
}).map(function (latlng) { | ||
return new L.LatLng(latlng.lat, latlng.lng); | ||
}); | ||
} | ||
|
||
function _convertToLeafletLatLng(latlng) { | ||
return new L.LatLng(latlng.lat, latlng.lng); | ||
} | ||
|
||
function _convertToLeafletMultiLatLngs(paths) { | ||
return paths.map(function(latlngs) { | ||
return _convertToLeafletLatLngs(latlngs); | ||
}); | ||
} | ||
|
||
function _getOptions(path, defaults) { | ||
var options = { | ||
weight: defaults.path.weight, | ||
color: defaults.path.color, | ||
opacity: defaults.path.opacity | ||
}; | ||
|
||
if(isDefined(path.stroke)) { | ||
options.stroke = path.stroke; | ||
} | ||
if(isDefined(path.fill)) { | ||
options.fill = path.fill; | ||
} | ||
if(isDefined(path.fillColor)) { | ||
options.fillColor = path.fillColor; | ||
} | ||
if(isDefined(path.fillOpacity)) { | ||
options.fillOpacity = path.fillOpacity; | ||
} | ||
if(isDefined(path.smoothFactor)) { | ||
options.smoothFactor = path.smoothFactor; | ||
} | ||
if(isDefined(path.noClip)) { | ||
options.noClip = path.noClip; | ||
} | ||
|
||
return options; | ||
} | ||
|
||
var _updatePathOptions = function(path, data) { | ||
if (isDefined(data.weight)) { | ||
path.setStyle({ weight: data.weight }); | ||
} | ||
|
||
if (isDefined(data.color)) { | ||
path.setStyle({ color: data.color }); | ||
} | ||
|
||
if (isDefined(data.opacity)) { | ||
path.setStyle({ opacity: data.opacity }); | ||
} | ||
}; | ||
|
||
var _isValidPolyline = function(latlngs) { | ||
if (!isArray(latlngs)) { | ||
return false; | ||
} | ||
for (var i in latlngs) { | ||
var point = latlngs[i]; | ||
if (!isValidPoint(point)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
var pathTypes = { | ||
polyline: { | ||
isValid: function(pathData) { | ||
var latlngs = pathData.latlngs; | ||
return _isValidPolyline(latlngs); | ||
}, | ||
createPath: function(options) { | ||
return new L.Polyline([], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLngs(_convertToLeafletLatLngs(data.latlngs)); | ||
_updatePathOptions(path, data); | ||
return; | ||
} | ||
}, | ||
multiPolyline: { | ||
isValid: function(pathData) { | ||
var latlngs = pathData.latlngs; | ||
if (!isArray(latlngs) || latlngs.length !== 2) { | ||
return false; | ||
} | ||
|
||
for (var i in latlngs) { | ||
var polyline = latlngs[i]; | ||
if (!_isValidPolyline(polyline)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
createPath: function(options) { | ||
return new L.multiPolyline([[[0,0],[1,1]]], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLngs(_convertToLeafletMultiLatLngs(data.latlngs)); | ||
_updatePathOptions(path, data); | ||
return; | ||
} | ||
} , | ||
polygon: { | ||
isValid: function(pathData) { | ||
var latlngs = pathData.latlngs; | ||
return _isValidPolyline(latlngs); | ||
}, | ||
createPath: function(options) { | ||
return new L.Polygon([], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLngs(_convertToLeafletLatLngs(data.latlngs)); | ||
_updatePathOptions(path, data); | ||
return; | ||
} | ||
}, | ||
multiPolygon: { | ||
isValid: function(pathData) { | ||
var latlngs = pathData.latlngs; | ||
|
||
if (!isArray(latlngs) || latlngs.length !== 2) { | ||
return false; | ||
} | ||
|
||
for (var i in latlngs) { | ||
var polyline = latlngs[i]; | ||
if (!_isValidPolyline(polyline)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
createPath: function(options) { | ||
return new L.MultiPolygon([[[0,0],[1,1],[0,1]]], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLngs(_convertToLeafletMultiLatLngs(data.latlngs)); | ||
_updatePathOptions(path, data); | ||
return; | ||
} | ||
}, | ||
rectangle: { | ||
isValid: function(pathData) { | ||
var latlngs = pathData.latlngs; | ||
|
||
if (!isArray(latlngs) || latlngs.length !== 2) { | ||
return false; | ||
} | ||
|
||
for (var i in latlngs) { | ||
var point = latlngs[i]; | ||
if (!isValidPoint(point)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
createPath: function(options) { | ||
return new L.Rectangle([[0,0],[1,1]], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setBounds(new L.LatLngBounds(_convertToLeafletLatLngs(data.latlngs))); | ||
_updatePathOptions(path, data); | ||
} | ||
}, | ||
circle: { | ||
isValid: function(pathData) { | ||
var point= pathData.latlngs; | ||
return isValidPoint(point) && isNumber(pathData.radius); | ||
}, | ||
createPath: function(options) { | ||
return new L.Circle([0,0], 1, options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLng(_convertToLeafletLatLng(data.latlngs)); | ||
if (isDefined(data.radius)) { | ||
path.setRadius(data.radius); | ||
} | ||
_updatePathOptions(path, data); | ||
} | ||
}, | ||
circleMarker: { | ||
isValid: function(pathData) { | ||
var point= pathData.latlngs; | ||
return isValidPoint(point) && isNumber(pathData.radius); | ||
}, | ||
createPath: function(options) { | ||
return new L.CircleMarker([0,0], options); | ||
}, | ||
setPath: function(path, data) { | ||
path.setLatLng(_convertToLeafletLatLng(data.latlngs)); | ||
if (isDefined(data.radius)) { | ||
path.setRadius(data.radius); | ||
} | ||
_updatePathOptions(path, data); | ||
} | ||
} | ||
}; | ||
|
||
var _getPathData = function(path) { | ||
var pathData = {}; | ||
if (path.latlngs) { | ||
pathData.latlngs = path.latlngs; | ||
} | ||
|
||
if (path.radius) { | ||
pathData.radius = path.radius; | ||
} | ||
|
||
return pathData; | ||
}; | ||
|
||
return { | ||
setPathOptions: function(leafletPath, pathType, data) { | ||
pathTypes[pathType].setPath(leafletPath, data); | ||
}, | ||
createPath: function(name, path, defaults) { | ||
if(!isDefined(path.type)) { | ||
path.type = "polyline"; | ||
} | ||
var options = _getOptions(path, defaults); | ||
var pathData = _getPathData(path); | ||
|
||
if (!pathTypes[path.type].isValid(pathData)) { | ||
$log.error("[AngularJS - Leaflet] Invalid data passed to the " + path.type + " path"); | ||
return; | ||
} | ||
|
||
return pathTypes[path.type].createPath(options); | ||
} | ||
}; | ||
}); |