MediaWiki:Wikia.js/ratings.js

/** * Article Ratings script * * @author Cqm * * @notes Requires abusefilters to work */


 * ( function ( $, mw ) {

'use strict';

/**        * Cache mw.config values */   var conf = mw.config.get( [            'skin',            'wgNamespaceNumber',            'wgMainPageTitle',            'wgPageName',            'wgTitle',            'wgUserName'        ] ), /**        * Checks we can continue */       self = { init: function { // check we're in mainspace and not on the main page if ( conf.wgNamespaceNumber === 0 && conf.wgTitle !== conf.wgMainPageTitle ) { // don't load twice if ( $( '#RatingsModule' ).length ) { return; }

// only load for oasis if ( ['oasis', 'wikia'].indexOf( conf.skin ) === -1 ) { return; }

// wait for sidebar to load before adding anything to it                   if ( $( '#WikiaRail > .module' ).length ) { self.loadForm; } else { setTimeout( self.init, 500 ); }               }            },

/**            * Creates the ratings form *            * Form currently in the sidebar * @todo explore other places we can use it (a/b testing) */           loadForm: function  {

var $form = $( ' ' ) .attr( 'id', 'RatingsModule' ) .addClass( 'module' ) .addClass( 'RatingsModule' ) .append(                       $( ' ' )                            .attr( 'id', 'ar-header' )                            .text( 'Rate this page' ),                        $( ' ' )                            .attr( { 'action': '#', 'id': 'ar-form' } )                           .on( 'submit', self.submitFeedback )                            .append( $( ' ' )                                   .attr( 'id', 'ar-rating' ) .append(                                       $( ' ' )                                            .addClass( 'star' )                                            .text( '☆' ),                                        $( ' ' )                                            .addClass( 'star' )                                            .text( '☆' ),                                        $( ' ' )                                            .addClass( 'star' )                                            .text( '☆' ),                                        $( ' ' )                                            .addClass( 'star' )                                            .text( '☆' ),                                        $( ' ' )                                            .addClass( 'star' )                                            .text( '☆' )                                    ), $( ' ' )                                   .attr( 'id', 'ar-extras' ) .append(                                       $( ' ' )                                            .attr( { 'id': 'ar-comment', 'placeholder': 'Add any comments or suggestions here.', 'maxlength': 2000 } ),                                       $( ' ' )                                            .attr( { 'id': 'ar-submit', 'type': 'submit' } )                                           .val( 'Submit rating' )                                    ) )                   );

if ( !!conf.wgUserName ) { $( '#WikiaRecentActivity' ).after( $form ); } else { $( '#WikiaRail .module' ).first.before( $form ); }

$( '.star' ).on( 'click', function {                    var $this = $( this ),                        $rating = $( '#ar-rating' );

// show textarea for further comment // and submit button $( '#ar-extras' ).show( 400 );

// select the star $rating.children( '.star' ).removeClass( 'selected' ); $this.addClass( 'selected' );

// store the rating // $.fn.data desn't update the DOM $rating.attr( 'data-rating', $this.nextAll.length + 1 ); } );           },

/**            * 'Submits' the feedback to the page */           submitFeedback: function  { var rating = $( '#ar-rating' ).attr( 'data-rating' ), comment = $( '#ar-comment' ).val, // limit comment to 2k characters // fix for non HTML5 browsers - IE8/9 comment2 = comment.length > 2000 ? comment.substring( 0, 2000 ) : comment, toSubmit = JSON.stringify( {                       rating: rating,                        comment: comment2                    } ), api = new mw.Api, params = { action: 'edit', title: 'RuneScape:Feedback/' + mw.util.wikiUrlencode( conf.wgPageName ), text: toSubmit, // token should be last token: mw.user.tokens.get( 'editToken' ) };

api .post( params ) .done( function ( data ) {                       mw.log( data )                    } ) .always( function {                        $( '#ar-form' ).remove;                        $( '#ar-header' ).after( $( ' ' )                               .attr( 'id', 'ar-submitted' ) .text( 'Thanks for your feedback.' ) );                       // do something else here, edit link?                    } );

return false; }       };

mw.loader.using( ['mediawiki.util', 'mediawiki.api'], function {        $( self.init );    } );

}( this.jQuery, this.mediaWiki ) );