2015-01-20 09:39:15 +01:00
/ * *
* Leaflet integration into contao .
*
* This class provides some helpers for loading layer data manages maps and map objects .
* /
L . Contao = L . Class . extend ( {
2014-12-29 12:17:40 +01:00
includes : L . Mixin . Events ,
2015-01-22 16:09:54 +01:00
statics : {
/ * *
* Contao extension attribution .
*
* You are not allowed to remove or change it . Contact me if you want to buy an removal license .
* /
2016-10-05 14:10:27 +02:00
ATTRIBUTION : ' | <a href="https://netzmacht.de/contao-leaflet" title="Powered by Leaflet extension for Contao CMS developed by netzmacht David Molineus">netzmacht</a>'
2015-01-22 16:09:54 +01:00
} ,
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 .
* /
2015-01-20 09:39:15 +01:00
initialize : function ( ) {
2016-10-05 09:25:01 +02:00
L . Icon . Default . imagePath = 'assets/leaflet/libs/leaflet/images/' ;
2015-01-08 11:01:22 +01:00
2015-01-12 09:37:43 +01:00
this . setGeoJsonListeners ( L . GeoJSON ) ;
2015-01-07 09:32:14 +01:00
} ,
/ * *
* Add map to map registry .
*
* @ param id The map id .
* @ param map The map object .
*
2015-01-15 13:01:58 +01:00
* @ returns { L . contao }
2015-01-07 09:32:14 +01:00
* /
2014-12-29 12:17:40 +01:00
addMap : function ( id , map ) {
this . maps [ id ] = map ;
2015-01-20 09:39:15 +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 .
*
2015-01-15 13:01:58 +01:00
* @ returns { L . contao }
2015-01-07 09:32:14 +01:00
* /
2015-01-20 09:39:15 +01:00
addIcon : function ( id , icon ) {
2015-01-06 21:30:57 +01:00
this . icons [ id ] = icon ;
2015-01-20 09:39:15 +01:00
this . fire ( 'icon:added' , { id : id , icon : icon } ) ;
2015-01-06 21:30:57 +01:00
return this ;
} ,
2015-01-07 09:32:14 +01:00
/ * *
* Load icon definitions .
*
* @ param icons List of icon definitions .
*
* @ return void
* /
2015-01-20 09:39:15 +01:00
loadIcons : function ( icons ) {
2015-01-07 09:32:14 +01:00
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-20 09:39:15 +01:00
getIcon : function ( id ) {
2015-01-06 21:30:57 +01:00
if ( typeof ( this . icons [ id ] ) === 'undefined' ) {
return null ;
}
return this . icons [ id ] ;
} ,
2015-01-13 14:54:23 +01:00
/ * *
2016-11-14 09:10:31 +01:00
* Load data from an url into a layer using omnivore .
2015-01-13 14:54:23 +01:00
*
2015-01-15 12:45:23 +01:00
* @ param hash The leaflet url hash .
2015-01-13 14:54:23 +01:00
* @ param type The response content format .
* @ param options Parser options
* @ param customLayer optional custom layer .
* @ param map Pass a map object so that the data loading events are passed to the map .
* /
2015-01-20 09:39:15 +01:00
load : function ( hash , type , options , customLayer , map ) {
2015-01-27 00:02:17 +01:00
var url = this . createRequestUrl ( hash , map ) ,
2015-01-21 14:24:22 +01:00
layer = omnivore [ type ] ( url , options , customLayer ) ;
2015-01-13 14:54:23 +01:00
2015-01-15 00:53:19 +01:00
if ( map ) {
2015-01-15 01:10:36 +01:00
// Required because Control.Loading tries to get _leafet_id which is created here.
L . stamp ( layer ) ;
2016-11-14 09:10:31 +01:00
// Add listener for map bounds changes.
2015-01-27 00:02:17 +01:00
if ( map . options . dynamicLoad && layer . options . boundsMode == 'fit' ) {
layer . options . requestHash = hash ;
map . on ( 'moveend' , layer . refreshData , layer ) ;
map . on ( 'layerremove' , function ( e ) {
if ( e . layer === layer ) {
map . off ( 'moveend' , layer . updateBounds , layer ) ;
}
} ) ;
}
2015-01-20 09:39:15 +01:00
map . fire ( 'dataloading' , { layer : layer } ) ;
2015-01-13 14:54:23 +01:00
2015-01-20 09:39:15 +01:00
layer . on ( 'ready' , function ( ) {
map . calculateFeatureBounds ( layer ) ;
map . fire ( 'dataload' , { layer : layer } ) ;
2015-01-15 00:53:19 +01:00
} ) ;
2015-01-20 09:39:15 +01:00
layer . on ( 'error' , function ( ) {
map . fire ( 'dataload' , { layer : layer } ) ;
2015-01-15 00:53:19 +01:00
} ) ;
}
2015-01-13 14:54:23 +01:00
return layer ;
} ,
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-20 09:39:15 +01:00
pointToLayer : function ( feature , latlng ) {
2015-01-27 00:02:17 +01:00
var type = 'marker' ;
2015-01-07 17:59:56 +01:00
var marker = null ;
2015-01-06 18:49:22 +01:00
2015-01-07 10:33:35 +01:00
if ( feature . properties ) {
2015-01-20 09:39:15 +01:00
feature . properties . bounds = true ;
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 ) ;
2015-01-21 18:21:28 +01:00
2015-01-07 17:59:56 +01:00
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-20 09:39:15 +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 ) ;
2015-01-20 09:39:15 +01:00
this . fire ( 'feature:added' , { feature : feature , layer : layer } ) ;
2015-01-07 17:59:56 +01:00
}
} ,
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 ) {
2015-01-27 17:14:04 +01:00
obj . bindPopup ( feature . properties . popup , feature . properties . popupOptions ) ;
2015-01-07 10:33:35 +01:00
} else if ( feature . properties . popupContent ) {
2015-01-27 17:14:04 +01:00
obj . bindPopup ( feature . properties . popupContent , feature . properties . popupOptions ) ;
2015-01-06 18:49:22 +01:00
}
}
2015-01-12 09:37:43 +01:00
} ,
/ * *
* Set the default geojson listeners to the prototype .
*
* @ param obj
* /
2015-01-20 09:39:15 +01:00
setGeoJsonListeners : function ( obj ) {
2015-01-12 09:37:43 +01:00
if ( obj && obj . prototype ) {
obj . prototype . options = {
pointToLayer : this . pointToLayer . bind ( this ) ,
onEachFeature : this . onEachFeature . bind ( this )
} ;
}
2015-01-15 12:45:23 +01:00
} ,
/ * *
* Create request url by appending the hash to the current url .
*
2015-01-27 00:02:17 +01:00
* @ param { string } value The hash .
* @ param { L . Map } map The map .
2015-01-15 12:45:23 +01:00
*
* @ returns { string }
* /
2015-01-27 00:02:17 +01:00
createRequestUrl : function ( value , map ) {
var bounds ,
key = 'leaflet' ,
params = document . location . search . substr ( 1 ) . split ( '&' ) ;
2015-01-15 12:45:23 +01:00
2015-01-27 00:02:17 +01:00
value = encodeURIComponent ( value ) ;
2015-01-15 12:45:23 +01:00
if ( params == '' ) {
2015-01-27 00:02:17 +01:00
value = document . location . pathname + '?' + [ key , value ] . join ( '=' ) ;
2015-01-15 12:45:23 +01:00
} else {
2015-01-20 09:39:15 +01:00
var i = params . length ;
var x ;
while ( i -- ) {
2015-01-15 12:45:23 +01:00
x = params [ i ] . split ( '=' ) ;
if ( x [ 0 ] == key ) {
x [ 1 ] = value ;
params [ i ] = x . join ( '=' ) ;
break ;
}
}
if ( i < 0 ) {
params [ params . length ] = [ key , value ] . join ( '=' ) ;
}
2015-01-28 16:05:55 +01:00
value = document . location . pathname + '?' + params . join ( '&' ) ;
2015-01-15 12:45:23 +01:00
}
2015-01-27 00:02:17 +01:00
if ( map ) {
if ( map . options . dynamicLoad ) {
bounds = map . getBounds ( ) ;
value += '&f=bbox&v=' ;
value += bounds . getSouth ( ) + ',' + bounds . getWest ( ) ;
value += ',' + bounds . getNorth ( ) + ',' + bounds . getEast ( ) ;
}
}
return value ;
2014-12-29 12:17:40 +01:00
}
2015-01-20 09:39:15 +01:00
} ) ;
2015-01-22 16:09:54 +01:00
/ * *
* Start Contao integration .
* /
2015-01-20 09:39:15 +01:00
L . contao = new L . Contao ( ) ;