Completed
Push — master ( 618f2a...0b0c20 )
by Manolo
08:12
created

msalsas_voting_bottomBar.js ➔ closeModal   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
dl 0
loc 7
rs 10
nop 1
1
// This file is part of the MsalsasVotingBundle package.
2
//
3
// (c) Manolo Salsas
4
//
5
//     For the full copyright and license information, please view the LICENSE
6
//     file that was distributed with this source code.
7
8
(function() {
9
    document.addEventListener('DOMContentLoaded', function() {
10
        var voteNegativeForm = document.querySelectorAll('.msalsas-voting-bottom-bar form');
11
        for (var i = 0; i < voteNegativeForm.length; i++) {
12
            if (voteNegativeForm[i].addEventListener) {
13
                voteNegativeForm[i].addEventListener('change', voteNegative, false);
14
            } else {
15
                voteNegativeForm[i].attachEvent('onchange', voteNegative);
16
            }
17
        }
18
    });
19
20
    function voteNegative(evt) {
21
        var form = evt.target.parentNode;
22
        var id = form.dataset.id;
23
        var options =  form.ratings.options;
24
        window.msalsasVoting_Selected = options[form.ratings.selectedIndex];
25
        var elem = document.getElementById('msalsas-voting-problem-' + id);
26
        var url = elem.dataset.url;
27
        var http = new XMLHttpRequest();
28
        http.open('POST', url, true);
29
        http.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
30
31
        http.onreadystatechange = function() {
32
            if(http.readyState == 4 && http.status == 200) {
33
                var downVotes = document.getElementById('msalsas-voting-bottom-bar-votes-down-' + id);
34
                downVotes.innerHTML = document.createTextNode(http.responseText).wholeText;
35
                var buttonElem = document.getElementById('msalsas-voting-a-shake-' + id);
36
                buttonElem.innerHTML = '<span>' + window.msalsasVoting_Selected.text + '</span>';
37
            } else if(http.readyState == 4 && http.status >= 400) {
38
                if (http.responseText.length < 50) {
39
                    showModal(http.responseText);
40
                } else {
41
                    showModal('Error');
42
                }
43
            }
44
        };
45
        if (window.msalsasVoting_Selected.value !== '0') {
46
            http.send(window.msalsasVoting_Selected.value);
47
        }
48
    }
49
50
    function showModal(message) {
51
        message = message.replace (/(^")|("$)/g, '');
52
        var modal = document.getElementById('msalsas-modal');
53
        var span = document.getElementsByClassName("msalsas-close")[0];
54
55
        if (!modal || !span) {
56
            alert(message);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
57
            return;
58
        }
59
        document.getElementById('msalsas-modal-text').innerText = message;
60
        modal.style.display = "block";
61
62
        if (span.addEventListener) {
63
            span.addEventListener('click', closeModal, false);
64
        } else {
65
            span.attachEvent('onclick', closeModal);
66
        }
67
68
        if (window.addEventListener) {
69
            window.addEventListener('click', closeModal, false);
70
        } else {
71
            window.attachEvent('onclick', closeModal);
72
        }
73
    }
74
75
    function closeModal(event) {
76
        var modal = document.getElementById('msalsas-modal');
77
        var span = document.getElementsByClassName("msalsas-close")[0];
78
        if (event.target === modal || event.target === span) {
79
            modal.style.display = "none";
80
        }
81
    }
82
})();