Ongoing development.

This commit is contained in:
David Molineus
2017-01-18 10:56:03 +01:00
parent dca072693f
commit 5086041111
8 changed files with 247 additions and 1 deletions

10
.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
/vendor/
/bin/
/demo/
/assets/js/*.js
/node_modules/
/.tx/
coverage.xml
composer.lock
npm-debug.log

View File

@@ -22,7 +22,7 @@
"php":">=5.5.0",
"contao/core-bundle":"~3.5 | ~4.3",
"contao-community-alliance/composer-plugin": "~2.4 | ~3.0",
"netzmacht/contao-leaflet-libraries":"~2.0"
"netzmacht/contao-leaflet-libraries":"~1.0"
},
"require-dev": {
"netzmacht/contao-build-tools": "~1.1"

102
src/GeocodeWidget.php Normal file
View File

@@ -0,0 +1,102 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\GeocodeWidget;
/**
* Class GeocodeWidget
*
* @package Netzmacht\Contao\Leaflet\GeocodeWidget
*/
class GeocodeWidget extends \Widget
{
/**
* Submit user input.
*
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Add a for attribute.
*
* @var boolean
*/
protected $blnForAttribute = true;
/**
* Template.
*
* @var string
*/
protected $strTemplate = 'be_widget';
/**
* Template name.
*
* @var string
*/
protected $widgetTemplate = 'be_widget_leaflet_geocode';
/**
* Validate the input.
*
* @param mixed $value Given value.
*
* @return mixed
*/
protected function validator($value)
{
$value = parent::validator($value);
if (!$value) {
return $value;
}
if (
// See: http://stackoverflow.com/a/18690202
!preg_match(
'^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)(,[-+]?\d+)?$',
$value
)
) {
$this->addError(
sprintf(
$GLOBALS['TL_LANG']['ERR']['leaflet_invalid_coordinate'],
$value
)
);
}
return $value;
}
/**
* Generate the widget.
*
* @return string
*/
public function generate()
{
$template = new \BackendTemplate($this->widgetTemplate);
$template->setData(
[
'widget' => $this,
'value' => specialchars($this->value),
'class' => $this->strClass ? ' ' . $this->strClass : '',
'id' => $this->strId,
'attributes' => $this->getAttributes(),
'wizard' => $this->wizard,
]
);
return $template->parse();
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\GeocodeWidget;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Class LeafletGeocodeWidgetBundle
*
* @package Netzmacht\Contao\Leaflet\GeocodeWidget
*/
class LeafletGeocodeWidgetBundle extends Bundle
{
}

View File

@@ -0,0 +1,11 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
$GLOBALS['BE_FFL']['leaflet_geocode'] = 'Netzmacht\Contao\Leaflet\GeocodeWidget\GeocodeWidget';

View File

@@ -0,0 +1,11 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
$GLOBALS['TL_LANG']['ERR']['leaflet_invalid_coordinate'] = 'Die angegebenen Koordinaten sind ungültig.';

View File

@@ -0,0 +1,11 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
$GLOBALS['TL_LANG']['ERR']['leaflet_invalid_coordinate'] = 'Invalid coordinates given.';

View File

@@ -0,0 +1,79 @@
<?php
$GLOBALS['TL_CSS'][] = 'assets/leaflet/libs/leaflet/leaflet.css';
$GLOBALS['TL_CSS'][] = 'assets/leaflet/libs/control-geocoder/Control.Geocoder.css';
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/leaflet/leaflet.js';
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/control-geocoder/Control.Geocoder.js';
?>
<input type="text"
name="<?= $this->name ?>"
id="ctrl_<?= $this->id ?>"
class="tl_text tl_leaflet_geocode<?= $this->class ?>"
value="<?= $this->value ?>"<?= $this->attributes ?>
onfocus="Backend.getScrollOffset()"
>
<div id="map_<?php echo $this->name; ?>" style="margin-top: 5px; width: calc(100% - 28px); height: 300px"></div>
<script>
var map = L.map('map_<?php echo $this->name; ?>').setView([0, 0], 2);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var element = $('<?php echo $this->name; ?>');
var geocoder = L.Control.geocoder({
defaultMarkGeocode: false,
collapsed: false,
placeholder: '<?php echo $GLOBALS['TL_LANG']['leaflet']['searchPosition']; ?>'
}).addTo(map);
geocoder.on('markgeocode', function(event) {
var container = document.createElement('div');
var link = document.createElement('button');
var result = event.geocode;
link.set('style', 'margin-left: 10px;');
link.appendText('<?php echo $GLOBALS['TL_LANG']['leaflet']['applyPosition']; ?>');
link.addEvent('click', function(e) {
e.stop();
element.set('value', result.center.lat + ',' + result.center.lng);
});
container.appendHTML(result.html || result.name);
container.appendChild(link);
if (this._geocodeMarker) {
map.removeLayer(this._geocodeMarker);
}
map.fitBounds(result.bbox, { padding: [0, 70]});
map.panTo(result.center);
this._geocodeMarker = new L.Marker(result.center, {draggable: true})
.bindPopup(container, {
keepInView: true,
autoPanPaddingTopLeft: [0, 70]
})
.addTo(map)
.openPopup();
this._geocodeMarker.on('dragend', function (event) {
element.set('value', event.target._latlng.lat + ',' + event.target._latlng.lng);
});
});
<?php if ($this->marker): ?>
geocoder._geocodeMarker = L.marker(<?php echo $this->marker; ?>, {draggable: true}).addTo(map);
geocoder._geocodeMarker.on('dragend', function (event) {
element.set('value', event.target._latlng.lat + ',' + event.target._latlng.lng);
});
map.setZoom(16);
map.panTo(<?php echo $this->marker; ?>);
<?php endif; ?>
</script>
<?= $this->wizard ?>