home » zplus/freepost.git
ID: bce080696b1612ed7f1e775c4369e312830e3496
114 lines — 4K — 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.
*/


/* This is a small script to hide the "up/down" arrow when users upvote
 * posts and comments. The only reason for this is to give some feedback
 * to the user after clicks.
 */

/**
 * Change arrows class when voting.
 */
function vote (action, dom_element)
{
    var arrow_up     = dom_element.children[0];
    var vote_counter = dom_element.children[1];
    var arrow_down   = dom_element.children[2];
    
    // Voted/Upvoted
    var current_status = 0;
    
    if (arrow_up.classList.contains('upvoted'))
        current_status = 1;
    
    if (arrow_down.classList.contains('downvoted'))
        current_status = -1;
    
    // Current vote
    var current_vote = Number (vote_counter.textContent);
    
    // Remove class from arrows
    arrow_up.classList.remove ('upvoted');
    arrow_down.classList.remove ('downvoted');
    
    // Toggle upvote class for arrow
    if ("up" == action)
        switch (current_status)
        {
            case -1:
                vote_counter.textContent = current_vote + 2;
                arrow_up.classList.add ('upvoted');
                break;
            case 0:
                vote_counter.textContent = current_vote + 1;
                arrow_up.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;
                arrow_down.classList.add ('downvoted');
                break;
            case 1:
                vote_counter.textContent = current_vote - 2;
                arrow_down.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
     */
    let vote_sections = document.getElementsByClassName ('vote');
    
    // Bind vote() event to up/down vote arrows
    for (var i = 0; i < vote_sections.length; i++)
    {
        vote_sections[i].children[0].addEventListener ('click', function () { vote ('up',   this.parentNode) });
        vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) });
    }

});