New polygon field

This commit is contained in:
2021-09-05 23:45:39 +02:00
parent 024010b867
commit c48e06c80e
4 changed files with 138 additions and 25553 deletions

25554
dist/js/field.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,96 @@
<template>
<div @keydown.backspace="removeLastMarker">
<field-map :bounds="bounds" :center="center" :field="field" @click="createMarker">
<l-marker v-for="marker in markers" :lat-lng="marker" :draggable="edit" @dragend="triggerChange" />
<l-polygon :lat-lngs="value" :visible="true" />
</field-map>
<l-control position="bottomleft" >
<button @click="clickHandler">
I am a useless button!
</button>
</l-control>
</div>
</template>
<script>
import {LMarker, LPolygon} from 'vue2-leaflet'
export default {
props: {
value: {
type: Object,
default: null,
},
field: {
type: Object,
required: true,
},
edit: {
type: Boolean,
default: false,
}
},
data() {
return {
startBounds: undefined,
}
},
computed: {
markers() {
if (!this.value[0]) {
return [];
}
return this.value;
},
center() {
if (!this.markers.length) {
return {
lat: this.field.map.center.latitude,
lng: this.field.map.center.longitude,
};
}
return this.bounds.getCenter();
},
bounds() {
if (this.startBounds !== undefined) {
return this.startBounds;
}
if (!this.markers.length) {
return null;
}
return this.startBounds = new L.LatLngBounds(this.markers);
},
},
methods: {
triggerChange() {
this.$emit('input', this.markers);
},
removeLastMarker() {
this.markers.splice(-1,1);
this.triggerChange();
},
createMarker(evt) {
this.markers.push(evt.latlng);
this.triggerChange();
},
clickHandler() {
alert('clicked');
},
},
components: {
LMarker,
LPolyline,
}
}
</script>

View File

@@ -6,6 +6,7 @@ Nova.booting((Vue, router) => {
Vue.component('field-map', require('./components/FieldMap'));
Vue.component('field-marker', require('./components/fields/Marker'));
Vue.component('field-polyline', require('./components/fields/Polyline'));
Vue.component('field-polygon', require('./components/fields/Polygon'));
// Config leaflet images to load images from CDN
L.Icon.Default.imagePath = 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.4/images/';

40
src/Polygon.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace Lassehaslev\NovaMapFields;
class Polygon extends MapField
{
/**
* Create a new field.
* And set default properties.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
$this->help('Click on the map to create a new point. Drag a marker to change the point. When the map is selected, you can press [backspace] to remove markers.');
parent::__construct($name, $attribute = null, $resolveCallback);
}
/**
* Resolve the attribute before sending to frontend.
*
* @param mixed $resource
* @param mixed|null $attribute
*
* @return array
*/
public function resolveAttribute($resource, $attribute = null)
{
$value = $resource->{$attribute};
if (is_array($value)) {
return $value;
}
return json_decode($value);
}
}