2021-09-09 22:13:48 +00:00
|
|
|
<?php
|
2022-02-19 00:53:42 +00:00
|
|
|
|
2022-05-23 07:32:40 +00:00
|
|
|
namespace MediaWiki\Skins\Vector;
|
2022-02-19 00:53:42 +00:00
|
|
|
|
2022-10-27 16:09:12 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-12-07 01:05:22 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentDropdown;
|
2022-10-27 23:52:23 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentMainMenu;
|
2022-12-02 18:09:52 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentPageTools;
|
2022-10-20 21:32:07 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentPinnableHeader;
|
2022-10-27 16:11:32 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentSearchBox;
|
2022-12-07 00:42:31 +00:00
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentStickyHeader;
|
|
|
|
use MediaWiki\Skins\Vector\Components\VectorComponentTableOfContents;
|
2022-10-27 16:09:12 +00:00
|
|
|
|
2021-09-09 22:13:48 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Skins
|
|
|
|
* @package Vector
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class SkinVector22 extends SkinVector {
|
2022-07-11 15:10:26 +00:00
|
|
|
private const STICKY_HEADER_ENABLED_CLASS = 'vector-sticky-header-enabled';
|
2022-03-17 23:02:39 +00:00
|
|
|
|
2022-10-27 21:18:53 +00:00
|
|
|
/**
|
|
|
|
* Show the ULS button if it's modern Vector, languages in header is enabled,
|
|
|
|
* and the ULS extension is enabled. Hide it otherwise.
|
|
|
|
* There is no point in showing the language button if ULS extension is unavailable
|
|
|
|
* as there is no ways to add languages without it.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldHideLanguages(): bool {
|
|
|
|
return !$this->isLanguagesInContent() || !$this->isULSExtensionEnabled();
|
|
|
|
}
|
|
|
|
|
2022-10-27 19:36:08 +00:00
|
|
|
/**
|
|
|
|
* Determines if the language switching alert box should be in the sidebar.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function shouldLanguageAlertBeInSidebar(): bool {
|
|
|
|
$featureManager = VectorServices::getFeatureManager();
|
|
|
|
$isMainPage = $this->getTitle() ? $this->getTitle()->isMainPage() : false;
|
|
|
|
$shouldShowOnMainPage = $isMainPage && !empty( $this->getLanguagesCached() ) &&
|
|
|
|
$featureManager->isFeatureEnabled( Constants::FEATURE_LANGUAGE_IN_MAIN_PAGE_HEADER );
|
|
|
|
return ( $this->isLanguagesInContentAt( 'top' ) && !$isMainPage && !$this->shouldHideLanguages() &&
|
|
|
|
$featureManager->isFeatureEnabled( Constants::FEATURE_LANGUAGE_ALERT_IN_SIDEBAR ) ) ||
|
|
|
|
$shouldShowOnMainPage;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
/**
|
|
|
|
* Annotates table of contents data with Vector-specific information.
|
|
|
|
*
|
2022-08-15 16:34:28 +00:00
|
|
|
* In tableOfContents.js we have tableOfContents::getTableOfContentsSectionsData(),
|
|
|
|
* that yields the same result as this function, please make sure to keep them in sync.
|
2022-12-07 00:42:31 +00:00
|
|
|
* FIXME: This code should be moved to VectorComponentTableOfContents.
|
2022-08-15 16:34:28 +00:00
|
|
|
*
|
2022-03-29 20:51:45 +00:00
|
|
|
* @param array $tocData
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getTocData( array $tocData ): array {
|
|
|
|
// If the table of contents has no items, we won't output it.
|
|
|
|
// empty array is interpreted by Mustache as falsey.
|
|
|
|
if ( empty( $tocData ) || empty( $tocData[ 'array-sections' ] ) ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate button labels for collapsible TOC sections
|
|
|
|
foreach ( $tocData[ 'array-sections' ] as &$section ) {
|
|
|
|
if ( $section['is-top-level-section'] && $section['is-parent-section'] ) {
|
|
|
|
$section['vector-button-label'] =
|
|
|
|
$this->msg( 'vector-toc-toggle-button-label', $section['line'] )->text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 21:32:07 +00:00
|
|
|
// ToC is pinned by default, hardcoded for now
|
|
|
|
$isTocPinned = true;
|
|
|
|
$pinnableHeader = new VectorComponentPinnableHeader(
|
|
|
|
$this->getContext(),
|
|
|
|
$isTocPinned,
|
|
|
|
'vector-toc',
|
2022-12-07 22:15:31 +00:00
|
|
|
null,
|
2022-11-14 18:24:43 +00:00
|
|
|
false,
|
|
|
|
'h2'
|
2022-10-20 21:32:07 +00:00
|
|
|
);
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
return array_merge( $tocData, [
|
|
|
|
'is-vector-toc-beginning-enabled' => $this->getConfig()->get(
|
|
|
|
'VectorTableOfContentsBeginning'
|
|
|
|
),
|
|
|
|
'vector-is-collapse-sections-enabled' =>
|
|
|
|
$tocData[ 'number-section-count'] >= $this->getConfig()->get(
|
|
|
|
'VectorTableOfContentsCollapseAtCount'
|
2022-10-20 21:32:07 +00:00
|
|
|
),
|
2022-11-28 21:08:40 +00:00
|
|
|
'is-pinned' => $isTocPinned,
|
2022-10-20 21:32:07 +00:00
|
|
|
'data-pinnable-header' => $pinnableHeader->getTemplateData(),
|
2022-03-29 20:51:45 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:16:07 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getTocPageTitleData(): array {
|
|
|
|
return Hooks::updateDropdownMenuData( [
|
|
|
|
'id' => 'vector-page-titlebar-toc',
|
|
|
|
'class' => 'vector-page-titlebar-toc mw-ui-icon-flush-left',
|
|
|
|
'is-pinned' => true,
|
|
|
|
'button' => true,
|
|
|
|
'text-hidden' => true,
|
|
|
|
'icon' => 'listBullet'
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2022-04-27 19:32:19 +00:00
|
|
|
/**
|
|
|
|
* Merges the `view-overflow` menu into the `action` menu.
|
|
|
|
* This ensures that the previous state of the menu e.g. emptyPortlet class
|
|
|
|
* is preserved.
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function mergeViewOverflowIntoActions( $data ) {
|
|
|
|
$portlets = $data['data-portlets'];
|
|
|
|
$actions = $portlets['data-actions'];
|
|
|
|
$overflow = $portlets['data-views-overflow'];
|
2022-04-29 18:16:08 +00:00
|
|
|
// if the views overflow menu is not empty, then signal that the more menu despite
|
|
|
|
// being initially empty now has collapsible items.
|
|
|
|
if ( !$overflow['is-empty'] ) {
|
|
|
|
$data['data-portlets']['data-actions']['class'] .= ' vector-has-collapsible-items';
|
|
|
|
}
|
2022-04-27 19:32:19 +00:00
|
|
|
$data['data-portlets']['data-actions']['html-items'] = $overflow['html-items'] . $actions['html-items'];
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:10:26 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getHtmlElementAttributes() {
|
|
|
|
$original = parent::getHtmlElementAttributes();
|
|
|
|
|
|
|
|
if ( VectorServices::getFeatureManager()->isFeatureEnabled( Constants::FEATURE_STICKY_HEADER ) ) {
|
|
|
|
// T290518: Add scroll padding to root element when the sticky header is
|
|
|
|
// enabled. This class needs to be server rendered instead of added from
|
|
|
|
// JS in order to correctly handle situations where the sticky header
|
|
|
|
// isn't visible yet but we still need scroll padding applied (e.g. when
|
|
|
|
// the user navigates to a page with a hash fragment in the URI). For this
|
|
|
|
// reason, we can't rely on the `vector-sticky-header-visible` class as it
|
|
|
|
// is added too late.
|
|
|
|
//
|
|
|
|
// Please note that this class applies scroll padding which does not work
|
|
|
|
// when applied to the body tag in Chrome, Safari, and Firefox (and
|
|
|
|
// possibly others). It must instead be applied to the html tag.
|
|
|
|
$original['class'] = implode( ' ', [ $original['class'] ?? '', self::STICKY_HEADER_ENABLED_CLASS ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $original;
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:09:12 +00:00
|
|
|
/**
|
|
|
|
* Determines wheather the initial state of sidebar is visible on not
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isMainMenuVisible() {
|
|
|
|
$skin = $this->getSkin();
|
|
|
|
if ( $skin->getUser()->isRegistered() ) {
|
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
$userPrefSidebarState = $userOptionsLookup->getOption(
|
|
|
|
$skin->getUser(),
|
|
|
|
Constants::PREF_KEY_SIDEBAR_VISIBLE
|
|
|
|
);
|
|
|
|
|
|
|
|
$defaultLoggedinSidebarState = $this->getConfig()->get(
|
|
|
|
Constants::CONFIG_KEY_DEFAULT_SIDEBAR_VISIBLE_FOR_AUTHORISED_USER
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the sidebar user preference has been set, return that value,
|
|
|
|
// if not, then the default sidebar state for logged-in users.
|
|
|
|
return ( $userPrefSidebarState !== null )
|
|
|
|
? (bool)$userPrefSidebarState
|
|
|
|
: $defaultLoggedinSidebarState;
|
|
|
|
}
|
|
|
|
return $this->getConfig()->get(
|
|
|
|
Constants::CONFIG_KEY_DEFAULT_SIDEBAR_VISIBLE_FOR_ANONYMOUS_USER
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-19 00:53:42 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
2022-04-01 15:53:03 +00:00
|
|
|
$featureManager = VectorServices::getFeatureManager();
|
|
|
|
$parentData = parent::getTemplateData();
|
2022-12-07 00:42:31 +00:00
|
|
|
$stickyHeader = new VectorComponentStickyHeader();
|
|
|
|
$toc = new VectorComponentTableOfContents();
|
2022-12-09 21:27:12 +00:00
|
|
|
$parentData['data-toc'] = $toc->getTemplateData() + $this->getTocData( $parentData['data-toc'] ?? [] );
|
2022-03-29 20:51:45 +00:00
|
|
|
|
2022-04-27 19:32:19 +00:00
|
|
|
$parentData = $this->mergeViewOverflowIntoActions( $parentData );
|
|
|
|
|
2022-10-27 23:46:19 +00:00
|
|
|
// FIXME: Move to component (T322089)
|
|
|
|
$parentData['data-vector-user-links'] = $this->getUserLinksTemplateData(
|
|
|
|
$parentData['data-portlets']['data-user-menu'],
|
|
|
|
$parentData['data-portlets'][ 'data-vector-user-menu-overflow' ],
|
|
|
|
$this->getUser()
|
|
|
|
);
|
2022-10-27 23:52:23 +00:00
|
|
|
$parentData['data-portlets']['data-variants'] = $this->updateVariantsMenuLabel(
|
|
|
|
$parentData['data-portlets']['data-variants']
|
|
|
|
);
|
2022-10-27 23:46:19 +00:00
|
|
|
|
|
|
|
$langData = $parentData['data-portlets']['data-languages'] ?? null;
|
|
|
|
if ( $langData ) {
|
|
|
|
$parentData['data-lang-btn'] = $this->getULSPortletData(
|
|
|
|
$langData,
|
|
|
|
count( $this->getLanguagesCached() ),
|
|
|
|
$this->isLanguagesInContentAt( 'top' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:55:23 +00:00
|
|
|
$isPageToolsEnabled = $featureManager->isFeatureEnabled( Constants::FEATURE_PAGE_TOOLS );
|
2022-12-07 22:15:31 +00:00
|
|
|
$isPageToolsPinned = $featureManager->isFeatureEnabled( Constants::FEATURE_PAGE_TOOLS_PINNED );
|
2022-12-02 18:09:52 +00:00
|
|
|
$sidebar = $parentData[ 'data-portlets-sidebar' ];
|
|
|
|
$toolbox = [];
|
2022-12-08 19:26:03 +00:00
|
|
|
$restPortlets = $parentData[ 'data-portlets-sidebar' ][ 'array-portlets-rest' ];
|
2022-11-08 19:55:23 +00:00
|
|
|
if ( $isPageToolsEnabled ) {
|
2022-12-02 18:09:52 +00:00
|
|
|
$toolboxMenuIndex = array_search(
|
|
|
|
VectorComponentPageTools::TOOLBOX_ID,
|
|
|
|
array_column(
|
2022-12-08 19:26:03 +00:00
|
|
|
$restPortlets,
|
2022-12-02 18:09:52 +00:00
|
|
|
'id'
|
|
|
|
)
|
|
|
|
);
|
2022-11-08 19:55:23 +00:00
|
|
|
|
2022-12-02 18:09:52 +00:00
|
|
|
if ( $toolboxMenuIndex !== false ) {
|
2022-12-09 16:29:49 +00:00
|
|
|
// Splice removes the toolbox menu from the $restPortlets array
|
|
|
|
// and current returns the first value of array_splice, i.e. the $toolbox menu data.
|
|
|
|
$toolbox = current( array_splice( $restPortlets, $toolboxMenuIndex, 1 ) );
|
2022-12-08 19:26:03 +00:00
|
|
|
$parentData[ 'data-portlets-sidebar' ]['array-portlets-rest'] = $restPortlets;
|
2022-12-02 18:09:52 +00:00
|
|
|
$sidebar = $parentData[ 'data-portlets-sidebar' ];
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 16:11:32 +00:00
|
|
|
$config = $this->getConfig();
|
2022-12-07 01:05:22 +00:00
|
|
|
$mainMenuDropdownClass = $this->getUser()->isAnon() ? 'vector-main-menu-btn-dropdown-anon ' : '';
|
|
|
|
$mainMenuDropdownClass .= 'vector-main-menu-dropdown';
|
|
|
|
$mainMenuDropdown = new VectorComponentDropdown(
|
|
|
|
'vector-main-menu-dropdown',
|
|
|
|
$this->msg( 'vector-main-menu-label' )->text(),
|
|
|
|
$mainMenuDropdownClass,
|
|
|
|
'menu'
|
|
|
|
);
|
|
|
|
|
2022-10-27 19:36:08 +00:00
|
|
|
$components = [
|
2022-10-27 16:11:32 +00:00
|
|
|
'data-search-box' => new VectorComponentSearchBox(
|
|
|
|
$parentData['data-search-box'],
|
|
|
|
true,
|
|
|
|
// is primary mode of search
|
|
|
|
true,
|
|
|
|
'searchform',
|
|
|
|
true,
|
|
|
|
$config,
|
|
|
|
Constants::SEARCH_BOX_INPUT_LOCATION_MOVED,
|
|
|
|
$this->getContext()
|
|
|
|
),
|
2022-12-07 01:05:22 +00:00
|
|
|
'data-main-menu-dropdown' => $mainMenuDropdown,
|
2022-10-27 23:52:23 +00:00
|
|
|
'data-portlets-main-menu' => new VectorComponentMainMenu(
|
2022-12-02 18:09:52 +00:00
|
|
|
$sidebar,
|
2022-10-27 23:52:23 +00:00
|
|
|
$this,
|
2022-11-30 22:22:33 +00:00
|
|
|
$this->shouldLanguageAlertBeInSidebar(),
|
|
|
|
$parentData['data-portlets']['data-languages'] ?? [],
|
2022-10-27 23:52:23 +00:00
|
|
|
),
|
2022-12-02 18:09:52 +00:00
|
|
|
'data-page-tools' => $isPageToolsEnabled ? new VectorComponentPageTools(
|
|
|
|
$toolbox,
|
|
|
|
$parentData['data-portlets']['data-actions'] ?? [],
|
2022-12-07 22:15:31 +00:00
|
|
|
$isPageToolsPinned,
|
2022-12-02 18:09:52 +00:00
|
|
|
$this
|
|
|
|
) : null,
|
2022-12-07 01:05:22 +00:00
|
|
|
'data-page-tools-dropdown' => $isPageToolsEnabled ?
|
|
|
|
new VectorComponentDropdown( 'vector-page-tools', $this->msg( 'toolbox' )->text() ) : null,
|
2022-10-27 19:36:08 +00:00
|
|
|
];
|
|
|
|
foreach ( $components as $key => $component ) {
|
|
|
|
// Array of components or null values.
|
|
|
|
if ( $component ) {
|
|
|
|
$parentData[$key] = $component->getTemplateData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:11:32 +00:00
|
|
|
$searchStickyHeader = new VectorComponentSearchBox(
|
|
|
|
$parentData['data-search-box'],
|
|
|
|
// Collapse inside search box is disabled.
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
'vector-sticky-search-form',
|
|
|
|
false,
|
|
|
|
$config,
|
|
|
|
Constants::SEARCH_BOX_INPUT_LOCATION_MOVED,
|
|
|
|
$this->getContext()
|
|
|
|
);
|
|
|
|
|
2022-07-25 20:13:58 +00:00
|
|
|
return array_merge( $parentData, [
|
2022-10-27 21:18:53 +00:00
|
|
|
'is-language-in-content' => $this->isLanguagesInContent(),
|
|
|
|
'is-language-in-content-top' => $this->isLanguagesInContentAt( 'top' ),
|
|
|
|
'is-language-in-content-bottom' => $this->isLanguagesInContentAt( 'bottom' ),
|
2022-10-27 16:09:12 +00:00
|
|
|
'is-main-menu-visible' => $this->isMainMenuVisible(),
|
2022-07-25 20:13:58 +00:00
|
|
|
// Cast empty string to null
|
|
|
|
'html-subtitle' => $parentData['html-subtitle'] === '' ? null : $parentData['html-subtitle'],
|
2022-11-21 22:16:07 +00:00
|
|
|
'data-page-titlebar-toc' => $this->getTocPageTitleData(),
|
2022-04-01 15:53:03 +00:00
|
|
|
'data-vector-sticky-header' => $featureManager->isFeatureEnabled(
|
|
|
|
Constants::FEATURE_STICKY_HEADER
|
2022-12-07 00:42:31 +00:00
|
|
|
) ? $stickyHeader->getTemplateData() + $this->getStickyHeaderData(
|
2022-10-27 16:11:32 +00:00
|
|
|
$searchStickyHeader->getTemplateData(),
|
2022-04-01 15:53:03 +00:00
|
|
|
$featureManager->isFeatureEnabled(
|
|
|
|
Constants::FEATURE_STICKY_HEADER_EDIT
|
|
|
|
)
|
|
|
|
) : false,
|
2022-11-08 19:55:23 +00:00
|
|
|
'is-page-tools-enabled' => $isPageToolsEnabled
|
2022-07-25 20:13:58 +00:00
|
|
|
] );
|
2022-02-19 00:53:42 +00:00
|
|
|
}
|
2021-09-09 22:13:48 +00:00
|
|
|
}
|