2019-04-08 17:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
namespace MediaWiki\Minerva\Menu\Main;
|
|
|
|
|
2019-06-21 16:22:17 +00:00
|
|
|
use Html;
|
|
|
|
use MessageLocalizer;
|
|
|
|
use MinervaUI;
|
|
|
|
use SpecialPage;
|
2019-10-11 19:04:00 +00:00
|
|
|
use MediaWiki\Special\SpecialPageFactory;
|
2019-06-21 16:22:17 +00:00
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Director responsible for building Main Menu
|
|
|
|
*/
|
2019-08-02 09:00:18 +00:00
|
|
|
final class MainMenuDirector {
|
2019-04-08 17:08:57 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-02 09:00:18 +00:00
|
|
|
* @var IMainMenuBuilder
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
|
|
|
private $builder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $menuData;
|
|
|
|
|
2019-06-21 16:22:17 +00:00
|
|
|
/**
|
|
|
|
* @var MessageLocalizer
|
|
|
|
*/
|
|
|
|
private $msgLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Director responsible for Main Menu building
|
|
|
|
*
|
2019-08-02 09:00:18 +00:00
|
|
|
* @param IMainMenuBuilder $builder
|
2019-06-21 16:22:17 +00:00
|
|
|
* @param MessageLocalizer $messageLocalizer Used for translating texts in menu toggle
|
|
|
|
* @param SpecialPageFactory $specialPageFactory Used to check for MobileMenu special page
|
|
|
|
* existence
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2019-06-21 16:22:17 +00:00
|
|
|
public function __construct(
|
2019-08-02 09:00:18 +00:00
|
|
|
IMainMenuBuilder $builder,
|
2019-06-21 16:22:17 +00:00
|
|
|
MessageLocalizer $messageLocalizer,
|
|
|
|
SpecialPageFactory $specialPageFactory
|
|
|
|
) {
|
2019-04-08 17:08:57 +00:00
|
|
|
$this->builder = $builder;
|
2019-06-21 16:22:17 +00:00
|
|
|
$this->msgLocalizer = $messageLocalizer;
|
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a data representation of the main menus
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-21 16:52:22 +00:00
|
|
|
public function getMenuData(): array {
|
2019-04-08 17:08:57 +00:00
|
|
|
if ( $this->menuData === null ) {
|
|
|
|
$this->menuData = $this->buildMenu();
|
|
|
|
}
|
|
|
|
return $this->menuData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the menu data array that can be passed to views/javascript
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-21 16:52:22 +00:00
|
|
|
private function buildMenu(): array {
|
2019-04-08 17:08:57 +00:00
|
|
|
$menuData = [
|
2019-06-21 16:22:17 +00:00
|
|
|
'buttonHTML' => $this->prepareToggle(),
|
|
|
|
'items' => [
|
|
|
|
'groups' => [],
|
|
|
|
'sitelinks' => $this->builder->getSiteLinks()->getEntries()
|
|
|
|
]
|
2019-04-08 17:08:57 +00:00
|
|
|
];
|
|
|
|
foreach ( $this->builder->getGroups() as $group ) {
|
|
|
|
if ( $group->hasEntries() ) {
|
2019-06-21 16:22:17 +00:00
|
|
|
$menuData['items']['groups'][] = $group->getEntries();
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $menuData;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:22:17 +00:00
|
|
|
/**
|
|
|
|
* Prepare the button opens the main side menu
|
|
|
|
* @return string Rendered Menu button as HTML
|
|
|
|
*/
|
|
|
|
protected function prepareToggle() {
|
|
|
|
$url = $this->specialPageFactory->exists( 'MobileMenu' ) ?
|
|
|
|
SpecialPage::getTitleFor( 'MobileMenu' )->getLocalURL() : '#';
|
|
|
|
$title = $this->msgLocalizer->msg( 'mobile-frontend-main-menu-button-tooltip' )->text();
|
|
|
|
$tooltip = $this->msgLocalizer->msg( 'mobile-frontend-main-menu-button-tooltip' )
|
|
|
|
->text();
|
|
|
|
|
|
|
|
return Html::element( 'a', [
|
|
|
|
'title' => $title,
|
|
|
|
'href' => $url,
|
2019-09-11 12:19:47 +00:00
|
|
|
'class' => MinervaUI::iconClass(
|
|
|
|
'mainmenu', 'element', 'main-menu-button mw-ui-icon-flush-left'
|
|
|
|
),
|
2019-08-08 21:43:04 +00:00
|
|
|
'data-event-name' => 'ui.mainmenu',
|
2019-06-21 16:22:17 +00:00
|
|
|
'id' => 'mw-mf-main-menu-button',
|
|
|
|
], $tooltip );
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|