From 3608f7cd48b6f1de2c89bfd8ef28f1508bc488c8 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 6 Jan 2015 18:49:22 +0100 Subject: [PATCH] Ongoing development. --- composer.json | 3 +- module/assets/js/contao-leaflet.js | 23 +- module/config/config.php | 19 +- module/dca/tl_leaflet_icon.php | 257 ++++++++++++++++++ module/dca/tl_leaflet_layer.php | 13 +- module/dca/tl_leaflet_map.php | 7 + module/dca/tl_leaflet_marker.php | 3 - module/languages/en/tl_leaflet_layer.php | 2 + .../Leaflet/Controller/GeoJsonController.php | 10 +- src/Netzmacht/Contao/Leaflet/Dca/Layer.php | 24 ++ src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php | 4 +- .../Mapper/Layer/AbstractLayerMapper.php | 7 + .../Mapper/Layer/MarkersLayerMapper.php | 28 +- .../Contao/Leaflet/Mapper/UI/MarkerMapper.php | 74 +++++ .../Leaflet/Subscriber/EncoderSubscriber.php | 2 +- 15 files changed, 439 insertions(+), 37 deletions(-) create mode 100644 module/dca/tl_leaflet_icon.php create mode 100644 src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php diff --git a/composer.json b/composer.json index 79eef63..785ffdd 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,8 @@ }, "contao": { "sources":{ - "module": "system/modules/leaflet" + "module": "system/modules/leaflet", + "assets": "assets/leaflet" } } } diff --git a/module/assets/js/contao-leaflet.js b/module/assets/js/contao-leaflet.js index cede4e6..7eb75f6 100644 --- a/module/assets/js/contao-leaflet.js +++ b/module/assets/js/contao-leaflet.js @@ -6,7 +6,7 @@ L.Contao = L.Class.extend( { addMap: function (id, map) { this.maps[id] = map; - this.fire('mapadded', { id: id, map: map}); + this.fire('map:added', { id: id, map: map}); return this; }, @@ -17,9 +17,28 @@ L.Contao = L.Class.extend( { } return this.maps[id]; + }, + + pointToLayer: function(feature, latlng) { + var marker = L.marker(latlng, feature.properties.options); + + this.applyFeatureMethods(marker, feature); + this.fire('marker:created', { marker: marker, feature: feature, latlng: latlng }); + + return marker; + }, + + applyFeatureMethods: function(obj, feature) { + if (feature.properties && feature.properties.methods) { + for (var i=0; i < feature.properties.methods.length; i++) { + var method = feature.properties.methods[i]; + + obj[method[0]].apply(obj, method[1]); + } + } } }); -L.Icon.Default.imagePath = 'system/modules/leaflet/assets/leaflet/leaflet/images'; +L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; window.ContaoLeaflet = new L.Contao(); diff --git a/module/config/config.php b/module/config/config.php index 76a01ae..959619f 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -4,7 +4,14 @@ * Backend module. */ $GLOBALS['BE_MOD']['content']['leaflet'] = array( - 'tables' => array('tl_leaflet_map', 'tl_leaflet_layer', 'tl_leaflet_control', 'tl_leaflet_marker'), + 'tables' => array( + 'tl_leaflet_map', + 'tl_leaflet_layer', + 'tl_leaflet_control', + 'tl_leaflet_marker', + 'tl_leaflet_vector', + 'tl_leaflet_icon', + ), 'icon' => 'system/modules/leaflet/assets/img/leaflet.png', 'stylesheet' => 'system/modules/leaflet/assets/css/backend.css', ); @@ -37,6 +44,7 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ZoomCon $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ScaleControlMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\LayersControlMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\AttributionControlMapper'; +$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper'; /* * Leaflet encoders. @@ -98,6 +106,7 @@ $GLOBALS['LEAFLET_LAYERS'] = array ( 'children' => false, 'icon' => 'system/modules/leaflet/assets/img/vectors.png', + 'vectors' => true, ), ); @@ -132,22 +141,22 @@ require_once TL_ROOT . '/system/modules/leaflet/config/leaflet_providers.php'; */ $GLOBALS['LEAFLET_ASSETS']['leaflet'] = array( 'css' => array( - array('system/modules/leaflet/assets/leaflet/leaflet/leaflet.css', 'file') + array('assets/leaflet/libs/leaflet/leaflet.css', 'file') ), 'javascript' => array( - array('system/modules/leaflet/assets/leaflet/leaflet/leaflet.js', 'file') + array('assets/leaflet/libs/leaflet/leaflet.js', 'file') ) ); $GLOBALS['LEAFLET_ASSETS']['leaflet-providers'] = array( 'javascript' => array( - array('system/modules/leaflet/assets/leaflet/leaflet-providers/leaflet-providers.js', 'file') + array('assets/leaflet/libs/leaflet-providers/leaflet-providers.js', 'file') ) ); $GLOBALS['LEAFLET_ASSETS']['leaflet-ajax'] = array( 'javascript' => array( - array('system/modules/leaflet/assets/leaflet/leaflet-ajax/leaflet.ajax.min.js', 'file') + array('assets/leaflet/libs/leaflet-ajax/leaflet.ajax.min.js', 'file') ) ); diff --git a/module/dca/tl_leaflet_icon.php b/module/dca/tl_leaflet_icon.php new file mode 100644 index 0000000..187b0f1 --- /dev/null +++ b/module/dca/tl_leaflet_icon.php @@ -0,0 +1,257 @@ + array( + 'dataContainer' => 'Table', + 'enableVersioning' => true, + 'ptable' => 'tl_leaflet_layer', + 'sql' => array + ( + 'keys' => array + ( + 'id' => 'primary' + ) + ) + ), + + 'list' => array + ( + 'sorting' => array + ( + 'mode' => 1, + 'fields' => array('title'), + 'flag' => 1, + 'headerFields' => array('title', 'type'), + 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\Marker', 'generateRow'), + ), + 'label' => array + ( + 'fields' => array('title'), + 'format' => '%s', + ), + 'global_operations' => array + ( + 'map' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['map'], + 'href' => 'table=tl_leaflet_map', + 'icon' => 'system/modules/leaflet/assets/img/leaflet.png', + 'attributes' => 'onclick="Backend.getScrollOffset();" accesskey="m"' + ), + 'all' => array + ( + 'label' => &$GLOBALS['TL_LANG']['MSC']['all'], + 'href' => 'act=select', + 'class' => 'header_edit_all', + 'attributes' => 'onclick="Backend.getScrollOffset();" accesskey="e"' + ), + ), + 'operations' => array + ( + 'edit' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['edit'], + 'href' => 'act=edit', + 'icon' => 'edit.gif' + ), + 'copy' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['copy'], + 'href' => 'act=copy', + 'icon' => 'copy.gif' + ), + 'delete' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['delete'], + 'href' => 'act=delete', + 'icon' => 'delete.gif', + 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"' + ), + 'toggle' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['toggle'], + 'icon' => 'visible.gif', + 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', + 'button_callback' => \Netzmacht\Contao\DevTools\Dca::createToggleIconCallback( + 'tl_leaflet_icon', + 'active' + ) + ), + 'show' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['show'], + 'href' => 'act=show', + 'icon' => 'show.gif' + ) + ) + ), + + 'metapalettes' => array( + 'default' => array( + 'title' => array('title', 'alias', 'type'), + 'content' => array('tooltip', 'alt',), + 'config' => array( + ':hide', + 'iconUrl', + 'iconRetinaUrl', + 'iconAnchor', + 'popupAnchor', + 'iconClassName', + 'shadowImage', + 'shadowRetinaImage', + 'shadowAnchor', + ), + 'active' => array('active') + ), + ), + + 'fields' => array + ( + 'id' => array + ( + 'sql' => "int(10) unsigned NOT NULL auto_increment" + ), + 'tstamp' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'pid' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'title' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['title'], + 'exclude' => true, + 'inputType' => 'text', + 'eval' => array('mandatory' => true, 'maxlength' => 255, 'tl_class' => 'w50'), + 'sql' => "varchar(255) NOT NULL default ''" + ), + 'alias' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['alias'], + 'exclude' => true, + 'inputType' => 'text', + 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50'), + 'sql' => "varchar(255) NOT NULL default ''" + ), + 'active' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['active'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class' => 'w50'), + 'sql' => "char(1) NOT NULL default ''" + ), + 'iconUrl' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_content']['iconUrl'], + 'exclude' => true, + 'inputType' => 'fileTree', + 'eval' => array( + 'filesOnly' => true, + 'fieldType' => 'radio', + 'mandatory' => true, + 'tl_class' => 'clr w50', + 'extensions' => 'gif,png,svg,jpg' + ), + 'sql' => "binary(16) NULL", + ), + 'iconRetinaUrl' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_content']['iconRetinaUrl'], + 'exclude' => true, + 'inputType' => 'fileTree', + 'eval' => array( + 'filesOnly' => true, + 'fieldType' => 'radio', + 'mandatory' => false, + 'tl_class' => 'w50', + 'extensions' => 'gif,png,svg,jpg' + ), + 'sql' => "binary(16) NULL", + ), + 'shadowImage' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_content']['shadowImage'], + 'exclude' => true, + 'inputType' => 'fileTree', + 'eval' => array( + 'filesOnly' => true, + 'fieldType' => 'radio', + 'mandatory' => false, + 'tl_class' => 'clr w50', + 'extensions' => 'gif,png,svg,jpg' + ), + 'sql' => "binary(16) NULL", + ), + 'shadowRetinaImage' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_content']['shadowRetinaImage'], + 'exclude' => true, + 'inputType' => 'fileTree', + 'eval' => array( + 'filesOnly' => true, + 'fieldType' => 'radio', + 'mandatory' => false, + 'tl_class' => 'w50', + 'extensions' => 'gif,png,svg,jpg' + ), + 'sql' => "binary(16) NULL", + ), + 'iconAnchor' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['iconAnchor'], + 'exclude' => true, + 'inputType' => 'text', + 'save_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + ), + 'eval' => array( + 'maxlength' => 255, + 'tl_class' => 'w50', + 'nullIfEmpty' => true, + ), + 'sql' => "varchar(255) NULL" + ), + 'shadowAnchor' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['shadowAnchor'], + 'exclude' => true, + 'inputType' => 'text', + 'save_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + ), + 'eval' => array( + 'maxlength' => 255, + 'tl_class' => 'w50', + 'nullIfEmpty' => true, + ), + 'sql' => "varchar(255) NULL" + ), + 'popupAnchor' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['popupAnchor'], + 'exclude' => true, + 'inputType' => 'text', + 'save_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + ), + 'eval' => array( + 'maxlength' => 255, + 'tl_class' => 'w50', + 'nullIfEmpty' => true, + ), + 'sql' => "varchar(255) NULL" + ), + 'className' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['className'], + 'exclude' => true, + 'inputType' => 'text', + 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50'), + 'sql' => "varchar(255) NOT NULL default ''" + ), + ), +); diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index c12e5cb..0fd83ae 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -55,6 +55,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'icon' => 'edit.gif', 'button_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'generateMarkersButton'), ), + 'vectors' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['vectors'], + 'href' => 'table=tl_leaflet_vector', + 'icon' => 'edit.gif', + 'button_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'generateVectorsButton'), + ), 'edit' => array ( 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['edit'], @@ -108,10 +115,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'default' => array( 'title' => array('title', 'alias', 'type'), 'active' => array('active'), - 'expert' => array('deferred'), ), 'markers extends default' => array( - '+title' => array('markerCluster'), + '+title' => array('markerCluster'), + '+active' => array('deferred') ), ), @@ -243,7 +250,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array ), 'deferred' => array ( - 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['deferred'], + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'], 'exclude' => true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50', 'submitOnChange' => false), diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php index 8b88e6a..75c82f9 100644 --- a/module/dca/tl_leaflet_map.php +++ b/module/dca/tl_leaflet_map.php @@ -37,6 +37,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'icon' => 'system/modules/leaflet/assets/img/layers.png', 'attributes' => 'onclick="Backend.getScrollOffset();" accesskey="e"' ), + 'icons' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['icons'], + 'href' => 'table=tl_leaflet_icon', + 'icon' => 'system/modules/leaflet/assets/img/layers.png', + 'attributes' => 'onclick="Backend.getScrollOffset();" accesskey="e"' + ), 'all' => array ( 'label' => &$GLOBALS['TL_LANG']['MSC']['all'], diff --git a/module/dca/tl_leaflet_marker.php b/module/dca/tl_leaflet_marker.php index 308ad3a..328d1a9 100644 --- a/module/dca/tl_leaflet_marker.php +++ b/module/dca/tl_leaflet_marker.php @@ -189,7 +189,6 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['addPopup'], 'exclude' => true, 'inputType' => 'checkbox', - 'default' => true, 'eval' => array('tl_class' => 'w50', 'submitOnChange' => true), 'sql' => "char(1) NOT NULL default ''" ), @@ -198,7 +197,6 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['popupContent'], 'exclude' => true, 'inputType' => 'text', - 'default' => true, 'eval' => array('mandatory' => true, 'rte' => 'tinyMCE', 'helpwizard' => true, 'tl_class' => 'clr'), 'explanation' => 'insertTags', 'sql' => "mediumtext NULL" @@ -208,7 +206,6 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['customIcon'], 'exclude' => true, 'inputType' => 'checkbox', - 'default' => true, 'eval' => array('tl_class' => 'w50', 'submitOnChange' => true), 'sql' => "char(1) NOT NULL default ''" ), diff --git a/module/languages/en/tl_leaflet_layer.php b/module/languages/en/tl_leaflet_layer.php index a92d04d..f05acc8 100644 --- a/module/languages/en/tl_leaflet_layer.php +++ b/module/languages/en/tl_leaflet_layer.php @@ -34,3 +34,5 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['active'][0] = 'Activate $GLOBALS['TL_LANG']['tl_leaflet_layer']['active'][1] = 'Activate layer on the map.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][0] = 'Marker cluster'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][1] = 'Choose a marker cluster layer so that markers get clustered.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'][0] = 'Deferred loading'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'][1] = 'Load data of the layer deferred using ajax.'; diff --git a/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php b/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php index 7b678bd..9653761 100644 --- a/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php +++ b/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php @@ -34,8 +34,14 @@ class GeoJsonController public function execute() { - $collection = $this->mapService->getFeatureCollection(\Input::get('id')); + try { + $collection = $this->mapService->getFeatureCollection(\Input::get('id')); - return json_encode($collection); + header('Content-Type: application/json'); + echo json_encode($collection, JSON_UNESCAPED_SLASHES); + } + catch(\Exception $e) { + header('HTTP/1.0 403 Forbidden'); + } } } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php index bda9818..9e48953 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php @@ -131,6 +131,30 @@ class Layer return ''; } + return $this->generateButton($row, $href, $label, $title, $icon, $attributes); + } + + public function generateVectorsButton($row, $href, $label, $title, $icon, $attributes) + { + if (empty($this->layers[$row['type']]['vectors'])) { + return ''; + } + + return $this->generateButton($row, $href, $label, $title, $icon, $attributes); + } + + /** + * @param $row + * @param $href + * @param $label + * @param $title + * @param $icon + * @param $attributes + * + * @return string + */ + protected function generateButton($row, $href, $label, $title, $icon, $attributes) + { return sprintf( '%s ', \Backend::addToUrl($href . '&id=' . $row['id']), diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php b/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php index 54be2be..e7dde28 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php @@ -51,8 +51,8 @@ class Leaflet $template->field = 'ctrl_' . $dataContainer->field; try { - $latLng = LatLng::fromString($dataContainer->value); - $template->marker = $latLng->toJson(); + $latLng = LatLng::fromString($dataContainer->value); + $template->marker = json_encode($latLng); } catch(\Exception $e) { } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/AbstractLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/AbstractLayerMapper.php index b85fe26..92f494a 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/AbstractLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/AbstractLayerMapper.php @@ -21,4 +21,11 @@ class AbstractLayerMapper extends AbstractTypeMapper * @var string */ protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\LayerModel'; + + protected function initialize() + { + parent::initialize(); + + $this->addOption('label', 'title'); + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php index 9f05e4f..4c3d83e 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php @@ -14,11 +14,12 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Layer; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper; use Netzmacht\Contao\Leaflet\Model\MarkerModel; +use Netzmacht\Javascript\Type\Value\Reference; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; +use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Group\LayerGroup; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; -use Netzmacht\LeafletPHP\Definition\UI\Marker; use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax; class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper @@ -58,21 +59,25 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper protected function doBuild( Definition $definition, \Model $model, - DefinitionMapper $builder, + DefinitionMapper $mapper, LatLngBounds $bounds = null ) { if ($definition instanceof GeoJsonAjax) { - $base = \Config::get('websitePath') . '/system/modules/leaflet/public/geojson.php?id='; + $base = \Config::get('websitePath') . '/assets/leaflet/geojson.php?id='; $definition->setUrl($base . $model->id); } elseif ($definition instanceof LayerGroup) { $collection = $this->loadMarkerModels($model); if ($collection) { foreach ($collection as $item) { - $definition->addLayer($this->createMarker($item)); + $definition->addLayer($mapper->handle($item)); } } } + + if ($definition instanceof GeoJson) { + $definition->setPointToLayer(new Reference('ContaoLeaflet.pointToLayer', 'ContaoLeaflet')); + } } /** @@ -89,26 +94,13 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper if ($collection) { foreach ($collection as $item) { - $feature->addFeature($this->createMarker($item)->getFeature()); + $feature->addFeature($mapper->handle($item)->toGeoJson()); } } return $feature; } - /** - * @param $item - * - * @return Marker - */ - protected function createMarker($item) - { - $marker = new Marker('marker_' . $item->id, $item->coordinates); - $marker->setTitle($item->tooltip); - - return $marker; - } - /** * @param \Model $model * diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php new file mode 100644 index 0000000..6ad46a2 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php @@ -0,0 +1,74 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Mapper\UI; + + +use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper; +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; +use Netzmacht\LeafletPHP\Definition\UI\Marker; + +class MarkerMapper extends AbstractMapper +{ + /** + * Class of the model being build. + * + * @var string + */ + protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\MarkerModel'; + + /** + * Class of the definition being created. + * + * @var string + */ + protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\UI\Marker'; + + /** + * {@inheritdoc} + */ + protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) + { + $arguments = parent::buildConstructArguments($model, $mapper, $bounds); + $arguments[] = $model->coordinates ?: null; + + return $arguments; + } + + /** + * {@inheritdoc} + */ + protected function initialize() + { + $this + ->addConditionalOption('tooltip', 'title', 'tooltip') + ->addConditionalOption('alt') + ->addOptions('clickable', 'keyboard', 'draggable'); + } + + /** + * {@inheritdoc} + */ + protected function doBuild( + Definition $definition, + \Model $model, + DefinitionMapper $builder, + LatLngBounds $bounds = null + ) { + if ($definition instanceof Marker) { + if ($model->addPopup) { + $definition->bindPopup($model->popupContent); + } + } + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index 8998e89..22544cb 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -71,7 +71,7 @@ HTML; $object = $event->getObject(); if ($object instanceof Map) { - $line = sprintf('window.ContaoLeaflet.addMap(\'%s\', (function() {', $object->getId()); + $line = sprintf('ContaoLeaflet.addMap(\'%s\', (function() {', $object->getId()); $event->getOutput()->addLine($line); } }