resources/assets/js/profile/edit.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 1.45

Size

Lines of Code 89
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
c 0
b 0
f 0
dl 0
loc 89
rs 10
wmc 16
mnd 5
bc 5
fnc 11
bpm 0.4545
cpm 1.4544
noi 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A Edit.autoDisplayApiCapabilities 0 8 2
A Edit.intiSwitchListener 0 11 3
A Edit.autoDisplaySwitch 0 7 2
A Edit.constructor 0 7 1
B Edit.initCheckboxListener 0 35 6
A Edit.autoCheck 0 14 2
1
export class Edit {
2
    constructor() {
3
        this.intiSwitchListener()
4
        this.initCheckboxListener()
5
        this.autoCheck()
6
        this.autoDisplayApiCapabilities()
7
        this.autoDisplaySwitch()
8
    }
9
10
    intiSwitchListener() {
11
        $('#manage-api-capabilities').on('change', (event) => {
12
            let element = $(event.currentTarget)
13
14
            if (element.is(':checked')) {
15
                $("#permissions-table .for-api").removeClass('hide')
16
            } else {
17
                $("#permissions-table .for-api").addClass('hide')
18
            }
19
        })
20
    }
21
22
    initCheckboxListener() {
23
        // Select all checkbox
24
        $('#permissions-table input.select-all').on('change', (event) => {
25
            let element = $(event.currentTarget)
26
27
            if (element.is(':checked')) {
28
                $("#permissions-table input[type='checkbox']").prop('checked', true)
29
            } else {
30
                $("#permissions-table input[type='checkbox']").prop('checked', false)
31
            }
32
33
            this.autoCheck()
34
        })
35
36
        // Select row checkboxes
37
        $('#permissions-table input.select-row').on('change', (event) => {
38
            let element = $(event.currentTarget)
39
            let parentElement = $(element).parents('tr:first')
40
41
            if (element.is(':checked')) {
42
                $(".select-item", parentElement).prop('checked', true)
43
            } else {
44
                $(".select-item", parentElement).prop('checked', false)
45
            }
46
47
            this.autoCheck()
48
        })
49
50
        // Select item checkboxes
51
        $('#permissions-table input.select-item').on('change', (event) => {
52
            let element = $(event.currentTarget)
0 ignored issues
show
Unused Code introduced by
The variable element seems to be never used. Consider removing it.
Loading history...
53
54
            this.autoCheck()
55
        })
56
    }
57
58
    autoCheck() {
59
        // All modules
60
        const globalCheckedCount = $("#permissions-table .select-item:checked").length
61
        const globalTotalCount = $("#permissions-table .select-item").length
62
        $("#permissions-table .select-all").prop('checked', globalCheckedCount === globalTotalCount)
63
64
        // Each row
65
        $("#permissions-table tbody tr").each((index, el) => {
66
            let rowElement = $(el)
67
            let rowCheckedCount = $(".select-item:checked", rowElement).length
68
            let rowTotalCount = $(".select-item", rowElement).length
69
            $(".select-row", rowElement).prop('checked', rowCheckedCount === rowTotalCount)
70
        })
71
    }
72
73
    autoDisplayApiCapabilities() {
74
        const globalApiTotalCount = $("#permissions-table td.for-api input.select-item").length
75
        const globalApiCheckedCount = $("#permissions-table td.for-api input.select-item:checked").length
76
77
        if (globalApiCheckedCount > 0 && globalApiTotalCount !== globalApiCheckedCount) {
78
            $('#manage-api-capabilities').prop('checked', true).change()
79
        }
80
    }
81
82
    autoDisplaySwitch() {
83
        const globalApiTotalCount = $("#permissions-table td.for-api input.select-item").length
84
85
        if (globalApiTotalCount > 0) {
86
            $(".api-switch").removeClass('hide')
87
        }
88
    }
89
}