Conditions | 1 |
Paths | 1 |
Total Lines | 75 |
Code Lines | 53 |
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 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); |
||
|
|||
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 | })(); |