mirror of
				https://github.com/KevinMidboe/nova-map-fields.git
				synced 2025-10-29 17:50:27 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			103 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Vue
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Vue
		
	
	
	
	
	
| <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" v-if="edit">
 | |
|             <button @click="removeLastMarker">
 | |
|             Slett forrige
 | |
|             </button>
 | |
|             <button @click="removeAllMarkers">
 | |
|             Slett alle
 | |
|             </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(event) {
 | |
|             this.markers.splice(-1,1);
 | |
|             this.triggerChange();
 | |
|             event.preventDefault();
 | |
|         },
 | |
|         removeAllMarkers(event) {
 | |
|             this.value = [];
 | |
|             this.triggerChange();
 | |
|             event.preventDefault();
 | |
|         },
 | |
|         createMarker(evt) {
 | |
|             this.markers.push(evt.latlng);
 | |
|             this.triggerChange();
 | |
|         },
 | |
|     },
 | |
| 
 | |
|     components: {
 | |
|         LMarker,
 | |
|         LPolygon,
 | |
|     }
 | |
| }
 | |
| </script>
 |