33
|
33
|
|
* to the user after clicks.
|
34
|
34
|
|
*/
|
35
|
35
|
|
|
36
|
|
- |
function vote (action, dom_element) {
|
|
36
|
+ |
/**
|
|
37
|
+ |
* Change arrows class when voting.
|
|
38
|
+ |
*/
|
|
39
|
+ |
function vote (action, dom_element)
|
|
40
|
+ |
{
|
37
|
41
|
|
var arrow_up = dom_element.children[0];
|
38
|
42
|
|
var vote_counter = dom_element.children[1];
|
39
|
43
|
|
var arrow_down = dom_element.children[2];
|
41
|
45
|
|
// Voted/Upvoted
|
42
|
46
|
|
var current_status = 0;
|
43
|
47
|
|
|
44
|
|
- |
if ("upvoted" == arrow_up.className)
|
|
48
|
+ |
if (arrow_up.classList.contains('upvoted'))
|
45
|
49
|
|
current_status = 1;
|
46
|
50
|
|
|
47
|
|
- |
if ("downvoted" == arrow_down.className)
|
|
51
|
+ |
if (arrow_down.classList.contains('downvoted'))
|
48
|
52
|
|
current_status = -1;
|
49
|
53
|
|
|
50
|
54
|
|
// Current vote
|
51
|
55
|
|
var current_vote = Number (vote_counter.textContent);
|
52
|
56
|
|
|
53
|
57
|
|
// Remove class from arrows
|
54
|
|
- |
arrow_up.className = "";
|
55
|
|
- |
arrow_down.className = "";
|
|
58
|
+ |
arrow_up.classList.remove ('upvoted');
|
|
59
|
+ |
arrow_down.classList.remove ('downvoted');
|
56
|
60
|
|
|
57
|
61
|
|
// Toggle upvote class for arrow
|
58
|
62
|
|
if ("up" == action)
|
60
|
64
|
|
{
|
61
|
65
|
|
case -1:
|
62
|
66
|
|
vote_counter.textContent = current_vote + 2;
|
63
|
|
- |
arrow_up.className = "upvoted";
|
|
67
|
+ |
arrow_up.classList.add ('upvoted');
|
64
|
68
|
|
break;
|
65
|
69
|
|
case 0:
|
66
|
70
|
|
vote_counter.textContent = current_vote + 1;
|
67
|
|
- |
arrow_up.className = "upvoted";
|
|
71
|
+ |
arrow_up.classList.add ('upvoted');
|
68
|
72
|
|
break;
|
69
|
73
|
|
case 1:
|
70
|
74
|
|
vote_counter.textContent = current_vote - 1;
|
80
|
84
|
|
break;
|
81
|
85
|
|
case 0:
|
82
|
86
|
|
vote_counter.textContent = current_vote - 1;
|
83
|
|
- |
arrow_down.className = "downvoted";
|
|
87
|
+ |
arrow_down.classList.add ('downvoted');
|
84
|
88
|
|
break;
|
85
|
89
|
|
case 1:
|
86
|
90
|
|
vote_counter.textContent = current_vote - 2;
|
87
|
|
- |
arrow_down.className = "downvoted";
|
|
91
|
+ |
arrow_down.classList.add ('downvoted');
|
88
|
92
|
|
break;
|
89
|
93
|
|
}
|
90
|
|
- |
} |
90
|
|
> |
\ No newline at end of file
|
|
94
|
+ |
}
|
|
95
|
+ |
|
|
96
|
+ |
// Wait DOM to be ready...
|
|
97
|
+ |
document.addEventListener ('DOMContentLoaded', function() {
|
|
98
|
+ |
|
|
99
|
+ |
/**
|
|
100
|
+ |
* A "vote section" is a <span/> containing
|
|
101
|
+ |
* - up arrow
|
|
102
|
+ |
* - votes sum
|
|
103
|
+ |
* - down arrow
|
|
104
|
+ |
*/
|
|
105
|
+ |
let vote_sections = document.getElementsByClassName ('vote');
|
|
106
|
+ |
|
|
107
|
+ |
// Bind vote() event to up/down vote arrows
|
|
108
|
+ |
for (var i = 0; i < vote_sections.length; i++)
|
|
109
|
+ |
{
|
|
110
|
+ |
vote_sections[i].children[0].addEventListener ('click', function () { vote ('up', this.parentNode) });
|
|
111
|
+ |
vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) });
|
|
112
|
+ |
}
|
|
113
|
+ |
|
|
114
|
+ |
});
|