User:Nx/Extensions/wigo3/js/wigo3.js

From RationalWiki
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/*<![CDATA[*/ 

//kept for compatibility
function wigoupdate(req,avg)
{
  if (req.readyState == 4 && req.status == 200)
  {
    arr = req.responseText.split(':');
    span = document.getElementById(arr[0]);
    if (span)
    {
      origvote = parseInt(span.innerHTML,10);
      if (avg != null && avg) {
        newvote = Math.round((arr[1]/arr[2])*100)/100;
      } else {
        newvote = arr[1];
      }
      if (origvote != newvote || span.title != arr[3]) {
        span.innerHTML = newvote;
        span.title = arr[3];
        wigoinvalidate();
      }
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }
}

//for new up-down voting
//ajax return value is in the form of "$pollid:$plus:$minus:$zero:$totalvotes:$totaltooltip:$distribtitle:$myvote:$result"
function wigoupdate2(req)
{
  if (req.readyState == 4 && req.status == 200)
  {
    arr = req.responseText.split(':');
    span = document.getElementById(arr[0]);
    if (span)
    {
      origvote = parseInt(span.innerHTML,10);
      plus = arr[1];
      minus = arr[2];
      zero = arr[3];
      newvote = plus - minus;
      newtotal = plus + minus + zero;
      newtotalvotes = arr[4];
      newtotaltooltip = arr[5];
      newdistribtitle = arr[6];
      myvote = arr[7];
      if (origvote != newvote || span.title != newtotaltooltip) {
        span.innerHTML = newvote;
        span.title = newtotaltooltip;
        //update buttons
        upbutton = document.getElementById(arr[0] + "-up");
        neutralbutton = document.getElementById(arr[0] + "-neutral");
        downbutton = document.getElementById(arr[0] + "-down");
        //up
        if (upbutton) {
          if (myvote == 1) {
            //add class
            if (!upbutton.className || upbutton.className == "") {
              upbutton.className = "myvotebutton";
            } else {
              upbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (upbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              upbutton.className = upbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //neutral
        if (neutralbutton) {
          if (myvote == 0) {
            //add class
            if (!neutralbutton.className || neutralbutton.className == "") {
              neutralbutton.className = "myvotebutton";
            } else {
              neutralbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (neutralbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              neutralbutton.className = neutralbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //down
        if (downbutton) {
          if (myvote == -1) {
            //add class
            if (!downbutton.className || downbutton.className == "") {
              downbutton.className = "myvotebutton";
            } else {
              downbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (downbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              downbutton.className = downbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //update distribution bar
        distribtable = document.getElementById(arr[0] + "-dist");
        if (distribtable) {
          distribtable.title = newdistribtitle;
          //calculate percentages
          if (newtotalvotes != 0) {
            uppercent = (plus / newtotalvotes) * 100;
            downpercent = (minus / newtotalvotes) * 100;
            neutralpercent = (zero / newtotalvotes) * 100;
          } else {
            uppercent = 0;
            downpercent = 0;
            neutralpercent = 0;
          }
          if (uppercent != 0) {
            uppercent += "%";
          }
          if (neutralpercent != 0) {
            neutralpercent += "%";
          }
          if (downpercent != 0) {
            downpercent += "%";
          }
          //update widths
          tr = distribtable.getElementsByTagName("tr")[0]
          //tr.style.display = "";
          tds = tr.getElementsByTagName("td");
          tds[0].style.display = "";
          tds[1].style.display = "";
          tds[2].style.display = "";
          tds[0].style.width = uppercent;
          tds[1].style.width = neutralpercent;
          tds[2].style.width = downpercent;
        }
        wigoinvalidate();
      }
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }
}

function wigoinvalidated(req)
{
  if (req.readyState == 4 && req.status == 200)
  {
    if (req.responseText != "ok") {
      alert('An error occured while invalidating the cache' + req.responseText);
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }

}

function wigoinvalidate() {
  sajax_do_call('wigoinvalidate',[wgPageName],wigoinvalidated);
}

function wigovoteup(voteid)
{
  sajax_do_call('wigovote2',[voteid,1],wigoupdate2);
}

function wigovotedown(voteid)
{
  sajax_do_call('wigovote2',[voteid,-1],wigoupdate2);
}

function wigovotereset(voteid)
{
  sajax_do_call('wigovote2',[voteid,0],wigoupdate2);
}

function wigoupdateavg(req)
{
  wigoupdate(req,true);
}

function wigovotesend(voteid,val,min,max)
{
  if (val < min || val > max) {
    alert('Invalid value');
  } else if (voteid.substr(0,6) != 'slider') {
    alert('Invalid vote id');
  } else {
    sajax_do_call('wigovote',[voteid,val,min,max],wigoupdateavg);
  }
}

/*]]>*/