MediaWiki:ArticleRating.js

/** * @name           ArticleRating * @description    Creates an article rating system * @author         KhangND */ (function {   var config = mw.config.get([ 'wgNamespaceNumber', 'wgArticleId', 'wgUserId' ]);

/* @protections */ if(config.wgNamespaceNumber !== 0  // not article    || config.wgUserId === null         // not user    || window.RatingLoaded) { return; } window.RatingLoaded = true;

var api = new mw.Api, storage = 'Template:Rating-storage', options = {}, ratingInfo = {}; ratingInfo[config.wgArticleId] = {}, ratingInfo[config.wgArticleId][config.wgUserId] = 0;

/**    * @method      preload * @description Preloads options and retrieve article rating data * @param      {Object} i18nLoaded * @return     {void} */   function preload(i18nLoaded) { options = $.extend({           values: ['Worst', 'Bad', 'Average', 'Good', 'Great']        }, window.ArticleRating); // Retrieve article rating data from storage api.get({           action: 'parse',            text: '',            prop: 'text',            disablepp: true        }).done(function(data) {            data = $(data.parse.text['*']);            if(data.find('a').length) // Storage doesn't exist -> exit                return;

data = JSON.parse(data.text); ratingInfo = $.extend(ratingInfo, data); init(ratingInfo); });   }

/**    * @method      init * @description Initiates UI and display rating data * @param      {Object} * @return     {void} */   function init(data) { $(' ', {           'class': 'rail-module',            id: 'rating-module',            prependTo: '#WikiaRail',            append: [                $(' ', { text: 'Rate This Article' }),                $('', { id: 'rating-stars' }),                $(' ',{ id: 'rating-info', append: [ $(' ', {                           id: 'rating-my',                            text: 'My rating: ',                            append: $('', { text: '-' })                        }), $(' ', {                           id: 'rating-text',                            append: $('', { text: '-' })                        }), $(' ', {                           id: 'rating-total',                            text: 'Total: ',                            append: $('', { text: '-' })                        }), $(' ', {                           id: 'rating-avg',                            text: 'Average: ',                            append: $('', { text: '-' })                        }) ]               }),                $(' ', { id: 'rating-footer', append: [ $(' ', { text: 'Powered by ArticleRating'}), $('', {                           href: '//dev.fandom.com/wiki/ArticleRating',                            text: 'Documentation'                        }), ' | ',                       $('', {                            href: '//dev.fandom.com/wiki/Talk:ArticleRating',                            text: 'Report bug'                        }) ]               })            ]        });

for(var i = 1; i <= 5; i++) { var $star = $('', {               'data-rating': i,                text: '★',                appendTo: '#rating-stars',                hover: hover,                click: rate            }); if(i <= ratingInfo[config.wgArticleId][config.wgUserId]) $star.addClass('active'); }

display(ratingInfo); }

/**    * @method      display * @description Displays rating info on UI    * @param       {Object} ratingInfo * @return     {void} */   function display(ratingInfo) { var $myrate = $('#rating-my > b'), $total = $('#rating-total > b'), $avg   = $('#rating-avg > b'), $text  = $('#rating-text > b'), value = ratingInfo[config.wgArticleId][config.wgUserId], total = Object.keys(ratingInfo[config.wgArticleId]).length, sum  = 0; $.each(           Object.values(ratingInfo[config.wgArticleId]),            function(i, v) {                sum += v;            }        );

if(typeof value === 'string') value = parseInt(value); if(value === 0) return;

$myrate.text(value); $text .text(options.values[value - 1]); $total .text(total); $avg  .text((sum / total).toFixed(1)); }

/* @handlers: rate, hover */ function rate { $(this).addClass('active'); $(this).prevAll.addClass('active'); $(this).nextAll.removeClass('active');

// sets rating value ratingInfo[config.wgArticleId][config.wgUserId] = $(this).data('rating'); // saves to storage api.post({           action: 'edit',            title: storage,            minor: true,            token: mw.user.tokens.get('editToken'),            text: JSON.stringify(ratingInfo)        }).done(function {            display(ratingInfo);        }); }

function hover { $(this).toggleClass('hover'); $(this).prevAll.toggleClass('hover'); }

importArticles({       type: 'style',        articles: 'u:dev:MediaWiki:Rating.css'    }, {        type: 'script',        articles: ''    }); preload; });