freepost.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, vote_dom)
  34. {
  35. var arrow_up = vote_dom.querySelector ('button[title="upvote"]');
  36. var vote_counter = vote_dom.querySelector ('.count')
  37. var arrow_down = vote_dom.querySelector ('button[title="downvote"]');
  38. // Voted/Upvoted
  39. var current_status = 0;
  40. if (vote_dom.classList.contains('upvoted'))
  41. current_status = 1;
  42. if (vote_dom.classList.contains('downvoted'))
  43. current_status = -1;
  44. // Current vote
  45. var current_vote = Number (vote_counter.textContent);
  46. // Remove any existing upvoted/downvoted class
  47. vote_dom.classList.remove ('upvoted');
  48. vote_dom.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. vote_dom.classList.add ('upvoted');
  56. break;
  57. case 0:
  58. vote_counter.textContent = current_vote + 1;
  59. vote_dom.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. vote_dom.classList.add ('downvoted');
  75. break;
  76. case 1:
  77. vote_counter.textContent = current_vote - 2;
  78. vote_dom.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. var vote_sections = document.querySelectorAll ('.vote ');
  94. // Bind vote() event to up/down vote arrows
  95. for (var 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]
  100. .querySelector ('button[title="upvote"]')
  101. .addEventListener ('click', function () {
  102. vote ('up', this.closest ('.vote'))
  103. });
  104. vote_sections[i]
  105. .querySelector ('button[title="downvote"]')
  106. .addEventListener ('click', function () {
  107. vote ('down', this.closest ('.vote'))
  108. });
  109. }
  110. // Bind onkeydown()/onkeyup() event to keys
  111. document.onkeydown = document.onkeyup = function(e) {
  112. // Set the current key code as TRUE/FALSE
  113. pressed_key[e.keyCode] = e.type == 'keydown';
  114. // If Ctrl+Enter have been pressed
  115. // Key codes: Ctrl=17, Enter=13
  116. if (pressed_key[17] && pressed_key[13])
  117. {
  118. // Select all forms in the current page with class "shortcut-submit"
  119. var forms = document.querySelectorAll ("form.shortcut-submit");
  120. for (var i = 0; i < forms.length; i++)
  121. forms[i].submit ();
  122. }
  123. }
  124. // Function to hide/show menu when burger-icon has been clicked
  125. var burger_menus = document.getElementsByClassName ('burger-icon')
  126. for (var i = 0; i < burger_menus.length; i++)
  127. burger_menus[i].addEventListener ('click', function (event) {
  128. // Toggle menu visibility
  129. document.getElementById ('menu').classList.toggle ('visible');
  130. this.classList.toggle ('open');
  131. });
  132. });