assets/js/tristate_checkboxes.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1

Size

Lines of Code 43
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 43
rs 10
wmc 7
mnd 0
bc 0
fnc 7
bpm 0
cpm 1
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A TristateHelper.constructor 0 3 1
B TristateHelper.registerTriStateCheckboxes 0 31 6
1
/*
2
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
3
 *
4
 *  Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
5
 *
6
 *  This program is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU Affero General Public License as published
8
 *  by the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU Affero General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU Affero General Public License
17
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
'use strict';
21
22
import TristateCheckbox from "./lib/TristateCheckbox";
23
24
class TristateHelper {
25
    constructor() {
26
        this.registerTriStateCheckboxes();
27
    }
28
29
    registerTriStateCheckboxes() {
30
        //Initialize tristate checkboxes and if needed the multicheckbox functionality
31
32
        const listener = () => {
33
34
            const tristates = document.querySelectorAll("input.tristate");
35
36
            tristates.forEach(tristate => {
37
               TristateCheckbox.getInstance(tristate);
38
            });
39
40
41
            //Register multi checkboxes in permission tables
42
            const multicheckboxes = document.querySelectorAll("input.permission_multicheckbox");
43
            multicheckboxes.forEach(multicheckbox => {
44
               multicheckbox.addEventListener("change", (event) => {
45
                    const newValue =  TristateCheckbox.getInstance(event.target).state;
46
                    const row = event.target.closest("tr");
47
48
                    //Find all tristate checkboxes in the same row and set their state to the new value
49
                    const tristateCheckboxes = row.querySelectorAll("input.tristate");
50
                    tristateCheckboxes.forEach(tristateCheckbox => {
51
                        TristateCheckbox.getInstance(tristateCheckbox).state = newValue;
52
                    });
53
               });
54
            });
55
        }
56
57
        document.addEventListener("turbo:load", listener);
58
        document.addEventListener("turbo:render", listener);
59
    }
60
}
61
62
export default new TristateHelper();