Conditions | 1 |
Paths | 1 |
Total Lines | 68 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | // This file is part of the MsalsasVotingBundle package. |
||
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); |
||
|
|||
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 | })(); |