PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.14.6
Independent Analytics – WordPress Analytics Plugin v2.14.6
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / javascript-source / controllers / overview / checkbox_group.js
checkbox_group.js
56 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {Controller} from "@hotwired/stimulus"
2
3 export default class extends Controller {
4 static targets = ['groupTab', 'group']
5
6 static values = {
7 max: { type: Number, default: -1 }
8 }
9
10 connect() {
11 this.updateCheckboxStates()
12 this.element.addEventListener('change', (event) => {
13 this.updateCheckboxStates()
14 })
15 }
16
17 updateCheckboxStates() {
18 const checkboxes = Array.from(
19 this.element.querySelectorAll('input[type=checkbox]')
20 )
21 const totalChecked = checkboxes.filter(checkbox => checkbox.checked).length
22
23 if (totalChecked === 1) {
24 // Disable checked boxes so they can't select 0
25 checkboxes.forEach(checkbox => {
26 if (checkbox.checked) {
27 checkbox.disabled = true
28 }
29 })
30 } else if (totalChecked === this.maxValue) {
31 // Disable unchecked boxes so they can't select more than 4
32 checkboxes.forEach(checkbox => {
33 if (!checkbox.checked) {
34 checkbox.disabled = true
35 }
36 })
37 } else {
38 // Ensure everything is enabled
39 checkboxes.forEach(checkbox => {
40 checkbox.disabled = false
41 })
42 }
43 }
44
45 changeTab(event) {
46 const selectedGroup = event.currentTarget.dataset.groupName
47
48 this.groupTabTargets.forEach(groupTab => {
49 groupTab.classList.toggle('selected', groupTab.dataset.groupName === selectedGroup)
50 })
51
52 this.groupTargets.forEach(group => {
53 group.classList.toggle('selected', group.dataset.groupName === selectedGroup)
54 })
55 }
56 }