Issues (8)

Resources/public/js/easyconfig.js (2 issues)

1
document.addEventListener('DOMContentLoaded', function () {
2
    // Making fields readonly those contain global value
3
    (function () {
4
        const checkBoxes = document.querySelectorAll(".derived-from-global");
5
        for (var i = 0; i < checkBoxes.length; i++) {
6
            const $checkBoxEl = checkBoxes[i];
7
8
            if ($checkBoxEl.checked) {
9
                var $inputField = $checkBoxEl.id.replace('Preference', '');
10
                document.getElementById($inputField).readOnly = true;
11
            }
12
        }
13
    })();
14
15
    var form = document.getElementById('config-form-0');
16
    form.addEventListener('submit', function (e) {
17
        e.preventDefault();
18
19
        var formData = new FormData(form);
20
        var xhr = new XMLHttpRequest();
21
        xhr.open("POST", form.action, true);
22
        // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
23
24
        xhr.onload = function () {
25
            if (xhr.status === 200) {
26
                location.reload();
27
            }
28
        }
29
30
        xhr.onerror = function () {
31
            console.log("Error:", xhr.statusText);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
32
        };
33
34
        xhr.send(formData);
35
    });
36
37
    function getValueBySpecificKey(url, params, inputEl)
38
    {
39
        const xhr = new XMLHttpRequest();
40
        xhr.open('GET', url + '?' + new URLSearchParams(params));
0 ignored issues
show
The variable URLSearchParams seems to be never declared. If this is a global, consider adding a /** global: URLSearchParams */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
        xhr.onreadystatechange = function () {
42
            if (xhr.readyState === XMLHttpRequest.DONE) {
43
                if (xhr.status === 200) {
44
                    inputEl.value = xhr.responseText.replace(/^"|"$/g, '');
45
                } else {
46
                    console.error('Error:', xhr.status);
47
                }
48
            }
49
        };
50
51
        xhr.send();
52
    }
53
54
    document.querySelectorAll('.derived-from-global').forEach(function (e) {
55
        e.addEventListener('change', function () {
56
            var $inputID = this.getAttribute('id').replace('Preference', '');
57
            var $fieldName =  this.getAttribute('name').replace('Preference', '');
58
            var $fieldKey = $fieldName.substring($fieldName.indexOf('[') + 1, $fieldName.indexOf(']'));
59
            var $inputEl = document.getElementById($inputID);
60
            var isGlobal;
61
62
            if (this.checked) {
63
                $inputEl.readOnly = true;
64
                isGlobal = 1;
65
            } else {
66
                $inputEl.readOnly = false;
67
                isGlobal = 0;
68
            }
69
70
            var $url = document.querySelector('.value-retrieving-url').dataset.url;
71
            var $groupKey = document.querySelector('.group-key').dataset.groupkey;
72
            var $params = {
73
                'is_global': isGlobal,
74
                'key': $groupKey + '.' + $fieldKey
75
            };
76
77
            getValueBySpecificKey($url, $params, $inputEl);
78
        });
79
    });
80
});
81