xref: /webtrees/resources/views/admin/location-edit.phtml (revision 89d9f848971c18fc65ca9e88ca765e1a416a3cb0)
1d70512abSGreg Roach<?php
2d70512abSGreg Roach
3d70512abSGreg Roachuse Fisharebest\Webtrees\I18N;
4d70512abSGreg Roachuse Fisharebest\Webtrees\View;
5d70512abSGreg Roach
6d70512abSGreg Roach?>
7dd6b2bfcSGreg Roach
8dd6b2bfcSGreg Roach<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>
9dd6b2bfcSGreg Roach
10dd6b2bfcSGreg Roach<h1><?= $title ?></h1>
11dd6b2bfcSGreg Roach
12dd6b2bfcSGreg Roach<div class="form-group row">
13dd6b2bfcSGreg Roach    <div class="col-sm-10 offset-sm-1">
140b4092edSGreg Roach        <div id="osm-map" class="wt-ajax-load col-sm-12 osm-admin-map" dir="ltr"></div>
15dd6b2bfcSGreg Roach    </div>
16dd6b2bfcSGreg Roach</div>
17dd6b2bfcSGreg Roach
18dd6b2bfcSGreg Roach<form method="post">
19dd6b2bfcSGreg Roach    <?= csrf_field() ?>
208af6bbf8SGreg Roach    <input type="hidden" name="place_id" value="<?= e($place_id) ?>">
21dd6b2bfcSGreg Roach
22dd6b2bfcSGreg Roach    <div class="form-group row">
23dd6b2bfcSGreg Roach        <label class="col-form-label col-sm-1" for="new_place_name">
24dd6b2bfcSGreg Roach            <?= I18N::translate('Place') ?>
25dd6b2bfcSGreg Roach        </label>
26dd6b2bfcSGreg Roach        <div class="col-sm-5">
27*89d9f848SGreg Roach            <input type="text" id="new_place_name" name="new_place_name" value="<?= e($location->locationName()) ?>" class="form-control" required>
28dd6b2bfcSGreg Roach        </div>
29dd6b2bfcSGreg Roach        <label class="col-form-label col-sm-1" for="icon">
30dd6b2bfcSGreg Roach            <?= I18N::translate('Flag') ?>
31dd6b2bfcSGreg Roach        </label>
32dd6b2bfcSGreg Roach        <div class="col-sm-4">
33dd6b2bfcSGreg Roach            <div class="input-group" dir="ltr">
34b6c326d8SGreg Roach                <input type="text" name="icon" id="icon" class="form-control" value="<?= e($location->icon()) ?>">
35dd6b2bfcSGreg Roach            </div>
36dd6b2bfcSGreg Roach        </div>
37dd6b2bfcSGreg Roach    </div>
38dd6b2bfcSGreg Roach
39dd6b2bfcSGreg Roach    <div class="form-group row">
4056a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_lati">
41dd6b2bfcSGreg Roach            <?= I18N::translate('Latitude') ?>
42dd6b2bfcSGreg Roach        </label>
43dd6b2bfcSGreg Roach        <div class="col-sm-3">
44dd6b2bfcSGreg Roach            <div class="input-group">
45*89d9f848SGreg Roach                <input type="text" dir="ltr" id="new_place_lati" class="editable form-control" name="new_place_lati" required placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($lat) ?>"
46dd6b2bfcSGreg Roach                >
47dd6b2bfcSGreg Roach            </div>
48dd6b2bfcSGreg Roach        </div>
49dd6b2bfcSGreg Roach
5056a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_long">
51dd6b2bfcSGreg Roach            <?= I18N::translate('Longitude') ?>
52dd6b2bfcSGreg Roach        </label>
53dd6b2bfcSGreg Roach        <div class="col-sm-3">
54dd6b2bfcSGreg Roach            <div class="input-group">
55*89d9f848SGreg Roach                <input type="text" dir="ltr" id="new_place_long" class="editable form-control" name="new_place_long" required placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($lng) ?>"
56dd6b2bfcSGreg Roach                >
57dd6b2bfcSGreg Roach            </div>
58dd6b2bfcSGreg Roach        </div>
59dd6b2bfcSGreg Roach        <label class="col-form-label col-sm-1" for="new_zoom_factor">
60dd6b2bfcSGreg Roach            <?= I18N::translate('Zoom') ?>
61dd6b2bfcSGreg Roach        </label>
62dd6b2bfcSGreg Roach        <div class="col-sm-2">
63*89d9f848SGreg Roach            <input type="text" id="new_zoom_factor" name="new_zoom_factor" value="<?= e($location->zoom()) ?>" class="form-control" required readonly>
64dd6b2bfcSGreg Roach        </div>
65dd6b2bfcSGreg Roach    </div>
66dd6b2bfcSGreg Roach
67dd6b2bfcSGreg Roach    <div class="form-group row">
68dd6b2bfcSGreg Roach        <div class="col-sm-10 offset-sm-1">
69dd6b2bfcSGreg Roach            <button class="btn btn-primary" type="submit">
70dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
71dd6b2bfcSGreg Roach                I18N::translate('save')
72dd6b2bfcSGreg Roach                ?>
73dd6b2bfcSGreg Roach            </button>
74dd6b2bfcSGreg Roach            <a class="btn btn-secondary" href="<?= e(route('map-data', ['parent_id' => $parent_id])) ?>">
75dd6b2bfcSGreg Roach                <?= I18N::translate('cancel') ?>
76dd6b2bfcSGreg Roach            </a>
77dd6b2bfcSGreg Roach        </div>
78dd6b2bfcSGreg Roach    </div>
79dd6b2bfcSGreg Roach</form>
80dd6b2bfcSGreg Roach
81dd6b2bfcSGreg Roach<?php View::push('styles') ?>
82dd6b2bfcSGreg Roach<style>
83dd6b2bfcSGreg Roach    .osm-admin-map {
84dd6b2bfcSGreg Roach        height: 55vh;
85dd6b2bfcSGreg Roach        border: 1px solid darkGrey
86dd6b2bfcSGreg Roach    }
87dd6b2bfcSGreg Roach</style>
88dd6b2bfcSGreg Roach<?php View::endpush() ?>
89dd6b2bfcSGreg Roach
90dd6b2bfcSGreg Roach<?php View::push('javascript') ?>
9174b9ba3fSGreg Roach<script>
92caa53803SDavid Drury    'use strict';
93dd6b2bfcSGreg Roach
94dd6b2bfcSGreg Roach    window.WT_OSM_ADMIN = (function () {
95caa53803SDavid Drury        const minZoom = 2;
962cbb0620SDavid Drury
972cbb0620SDavid Drury        let provider = <?= json_encode($provider) ?>;
98caa53803SDavid Drury        let data = <?= json_encode($data) ?>;
99dd6b2bfcSGreg Roach        let map = null;
100caa53803SDavid Drury        let add_place = <?= json_encode($place_id === 0) ?>;
101caa53803SDavid Drury
102caa53803SDavid Drury        // map components
103dd6b2bfcSGreg Roach
10475e7614aSGreg Roach        // postcss_image_inliner breaks the autodetection of image paths.
10575e7614aSGreg Roach        L.Icon.Default.imagePath = <?= json_encode(asset('css/images/')) ?>;
10675e7614aSGreg Roach
107caa53803SDavid Drury        // draggable marker
108caa53803SDavid Drury        let marker = L.marker(data.coordinates, {
109caa53803SDavid Drury            draggable: true,
110caa53803SDavid Drury        })
111caa53803SDavid Drury            .on('dragend', function (e) {
112dd6b2bfcSGreg Roach                let coords = marker.getLatLng();
113dd6b2bfcSGreg Roach                map.panTo(coords);
11462be603fSGreg Roach                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
11562be603fSGreg Roach                $('#new_place_long').val(Number(coords.lng).toFixed(5));
11662be603fSGreg Roach                $('#new_zoom_factor').val(Number(map.getZoom()));
117dd6b2bfcSGreg Roach            });
118dd6b2bfcSGreg Roach
119caa53803SDavid Drury        //reset map to initial state
120caa53803SDavid Drury        let resetControl = L.Control.extend({
121caa53803SDavid Drury            options: {
122caa53803SDavid Drury                position: 'topleft'
123caa53803SDavid Drury            },
124caa53803SDavid Drury            onAdd: function (map) {
125caa53803SDavid Drury                let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
126caa53803SDavid Drury                container.onclick = function () {
127caa53803SDavid Drury                    map.setView(data.coordinates, data.zoom);
128caa53803SDavid Drury                    marker.setLatLng(data.coordinates);
129caa53803SDavid Drury                    $('form').trigger('reset');
130caa53803SDavid Drury                    return false;
131caa53803SDavid Drury                };
13247ca61d5SGreg Roach                let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
133caa53803SDavid Drury                let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container);
134caa53803SDavid Drury                anchor.setAttribute('aria-label', reset);
135caa53803SDavid Drury                anchor.href = '#';
136caa53803SDavid Drury                anchor.title = reset;
137caa53803SDavid Drury                anchor.role = 'button';
138caa53803SDavid Drury                let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
139caa53803SDavid Drury                image.alt = reset;
140dd6b2bfcSGreg Roach
141caa53803SDavid Drury                return container;
142caa53803SDavid Drury            }
143caa53803SDavid Drury        });
144dd6b2bfcSGreg Roach
145caa53803SDavid Drury        // zoom control with localised text
146caa53803SDavid Drury        let zoomCtl = new L.control.zoom({
147caa53803SDavid Drury            zoomInTitle:  <?= json_encode(I18N::translate('Zoom in')) ?>,
148caa53803SDavid Drury            zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
149caa53803SDavid Drury        });
150caa53803SDavid Drury
151caa53803SDavid Drury        // Geocoder (place lookup)
152caa53803SDavid Drury        let geocoder = new L.Control.geocoder({
153caa53803SDavid Drury            defaultMarkGeocode: false,
15422b076ddSGreg Roach            expand: 'click',
155caa53803SDavid Drury            showResultIcons: true,
156caa53803SDavid Drury            query: '<?= e($location->locationName()) ?>',
157caa53803SDavid Drury            placeholder: <?= json_encode(I18N::translate('Place')) ?>,
158caa53803SDavid Drury            errorMessage: <?= json_encode(I18N::translate('Nothing found.')) ?>,
15947ca61d5SGreg Roach            iconLabel: <?= json_encode(I18N::translate('Search')) ?>
160caa53803SDavid Drury        })
161caa53803SDavid Drury            .on('markgeocode', function (result) {
162caa53803SDavid Drury                let coords = result.geocode.center;
163caa53803SDavid Drury                let place = result.geocode.name.split(',', 1);
164caa53803SDavid Drury                marker.setLatLng(coords);
165caa53803SDavid Drury                map.panTo(coords);
16662be603fSGreg Roach                if (add_place) {
16762be603fSGreg Roach                    $('#new_place_name').val(place.shift());
168caa53803SDavid Drury                }
16962be603fSGreg Roach                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
17062be603fSGreg Roach                $('#new_place_long').val(Number(coords.lng).toFixed(5));
17162be603fSGreg Roach                $('#new_zoom_factor').val(Number(map.getZoom()));
17262be603fSGreg Roach            });
173caa53803SDavid Drury
174caa53803SDavid Drury        /**
175caa53803SDavid Drury         *
176caa53803SDavid Drury         * @private
177caa53803SDavid Drury         */
178dd6b2bfcSGreg Roach        $(function () {
179caa53803SDavid Drury            // geocoder button tooltip
180caa53803SDavid Drury            $('.leaflet-control-geocoder-icon')
18147ca61d5SGreg Roach                .attr('title', <?= json_encode(I18N::translate('Search')) ?>);
182caa53803SDavid Drury
183caa53803SDavid Drury            $('.editable').on('change', function (e) {
184caa53803SDavid Drury                let lat = $('#new_place_lati').val();
185caa53803SDavid Drury                let lng = $('#new_place_long').val();
186dd6b2bfcSGreg Roach                marker.setLatLng([lat, lng]);
187dd6b2bfcSGreg Roach                map.panTo([lat, lng]);
188dd6b2bfcSGreg Roach            });
189dd6b2bfcSGreg Roach        });
190dd6b2bfcSGreg Roach
191caa53803SDavid Drury        // Create the map with all controls and layers
192caa53803SDavid Drury        map = L.map('osm-map', {
193caa53803SDavid Drury            minZoom: minZoom, // maxZoom set by leaflet-providers.js
194caa53803SDavid Drury            zoomControl: false,   // remove default
195caa53803SDavid Drury        })
196caa53803SDavid Drury            .addControl(new resetControl())
197caa53803SDavid Drury            .addControl(zoomCtl)
198caa53803SDavid Drury            .addControl(geocoder)
199caa53803SDavid Drury            .addLayer(marker)
200caa53803SDavid Drury            .addLayer(L.tileLayer(provider.url, provider.options))
201caa53803SDavid Drury            .setView(data.coordinates, data.zoom)
202caa53803SDavid Drury            .on('zoomend', function (e) {
203caa53803SDavid Drury                $('#new_zoom_factor').val(map.getZoom());
204caa53803SDavid Drury                map.panTo(marker.getLatLng());
205caa53803SDavid Drury            });
206dd6b2bfcSGreg Roach
207caa53803SDavid Drury        return 'Leaflet map interface for webtrees-2';
208dd6b2bfcSGreg Roach    })();
209dd6b2bfcSGreg Roach</script>
210dd6b2bfcSGreg Roach<?php View::endpush() ?>
211