scripts.js ➔ checkSingleCheckbox   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 2.4
c 0
b 0
f 0
cc 16

How to fix   Complexity   

Complexity

Complex classes like scripts.js ➔ checkSingleCheckbox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// PARAMETERS
2
const SCRIPT = document.getElementById('__laravel-cookie-consent-script');
3
const COOKIE_CONSENT_NAME = SCRIPT.getAttribute('data-cookie-name');
4
const COOKIE_LIFETIME = SCRIPT.getAttribute('data-cookie-lifetime');
5
const COOKIE_TYPES = JSON.parse(SCRIPT.getAttribute('data-cookie-types'));
6
let COOKIE_SELECTED = JSON.parse(SCRIPT.getAttribute('data-cookie-selected'));
7
const COOKIE_VALUE_YES = "YES";
8
const SESSION_SECURE = SCRIPT.getAttribute('data-session-secure');
9
const COOKIE_BTN_LABEL_ACCEPT_ALL = SCRIPT.getAttribute('data-button-label-accept-all');
10
const COOKIE_BTN_LABEL_ACCEPT_SELECTED = SCRIPT.getAttribute('data-button-label-accept-selected');
11
12
// ELEMENTS
13
let cookieBanner = document.getElementById("__cookie-consent");
14
let cookieBannerOpenLinks = document.getElementsByClassName("__cookie-consent-open-link");
15
let acceptButton = document.getElementById("__cookie-consent-accept");
16
let rejectButton = document.getElementById("__cookie-consent-reject");
17
let saveChangesButton = document.getElementById("__cookie-consent-save-changes");
18
let closeButton = document.getElementById("__cookie-consent-close-button");
19
let closeRejectButton = document.getElementById("__cookie-consent-reject-button");
20
let manageCookieButton = document.getElementById("manage-cookies-btn");
21
let cookieDescriptionModal = document.getElementById("__cookie-descriptions");
22
let accordions = document.getElementsByClassName("__cookie-modal-accordion");
23
let closeModalButton = document.getElementById("__cookie-info-close-button");
24
25
// SCRIPT EXECUTION
26
window.laravelCookieConsent = (function () {
27
    function rejectAllCookies() {
28
        setCookieConsentClose();
29
        closeCookieDialog();
30
        refreshPage();
31
    }
32
    function consentSelectedCookies() {
33
        if(this.textContent === COOKIE_BTN_LABEL_ACCEPT_SELECTED) {
34
            COOKIE_SELECTED.forEach(cookie_name => setCookie(cookie_name, COOKIE_VALUE_YES, COOKIE_LIFETIME));
35
        }
36
        else {
37
            let allCookieSelected = Object.values(COOKIE_TYPES);
38
            allCookieSelected.forEach(cookie_name => {
39
                if(cookie_name !== 'laravel_cookie_consent_necessary') {
40
                    setCookie(cookie_name, COOKIE_VALUE_YES, COOKIE_LIFETIME);
41
                }
42
            });
43
        }
44
        setCookieConsentClose();
45
        closeCookieDialog();
46
        refreshPage();
47
    }
48
    function saveChangesCookies() {
49
        let allCookies = Object.values(COOKIE_TYPES);
50
        allCookies.forEach(cookie_name => {
51
            if(cookie_name !== 'laravel_cookie_consent_necessary') {
52
                if(COOKIE_SELECTED.includes(cookie_name)) {
53
                    setCookie(cookie_name, COOKIE_VALUE_YES, COOKIE_LIFETIME);
54
                }
55
                else {
56
                    removeCookie(cookie_name);
57
                }
58
            }
59
        });
60
        setCookieConsentClose();
61
        closeCookieDialog();
62
        refreshPage();
63
    }
64
    function setCookieConsentClose() {
65
        setCookie(COOKIE_CONSENT_NAME, COOKIE_VALUE_YES, COOKIE_LIFETIME);
66
    }
67
    function cookieExists(name) {
68
        return (document.cookie.split('; ').indexOf(name + '=' + COOKIE_VALUE_YES) !== -1);
69
    }
70
    function closeCookieDialog() {
71
        cookieBanner.classList.add("__cookie-consent-closed");
72
    }
73
    function openCookieDialog() {
74
        if(cookieBanner.classList.contains('__cookie-consent-closed')) {
75
            cookieBanner.classList.remove("__cookie-consent-closed");
76
        }
77
    }
78
    function openCookieDescription() {
79
        cookieDescriptionModal.classList.add('open');
80
    }
81
    function closeCookieDescription() {
82
        cookieDescriptionModal.classList.remove('open');
83
    }
84
    function toggleAccordion() {
85
        let type = this.dataset.type;
86
        for (let i = 0; i < accordions.length; i++) {
87
            let isOpen = accordions[i].classList.contains('open');
88
            accordions[i].classList.remove('open');
89
            if(!isOpen && accordions[i].dataset.type === type) {
90
                accordions[i].classList.add('open');
91
            }
92
        }
93
    }
94
    function refreshPage() {
95
        location.reload();
96
    }
97
    function setCookie(name, value, expirationInDays) {
98
        const date = new Date();
99
        date.setTime(date.getTime() + (expirationInDays * 24 * 60 * 60 * 1000));
100
        document.cookie = name + '=' + value + ';' + 'expires=' + date.toUTCString() + ';path=/' + (SESSION_SECURE === '1' ? ';secure' : '');
101
    }
102
    function removeCookie(name) {
103
        document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
104
    }
105
    if(cookieExists(COOKIE_CONSENT_NAME)) {
106
        closeCookieDialog();
107
    }
108
    if(acceptButton) { acceptButton.addEventListener('click', consentSelectedCookies); }
109
    if(rejectButton) { rejectButton.addEventListener('click', rejectAllCookies); }
110
    if(saveChangesButton) { saveChangesButton.addEventListener('click', saveChangesCookies); }
111
    for (let i = 0; i < cookieBannerOpenLinks.length; i++) {
112
        cookieBannerOpenLinks[i].addEventListener("click", openCookieDialog);
113
    }
114
    if(closeButton) { closeButton.addEventListener('click', closeCookieDialog); }
115
    if(closeRejectButton) { closeRejectButton.addEventListener('click', rejectAllCookies); }
116
    if(closeModalButton) { closeModalButton.addEventListener('click', closeCookieDescription);}
117
    if(manageCookieButton){  manageCookieButton.addEventListener('click', openCookieDescription); }
118
    for (let i = 0; i < accordions.length; i++) {
119
        accordions[i].addEventListener("click", toggleAccordion);
120
    }
121
    return {
122
        consentSelectedCookies: consentSelectedCookies,
123
        rejectAllCookies: rejectAllCookies,
124
        closeCookieDialog: closeCookieDialog,
125
        openCookieDialog: openCookieDialog
126
    };
127
})();
128
129
// CLICK IN THE SINGLE CHECKBOX ACCEPT
130
const single_checkboxes = document.getElementsByClassName("single_checkbox_accept");
131
for (let k = 0; k < single_checkboxes.length; k++) {
132
    single_checkboxes[k].addEventListener("change", function() {
133
        checkSingleCheckbox(this, this.checked);
134
    });
135
}
136
137
// CHECK A SINGLE CHECKBOX ACCEPT
138
function checkSingleCheckbox(element, checked) {
139
    element.checked = checked;
140
    if(checked) {
141
        COOKIE_SELECTED.push(element.value);
142
    }
143
    else {
144
        COOKIE_SELECTED.splice(COOKIE_SELECTED.indexOf(element.value), 1);
145
    }
146
    if(acceptButton) {
147
        if(COOKIE_SELECTED.length > 0) {
148
            acceptButton.textContent = COOKIE_BTN_LABEL_ACCEPT_SELECTED;
149
        }
150
        else {
151
            acceptButton.textContent = COOKIE_BTN_LABEL_ACCEPT_ALL;
152
        }
153
    }
154
}
155