mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/Toolbar.js
Stephen Niedzielski edb4385345 Hygiene: extract ToggleList to a reusable component
A list that toggles visibility via the checkbox hack is needed in at
least two spots: the page actions overflow menu and the user menu. This
patch makes several refactors to turn what was previously hardcoded into
page actions a reusable component:

- Start a new components directory. Components are reusable and
  composable. The subdirectories are organized by function, not
  ResourceLoader module bundling which greatly improves the ability to
  see a component's full functionality in one directory instead of
  examining the entire codebase. See updates to README.

- Extract pageactions.less into:
  - ToggleList.less: LESS for any checkbox hack list.
  - DropDownList.less: LESS for lists that open downwards.
  - MenuListItem.less: LESS for list items of menus.
  The division makes it easier to see concerns, dependencies, and change
  code.

- Move pageActionMenu.mustache to a component and extract ToggleList
  template.

- Extract ToggleList.js from Toolbar.js.

Bug: T214540
Change-Id: I171831469a6733c458bc5c7ba249a5096ca975b8
2019-07-04 13:48:13 -06:00

114 lines
3.4 KiB
JavaScript

( function ( M ) {
var
mobile = M.require( 'mobile.startup' ),
ToggleList = M.require( 'skins.minerva.scripts/ToggleList' ),
downloadPageAction = M.require( 'skins.minerva.scripts/downloadPageAction' ),
Icon = mobile.Icon,
skin = M.require( 'mobile.init/skin' ),
/** The top level menu. */
toolbarSelector = '.page-actions-menu',
/** The secondary overflow submenu component container. */
overflowSubmenuSelector = '#page-actions-overflow',
overflowListSelector = '.toggle-list__list';
/**
* @param {Window} window
* @param {Element} toolbar
* @param {OO.EventEmitter} eventBus
* @return {void}
*/
function bind( window, toolbar, eventBus ) {
var overflowSubmenu = toolbar.querySelector( overflowSubmenuSelector );
if ( overflowSubmenu ) {
ToggleList.bind( window, overflowSubmenu, eventBus, true );
}
}
/**
* @param {Window} window
* @param {Element} toolbar
* @return {void}
*/
function render( window, toolbar ) {
var
overflowSubmenu = toolbar.querySelector( overflowSubmenuSelector ),
overflowList = toolbar.querySelector( overflowListSelector );
renderEditButton();
renderDownloadButton( window, overflowList );
if ( overflowSubmenu ) {
ToggleList.render( overflowSubmenu, true );
}
}
/**
* Initialize page edit action link (#ca-edit)
*
* Mark the edit link as disabled if the user is not actually able to edit the page for some
* reason (e.g. page is protected or user is blocked).
*
* Note that the link is still clickable, but clicking it will probably open a view-source
* form or display an error message, rather than open an edit form.
*
* FIXME: Review this code as part of T206262
*
* @ignore
*/
function renderEditButton() {
var
// FIXME: create a utility method to generate class names instead of
// constructing temporary objects. This affects disabledEditIcon,
// enabledEditIcon, enabledEditIcon, and disabledClass and
// a number of other places in the code base.
disabledEditIcon = new Icon( {
name: 'edit',
glyphPrefix: 'minerva'
} ),
enabledEditIcon = new Icon( {
name: 'edit-enabled',
glyphPrefix: 'minerva'
} ),
enabledClass = enabledEditIcon.getGlyphClassName(),
disabledClass = disabledEditIcon.getGlyphClassName();
if ( mw.config.get( 'wgMinervaReadOnly' ) ) {
// eslint-disable-next-line no-jquery/no-global-selector
$( '#ca-edit' )
.removeClass( enabledClass )
.addClass( disabledClass );
}
}
/**
* Initialize and inject the download button
*
* There are many restrictions when we can show the download button, this function should handle
* all device/os/operating system related checks and if device supports printing it will inject
* the Download icon
* @param {Window} window
* @param {Element|null} overflowList
* @return {void}
*/
function renderDownloadButton( window, overflowList ) {
var $downloadAction = downloadPageAction( skin,
mw.config.get( 'wgMinervaDownloadNamespaces', [] ), window, !!overflowList );
if ( $downloadAction ) {
if ( overflowList ) {
$downloadAction.appendTo( overflowList );
} else {
$downloadAction.insertAfter( '.page-actions-menu__list-item:first-child' );
}
mw.track( 'minerva.downloadAsPDF', {
action: 'buttonVisible'
} );
}
}
M.define( 'skins.minerva.scripts/Toolbar', {
selector: toolbarSelector,
bind: bind,
render: render
} );
}( mw.mobileFrontend ) );