Add BharatpediaNeo three-dot menu support to Bharat Mitra |
Make the BharatpediaNeo menu entry reliable in Bharat Mitra |
||
| Line 99: | Line 99: | ||
} | } | ||
$( function () { | $( function () { | ||
/* BharatpediaNeo skin: put the entry in the skin's own three-dot (more) menu */ | /* 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 neoMenu = document.querySelector( '.bpneo-title-actions .bpneo-action-menu' ); | ||
var neoBtn = document.querySelector( '.bpneo-title-actions .bpneo-menu-btn' ); | var neoBtn = document.querySelector( '.bpneo-title-actions .bpneo-menu-btn' ); | ||
if ( neoMenu | 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 ); | |||
} ); | |||
neoMenu.appendChild( neoItem ); | |||
return true; | |||
} | } | ||
function placeNormal() { | |||
var placed = null, placedPortlet = ''; | var placed = null, placedPortlet = ''; | ||
[ 'p-views', 'p-cactions', 'p-actions', 'p-personal' ].forEach( function ( portlet ) { | [ 'p-views', 'p-cactions', 'p-actions', 'p-personal' ].forEach( function ( portlet ) { | ||
| Line 135: | Line 136: | ||
$( document.body ).append( btn ); | $( document.body ).append( btn ); | ||
} | } | ||
} | |||
if ( mw.config.get( 'wgSkin' ) === 'bharatpedianeo' ) { | |||
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 */ | /* If an old revision or a diff is being viewed, show the restore bar */ | ||
Revision as of 10:45, 1 November 2026
/* <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: 'xfd', label: 'Put in deletion discussion', 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: 'block', label: 'Block (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();
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 );
} );
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 );
}
}
if ( mw.config.get( 'wgSkin' ) === 'bharatpedianeo' ) {
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 );
}
} ).catch( function () { /* silent */ } );
}
} );
}() );
/* </nowiki> */