Personal tools

Sponsorname

Your company could advertise here

www.fla-consulting.eu

MediaWiki:Common.js: Difference between revisions

From GxPlex

Jump to: navigation, search
feat: VoteNY toggle button – restore star view after voting
 
fix: use post-vote display HTML for star toggle instead of pre-vote snapshot
Line 8: Line 8:
}
}


// Capture star HTML before the vote request fires
$( document ).ajaxComplete( function ( event, xhr, settings ) {
$( 'body' ).on( 'click', '.vote-rating-star', function () {
var params;
var id = $( this ).data( 'vote-id' );
try {
var $c = $( '#rating_' + id );
params = new URLSearchParams( settings.data );
if ( !$c.data( 'gplx-stars-html' ) ) {
} catch ( e ) {
$c.data( 'gplx-stars-html', $c.html() );
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;
}
}
} );


// Watch each rating container; inject toggle button when score replaces stars
var displayHtml = data && data.voteny && data.voteny.display;
$( '[id^="rating_"]' ).each( function () {
var pageId = params.get( 'pageId' );
var el = this;
if ( !displayHtml || !pageId ) {
new MutationObserver( function () {
return;
var $el = $( el );
}
if (
 
$el.data( 'gplx-stars-html' ) &&
var $el = $( '#rating_' + pageId );
!$el.find( '.vote-rating-star' ).length &&
$el.data( 'gplx-stars-html', displayHtml );
!$el.find( '.gplx-vote-toggle' ).length
 
) {
if ( !$el.find( '.vote-rating-star' ).length && !$el.find( '.gplx-vote-toggle' ).length ) {
$el.append(
$el.append(
$( '<button>' )
$( '<button>' )
.addClass( 'gplx-vote-toggle' )
.addClass( 'gplx-vote-toggle' )
.text( 'Show rating' )
.text( 'Show rating' )
.on( 'click', function () {
.on( 'click', function () {
var html = $el.data( 'gplx-stars-html' );
$el.html( $el.data( 'gplx-stars-html' ) );
$el.removeData( 'gplx-stars-html' );
$el.removeData( 'gplx-stars-html' );
$el.html( html );
} )
} )
);
);
}
}
} ).observe( el, { childList: true } );
} );
} );
} );
} );


}( 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 ) );