hecate 0.27.1

OpenStreetMap Inspired Data Storage Backend Focused on Performance and GeoJSON Interchange
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*jshint browser:true,curly: false */
/* global L */

window.onload = () => {
    window.vue = new Vue({
        el: '#app',
        data: {
            credentials: {
                map: { key: 'pk.eyJ1Ijoic2JtYTQ0IiwiYSI6ImNpcXNycTNqaTAwMDdmcG5seDBoYjVkZGcifQ.ZVIe6sjh0QGeMsHpBvlsEA' },
                authed: false,
                username: '',
                uid: false
            },
            panel: 'Deltas', //Store the current panel view (Deltas, Styles, Bounds, etc)
            feature: false, //Store the currently selected feature - overides panel view
            delta: false, //Store the currently selected delta - overrides panel view
            deltas: [], //Store a list of the most recent deltas
            bounds: [], //Store a list of all bounds
            pstyles: [], //If the user is authenticated, store a list of their private styles
            styles: [], //Store a list of public styles
            layers: [], //Store list of GL layer names so they can be easily removed
            style: false, //Store the id of the current style - false for generic style
            modal: {
                type: false,
                error: {
                    header: '',
                    body: ''
                },
                ok: {
                    header: '',
                    body: ''
                },
                style_set: {
                    style: '',
                    username: '',
                    uid: false,
                    public: false,
                    name: ''
                },
                login: {
                    username: '',
                    password: ''
                },
                register: {
                    username: '',
                    password: '',
                    email: ''
                }
            }
        },
        components: {
            heading: {
                template: `
                    <div class='flex-child px12 py12'>
                        <h3 class='fl py6 txt-m txt-bold' v-text='title'></h3>
                        <button v-if='is_authed' @click='logout' class='fr px12 py12 btn round bg-white bg-darken25-on-hover color-gray-dark fr'><svg class='icon'><use xlink:href='#icon-logout'></use></svg></button>
                        <button @click='login_show' class='fr px12 py12 btn round bg-white bg-darken25-on-hover color-gray-dark fr'><svg class='icon'><use xlink:href='#icon-user'></use></svg></button>
                    </div>
                `,
                props: [ 'is_authed', 'login_show', 'logout', 'title' ]
            },
            panel: {
                data: function() {
                    return {
                        panel: 'Deltas'
                    };
                },
                watch: {
                    panel: function() {
                        this.$emit('input', this.panel);
                    }
                },
                template: `
                    <div class='select-container'>
                        <select v-model="panel" class='select select--s select--stroke'>
                            <option>Deltas</option>
                            <option>Bounds</option>
                            <option>Styles</option>
                        </select>
                        <div class='select-arrow'></div>
                    </div>
                `
            },
            foot: {
                template: `
                    <div class='flex-child px12 py12 bg-gray-faint round-b-ml txt-s flex-child'>
                        <div align='center'><a href="https://github.com/ingalls/hecate">Powered By Hecate Server</a></div>
                    </div>
                `
            }
        },
        created: function() {
            this.logout();
            this.deltas_refresh();
        },
        watch: {
            panel: function() {
                this.refresh();
            },
            delta: function() {
                //Reset Normal Map
                if (!this.delta) {
                    this.map_unstyle();
                    this.map_default_style();

                    this.map.getSource('hecate-delta').setData({ type: 'FeatureCollection', features: [] });
                } else {
                    this.map.getSource('hecate-delta').setData(this.delta.features);
                    this.map_unstyle();
                    this.map_delta_style();

                    this.delta.bbox = turf.bbox(this.delta.features);
                    this.map.fitBounds(this.delta.bbox);
                }
            }
        },
        mounted: function(e) {
            mapboxgl.accessToken = this.credentials.map.key;
            this.map = new mapboxgl.Map({
                container: 'map',
                style: 'mapbox://styles/mapbox/satellite-v9',
                center: [-96, 37.8],
                hash: true,
                maxzoom: 14,
                zoom: 3
            });

            this.map.on('load', () => {
                this.map.addSource('hecate-data', {
                    type: 'vector',
                    maxzoom: 14,
                    tiles: [ `http://${window.location.host}/api/tiles/{z}/{x}/{y}` ]
                });

                this.map.addSource('hecate-delta', {
                    type: 'geojson',
                    data: { type: 'FeatureCollection', features: [] }
                });

                this.map_default_style();
            });

            this.map.addControl(new MapboxGeocoder({
                accessToken: mapboxgl.accessToken,
            }));

            this.map.on('click', (e) => {
                if (this.delta) return; //Don't currently support showing features within a delta

                let clicked = this.map.queryRenderedFeatures(e.point)[0];

                if (clicked && clicked.properties['hecate:id']) this.feature_get(clicked.properties['hecate:id']);
            });
        },
        methods: {
            ok: function(header, body) {
                this.modal.ok.header = header;
                this.modal.ok.body = body;
                this.modal.type = 'ok';
            },
            logout: function() {
                this.credentials.authed = false;
                document.cookie = 'session=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'
                this.refresh();
            },
            refresh: function() {
                if (this.panel === 'Bounds') {
                    this.bounds_refresh();
                } else if (this.panel === 'Styles') {
                    this.styles_refresh();
                } else if (this.panel === 'Deltas') {
                    this.deltas_refresh();
                }
            },
            login: function() {
                fetch(`http://${window.location.host}/api/user/session`, {
                    method: 'GET',
                    credentials: 'same-origin',
                    headers: new Headers({
                        'Authorization': 'Basic '+ btoa(`${this.modal.login.username}:${this.modal.login.password}`)
                    })
                }).then((response) => {
                    if (response.status === 200) {
                        response.json().then((response) => {
                            this.modal.type = false;

                            this.credentials.authed = true;
                            this.credentials.username = this.modal.login.username;
                            this.credentials.uid = parseInt(response);
                            this.modal.login = {
                                username: '',
                                password: ''
                            };

                            this.refresh(); //Refresh the current panel as loggin in may reveal private data/styles
                        });
                    } else {
                        return this.ok('Failed to Login', 'Failed to login with given credentials');
                    }
                }).catch((err) => {
                    return this.ok('Failed to Login', 'Failed to login with given credentials');
                });

            },
            login_show: function() {
                if (!this.credentials.authed) this.modal.type = 'login';
            },
            register: function() {
                fetch(`http://${window.location.host}/api/user/create?username=${this.modal.register.username}&password=${this.modal.register.password}&email=${this.modal.register.email}`).then((response) => {
                      return response.json();
                }).then((body) => {
                    this.modal.register = {
                        username: '',
                        password: '',
                        email: ''
                    };

                    this.ok('Account Created', 'Your account has been created! You can login now');
                });
            },
            register_show: function() {
                this.modal.type = 'register';
            },
            bounds_refresh: function() {
                fetch(`http://${window.location.host}/api/data/bounds`).then((response) => {
                      return response.json();
                }).then((body) => {
                    this.bounds = body;
                });
            },
            styles_refresh: function() {
                fetch(`http://${window.location.host}/api/styles`).then((response) => {
                      return response.json();
                }).then((body) => {
                    this.styles = body;
                });

                if (this.credentials.authed) {
                    fetch(`http://${window.location.host}/api/styles/${this.credentials.uid}`, {
                        credentials: 'same-origin'
                    }).then((response) => {
                          return response.json();
                    }).then((body) => {
                        this.pstyles = body.filter((style) => {
                            if (style.public) return false;
                            return true;
                        });
                    });
                } else {
                    this.pstyles = [];
                }
            },
            style_get: function(style_id, cb) {
                fetch(`http://${window.location.host}/api/style/${style_id}`, {
                    credentials: 'same-origin'
                }).then((response) => {
                      return response.json();
                }).then((body) => {
                    return cb(null, body);
                }).catch((err) => {
                    return cb(err);
                });
            },
            style_create: function(style, name) {
                this.modal.style_set.style = style ? style : '';
                this.modal.style_set.id = false;
                this.modal.style_set.username = this.credentials.username;
                this.modal.style_set.uid = this.credentials.uid;
                this.modal.style_set.name = name ? `Copy of ${name}` : '';
                this.modal.style_set.public = false;

                this.modal.type = 'style_set';
            },
            style_update: function(style_id, style_name, style) {
                if (!style_id) { //Create new style
                    fetch(`http://${window.location.host}/api/style`, {
                        method: 'POST',
                        credentials: 'same-origin',
                        headers: new Headers({
                            'Content-Type': 'application/json'
                        }),
                        body: JSON.stringify({
                            name: style_name,
                            style: style
                        })
                    }).then((response) => {
                        if (response.status === 200) {
                            this.refresh();
                            this.style_set(style_id, style);
                        } else {
                            return this.ok('Failed to push style', 'Failed to update style');
                        }
                    }).catch((err) => {
                        return this.ok('Failed to push style', 'Failed to update style');
                    });
                } else { //Update Existing Style
                    fetch(`http://${window.location.host}/api/style/${style_id}`, {
                        method: 'PATCH',
                        credentials: 'same-origin',
                        headers: new Headers({
                            'Content-Type': 'application/json'
                        }),
                        body: JSON.stringify({
                            name: style_name,
                            style: style
                        })
                    }).then((response) => {
                        if (response.status !== 200) return this.ok('Failed to push style', 'Failed to update style');

                        if (this.credentials.authed && this.modal.style_set.id) {
                            fetch(`http://${window.location.host}/api/style/${style_id}/${this.modal.style_set.public ? 'public' : 'private'}`, {
                                method: 'POST',
                                credentials: 'same-origin'
                            }).then((response) => {
                                if (response.status !== 200) return this.ok('Failed to push style', 'Failed to update style');

                                this.refresh();
                                this.style_set(style_id, style);
                            }).catch((err) => {
                                console.error(err);
                                return this.ok('Failed to push style', 'Failed to update style');
                            });
                        } else {
                            this.refresh();
                            this.style_set(style_id, style);
                        }
                    }).catch((err) => {
                        console.error(err);
                        return this.ok('Failed to push style', 'Failed to update style');
                    });
                
                }
            },
            style_set: function(style_id, style) {
                if (!style.version || style.version !== 8) return this.ok('Style Not Applied', 'The selected style could not be applied. The style version must be 8');
                if (!style.layers || style.layers.length === 0) return this.ok('Style Not Applied', 'The selected style could not be applied. The style must contain at least 1 layer');

                this.map_unstyle();

                for (let layer of style.layers) {
                    if (!layer.id) {
                        this.map_unstyle();
                        this.map_default_style();
                        return this.ok('Style Not Applied', 'Every layer in the style must have a unique id');
                    }

                    layer.source = 'hecate-data';
                    layer['source-layer'] = 'data',

                    this.layers.push(layer.id);
                    this.map.addLayer(layer);
                }

                this.modal.type = false;
            },
            style_set_modal: function(style_id) {
                this.style_get(style_id, (err, style) => {
                    if (err) return this.ok('Failed to retrieve style', err.message);

                    this.modal.style_set.style = JSON.stringify(style.style, null, 4);
                    this.modal.style_set.id = style.id;
                    this.modal.style_set.username = style.username;
                    this.modal.style_set.uid = style.uid;
                    this.modal.style_set.name = style.name;
                    this.modal.style_set.public = style.public;

                    this.modal.type = 'style_set';
                });
            },
            deltas_refresh: function() {
                fetch(`http://${window.location.host}/api/deltas`).then((response) => {
                      return response.json();
                }).then((body) => {
                    this.deltas.splice(0, this.deltas.length);
                    this.deltas = this.deltas.concat(body);
                });
            },
            delta_get: function(delta_id) {
                if (!delta_id) return;

                fetch(`http://${window.location.host}/api/delta/${delta_id}`).then((response) => {
                      return response.json();
                }).then((body) => {
                    body.features.features = body.features.features.map(feat => {
                        feat.properties._action = feat.action;
                        return feat;
                    });
                    this.delta = body;
                });
            },
            feature_get: function(feature_id) {
                if (!feature_id) return;

                fetch(`http://${window.location.host}/api/data/feature/${feature_id}`).then((response) => {
                      return response.json();
                }).then((body) => {
                    this.feature = body;
                });
            },
            map_delta_style: function() {
                let action_create = '#008000';
                let action_modify = '#FFFF00';
                let action_delete = '#FF0000';
               
                this.layers.push('hecate-delta-polygons');
                this.map.addLayer({
                    id: 'hecate-delta-polygons',
                    type: 'fill',
                    source: 'hecate-delta',
                    filter: ['==', '$type', 'Polygon'],
                    paint: {
                        'fill-opacity': 0.4,
                        'fill-color': [ 'match', [ 'get', '_action' ], 'create', action_create, 'modify', action_modify, 'delete', action_delete, action_create ]
                    }
                });

                this.layers.push('hecate-delta-polygon-outlines');
                this.map.addLayer({
                    id: 'hecate-delta-polygon-outlines',
                    type: 'line',
                    source: 'hecate-delta',
                    filter: ['==', '$type', 'Polygon'],
                    layout: {
                        'line-join': 'round',
                        'line-cap': 'round'
                    },
                    paint: {
                        'line-color': [ 'match', [ 'get', '_action' ], 'create', action_create, 'modify', action_modify, 'delete', action_delete, action_create ],
                        'line-width': 0.75
                    }
                })

                this.layers.push('hecate-delta-lines');
                this.map.addLayer({
                    id: 'hecate-delta-lines',
                    type: 'line',
                    source: 'hecate-delta',
                    filter: ['==', '$type', 'LineString'],
                    layout: {
                        'line-join': 'round',
                        'line-cap': 'round'
                    },
                    paint: {
                        'line-color': [ 'match', [ 'get', '_action' ], 'create', action_create, 'modify', action_modify, 'delete', action_delete, action_create ],
                        'line-width': 1.25
                    }
                });

                this.layers.push('hecate-delta-points');
                this.map.addLayer({
                    id: 'hecate-delta-points',
                    type: 'circle',
                    source: 'hecate-delta',
                    filter: ['==', '$type', 'Point'],
                    paint: {
                        'circle-color': [ 'match', [ 'get', '_action' ], 'create', action_create, 'modify', action_modify, 'delete', action_delete, action_create ],
                        'circle-radius': 4
                    }
                });

            },
            map_unstyle: function() {
                for (let layer of this.layers) {
                    this.map.removeLayer(layer);
                }
                this.layers = [];
            },
            map_default_style: function() {
                const foregroundColor = '#FF0000';

                this.layers.push('hecate-data-polygons');
                this.map.addLayer({
                    id: 'hecate-data-polygons',
                    type: 'fill',
                    source: 'hecate-data',
                    'source-layer': 'data',
                    filter: ['==', '$type', 'Polygon'],
                    paint: {
                        'fill-opacity': 0.1,
                        'fill-color': foregroundColor
                    }
                });

                this.layers.push('hecate-data-polygon-outlines');
                this.map.addLayer({
                    id: 'hecate-data-polygon-outlines',
                    type: 'line',
                    source: 'hecate-data',
                    'source-layer': 'data',
                    filter: ['==', '$type', 'Polygon'],
                    layout: {
                        'line-join': 'round',
                        'line-cap': 'round'
                    },
                    paint: {
                        'line-color': foregroundColor,
                        'line-width': 0.75,
                        'line-opacity': 0.75
                    }
                })

                this.layers.push('hecate-data-lines');
                this.map.addLayer({
                    id: 'hecate-data-lines',
                    type: 'line',
                    source: 'hecate-data',
                    'source-layer': 'data',
                    filter: ['==', '$type', 'LineString'],
                    layout: {
                        'line-join': 'round',
                        'line-cap': 'round'
                    },
                    paint: {
                        'line-color': foregroundColor,
                        'line-width': 1.25,
                        'line-opacity': 0.75
                    }
                });

                this.layers.push('hecate-data-points');
                this.map.addLayer({
                    id: 'hecate-data-points',
                    type: 'circle',
                    source: 'hecate-data',
                    'source-layer': 'data',
                    filter: ['==', '$type', 'Point'],
                    paint: {
                        'circle-color': foregroundColor,
                        'circle-radius': 4,
                        'circle-opacity': 0.85
                    }
                });

            }
        }
    });

    window.vue.moment = moment;
}