mediawiki-extensions-Discus.../modules/overflowMenu.js
Ed Sanders a74c00ba8c Prefer arrow functions for callbacks
This allows us to remove our `this` bindings.

Change-Id: Ie8c8c38d36af8a033b5181870c39f8981a57b939
2024-04-19 12:34:23 +01:00

57 lines
2 KiB
JavaScript

/**
* Build an overflow menu button and items for display adjacent to heading and comment thread items.
*
* @param {jQuery} $container
* @param {ThreadItemSet} pageThreads
*/
function init( $container, pageThreads ) {
mw.loader.using( [ 'oojs-ui-widgets', 'oojs-ui.styles.icons-editing-core' ] ).then( () => {
$container.find( '.ext-discussiontools-init-section-overflowMenuButton' ).each( ( i, button ) => {
// Comment ellipsis
var $threadMarker = $( button ).closest( '[data-mw-thread-id]' );
if ( !$threadMarker.length ) {
// Heading ellipsis
$threadMarker = $( button ).closest( '.ext-discussiontools-init-section' ).find( '[data-mw-thread-id]' );
}
var threadItem = pageThreads.findCommentById( $threadMarker.data( 'mw-thread-id' ) );
var buttonMenu = OO.ui.infuse( button, {
$overlay: true,
menu: {
classes: [ 'ext-discussiontools-init-section-overflowMenu' ],
horizontalPosition: threadItem.type === 'heading' ? 'end' : 'start'
}
} );
mw.loader.using( buttonMenu.getData().resourceLoaderModules || [] ).then( () => {
var itemConfigs = buttonMenu.getData().itemConfigs;
if ( !itemConfigs ) {
// We should never have missing itemConfigs, but if this happens, hide the empty menu
buttonMenu.toggle( false );
return;
}
var overflowMenuItemWidgets = itemConfigs.map( ( itemConfig ) => {
return new OO.ui.MenuOptionWidget( itemConfig );
} );
buttonMenu.getMenu().addItems( overflowMenuItemWidgets );
buttonMenu.getMenu().items.forEach( ( menuItem ) => {
mw.hook( 'discussionToolsOverflowMenuOnAddItem' ).fire( menuItem.getData().id, menuItem, threadItem );
} );
} );
buttonMenu.getMenu().on( 'choose', ( menuItem ) => {
mw.hook( 'discussionToolsOverflowMenuOnChoose' ).fire( menuItem.getData().id, menuItem, threadItem );
} );
} );
$container.find( '.ext-discussiontools-init-section-bar' ).on( 'click', ( e ) => {
// Don't toggle section when clicking on bar
e.stopPropagation();
} );
} );
}
module.exports = {
init: init
};