Passed
Push — master ( 40feff...710ff4 )
by Michael
01:41
created

www/src/DembeloMain/Resources/public/js/modal.js   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 56
rs 9.7251
cc 1
nc 1
nop 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A modal.js ➔ closeModal 0 5 1
A modal.js ➔ hitchClick 0 6 1
A modal.js ➔ initCloser 0 6 2
A modal.js ➔ paywall 0 10 3
A modal.js ➔ submitPaywall 0 19 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
(function () {
2
3
    function hitchClick(e) {
4
        var modalPaywall = document.querySelector('#modalPaywall');
5
        modalPaywall.classList.toggle('modal-open');
6
        modalPaywall.dataset.url = e.target.dataset.url;
7
        e.preventDefault();
8
    }
9
10
    function submitPaywall(e) {
11
        var modalPaywall = document.querySelector('#modalPaywall'),
12
            url = modalPaywall.dataset.url,
13
            httpRequest = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
14
15
        if (!httpRequest) {
16
            alert('Giving up :( Cannot create an XMLHTTP instance');
17
            return false;
18
        }
19
        httpRequest.onreadystatechange = function () {
20
            if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
21
                var response = JSON.parse(httpRequest.responseText);
22
                window.location = response.url;
23
            }
24
        };
25
        httpRequest.open('GET', url);
26
        httpRequest.send();
27
        e.preventDefault();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
28
    }
29
30
    function paywall() {
31
        var hitchLinks = document.querySelectorAll('.hitch-link'),
32
            submitter = document.querySelector('.paywall-submit');
33
        for(var i=0;i<hitchLinks.length;i++){
34
            hitchLinks[i].addEventListener('click', hitchClick, false);
35
        }
36
        if (submitter !== null) {
37
            submitter.addEventListener('click', submitPaywall);
38
        }
39
    }
40
41
    function closeModal(e) {
42
        var modalId = e.currentTarget.dataset.modal,
43
            modal = document.querySelector('#'+modalId);
44
        modal.classList.remove('modal-open');
45
    }
46
47
    function initCloser() {
48
        var modalCloser = document.querySelectorAll('.modal-closer');
49
        for(var i=0;i<modalCloser.length;i++){
50
            modalCloser[i].addEventListener('click', closeModal, false);
51
        }
52
    }
53
54
    initCloser();
55
    paywall();
56
})();