Improve icon handling.

This commit is contained in:
David Molineus
2015-01-07 09:32:14 +01:00
parent 6d453ffab9
commit df7b8d3c94
4 changed files with 121 additions and 21 deletions

View File

@@ -1,10 +1,31 @@
L.Contao = L.Class.extend( {
includes: L.Mixin.Events,
/**
* The map registry.
*/
maps: {},
/**
* The icons registry.
*/
icons: {},
/**
* Initialize Contao leaflet integration.
*/
initialize: function() {
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
},
/**
* Add map to map registry.
*
* @param id The map id.
* @param map The map object.
*
* @returns {L.Contao}
*/
addMap: function (id, map) {
this.maps[id] = map;
@@ -13,6 +34,13 @@ L.Contao = L.Class.extend( {
return this;
},
/**
* Get a map from the icon map. Returns null if not set.
*
* @param id The mapobject
*
* @returns {L.Map}|{*}
*/
getMap: function (id) {
if (typeof (this.maps[id]) === 'undefined') {
return null;
@@ -21,6 +49,14 @@ L.Contao = L.Class.extend( {
return this.maps[id];
},
/**
* Add an icon to the icon registry.
*
* @param id The icon id.
* @param icon The icon object.
*
* @returns {L.Contao}
*/
addIcon: function(id, icon) {
this.icons[id] = icon;
this.fire('icon:added', { id: id, icon: icon});
@@ -28,6 +64,27 @@ L.Contao = L.Class.extend( {
return this;
},
/**
* Load icon definitions.
*
* @param icons List of icon definitions.
*
* @return void
*/
loadIcons: function(icons) {
for (var i = 0; i < icons.length; i++) {
var icon = L[icons[i].type](icons[i].options);
this.addIcon(icons[i].id, icon);
}
},
/**
* Get an icon by its id.
*
* @param id Icon id.
*
* @returns {L.Icon}|{L.DivIcon}|{*}
*/
getIcon: function(id) {
if (typeof (this.icons[id]) === 'undefined') {
return null;
@@ -36,6 +93,14 @@ L.Contao = L.Class.extend( {
return this.icons[id];
},
/**
* Point to layer callback. Adds a geo json point to the layer.
*
* @param feature The geo json feature.
* @param latlng The converted latlng.
*
* @returns {L.Marker}|{*}
*/
pointToLayer: function(feature, latlng) {
var marker = L.marker(latlng, feature.properties.options);
@@ -53,6 +118,12 @@ L.Contao = L.Class.extend( {
return marker;
},
/**
* Apply feature methods.
*
* @param obj
* @param feature
*/
applyFeatureMethods: function(obj, feature) {
if (feature.properties && feature.properties.methods) {
for (var i=0; i < feature.properties.methods.length; i++) {
@@ -64,6 +135,4 @@ L.Contao = L.Class.extend( {
}
});
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
window.ContaoLeaflet = new L.Contao();