aboutsummaryrefslogtreecommitdiff
path: root/app/javascript/controllers/taxonomy_controller.js
blob: 16d57fab54f188b722c531acc6aa70d35fac8317 (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
// 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.")
    }

    connect() {
        console.log("Taxonomy controller connected.")

        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")
        }
    }

    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.classList.toggle('active')
    }

    section(event) {
        let catId = event.target.dataset.taxonomySectionId
        event.target.classList.toggle('active')
    }
}