summaryrefslogtreecommitdiff
path: root/resources/views/admin/location-edit.phtml
blob: 4213f0518dcd3948dce5b97a43f7d272663acd28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View; ?>
<?php ?>

<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>

<h1><?= $title ?></h1>

<div class="form-group row">
    <div class="col-sm-10 offset-sm-1">
        <div id="osm-map" class="wt-ajax-load col-sm-12 osm-admin-map"></div>
    </div>
</div>

<form method="post">
    <?= csrf_field() ?>
    <input type="hidden" name="place_id" value="<?= e($place_id) ?>">
    <input type="hidden" name="level" value="<?= count($hierarchy) ?>">
    <input type="hidden" name="place_long" value="<?= e($lng) ?>">
    <input type="hidden" name="place_lati" value="<?= e($lat) ?>">

    <div class="form-group row">
        <label class="col-form-label col-sm-1" for="new_place_name">
            <?= I18N::translate('Place') ?>
        </label>
        <div class="col-sm-5">
            <input type="text" id="new_place_name" name="new_place_name" value="<?= e($location->locationName()) ?>"
            class="form-control" required>
        </div>
        <label class="col-form-label col-sm-1" for="icon">
            <?= I18N::translate('Flag') ?>
        </label>
        <div class="col-sm-4">
            <div class="input-group" dir="ltr">
                <input type="text" name="icon" id="icon" class="form-control" value="<?= e($location->icon()) ?>">
            </div>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-form-label col-sm-1" for="new_place_lati">
            <?= I18N::translate('Latitude') ?>
        </label>
        <div class="col-sm-3">
            <div class="input-group">
                <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) ?>"
                >
            </div>
        </div>

        <label class="col-form-label col-sm-1" for="new_place_long">
            <?= I18N::translate('Longitude') ?>
        </label>
        <div class="col-sm-3">
            <div class="input-group">
                <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) ?>"
                >
            </div>
        </div>
        <label class="col-form-label col-sm-1" for="new_zoom_factor">
            <?= I18N::translate('Zoom') ?>
        </label>
        <div class="col-sm-2">
            <input type="text" id="new_zoom_factor" name="new_zoom_factor" value="<?= e($location->zoom()) ?>"
            class="form-control" required readonly>
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-10 offset-sm-1">
            <button class="btn btn-primary" type="submit">
                <?= /* I18N: A button label. */
                I18N::translate('save')
                ?>
            </button>
            <a class="btn btn-secondary" href="<?= e(route('map-data', ['parent_id' => $parent_id])) ?>">
                <?= I18N::translate('cancel') ?>
            </a>
        </div>
    </div>
</form>

<?php View::push('styles') ?>
<style>
    .osm-wrapper, .osm-user-map {
        height: 45vh
    }

    .osm-admin-map {
        height: 55vh;
        border: 1px solid darkGrey
    }

    .osm-sidebar {
        height: 100%;
        overflow-y: auto;
        padding: 0;
        margin: 0;
        border: 0;
        display: none;
        font-size: small;
    }

    .osm-sidebar .gchart {
        margin: 1px;
        padding: 2px
    }

    .osm-sidebar .gchart img {
        height: 15px;
        width: 25px
    }

    .osm-sidebar .border-danger:hover {
        cursor: not-allowed
    }
</style>
<?php View::endpush() ?>

<?php View::push('javascript') ?>
<script type="application/javascript">
    "use strict";

    window.WT_OSM_ADMIN = (function () {
        let baseData = {
            minZoom:         2,
            providerName:    "OpenStreetMap.Mapnik",
            providerOptions: [],
            I18N:            {
                zoomInTitle: <?= json_encode(I18N::translate('Zoom in')) ?>,
                zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
                reset: <?= json_encode(I18N::translate('Reset to initial map state')) ?>,
                noData: <?= json_encode(I18N::translate('No mappable items')) ?>,
                error: <?= json_encode(I18N::translate('An unknown error occurred')) ?>
            },
        };

        let map      = null;
        let marker   = L.marker([0, 0], {
            draggable: true,
        });
        /**
         *
         * @private
         */
        let _drawMap = function () {
            map = L.map("osm-map", {
                    center:      [0, 0],
                    minZoom:     baseData.minZoom, // maxZoom set by leaflet-providers.js
                    zoomControl: false, // remove default
                },
            );
            L.tileLayer.provider(baseData.providerName, baseData.providerOptions).addTo(map);
            L.control.zoom({ // Add zoom with localised text
                zoomInTitle:  baseData.I18N.zoomInTitle,
                zoomOutTitle: baseData.I18N.zoomOutTitle,
            }).addTo(map);

            // postcss_image_inliner breaks the autodetection of image paths.
            L.Icon.Default.imagePath = <?= json_encode(asset('css/images/')) ?>;

            marker
                .on("dragend", function (e) {
                    let coords = marker.getLatLng();
                    map.panTo(coords);
                    _update_Controls({
                        place:  "",
                        coords: coords,
                        zoom:   map.getZoom(),
                    });
                })
                .addTo(map);
            let searchControl = new window.GeoSearch.GeoSearchControl({
                provider:        new window.GeoSearch.OpenStreetMapProvider(),
                retainZoomLevel: true,
                autoClose:       true,
                showMarker:      false,
            });

            map
                .addControl(searchControl)
                .on("geosearch/showlocation", function (result) {
                    let lat   = result.location.y;
                    let lng   = result.location.x;
                    let place = result.location.label.split(",", 1);

                    marker.setLatLng([lat, lng]);
                    map.panTo([lat, lng]);

                    _update_Controls({
                        place:  place.shift(),
                        coords: {
                            "lat": lat,
                            "lng": lng,
                        },
                        zoom:   map.getZoom(),
                    });
                })
                .on("zoomend", function (e) {
                    $("#new_zoom_factor").val(map.getZoom());
                    map.panTo(marker.getLatLng());
                });
        };

        let data = <?= json_encode($data) ?>;

        /**
         *
         * @param newData
         * @private
         */
        let _update_Controls = function (newData) {
            let placeEl = $("#new_place_name");
            if (!placeEl.val().length && newData.place.length) {
                placeEl.val(newData.place);
            }
            $("#new_place_lati").val(Number(newData.coords.lat).toFixed(5)); // 5 decimal places (about 1 metre accuracy)
            $("#new_place_long").val(Number(newData.coords.lng).toFixed(5));
            $("#new_zoom_factor").val(Number(newData.zoom));
        };

        $(function () {
            $(".editable").on("change", function (e) {
                let lat = $("#new_place_lati").val();
                let lng = $("#new_place_long").val();
                marker.setLatLng([lat, lng]);
                map.panTo([lat, lng]);
            });
        });

        /**
         *
         * @param id
         */
        let initialize = function (id) {
            _drawMap();

            marker.setLatLng(data.coordinates);

            if (data.coordinates[0] === 0 && data.coordinates[1] === 0) {
                map.fitWorld();
            } else {
                map.setView(data.coordinates, data.zoom);
            }
        };

        return {
            /**
             *
             * @param id
             */
            drawMap: function (id) {
                initialize(id);
            },
        };
    })();

    WT_OSM_ADMIN.drawMap(<?= json_encode($ref) ?>);
</script>
<?php View::endpush() ?>