Move Contao integration into a separate file.

This commit is contained in:
David Molineus
2015-01-20 09:39:15 +01:00
parent e83e2c5482
commit f71376a628
2 changed files with 35 additions and 43 deletions

View File

@@ -1,20 +0,0 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
use Netzmacht\Contao\Leaflet\Frontend\DataController;
define('TL_MODE', 'FE');
require(dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])))) . '/system/initialize.php');
$container = $GLOBALS['container'];
$controller = new DataController($container['leaflet.map.service'], $container['input']);
$controller->execute();

View File

@@ -1,4 +1,9 @@
L.contao = new (L.Class.extend({ /**
* Leaflet integration into contao.
*
* This class provides some helpers for loading layer data manages maps and map objects.
*/
L.Contao = L.Class.extend({
includes: L.Mixin.Events, includes: L.Mixin.Events,
/** /**
@@ -21,7 +26,7 @@ L.contao = new (L.Class.extend({
/** /**
* Initialize Contao leaflet integration. * Initialize Contao leaflet integration.
*/ */
initialize: function() { initialize: function () {
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
this.setGeoJsonListeners(L.GeoJSON); this.setGeoJsonListeners(L.GeoJSON);
@@ -40,7 +45,7 @@ L.contao = new (L.Class.extend({
this.maps[id] = map; this.maps[id] = map;
this.fire('map:added', { id: id, map: map}); this.fire('map:added', {id: id, map: map});
return this; return this;
}, },
@@ -68,9 +73,9 @@ L.contao = new (L.Class.extend({
* *
* @returns {L.contao} * @returns {L.contao}
*/ */
addIcon: function(id, icon) { addIcon: function (id, icon) {
this.icons[id] = icon; this.icons[id] = icon;
this.fire('icon:added', { id: id, icon: icon}); this.fire('icon:added', {id: id, icon: icon});
return this; return this;
}, },
@@ -82,7 +87,7 @@ L.contao = new (L.Class.extend({
* *
* @return void * @return void
*/ */
loadIcons: function(icons) { loadIcons: function (icons) {
for (var i = 0; i < icons.length; i++) { for (var i = 0; i < icons.length; i++) {
var icon = L[icons[i].type](icons[i].options); var icon = L[icons[i].type](icons[i].options);
this.addIcon(icons[i].id, icon); this.addIcon(icons[i].id, icon);
@@ -96,7 +101,7 @@ L.contao = new (L.Class.extend({
* *
* @returns {L.Icon}|{L.DivIcon}|{*} * @returns {L.Icon}|{L.DivIcon}|{*}
*/ */
getIcon: function(id) { getIcon: function (id) {
if (typeof (this.icons[id]) === 'undefined') { if (typeof (this.icons[id]) === 'undefined') {
return null; return null;
} }
@@ -113,22 +118,23 @@ L.contao = new (L.Class.extend({
* @param customLayer optional custom layer. * @param customLayer optional custom layer.
* @param map Pass a map object so that the data loading events are passed to the map. * @param map Pass a map object so that the data loading events are passed to the map.
*/ */
load: function(hash, type, options, customLayer, map) { load: function (hash, type, options, customLayer, map) {
var url = this.createRequestUrl(hash); var url = this.createRequestUrl(hash);
var layer = omnivore[type](url, options, customLayer); var layer = omnivore[type](url, options, customLayer);
if (map) { if (map) {
// Required because Control.Loading tries to get _leafet_id which is created here. // Required because Control.Loading tries to get _leafet_id which is created here.
L.stamp(layer); L.stamp(layer);
map.fire('dataloading', { layer: layer }); map.fire('dataloading', {layer: layer});
layer.on('ready', function() { layer.on('ready', function () {
map.fire('dataload', { layer: layer }); map.calculateFeatureBounds(layer);
map.fire('dataload', {layer: layer});
}); });
layer.on('error', function() { layer.on('error', function () {
map.fire('dataload', { layer: layer }); map.fire('dataload', {layer: layer});
}); });
} }
@@ -143,11 +149,13 @@ L.contao = new (L.Class.extend({
* *
* @returns {L.Marker}|{*} * @returns {L.Marker}|{*}
*/ */
pointToLayer: function(feature, latlng) { pointToLayer: function (feature, latlng) {
var type = 'marker'; var type = 'marker';
var marker = null; var marker = null;
if (feature.properties) { if (feature.properties) {
feature.properties.bounds = true;
if (feature.properties.type) { if (feature.properties.type) {
type = feature.properties.type; type = feature.properties.type;
} }
@@ -179,7 +187,7 @@ L.contao = new (L.Class.extend({
this.bindPopupFromFeature(marker, feature); this.bindPopupFromFeature(marker, feature);
} }
this.fire('point:added', { marker: marker, feature: feature, latlng: latlng, type: type }); this.fire('point:added', {marker: marker, feature: feature, latlng: latlng, type: type});
return marker; return marker;
}, },
@@ -190,7 +198,7 @@ L.contao = new (L.Class.extend({
this.bindPopupFromFeature(layer, feature); this.bindPopupFromFeature(layer, feature);
this.fire('feature:added', { feature: feature, layer: layer}); this.fire('feature:added', {feature: feature, layer: layer});
} }
}, },
@@ -217,7 +225,7 @@ L.contao = new (L.Class.extend({
* *
* @param obj * @param obj
*/ */
setGeoJsonListeners: function(obj) { setGeoJsonListeners: function (obj) {
if (obj && obj.prototype) { if (obj && obj.prototype) {
obj.prototype.options = { obj.prototype.options = {
pointToLayer: this.pointToLayer.bind(this), pointToLayer: this.pointToLayer.bind(this),
@@ -233,16 +241,18 @@ L.contao = new (L.Class.extend({
* *
* @returns {string} * @returns {string}
*/ */
createRequestUrl: function(value) { createRequestUrl: function (value) {
value = encodeURIComponent(value); value = encodeURIComponent(value);
var key = 'leaflet'; var key = 'leaflet';
var params = document.location.search.substr(1).split('&'); var params = document.location.search.substr(1).split('&');
if (params == '') { if (params == '') {
return document.location.pathname + '?' + [key, value].join('='); return document.location.pathname + '?' + [key, value].join('=');
} else { } else {
var i = params.length; var x; while (i--) { var i = params.length;
var x;
while (i--) {
x = params[i].split('='); x = params[i].split('=');
if (x[0] == key) { if (x[0] == key) {
@@ -259,4 +269,6 @@ L.contao = new (L.Class.extend({
return document.location.pathname + params.join('&'); return document.location.pathname + params.join('&');
} }
} }
}))(); });
L.contao = new L.Contao();