home » zplus/freepost.git
ID: d92bbea5e322b5a1347b4b4c81c1127be9bc405e
132 lines — 5K — View raw


/*
    @licstart The following is the entire license notice for the JavaScript code in this page.

    This is the code powering <http://freepo.st>.
    Copyright © 2014-2016 zPlus
    Copyright © 2016 Adonay "adfeno" Felipe Nogueira <adfeno@openmailbox.org> <https://libreplanet.org/wiki/User:Adfeno>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    As additional permission under GNU GPL version 3 section 7, you may
    distribute non-source (e.g., minimized or compacted) forms of that code
    without the copy of the GNU GPL normally required by section 4, provided
    you include this license notice and a URL through which recipients can
    access the Corresponding Source.

    @licend The above is the entire license notice for the JavaScript code in this page.
*/

/**
 * Change arrows class when voting.
 */
function vote (action, vote_dom)
{
    var arrow_up     = vote_dom.querySelector ('button[title="upvote"]');
    var vote_counter = vote_dom.querySelector ('.count')
    var arrow_down   = vote_dom.querySelector ('button[title="downvote"]');

    // Voted/Upvoted
    var current_status = 0;

    if (vote_dom.classList.contains('upvoted'))
        current_status = 1;

    if (vote_dom.classList.contains('downvoted'))
        current_status = -1;

    // Current vote
    var current_vote = Number (vote_counter.textContent);

    // Remove any existing upvoted/downvoted class
    vote_dom.classList.remove ('upvoted');
    vote_dom.classList.remove ('downvoted');

    // Toggle upvote class for arrow
    if ("up" == action)
        switch (current_status)
        {
            case -1:
                vote_counter.textContent = current_vote + 2;
                vote_dom.classList.add ('upvoted');
                break;
            case 0:
                vote_counter.textContent = current_vote + 1;
                vote_dom.classList.add ('upvoted');
                break;
            case 1:
                vote_counter.textContent = current_vote - 1;
                break;
        }

    // Toggle downvote class for arrow
    if ("down" == action)
        switch (current_status)
        {
            case -1:
                vote_counter.textContent = current_vote + 1;
                break;
            case 0:
                vote_counter.textContent = current_vote - 1;
                vote_dom.classList.add ('downvoted');
                break;
            case 1:
                vote_counter.textContent = current_vote - 2;
                vote_dom.classList.add ('downvoted');
                break;
        }
}

// Wait DOM to be ready...
document.addEventListener ('DOMContentLoaded', function() {

    /**
     * A "vote section" is a <span/> containing
     *   - up arrow
     *   - votes sum
     *   - down arrow
     *
     * However, if the user is not logged in, there's only a text
     * with the sum of votes, eg. "2 votes" (no <tag> children).
     */
    var vote_sections = document.querySelectorAll ('.vote ');

    // Bind vote() event to up/down vote arrows
    for (var i = 0; i < vote_sections.length; i++)
        // See comment above on the "vote_sections" declaration.
        if (vote_sections[i].children.length > 0)
        {
            vote_sections[i]
                .querySelector ('button[title="upvote"]')
                .addEventListener ('click', function () {
                    vote ('up', this.closest ('.vote'))
                });

            vote_sections[i]
                .querySelector ('button[title="downvote"]')
                .addEventListener ('click', function () {
                    vote ('down', this.closest ('.vote'))
                });
        }

    // Function to hide/show menu when burger-icon has been clicked
    var burger_menus = document.getElementsByClassName('burger-icon');

    for (var i = 0; i < burger_menus.length; i++)
        burger_menus[i].addEventListener('click', function(event) {
            // Toggle menu visibility
            document.getElementById('menu').classList.toggle('visible');

            this.classList.toggle('open');
        });
});