Content deleted Content added
m Mr. Stradivarius moved page User:Mr. Stradivarius/ConfirmRollback.js to User:Mr. Stradivarius/gadgets/ConfirmRollback.js without leaving a redirect: in line with my other gadgets
allow the default MediaWiki confirmation dialog on mobile devices
Line 80: Line 80:
if ( rollbackStatus === 'hide' ) {
if ( rollbackStatus === 'hide' ) {
$rollbackLinks.css( 'display', 'none' );
$rollbackLinks.css( 'display', 'none' );
} else if ( rollbackStatus === 'allow' ) {
} else if ( rollbackStatus === 'allow' || deviceType === 'mobile' ) {
// MediaWiki adds its own confirmation dialog for rollback clicks on
// mobile devices, so default to using it rather than show the user two
// separate dialogs. Not much we can do from here for users who want to
// always allow rollback links on mobile, unfortunately.
$rollbackLinks.css( 'display', 'inline' );
$rollbackLinks.css( 'display', 'inline' );
} else if ( rollbackStatus === 'confirm' ) {
} else if ( rollbackStatus === 'confirm' ) {

Revision as of 11:41, 26 September 2015

// <nowiki>
/*                            ConfirmRollback
 * 
 * This script allows you to customise your rollback links. You can either
 * allow them, hide them, or ask for confirmation when you click them.
 * Furthermore, you can have different rollback settings for different pages,
 * and different settings for mobile and desktop devices.
 * 
 * To install, add the following to your [[Special:MyPage/skin.js]]:

importScript('User:Mr. Stradivarius/gadgets/ConfirmRollback.js') // Linkback: [[User:Mr. Stradivarius/gadgets/ConfirmRollback.js]]

 * See  [[User:Mr. Stradivarius/gadgets/ConfirmRollback]] for documentation.
 */

mw.loader.using( [ 'oojs-ui' ], function () {
	// Initialize variables.
	var pageType, deviceType, deviceDefaults, rollbackStatus;
	var mwConfig = mw.config.get( [ 'wgCanonicalSpecialPageName', 'wgAction' ] );
	var defaultConfig = {
		desktop: {
			watchlist: 'confirm',
			contributions: 'allow',
			recentchanges: 'allow',
			relatedchanges: 'allow',
			history: 'allow',
			diff: 'allow'
		},
		mobile: {
			watchlist: 'confirm',
			contributions: 'confirm',
			recentchanges: 'confirm',
			relatedchanges: 'confirm',
			history: 'confirm',
			diff: 'confirm'
		}
	};
	var scriptConfig = window.ConfirmRollback || {};

	// Find the page type, and exit if we are not viewing a page on which
	// rollback links can appear.
	if ( mwConfig.wgCanonicalSpecialPageName === 'Watchlist' ) {
		pageType = 'watchlist';
	} else if ( mwConfig.wgCanonicalSpecialPageName === 'Contributions' ) {
		pageType = 'contributions';
	} else if ( mwConfig.wgCanonicalSpecialPageName === 'Recentchanges' ) {
		pageType = 'recentchanges';
	} else if ( mwConfig.wgCanonicalSpecialPageName === 'Recentchangeslinked' ) {
		pageType = 'relatedchanges';
	} else if ( mwConfig.wgAction === 'history' ) {
		pageType = 'history';
	} else if ( $( location ).attr( 'href' ).indexOf( '&diff' ) !== -1 ) {
		pageType = 'diff';
	} else {
		return;
	}

	// Get device-specific config.
	deviceType = navigator.userAgent.match( /(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i ) ? 'mobile' : 'desktop';
	deviceDefaults = defaultConfig[ deviceType ];

	// Find the status of the rollback links on this page.
	function resolveRollbackStatus ( cfg ) {
		var ret;
		if ( typeof( cfg ) === 'object' ) {
			if ( cfg.mobile || cfg.desktop ) {
				return resolveRollbackStatus( cfg[ deviceType ] );
			} else {
				ret = cfg[ pageType ];
			}
		} else if ( cfg === 'hide' || cfg === 'allow' || cfg === 'confirm' ) {
			ret = cfg;
		}
		return ret || deviceDefaults[ pageType ] || 'confirm';
	}
	rollbackStatus = resolveRollbackStatus( scriptConfig, defaultConfig, deviceType );

	// Update the rollback links.
	var $rollbackLinks = $( '.mw-rollback-link' );
	if ( rollbackStatus === 'hide' ) {
		$rollbackLinks.css( 'display', 'none' );
	} else if ( rollbackStatus === 'allow' || deviceType === 'mobile' ) {
		// MediaWiki adds its own confirmation dialog for rollback clicks on
		// mobile devices, so default to using it rather than show the user two
		// separate dialogs. Not much we can do from here for users who want to
		// always allow rollback links on mobile, unfortunately.
		$rollbackLinks.css( 'display', 'inline' );
	} else if ( rollbackStatus === 'confirm' ) {
		// Initialize the dialog object.
		var confirmationDialog = new OO.ui.MessageDialog();
		var windowManager = new OO.ui.WindowManager();
		$( 'body' ).append( windowManager.$element );
		windowManager.addWindows( [ confirmationDialog ] );

		// Display rollback links if they are hidden.
		$rollbackLinks.css( 'display', 'inline' );

		// On a rollback click, open a confirmation dialog.
		$rollbackLinks.click( function ( event ) {
			event.preventDefault();
			windowManager.openWindow( confirmationDialog, {
				title: 'Confirm rollback',
				message: 'Are you sure you want to roll back this edit?',
				actions: [
					{ action: 'rollback', label: 'Roll back', flags: [ 'primary', 'destructive' ] },
					{ label: 'Cancel' }
				]
			} ).then( function ( opening ) {
				opening.then( function ( opened ) {
					opening.then( function ( closing, data ) {
						// The dialog is open. Check for the rollback action
						// and if detected, follow the original rollback link
						// URL.
						if ( data && data.action === 'rollback' ) {
							window.open( $( event.target ).attr( 'href' ), '_top' );
						}
					} );
				} );
			} );
		} );
	}
});

// </nowiki>