home » zplus/freepost.git
ID: 823796831bc6a8f16b9982aa94ac332da499c16a
89 lines — 3K — 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.
 */

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 ("upvoted" == arrow_up.className)
        current_status = 1;
    
    if ("downvoted" == arrow_down.className)
        current_status = -1;
    
    // Current vote
    var current_vote = Number (vote_counter.textContent);
    
    // Remove class from arrows
    arrow_up.className = "";
    arrow_down.className = "";
    
    // Toggle upvote class for arrow
    if ("up" == action)
        switch (current_status)
        {
            case -1:
                vote_counter.textContent = current_vote + 2;
                arrow_up.className = "upvoted";
                break;
            case 0:
                vote_counter.textContent = current_vote + 1;
                arrow_up.className = "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.className = "downvoted";
                break;
            case 1:
                vote_counter.textContent = current_vote - 2;
                arrow_down.className = "downvoted";
                break;
        }
}