home » zplus/freepost.git
Author zPlus <-> 2017-03-27 18:00:47
Committer zPlus <-> 2017-03-27 18:00:47
Commit 679cccc (patch)
Tree dfbd770
Parent(s)

Fix #47 Some issues with voting mechanism


commits diff: 3606d68..679cccc
2 files changed, 36 insertions, 12 deletionsdownload


Diffstat
-rw-r--r-- javascript/freepost.js 44
-rw-r--r-- template/vote.twig 4

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+34/-10 M   javascript/freepost.js
index 8237968..bce0806
old size: 3K - new size: 4K
@@ -33,7 +33,11 @@
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,18 +45,18 @@ function vote (action, dom_element) {
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,11 +64,11 @@ function vote (action, dom_element) {
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,11 +84,31 @@ function vote (action, dom_element) {
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 + });

+2/-2 M   template/vote.twig
index c3f3c74..8843578
old size: 1K - new size: 1023B
@@ -12,14 +12,14 @@
12 12
13 13 {% if user %}
14 14
15 - <a href="vote?up&{{ target }}={{ hash_id }}" target="vote_sink" class="{{ vote is defined and vote == 1 ? 'upvoted' }}" onclick="vote ('up', this.parentNode)">
15 + <a href="vote?up&{{ target }}={{ hash_id }}" target="vote_sink" class="{{ vote is defined and vote == 1 ? 'upvoted' }}">
16 16 <img title="bump" alt="bump" src="images/upvote.png" />
17 17 </a>
18 18
19 19 {# Show number of votes #}
20 20 <span class="count">{{ vote_count }}</span>
21 21
22 - <a href="vote?down&{{ target }}={{ hash_id }}" target="vote_sink" class="{{ vote is defined and vote == -1 ? 'downvoted' }}" onclick="vote ('down', this.parentNode)">
22 + <a href="vote?down&{{ target }}={{ hash_id }}" target="vote_sink" class="{{ vote is defined and vote == -1 ? 'downvoted' }}">
23 23 <img title="sink" alt="sink" src="images/downvote.png" />
24 24 </a>
25 25