aboutsummaryrefslogtreecommitdiff
path: root/app/javascript/controllers/taxonomy_controller.js
blob: 48d9a5297bf63a31744924d95e72c3ffd7697ac6 (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
// SPDX-FileCopyrightText: 2020 IN COMMON Collective <collective@incommon.cc>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

// Visit The Stimulus Handbook for more details
// https://stimulusjs.org/handbook/introduction
//

import { Controller } from "stimulus"

export default class extends Controller {
    static targets = [ "category", "deploy", "filter", "section", "toggle" ]

    initialize() {
        console.log("Taxonomy controller initialized.")

        this.overlays = {}
        this.sectionsOnMap = []
    }

    // Load filtering sidebar
    connect() {
        console.log("Taxonomy controller connected.")
        this.taxonomy_uuid = this.data.get('uuid')
        if (this.hasFilterTarget) {
            let url = `/taxonomies/${this.data.get('uuid')}/filter.js`
            console.log(`loading url = ${url}`)
            fetch(url, {
                headers: { accept: 'application/json'}
            })
                .then(response => response.text())
                .then(html => this.filterTarget.innerHTML = html)
        } else {
            console.log("Taxonomy filter is missing")
        }

        // Access the map
        if (this.map = document.querySelector('#map').parentElement.map.map) {
            console.log("Taxonomy Controller connected to this.map")
        } else {
            console.log("Taxonomy Controller could not load map!")
        }
    }


    deploy() {
        console.log(`deploying taxonomy ${this.data.get('uuid')}`);

        fetch(`/taxonomies/${this.data.get('uuid')}.js`)
            .then(response => response.text())
            .then(html => this.deployTarget.innerHTML = html);
    }

    toggle() {
        var cssClass = 'on';
        this.toggleTarget.classList.toggle(cssClass)
        this.filterTarget.parentNode.classList.toggle(cssClass)
    }

    category(event) {
        let catId = (event.target.dataset.taxonomyCategoryId || event.target.parentNode.parentNode.dataset.CategoryId)
        let secId = event.target.dataset.taxonomySectionId
        console.log(`Category: ${catId}/${secId}`)
        var active = event.target.classList.toggle('active')
        if (active) {
            console.log('activated')
        } else {
            console.log('deactivated')
        }
    }

    loadCategory(catId) {
        document.querySelector(`#category-${catId}`).lastChild.childNodes.forEach(function(li) {
            this._mapLayerToggleSection(li.dataset.taxonomySectionId)
        })
    }

    section(event) {
        let secId = event.target.dataset.taxonomySectionId
        console.log(`Section: ${secId} to be loaded`)
        this._loadMarkers(secId)
        this._mapLayerToggleSection(secId)
    }

    _loadMarkers(secId) {
        if (this.overlays[secId] == undefined) {
            console.log(`loading markers for section ${secId}`)
            let overlay = L.layerGroup();
            let markers = L.markerClusterGroup();

            fetch(`/sections/${secId}.json`, {
                headers: { 'X-CSRF-Token': this._csrfToken() }
            })
                .then(response => response.json())
                .then(data => {
                    L.geoJSON(data, {
                        pointToLayer: function (feature, latlng) {
                            return L.marker(latlng, {
                                attribution: feature.source,
                                icon: L.ExtraMarkers.icon({
                                    icon: feature.icon.name,
                                    extraClasses: feature.style.classes,
                                    markerColor: feature.style.color,
                                    shape: feature.icon.shape,
                                    prefix: 'fa',
                                    svg: true
                                })
                            }).bindPopup(feature.popup);
		                    },
                        onEachFeature: (feature, layer) => {
                            layer.on('click', () => this.onClick(layer))
                            layer.addTo(markers)
                        }
                    })
                })
            console.log(`cluster counts ${markers.length} markers`)
            this.overlays[secId] = markers
        }
    }

    onClick(layer) {
        console.log(layer)
    }

    _mapLayerToggleSection(secId) {
        if (this.sectionsOnMap.includes(secId)) {
            console.log(`removing section ${secId} from map`)
            this.map.removeLayer(this.overlays[secId])
            this.sectionsOnMap.pop(secId)
        } else {
            console.log(`adding section ${secId} to the map`)
            this.sectionsOnMap.push(secId)
            this.overlays[secId].addTo(this.map)
        }
    }

    _csrfToken() {
        return document.querySelector('[name=csrf-token]').content
    }
}