freepost.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. @licstart The following is the entire license notice for the JavaScript code in this page.
  3. This is the code powering <http://freepo.st>.
  4. Copyright © 2014-2016 zPlus
  5. Copyright © 2016 Adonay "adfeno" Felipe Nogueira <adfeno@openmailbox.org> <https://libreplanet.org/wiki/User:Adfeno>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Affero General Public License for more details.
  14. You should have received a copy of the GNU Affero General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. As additional permission under GNU GPL version 3 section 7, you may
  17. distribute non-source (e.g., minimized or compacted) forms of that code
  18. without the copy of the GNU GPL normally required by section 4, provided
  19. you include this license notice and a URL through which recipients can
  20. access the Corresponding Source.
  21. @licend The above is the entire license notice for the JavaScript code in this page.
  22. */
  23. /**
  24. * Store which keys have been pressed.
  25. * When a key has been pressed, pressed_key[e.keyCode] will be set
  26. * to TRUE. When a key is released, pressed_key[e.keyCode] will be
  27. * set to FALSE.
  28. */
  29. var pressed_key = [];
  30. /**
  31. * Change arrows class when voting.
  32. */
  33. function vote (action, dom_element)
  34. {
  35. let arrow_up = dom_element.children[0];
  36. let vote_counter = dom_element.children[1];
  37. let arrow_down = dom_element.children[2];
  38. // Voted/Upvoted
  39. let current_status = 0;
  40. if (arrow_up.classList.contains('upvoted'))
  41. current_status = 1;
  42. if (arrow_down.classList.contains('downvoted'))
  43. current_status = -1;
  44. // Current vote
  45. let current_vote = Number (vote_counter.textContent);
  46. // Remove class from arrows
  47. arrow_up.classList.remove ('upvoted');
  48. arrow_down.classList.remove ('downvoted');
  49. // Toggle upvote class for arrow
  50. if ("up" == action)
  51. switch (current_status)
  52. {
  53. case -1:
  54. vote_counter.textContent = current_vote + 2;
  55. arrow_up.classList.add ('upvoted');
  56. break;
  57. case 0:
  58. vote_counter.textContent = current_vote + 1;
  59. arrow_up.classList.add ('upvoted');
  60. break;
  61. case 1:
  62. vote_counter.textContent = current_vote - 1;
  63. break;
  64. }
  65. // Toggle downvote class for arrow
  66. if ("down" == action)
  67. switch (current_status)
  68. {
  69. case -1:
  70. vote_counter.textContent = current_vote + 1;
  71. break;
  72. case 0:
  73. vote_counter.textContent = current_vote - 1;
  74. arrow_down.classList.add ('downvoted');
  75. break;
  76. case 1:
  77. vote_counter.textContent = current_vote - 2;
  78. arrow_down.classList.add ('downvoted');
  79. break;
  80. }
  81. }
  82. // Wait DOM to be ready...
  83. document.addEventListener ('DOMContentLoaded', function() {
  84. /**
  85. * A "vote section" is a <span/> containing
  86. * - up arrow
  87. * - votes sum
  88. * - down arrow
  89. *
  90. * However, if the user is not logged in, there's only a text
  91. * with the sum of votes, eg. "2 votes" (no <tag> children).
  92. */
  93. let vote_sections = document.querySelectorAll ('.vote');
  94. // Bind vote() event to up/down vote arrows
  95. for (let i = 0; i < vote_sections.length; i++)
  96. // See comment above on the "vote_sections" declaration.
  97. if (vote_sections[i].children.length > 0)
  98. {
  99. vote_sections[i].children[0].addEventListener ('click', function () { vote ('up', this.parentNode) });
  100. vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) });
  101. }
  102. // Bind onkeydown()/onkeyup() event to keys
  103. document.onkeydown = document.onkeyup = function(e) {
  104. // Set the current key code as TRUE/FALSE
  105. pressed_key[e.keyCode] = e.type == 'keydown';
  106. // If Ctrl+Enter have been pressed
  107. // Key codes: Ctrl=17, Enter=13
  108. if (pressed_key[17] && pressed_key[13])
  109. {
  110. // Select all forms in the current page with class "shortcut-submit"
  111. let forms = document.querySelectorAll ("form.shortcut-submit");
  112. for (let i = 0; i < forms.length; i++)
  113. forms[i].submit ();
  114. }
  115. }
  116. });