MediaWiki:Voting.js

// Voting by Duckey Detective var voting = { init: function init { //If is on list of allowed namespaces if (voting.config.enabled.allowedNamespaces.indexOf(wgCanonicalNamespace) === -1) { return false; }

//If isn't excluded from voting if (voting.config.enabled.excludedPages.indexOf(wgTitle) !== -1) { return false; }

//If isn't article and it must be   if (voting.config.enabled.mustBeArticle && !wgIsArticle) { return false; }

//If custm check failed if (!voting.config.enabled.customCheck) { return false; }

//uncyclopedia and wowwiki are basically the same as monobook var isMonobook = skin === 'monobook' || skin === 'uncyclopedia' || skin === 'wowwiki';

var starSize = voting.config.style.starSize[skin] ? voting.config.style.starSize[skin] : voting.config.style.starSize.default; var _voting$config$style$ = voting.config.style.transitions, starsColor = _voting$config$style$.starsColor, starsWidth = _voting$config$style$.starsWidth, monobookStatsHeight = _voting$config$style$.monobookStatsHeight;

//Style tag generated by JS

var style = '\n/* Voting by Duckey Detective */\n/* Style tag generated by JS */\n\n.d-voting__stars-wrapper {\n /* Must use data, because it supports custom SVG fills and sizes */\n  background: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%22' + starSize + '%22%20height%3D%22' + starSize + '%22%20viewBox%3D%220%200%20256%20256%22%3E%0A%20%20%3Cpath%20fill%3D%22%23' + voting.config.style.starsColor.empty + '%22%20d%3D%22m56%2C241%2073-226%2073%2C226-192-140h238z%22/%3E%0A%3C/svg%3E") repeat-x;\n  height: ' + starSize + 'px;\n  width: ' + starSize * 5 + 'px;\n  margin: 0 auto;\n  position: relative;\n  overflow: hidden;\n}\n\n.d-voting__stars-inner {\n  /* Must use data, because it supports custom SVG fills and sizes */\n  background: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%22' + starSize + '%22%20height%3D%22' + starSize + '%22%20viewBox%3D%220%200%20256%20256%22%3E%0A%20%20%3Cpath%20fill%3D%22%23' + voting.config.style.starsColor.normal + '%22%20d%3D%22m56%2C241%2073-226%2073%2C226-192-140h238z%22/%3E%0A%3C/svg%3E") repeat-x;\n  height: 100%;\n  width: 0%;\n  -webkit-transition: width ' + starsWidth + ', background-image ' + starsColor + ';\n  -o-transition: width ' + starsWidth + ', background-image ' + starsColor + ';\n  -moz-transition: width ' + starsWidth + ', background-image ' + starsColor + ';\n  transition: width ' + starsWidth + ', background-image ' + starsColor + ';\n}\n\n.d-voting__stars-wrapper:hover .d-voting__stars-inner {\n  /* Must use data, because it supports custom SVG fills and sizes */\n  background: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%22' + starSize + '%22%20height%3D%22' + starSize + '%22%20viewBox%3D%220%200%20256%20256%22%3E%0A%20%20%3Cpath%20fill%3D%22%23' + voting.config.style.starsColor.hover + '%22%20d%3D%22m56%2C241%2073-226%2073%2C226-192-140h238z%22/%3E%0A%3C/svg%3E") repeat-x;\n}\n\n.d-voting__stars-list {\n  list-style: none !important;\n  padding: 0 !important;\n  margin: 0 !important;\n  position: absolute;\n  top: 0; right: 0; bottom: 0; left: 0;\n}\n\n.d-voting__stars-list > li {\n  display: inline-block;\n  width: ' + starSize + 'px;\n  height: ' + starSize + 'px;\n  padding: 0;\n  margin: 0;\n  cursor: pointer;\n}\n\n.d-voting__stats {\n  text-align: center;\n}\n\n.d-voting__stat, #d-voting__rating-name {\n  font-weight: bold;\n}\n\n#d-voting__monobook-see-more, #d-voting__monobook-see-less {\n  font-style: italic;\n  cursor: pointer;\n}\n\n.d-voting__monobook-stats {\n  max-height: 0;\n  overflow: hidden;\n  -webkit-transition: max-height ' + monobookStatsHeight + ';\n  -o-transition: max-height ' + monobookStatsHeight + ';\n  -moz-transition: max-height ' + monobookStatsHeight + ';\n  transition: max-height ' + monobookStatsHeight + ';\n}\n ';

$(style).appendTo('head');

//Clickable stars var stars = [1, 2, 3, 4, 5].map(function (x) {     return '';    }).join('');

//Stars element var starsDisplay = '\n \n' + stars + '\n ';

//Stats var statsDisplay = voting.config.strings.votesavg + ': ' + voting.config.strings.none + ' \n' + voting.config.strings.votescount + ': 0  \n' + voting.config.strings.uservote + ': ' + voting.config.strings.none + '  \n' + (voting.config.disableViews ? '' : voting.config.strings.views + ': 0  ') + '\n' + voting.config.strings.revcount + ': 1 ';

//If oasis use modules if (skin === 'oasis') { var module = '\n\n \n     \n  ' + voting.config.header + ' \n' + starsDisplay + '\n<div class="d-voting__stats" style="margin-top: 0.5em;">\n  <div id="d-voting__rating-name" style="min-height: 14px; margin-bottom: 0.5em;"> \n  ' + statsDisplay + '\n \n ';

$(module).prependTo('#WikiaRail'); }

