MediaWiki:Common.js: Difference between revisions
From GxPlex
Gxplex admin (talk | contribs) feat: VoteNY toggle button – restore star view after voting  |
Gxplex admin (talk | contribs) fix: use post-vote display HTML for star toggle instead of pre-vote snapshot |
||
| Line 8: | Line 8: | ||
} | } | ||
$( document ).ajaxComplete( function ( event, xhr, settings ) { | |||
$( | var params; | ||
var | try { | ||
params = new URLSearchParams( settings.data ); | |||
if ( ! | } catch ( e ) { | ||
return; | |||
} | |||
if ( | |||
params.get( 'action' ) !== 'voteny' || | |||
params.get( 'type' ) !== 'stars' || | |||
params.get( 'what' ) !== 'multi' | |||
) { | |||
return; | |||
} | |||
 | |||
var data; | |||
try { | |||
data = JSON.parse( xhr.responseText ); | |||
} catch ( e ) { | |||
return; | |||
} | } | ||
var displayHtml = data && data.voteny && data.voteny.display; | |||
var pageId = params.get( 'pageId' ); | |||
if ( !displayHtml || !pageId ) { | |||
return; | |||
} | |||
 | |||
var $el = $( '#rating_' + pageId ); | |||
$el.data( 'gplx-stars-html', displayHtml ); | |||
 | |||
if ( !$el.find( '.vote-rating-star' ).length && !$el.find( '.gplx-vote-toggle' ).length ) { | |||
$el.append( | |||
$( '<button>' ) | |||
.addClass( 'gplx-vote-toggle' ) | |||
.text( 'Show rating' ) | |||
.on( 'click', function () { | |||
$el.html( $el.data( 'gplx-stars-html' ) ); | |||
$el.removeData( 'gplx-stars-html' ); | |||
} ) | |||
); | |||
} | |||
} | |||
} ); | } ); | ||
} ); | } ); | ||
}( jQuery, mediaWiki ) ); | }( jQuery, mediaWiki ) ); | ||
Revision as of 16:05, 22 June 2026
/* VoteNY: restore star view after voting */
( function ( $, mw ) {
'use strict';
$( function () {
if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) {
return;
}
$( document ).ajaxComplete( function ( event, xhr, settings ) {
var params;
try {
params = new URLSearchParams( settings.data );
} catch ( e ) {
return;
}
if (
params.get( 'action' ) !== 'voteny' ||
params.get( 'type' ) !== 'stars' ||
params.get( 'what' ) !== 'multi'
) {
return;
}
var data;
try {
data = JSON.parse( xhr.responseText );
} catch ( e ) {
return;
}
var displayHtml = data && data.voteny && data.voteny.display;
var pageId = params.get( 'pageId' );
if ( !displayHtml || !pageId ) {
return;
}
var $el = $( '#rating_' + pageId );
$el.data( 'gplx-stars-html', displayHtml );
if ( !$el.find( '.vote-rating-star' ).length && !$el.find( '.gplx-vote-toggle' ).length ) {
$el.append(
$( '<button>' )
.addClass( 'gplx-vote-toggle' )
.text( 'Show rating' )
.on( 'click', function () {
$el.html( $el.data( 'gplx-stars-html' ) );
$el.removeData( 'gplx-stars-html' );
} )
);
}
} );
} );
}( jQuery, mediaWiki ) );