Files
contao-leaflet-maps/assets/maps/contao-leaflet.js

205 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-01-08 15:21:45 +01:00
L.Contao = L.Class.extend({
2014-12-29 12:17:40 +01:00
includes: L.Mixin.Events,
2015-01-08 15:21:45 +01:00
/**
* Contao extension attribution.
*
* You are not allowed to remove or change it. Contact me if you want to buy an removal license.
*/
attribution: ' | <a href="http://contao-leaflet.netzmacht.de/" title="Leaflet extension for Contao CMS">netzmacht <em>creative</em></a>',
2015-01-08 15:21:45 +01:00
2015-01-07 09:32:14 +01:00
/**
* The map registry.
*/
2014-12-29 12:17:40 +01:00
maps: {},
2015-01-07 09:32:14 +01:00
/**
* The icons registry.
*/
2015-01-06 21:30:57 +01:00
icons: {},
2015-01-07 09:32:14 +01:00
/**
* Initialize Contao leaflet integration.
*/
initialize: function() {
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
2015-01-08 11:01:22 +01:00
// Bind triggered data:loading and data:loaded events to the map so that the loading indicator
// is aware of that. Dataloading and dataloaded are the default events which leaflet uses but leaflet.ajax not.
L.Map.addInitHook(function () {
var map = this;
this.on('layeradd', function(e) {
2015-01-09 15:24:34 +01:00
if (e.layer.on) {
e.layer.on('data:loading', function() { map.fire('dataloading'); });
e.layer.on('data:loaded', function() { map.fire('dataload'); });
}
2015-01-08 11:01:22 +01:00
});
});
2015-01-09 23:02:09 +01:00
if (L.GeoJSON.AJAX) {
// Set default pointToLayer and onEachFeature handler.
L.GeoJSON.AJAX.prototype.options = {
pointToLayer: this.pointToLayer.bind(this),
onEachFeature: this.onEachFeature.bind(this)
};
}
2015-01-07 09:32:14 +01:00
},
/**
* Add map to map registry.
*
* @param id The map id.
* @param map The map object.
*
* @returns {L.Contao}
*/
2014-12-29 12:17:40 +01:00
addMap: function (id, map) {
map.map.attributionControl.setPrefix(map.map.attributionControl.options.prefix + this.attribution);
2014-12-29 12:17:40 +01:00
this.maps[id] = map;
2015-01-06 18:49:22 +01:00
this.fire('map:added', { id: id, map: map});
2014-12-29 12:17:40 +01:00
return this;
},
2015-01-07 09:32:14 +01:00
/**
* Get a map from the icon map. Returns null if not set.
*
* @param id The mapobject
*
* @returns {L.Map}|{*}
*/
2014-12-29 12:17:40 +01:00
getMap: function (id) {
2015-01-06 14:55:12 +01:00
if (typeof (this.maps[id]) === 'undefined') {
2014-12-29 12:17:40 +01:00
return null;
}
2015-01-06 14:55:12 +01:00
return this.maps[id];
2015-01-06 18:49:22 +01:00
},
2015-01-07 09:32:14 +01:00
/**
* Add an icon to the icon registry.
*
* @param id The icon id.
* @param icon The icon object.
*
* @returns {L.Contao}
*/
2015-01-06 21:30:57 +01:00
addIcon: function(id, icon) {
this.icons[id] = icon;
this.fire('icon:added', { id: id, icon: icon});
return this;
},
2015-01-07 09:32:14 +01:00
/**
* 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}|{*}
*/
2015-01-06 21:30:57 +01:00
getIcon: function(id) {
if (typeof (this.icons[id]) === 'undefined') {
return null;
}
return this.icons[id];
},
2015-01-07 09:32:14 +01:00
/**
* 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}|{*}
*/
2015-01-06 18:49:22 +01:00
pointToLayer: function(feature, latlng) {
2015-01-07 17:59:56 +01:00
var type = 'marker';
var marker = null;
2015-01-06 18:49:22 +01:00
2015-01-07 10:33:35 +01:00
if (feature.properties) {
2015-01-07 17:59:56 +01:00
if (feature.properties.type) {
type = feature.properties.type;
}
// constructor arguments given, use them.
if (feature.properties.arguments) {
marker = L[type].apply(L[type], feature.properties.arguments);
L.Util.setOptions(marker, feature.properties.options);
}
}
if (marker === null) {
marker = L[type](latlng, feature.properties.options);
}
if (feature.properties) {
if (feature.properties.radius) {
marker.setRadius(feature.properties.radius);
}
2015-01-07 10:33:35 +01:00
if (feature.properties.icon) {
var icon = this.getIcon(feature.properties.icon);
2015-01-06 21:30:57 +01:00
2015-01-07 10:33:35 +01:00
if (icon) {
marker.setIcon(icon);
}
2015-01-06 21:30:57 +01:00
}
2015-01-07 10:33:35 +01:00
this.bindPopupFromFeature(marker, feature);
2015-01-06 21:30:57 +01:00
}
2015-01-07 17:59:56 +01:00
this.fire('point:added', { marker: marker, feature: feature, latlng: latlng, type: type });
2015-01-06 18:49:22 +01:00
return marker;
},
2015-01-07 17:59:56 +01:00
onEachFeature: function (feature, layer) {
if (feature.properties) {
L.Util.setOptions(layer, feature.properties.options);
this.bindPopupFromFeature(layer, feature);
this.fire('feature:added', { feature: feature, layer: layer});
}
},
2015-01-07 09:32:14 +01:00
/**
2015-01-07 10:33:35 +01:00
* Bind popup from feature definitions.
*
* It accepts popup or popupContent as property.
2015-01-07 09:32:14 +01:00
*
2015-01-07 10:33:35 +01:00
* @param obj The object
* @param feature The geo json feature.
2015-01-07 09:32:14 +01:00
*/
2015-01-07 10:33:35 +01:00
bindPopupFromFeature: function (obj, feature) {
if (feature.properties) {
if (feature.properties.popup) {
obj.bindPopup(feature.properties.popup);
} else if (feature.properties.popupContent) {
obj.bindPopup(feature.properties.popupContent);
2015-01-06 18:49:22 +01:00
}
}
2014-12-29 12:17:40 +01:00
}
});
window.ContaoLeaflet = new L.Contao();