Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* <nowiki> */
/* Bharat Mitra: loader and menu */
( function () {
	'use strict';
	if ( !mw.config.get( 'wgUserName' ) ) { return; }
	var BM = window.BharatMitra = window.BharatMitra || { modules: {}, loaded: {} };
	if ( BM.menuInit ) { return; }
	BM.menuInit = true;

	function targetUser() {
		var u = mw.config.get( 'wgRelevantUserName' );
		if ( u ) { return u; }
		var ns = mw.config.get( 'wgNamespaceNumber' );
		if ( ns === 2 || ns === 3 ) { return mw.config.get( 'wgTitle' ).split( '/' )[ 0 ]; }
		return null;
	}
	function isRootUserPage() {
		var ns = mw.config.get( 'wgNamespaceNumber' );
		return ns === 3 || ( ns === 2 && mw.config.get( 'wgTitle' ).indexOf( '/' ) === -1 );
	}
	function isSysop() {
		return ( mw.config.get( 'wgUserGroups' ) || [] ).indexOf( 'sysop' ) !== -1;
	}
	function contentPage() {
		var ns = mw.config.get( 'wgNamespaceNumber' );
		if ( [ 0, 6, 10, 14 ].indexOf( ns ) !== -1 ) { return true; }
		return !!mw.config.get( 'wgIsRedirect' ) && ns >= 0 && ns % 2 === 0 && ns !== 2 && ns !== 8;
	}
	var ITEMS = [
		{ name: 'welcome', label: 'Welcome', show: function () { return !!targetUser() && isRootUserPage(); } },
		{ name: 'warn', label: 'Warn', show: function () { return !!targetUser() && isRootUserPage(); } },
		{ name: 'revert', label: 'Revert / Undo', show: function () {
			return mw.config.get( 'wgNamespaceNumber' ) >= 0 && !!mw.config.get( 'wgCurRevisionId' );
		} },
		{ name: 'speedy', label: 'Nominate for speedy deletion', show: contentPage },
		{ name: 'speedydelete', label: 'Delete page now (admin)', show: function () {
			return isSysop() && contentPage();
		}, action: function () {
			loadModule( 'speedy' ).then( function () {
				if ( BM.modules.speedy && BM.modules.speedy.openDelete ) { BM.modules.speedy.openDelete(); }
				else { mw.notify( 'This part could not be loaded.', { type: 'error' } ); }
			} ).catch( function () {
				mw.notify( 'This part could not be loaded.', { type: 'error' } );
			} );
		} },
		{ name: 'xfd', label: 'Put in deletion discussion', show: contentPage },
		{ name: 'xfd', label: 'Vote / close discussion', show: function () {
			var pn = mw.config.get( 'wgPageName' );
			return pn.indexOf( 'Bharatpedia:Articles_for_deletion/' ) === 0 ||
				pn.indexOf( 'Bharatpedia:Miscellany_for_deletion/' ) === 0;
		} },
		{ name: 'tag', label: 'Add maintenance tags', show: contentPage },
		{ name: 'protect', label: 'Request protection', show: function () {
			return mw.config.get( 'wgNamespaceNumber' ) >= 0;
		} },
		{ name: 'report', label: 'Report to administrators\' noticeboard', show: function () {
			return mw.config.get( 'wgNamespaceNumber' ) >= 0;
		} },
		{ name: 'redirect', label: 'Redirect tools', show: function () {
			return mw.config.get( 'wgNamespaceNumber' ) >= 0;
		} },
		{ name: 'autointerlink', label: 'Auto interlinker', show: function () {
			var bmNs = mw.config.get( 'wgNamespaceNumber' );
			return ( bmNs === 0 || bmNs === 118 ) && !mw.config.get( 'wgIsRedirect' );
		}, action: function () {
			window.open( mw.util.getUrl( 'Bharatpedia:Auto interlinker', { page: mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) } ), '_blank' );
		} },
		{ name: 'barefix', label: 'Bare reference fixer', show: function () {
			var bmNs = mw.config.get( 'wgNamespaceNumber' );
			return ( bmNs === 0 || bmNs === 118 ) && !mw.config.get( 'wgIsRedirect' );
		}, action: function () {
			window.open( mw.util.getUrl( 'Bharatpedia:Bare reference fixer', { page: mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) } ), '_blank' );
		} },
		{ name: 'archivebot', label: 'Request archive links', show: function () {
			return mw.config.get( 'wgNamespaceNumber' ) === 0 && !mw.config.get( 'wgIsRedirect' );
		}, action: function () {
			var bmPage = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
			if ( !window.confirm( 'Add "' + bmPage + '" to the BharatwikiArchiveBot request queue?' ) ) { return; }
			var bmApi = new mw.Api();
			var bmReqTitle = 'User:BharatwikiArchiveBot/Requests';
			bmApi.get( { action: 'parse', page: bmReqTitle, prop: 'sections' } ).then( function ( bmData ) {
				var bmSec = null;
				( bmData.parse.sections || [] ).forEach( function ( bmS ) {
					if ( bmS.line === 'New requests' ) { bmSec = bmS.index; }
				} );
				if ( !bmSec ) { mw.notify( 'The request queue section was not found.', { type: 'error' } ); return null; }
				return bmApi.get( { action: 'parse', page: bmReqTitle, section: bmSec, prop: 'wikitext' } ).then( function ( bmSecData ) {
					var bmText = ( bmSecData.parse && bmSecData.parse.wikitext && bmSecData.parse.wikitext[ '*' ] ) || '';
					if ( bmText.indexOf( '[[' + bmPage + ']]' ) !== -1 ) {
						mw.notify( '"' + bmPage + '" is already in the archive request queue.', { type: 'warn' } );
						return null;
					}
					return bmApi.postWithToken( 'csrf', {
						action: 'edit',
						title: bmReqTitle,
						section: bmSec,
						appendtext: '\n* [[' + bmPage + ']] ~~~~',
						summary: 'Request archive links for [[' + bmPage + ']] via Bharat Mitra'
					} ).then( function () {
						mw.notify( '"' + bmPage + '" added to the archive request queue.' );
					} );
				} );
			} ).catch( function () {
				mw.notify( 'The archive request could not be added.', { type: 'error' } );
			} );
		} },
		{ name: 'block', label: 'Block and notify (admin)', show: function () {
			return isSysop() && !!targetUser() && isRootUserPage();
		} },
		{ name: 'batch', label: 'Batch delete (admin)', show: function () { return isSysop(); } },
		{ name: 'config', label: 'Settings', show: function () { return true; } }
	];

	function loadModule( name ) {
		if ( BM.loaded[ name ] && BM.modules[ name ] ) {
			return $.Deferred().resolve().promise();
		}
		return mw.loader.using( [ 'ext.gadget.BharatMitra-core', 'ext.gadget.BharatMitra-' + name ] )
			.then( function () { BM.loaded[ name ] = true; } );
	}
	BM.loadModule = loadModule;

	var menu = null;
	function closeMenu() { if ( menu ) { menu.hide(); } }
	function buildMenu( anchor ) {
		if ( !menu ) {
			menu = $( '<div>' ).attr( 'id', 'bm-menu' ).hide();
			$( document.body ).append( menu );
			$( document ).on( 'click', function ( e ) {
				if ( menu.is( ':visible' ) && !$( e.target ).closest( '#bm-menu, #ca-bharatmitra, .bm-float' ).length ) {
					closeMenu();
				}
			} );
			$( document ).on( 'keydown', function ( e ) { if ( e.key === 'Escape' ) { closeMenu(); } } );
		}
		menu.empty();
		ITEMS.forEach( function ( item ) {
			if ( !item.show() ) { return; }
			var b = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-menu-item' ).text( item.label );
			b.on( 'click', function () {
				closeMenu();
				if ( item.action ) { item.action(); return; }
				loadModule( item.name ).then( function () {
					if ( BM.modules[ item.name ] ) { BM.modules[ item.name ].open(); }
					else { mw.notify( 'This part could not be loaded.', { type: 'error' } ); }
				} ).catch( function () {
					mw.notify( 'This part could not be loaded.', { type: 'error' } );
				} );
			} );
			menu.append( b );
		} );
		var help = $( '<a>' ).addClass( 'bm-menu-item bm-menu-link' )
			.attr( 'href', mw.util.getUrl( 'Bharatpedia:Bharat Mitra' ) ).text( 'Bharat Mitra help' );
		menu.append( help );
		var rect = anchor.getBoundingClientRect();
		menu.css( {
			top: ( rect.bottom + window.scrollY + 4 ) + 'px',
			left: Math.max( 8, Math.min( rect.left + window.scrollX, window.innerWidth - 292 ) ) + 'px'
		} );
		menu.toggle();
	}
	$( function () {
		/* BharatpediaNeo skin: put the entry in the skin's own three-dot (more) menu.
		   The skin settles its menu late, so keep trying for a while and
		   also add the entry at the moment the menu button is clicked. */
		function placeNeoItem() {
			var neoMenu = document.querySelector( '.bpneo-title-actions .bpneo-action-menu' );
			var neoBtn = document.querySelector( '.bpneo-title-actions .bpneo-menu-btn' );
			if ( !neoMenu || !neoBtn ) { return false; }
			if ( neoMenu.querySelector( '#ca-bharatmitra' ) ) { return true; }
			var neoItem = document.createElement( 'a' );
			neoItem.href = '#';
			neoItem.id = 'ca-bharatmitra';
			neoItem.textContent = '\u2605 Bharat Mitra';
			neoItem.addEventListener( 'click', function ( e ) {
				e.preventDefault();
				neoMenu.style.display = 'none';
				buildMenu( neoBtn );
			} );
			var printSection = null;
			var sections = neoMenu.querySelectorAll( '.bpneo-menu-section' );
			for ( var s = 0; s < sections.length; s++ ) {
				if ( /print/i.test( sections[ s ].textContent ) ) { printSection = sections[ s ]; break; }
			}
			if ( printSection ) { neoMenu.insertBefore( neoItem, printSection ); }
			else { neoMenu.appendChild( neoItem ); }
			return true;
		}
		function placeNormal() {
			var placed = null, placedPortlet = '';
			[ 'p-views', 'p-cactions', 'p-actions', 'p-personal' ].forEach( function ( portlet ) {
				if ( placed ) { return; }
				try {
					var li = mw.util.addPortletLink( portlet, '#', '\u2605 Bharat Mitra', 'ca-bharatmitra', 'Open the Bharat Mitra tools' );
					if ( li ) { placed = $( li ).find( 'a' ).first(); placedPortlet = portlet; }
				} catch ( e ) { /* next portlet */ }
			} );
			if ( placed && placed.length ) {
				if ( placedPortlet !== 'p-personal' ) { placed.addClass( 'bm-star-link' ); }
				placed.on( 'click', function ( e ) { e.preventDefault(); buildMenu( this ); } );
			} else {
				var btn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-float' ).text( '\u2605 Bharat Mitra' );
				btn.on( 'click', function () { buildMenu( this ); } );
				$( document.body ).append( btn );
			}
		}
		var isNeo = mw.config.get( 'wgSkin' ) === 'bharatpedianeo' || ( document.body && document.body.classList.contains( 'skin-bharatpedianeo' ) );
		if ( isNeo ) {
			if ( !placeNeoItem() ) {
				var neoTries = 0;
				var neoTimer = setInterval( function () {
					neoTries++;
					if ( placeNeoItem() || neoTries > 60 ) {
						clearInterval( neoTimer );
						if ( !document.getElementById( 'ca-bharatmitra' ) ) { placeNormal(); }
					}
				}, 250 );
			}
			document.addEventListener( 'click', function ( e ) {
				if ( e.target.closest && e.target.closest( '.bpneo-menu-btn' ) ) { placeNeoItem(); }
			}, true );
		} else {
			placeNormal();
		}
		/* If an old revision or a diff is being viewed, show the restore bar */
		var oldid = 0;
		var cur = mw.config.get( 'wgCurRevisionId' );
		if ( mw.config.get( 'wgDiffOldId' ) ) { oldid = mw.config.get( 'wgDiffOldId' ); }
		else if ( mw.config.get( 'wgRevisionId' ) && cur && mw.config.get( 'wgRevisionId' ) < cur ) {
			oldid = mw.config.get( 'wgRevisionId' );
		}
		if ( oldid && cur && oldid !== cur && mw.config.get( 'wgNamespaceNumber' ) >= 0 ) {
			loadModule( 'revert' ).then( function () {
				if ( BM.modules.revert && BM.modules.revert.addRestoreLink ) {
					BM.modules.revert.addRestoreLink( oldid );
				}
				if ( BM.modules.revert && BM.modules.revert.addDiffRevertLink && mw.config.get( 'wgDiffNewId' ) ) {
					BM.modules.revert.addDiffRevertLink();
				}
			} ).catch( function () { /* silent */ } );
		}
		/* On a draft page, show a notice when a mainspace article with the same name exists */
		if ( mw.config.get( 'wgNamespaceNumber' ) === 118 && !mw.config.get( 'wgIsRedirect' ) ) {
			var bmAction = mw.config.get( 'wgAction' );
			if ( bmAction === 'view' || bmAction === 'edit' || bmAction === 'submit' ) {
				loadModule( 'draftmatch' ).then( function () {
					if ( BM.modules.draftmatch && BM.modules.draftmatch.check ) {
						BM.modules.draftmatch.check();
					}
				} ).catch( function () { /* silent */ } );
			}
		}
		/* On the redirect special pages, show the batch redirect tools */
		if ( mw.config.get( 'wgNamespaceNumber' ) === -1 ) {
			var bmSpecial = mw.config.get( 'wgCanonicalSpecialPageName' );
			if ( bmSpecial === 'BrokenRedirects' || bmSpecial === 'DoubleRedirects' ) {
				loadModule( 'specialredirects' ).then( function () {
					if ( BM.modules.specialredirects && BM.modules.specialredirects.check ) {
						BM.modules.specialredirects.check();
					}
				} ).catch( function () { /* silent */ } );
			}
		}
	} );
}() );
/* </nowiki> */