Introduce new SingleMenuEntry

Instead of using $group->insert(....)->addComponent() we can define
our own small SingleMenuEntry that defines a standard menu entry with
a label and one icon. The icon name and tracking code by default will
be the same as the menu entry name.

Changes:
 - introduced SingleMenuEntry class
 - updated Definitions class to use new entry instead of using old
 MenuEntry class
 - changed the Contributions meny entry name to match icon/tracking

This should simplify code a bit, thanks to this approach we should be
able to remove the Definitions class in some future and keep building
MenuElements in the builders.

Bug: T221792
Change-Id: I1f145e8cd173b72b01a145b2dba3704593f17ab7
This commit is contained in:
Piotr Miazga 2019-05-14 12:40:56 +02:00
parent 6905b85d67
commit fa30f03495
3 changed files with 120 additions and 53 deletions

View File

@ -69,13 +69,12 @@ final class Definitions {
* @throws MWException
*/
public function insertContributionsMenuItem( Group $group ) {
$group->insert( 'contribs' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-contributions' )->escaped(),
SpecialPage::getTitleFor( 'Contributions', $this->user->getName() )->getLocalURL(),
MinervaUI::iconClass( 'contributions', 'before' ),
[ 'data-event-name' => 'contributions' ]
);
$group->insertEntry( new SingleMenuEntry(
'contributions',
$this->context->msg( 'mobile-frontend-main-menu-contributions' )->escaped(),
SpecialPage::getTitleFor( 'Contributions', $this->user->getName() )->getLocalURL()
) );
}
/**
@ -100,14 +99,11 @@ final class Definitions {
$watchlistQuery['filter'] = $filter;
}
}
$group->insert( 'watchlist' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-watchlist' )->escaped(),
$watchTitle->getLocalURL( $watchlistQuery ),
MinervaUI::iconClass( 'watchlist', 'before' ),
[ 'data-event-name' => 'watchlist' ]
);
$group->insertEntry( new SingleMenuEntry(
'watchlist',
$this->context->msg( 'mobile-frontend-main-menu-watchlist' )->escaped(),
$watchTitle->getLocalURL( $watchlistQuery )
) );
}
/**
@ -174,14 +170,11 @@ final class Definitions {
* @param Group $group
*/
public function insertHomeItem( Group $group ) {
// Home link
$group->insert( 'home' )
->addComponent(
$this->context->msg( 'mobile-frontend-home-button' )->escaped(),
Title::newMainPage()->getLocalURL(),
MinervaUI::iconClass( 'home', 'before' ),
[ 'data-event-name' => 'home' ]
);
$group->insertEntry( new SingleMenuEntry(
'home',
$this->context->msg( 'mobile-frontend-home-button' )->escaped(),
Title::newMainPage()->getLocalURL()
) );
}
/**
@ -226,14 +219,13 @@ final class Definitions {
public function insertMobileOptionsItem( Group $group ) {
$title = $this->context->getTitle();
$returnToTitle = $title->getPrefixedText();
$group->insert( 'settings' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-settings' )->escaped(),
SpecialPage::getTitleFor( 'MobileOptions' )->
getLocalURL( [ 'returnto' => $returnToTitle ] ),
MinervaUI::iconClass( 'settings', 'before' ),
[ 'data-event-name' => 'settings' ]
);
$group->insertEntry( new SingleMenuEntry(
'settings',
$this->context->msg( 'mobile-frontend-main-menu-settings' )->escaped(),
SpecialPage::getTitleFor( 'MobileOptions' )
->getLocalURL( [ 'returnto' => $returnToTitle ] )
) );
}
/**
@ -242,13 +234,13 @@ final class Definitions {
* @throws MWException
*/
public function insertPreferencesItem( Group $group ) {
$group->insert( 'preferences' )
->addComponent(
$this->context->msg( 'preferences' )->escaped(),
SpecialPage::getTitleFor( 'Preferences' )->getLocalURL(),
MinervaUI::iconClass( 'settings', 'before' ),
[ 'data-event-name' => 'preferences' ]
);
$group->insertEntry( new SingleMenuEntry(
'preferences',
$this->context->msg( 'preferences' )->escaped(),
SpecialPage::getTitleFor( 'Preferences' )->getLocalURL(),
true,
'settings'
) );
}
/**
@ -284,13 +276,13 @@ final class Definitions {
* @throws MWException
*/
public function insertSpecialPages( Group $group ) {
$group->insert( 'specialpages' )
->addComponent(
$group->insertEntry(
new SingleMenuEntry(
'specialpages',
$this->context->msg( 'specialpages' )->escaped(),
SpecialPage::getTitleFor( 'Specialpages' )->getLocalURL(),
MinervaUI::iconClass( 'specialpages', 'before' ),
[ 'data-event-name' => 'specialpages' ]
);
SpecialPage::getTitleFor( 'Specialpages' )->getLocalURL()
)
);
}
/**
@ -313,13 +305,11 @@ final class Definitions {
return;
}
$group->insert( 'communityportal' )
->addComponent(
$title->getText(),
$title->getLocalURL(),
MinervaUI::iconClass( 'communityportal', 'before' ),
[ 'data-event-name' => 'community-portal' ]
);
$group->insertEntry( new SingleMenuEntry(
'communityportal',
$title->getText(),
$title->getLocalURL()
) );
}
/**

View File

@ -0,0 +1,77 @@
<?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.
*/
namespace MediaWiki\Minerva\Menu;
use MinervaUI;
/**
* Model for a simple menu entries with label and icon
*/
class SingleMenuEntry implements IMenuEntry {
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $component;
/**
* Create a simple menu element with one component
*
* @param string $name An unique menu element identifier
* @param string $text Text to show on menu element
* @param string $url URL menu element points to
* @param bool|string $trackClicks Should clicks be tracked. To override the tracking code
* pass the tracking code as string
* @param string|null $iconName The Icon name, if not defined, the $name will be used
*/
public function __construct( $name, $text, $url, $trackClicks = true, $iconName = null ) {
$this->name = $name;
$this->component = [
'text' => $text,
'href' => $url,
'class' => MinervaUI::iconClass( $iconName ?? $name, 'before' ),
];
if ( $trackClicks !== false ) {
$this->component['data-event-name'] = $trackClicks === true ? $name : $trackClicks;
}
}
/**
* @inheritDoc
*/
public function getName() {
return $this->name;
}
/**
* @inheritDoc
*/
public function getCSSClasses(): array {
return [];
}
/**
* @inheritDoc
*/
public function getComponents(): array {
return [ $this->component ];
}
}

View File

@ -486,9 +486,9 @@ class SkinMinerva extends SkinTemplate {
*/
protected function getRelativeHistoryLink( Title $title, $timestamp ) {
$user = $this->getUser();
$userDate = $this->getLanguage()->userDate( $timestamp, $user );
$text = $this->msg(
'minerva-last-modified-date',
$this->getLanguage()->userDate( $timestamp, $user ),
'minerva-last-modified-date', $userDate,
$this->getLanguage()->userTime( $timestamp, $user )
)->parse();
return [