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

msalsas_voting_shakeIt.js ➔ showModal   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
c 0
b 0
f 0
nc 5
dl 0
loc 24
rs 9.0833
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 shakeItLink = document.querySelectorAll('.msalsas-voting-shake-it a');
11
        for (var i = 0; i < shakeItLink.length; i++) {
12
            if (shakeItLink[i].addEventListener) {
13
                shakeItLink[i].addEventListener('click', shakeIt, false);
14
            } else {
15
                shakeItLink[i].attachEvent('onclick', shakeIt);
16
            }
17
        }
18
    });
19
20
    function shakeIt(evt) {
21
        var shakeItButton = evt.target.parentNode;
22
        var id = shakeItButton.dataset.id;
23
        var url = shakeItButton.dataset.url;
24
        var shakenText = shakeItButton.dataset.shakentext;
25
        var http = new XMLHttpRequest();
26
        http.open('POST', url, true);
27
        http.setRequestHeader('Content-type', 'application/json');
28
        http.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
29
30
        http.onreadystatechange = function() {
31
            if(http.readyState == 4 && http.status == 200) {
32
                var shakesElem = document.getElementById('msalsas-voting-shakes-' + id);
33
                shakesElem.text = document.createTextNode(http.responseText).wholeText;
34
                var buttonElem = document.getElementById('msalsas-voting-a-shake-' + id);
35
                buttonElem.innerHTML = '<span>' + shakenText + '</span>';
36
            } else if(http.readyState == 4 && http.status >= 400) {
37
                showModal(http.responseText);
38
            }
39
        };
40
        http.send();
41
    }
42
43
    function showModal(message) {
44
        message = message.replace (/(^")|("$)/g, '');
45
        var modal = document.getElementById('msalsas-modal');
46
        var span = document.getElementsByClassName("msalsas-close")[0];
47
48
        if (!modal || !span) {
49
            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...
50
            return;
51
        }
52
        document.getElementById('msalsas-modal-text').innerText = message;
53
        modal.style.display = "block";
54
55
        if (span.addEventListener) {
56
            span.addEventListener('click', closeModal, false);
57
        } else {
58
            span.attachEvent('onclick', closeModal);
59
        }
60
61
        if (window.addEventListener) {
62
            window.addEventListener('click', closeModal, false);
63
        } else {
64
            window.attachEvent('onclick', closeModal);
65
        }
66
    }
67
68
    function closeModal(event) {
69
        var modal = document.getElementById('msalsas-modal');
70
        var span = document.getElementsByClassName("msalsas-close")[0];
71
        if (event.target === modal || event.target === span) {
72
            modal.style.display = "none";
73
        }
74
    }
75
})();