//If monobook use portlets else if (isMonobook) { var portlet = '<div class="portlet ' + voting.config.style.monobookClasses + '" id="p-voting">\n ' + voting.config.header + ' \n \n ' + starsDisplay + '\n  <div class="d-voting__stats" style="margin-top: 0.5em;">\n    <span id="d-voting__rating-name"> <span id="d-voting__monobook-see-more">' + voting.config.strings.seemore + '  \n    <div class="d-voting__monobook-stats">\n      ' + statsDisplay + ' \n      <div style="margin-top: 0.25em;" id="d-voting__monobook-see-less">' + voting.config.strings.hide + ' \n    \n  \n \n ';

//Monobook portlets can be lang based $(portlet).insertAfter(voting.config.monobookElement);

//Monobook handlers $('#d-voting__monobook-see-more').on('click', function {          $('.d-voting__monobook-stats').css('max-height', 100);          $('#d-voting__monobook-see-more').css('display', 'none');          voting.monobookHidden = false;        });

$('#d-voting__monobook-see-less').on('click', function {          $('.d-voting__monobook-stats').css('max-height', 0);          $('#d-voting__monobook-see-more').css('display', 'inline-block');          voting.monobookHidden = true;        }); }

//Cross-skin handlers $('.d-voting__stars-list > li').on({     mouseenter: function mouseenter {        $('#d-voting__rating-name').html(voting.config.ratings[parseInt($(this).data('rating'))]);        $('#d-voting__monobook-see-more').css('display', 'none');      },      mouseleave: function mouseleave {        $('#d-voting__rating-name').html('');        if (voting.monobookHidden) {          $('#d-voting__monobook-see-more').css('display', 'inline-block');        }      },      click: function click {        voting.vote($(this).data('rating'));      }    });

voting.check; return true; }, check: function check { //Request votes $.ajax({     url: '/api.php?format=json&action=query&list=wkvoteart&wkctime=5&wkuservote=1&wkpage=' + wgArticleId,      method: 'GET',      dataType: 'json'    }).done(function (data) {      voting.stats.uservote = parseInt(data.query.wkvoteart[wgArticleId].uservote);      voting.stats.votesavg = parseFloat(data.query.wkvoteart[wgArticleId].votesavg);      voting.stats.votescount = parseInt(data.query.wkvoteart[wgArticleId].votescount);

voting.update; });

//Request views and edits $.ajax({     url: '/api.php?format=json&action=query&prop=info&inprop=views|revcount&pageids=' + wgArticleId,      method: 'GET',      dataType: 'json'    }).done(function (data) {      voting.stats.views = parseInt(data.query.pages[wgArticleId].views);      voting.stats.revcount = parseInt(data.query.pages[wgArticleId].revcount);

voting.update; }); },  vote: function vote(rating) {    //No need to spam requests    if (voting.stats.uservote === rating) {      return;    }

var action = voting.stats.uservote ? 'update' : 'insert';

//Send vote request $.ajax({     url: '/api.php?format=json&action=' + action + '&list=wkvoteart&wkctime=5&wkuservote=1&wkvote=' + rating + '&wkpage=' + wgArticleId,      method: 'POST',      dataType: 'json'    }).done(function (data) {      voting.stats.votesavg = parseFloat(data.item.wkvoteart.avgvote);      voting.stats.uservote = parseInt(rating);

voting.update; }).fail(function { //Sometimes update request fails, I don't have any idea why $.ajax({       url: '/api.php?format=json&action=insert&list=wkvoteart&wkctime=5&wkuservote=1&wkvote=' + rating + '&wkpage=' + wgArticleId,        method: 'POST',        dataType: 'json'      }).done(function (data) {        voting.stats.votesavg = parseFloat(data.item.wkvoteart.avgvote);        voting.stats.uservote = parseInt(rating);

voting.update; });   });  },  update: function update { $('.d-voting__stars-inner').css('width', voting.stats.votesavg / 5 * 100 + '%');

var stats = ['votesavg', 'votescount', 'uservote', 'views', 'revcount']; for (var _iterator = stats, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator];;) { var _ref;

if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next; if (_i.done) break; _ref = _i.value; }

var stat = _ref;

if (voting.stats[stat] === null || Number.isNaN(voting.stats[stat])) { $('#d-voting__' + stat).html(voting.config.strings.none); } else { //Votesavg need 2 decimal places var roundedStat = stat === 'votesavg' ? parseFloat(Math.round(voting.stats[stat] * 100) / 100).toFixed(2) : voting.stats[stat]; $('#d-voting__' + stat).html(roundedStat); }   }  },  stats: { votesavg: null, votescount: null, uservote: null, views: null, revcount: null }, monobookHidden: true, config: { strings: { none: 'none', votesavg: 'Average vote', votescount: 'Votes', uservote: 'Your vote', views: 'Views', revcount: 'Edits', seemore: 'See more...', hide: '(<a>hide</a>)' },   style: { starsColor: { normal: 'FFDC00', empty: '999999', hover: 'FF851B' },     starSize: { default: 20 },     transitions: { starsWidth: '0.25s', starsColor: '0.25s', monobookStatsHeight: '0.5s' },     monobookClasses: '' },   enabled: { allowedNamespaces: ['', 'File'], excludedPages: [wgMainPageTitle], mustBeArticle: true, customCheck: function customCheck { return true; }   },    monobookElement: '#p-navigation', disableViews: false, header: function header { switch (wgCanonicalNamespace) { case '': return 'Rate article'; case 'File': return 'Rate file'; default: return 'Rate page'; }   },    ratings: function ratings { return { 1: 'Worst', 2: 'Bad', 3: 'Average', 4: 'Good', 5: 'Great' };   }  } };