2019-05-14 10:40:56 +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.
|
|
|
|
*/
|
|
|
|
|
2019-07-01 18:06:01 +00:00
|
|
|
namespace MediaWiki\Minerva\Menu\Entries;
|
2019-05-14 10:40:56 +00:00
|
|
|
|
2019-11-07 21:46:55 +00:00
|
|
|
use Message;
|
2019-05-14 10:40:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for a simple menu entries with label and icon
|
|
|
|
*/
|
|
|
|
class SingleMenuEntry implements IMenuEntry {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-07 18:04:21 +00:00
|
|
|
private $attributes;
|
2020-01-07 20:29:35 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $isJSOnly;
|
2019-05-14 10:40:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2022-01-13 00:38:16 +00:00
|
|
|
* @param string|array $className Additional CSS class names.
|
2019-05-14 10:40:56 +00:00
|
|
|
*/
|
2019-11-07 18:04:21 +00:00
|
|
|
public function __construct( $name, $text, $url, $className = '' ) {
|
2019-05-14 10:40:56 +00:00
|
|
|
$this->name = $name;
|
2022-01-13 00:38:16 +00:00
|
|
|
$menuClass = 'menu__item--' . $name;
|
2019-11-07 18:04:21 +00:00
|
|
|
|
|
|
|
$this->attributes = [
|
2023-08-01 10:57:48 +00:00
|
|
|
'data-icon' => [
|
|
|
|
'icon' => null,
|
|
|
|
],
|
2023-08-08 23:33:28 +00:00
|
|
|
'data-event-name' => null,
|
|
|
|
'title' => null,
|
|
|
|
'id' => null,
|
2019-05-14 10:40:56 +00:00
|
|
|
'text' => $text,
|
|
|
|
'href' => $url,
|
2023-08-08 23:33:28 +00:00
|
|
|
'role' => '',
|
|
|
|
'data-mw' => 'interface',
|
2022-01-13 00:38:16 +00:00
|
|
|
'class' => is_array( $className ) ?
|
|
|
|
implode( ' ', $className + [ $menuClass ] ) :
|
|
|
|
ltrim( $className . ' ' . $menuClass ),
|
2019-05-14 10:40:56 +00:00
|
|
|
];
|
2019-11-07 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 23:36:30 +00:00
|
|
|
/**
|
|
|
|
* Override the icon used in the home menu entry.
|
|
|
|
*
|
|
|
|
* @param string $icon
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function overrideIcon( $icon ) {
|
|
|
|
$this->setIcon( str_replace( 'minerva-', '', $icon ) );
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the text used in the home menu entry.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function overrideText( $text ) {
|
|
|
|
$this->attributes['text'] = $text;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:04:21 +00:00
|
|
|
/**
|
|
|
|
* Create a Single Menu entry with text, icon and active click tracking
|
|
|
|
*
|
|
|
|
* @param string $name Entry identifier
|
|
|
|
* @param string $text Entry label
|
|
|
|
* @param string $url The URL entry points to
|
|
|
|
* @param string $className Optional HTML classes
|
2022-01-13 00:38:16 +00:00
|
|
|
* @param string|null $icon defaults to $name if not specified
|
2022-06-21 16:20:53 +00:00
|
|
|
* @param bool $trackable Whether an entry will track clicks or not. Default is false.
|
2019-11-07 18:04:21 +00:00
|
|
|
* @return static
|
|
|
|
*/
|
2022-06-21 16:20:53 +00:00
|
|
|
public static function create( $name, $text, $url, $className = '', $icon = null, $trackable = false ) {
|
2019-11-07 18:04:21 +00:00
|
|
|
$entry = new static( $name, $text, $url, $className );
|
2022-06-21 16:20:53 +00:00
|
|
|
if ( $trackable ) {
|
|
|
|
$entry->trackClicks( $name );
|
|
|
|
}
|
2022-01-13 00:38:16 +00:00
|
|
|
if ( $icon === null ) {
|
|
|
|
$icon = $name;
|
|
|
|
}
|
|
|
|
$entry->setIcon( $icon );
|
2019-11-07 18:04:21 +00:00
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:46:55 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getCSSClasses(): array {
|
2020-01-07 20:29:35 +00:00
|
|
|
return $this->isJSOnly ? [ 'jsonly' ] : [];
|
2019-11-07 21:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getComponents(): array {
|
2023-08-08 23:33:28 +00:00
|
|
|
$attrs = [];
|
|
|
|
foreach ( [ 'id', 'href', 'data-event-name', 'data-mw', 'role' ] as $key ) {
|
|
|
|
$value = $this->attributes[$key];
|
|
|
|
if ( $value ) {
|
|
|
|
$attrs[] = [
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$btn = [
|
|
|
|
'tag-name' => 'a',
|
|
|
|
'label' => $this->attributes['text'],
|
|
|
|
'array-attributes' => $attrs,
|
|
|
|
'classes' => $this->attributes['class'],
|
|
|
|
'data-icon' => $this->attributes['data-icon'],
|
|
|
|
];
|
|
|
|
|
|
|
|
return [ $btn ];
|
2019-11-07 21:46:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 18:04:21 +00:00
|
|
|
/**
|
|
|
|
* @param string $eventName Should clicks be tracked. To override the tracking code
|
|
|
|
* pass the tracking code as string
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return $this
|
2019-11-07 18:04:21 +00:00
|
|
|
*/
|
|
|
|
public function trackClicks( $eventName ) {
|
|
|
|
$this->attributes['data-event-name'] = 'menu.' . $eventName;
|
2019-11-07 21:46:55 +00:00
|
|
|
return $this;
|
2019-11-07 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Menu entry icon
|
2021-12-17 10:29:11 +00:00
|
|
|
* @param string|null $iconName
|
|
|
|
* @param string $iconType
|
2019-11-07 21:46:55 +00:00
|
|
|
* @param string $additionalClassNames Additional classes
|
2020-04-29 22:13:47 +00:00
|
|
|
* @param string $iconPrefix either `wikimedia` or `minerva`
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return $this
|
2019-11-07 18:04:21 +00:00
|
|
|
*/
|
2022-06-21 16:20:53 +00:00
|
|
|
public function setIcon( $iconName,
|
|
|
|
$iconType = 'before',
|
|
|
|
$additionalClassNames = '',
|
|
|
|
$iconPrefix = 'minerva'
|
2020-03-25 02:02:02 +00:00
|
|
|
) {
|
2023-08-11 17:14:11 +00:00
|
|
|
if ( $iconName !== null ) {
|
|
|
|
$this->attributes['data-icon']['icon'] = $iconPrefix . '-' . $iconName;
|
|
|
|
} else {
|
|
|
|
$this->attributes['data-icon'] = null;
|
|
|
|
}
|
2023-08-08 23:33:28 +00:00
|
|
|
if ( $additionalClassNames ) {
|
|
|
|
$this->attributes['class'] .= ' ' . $additionalClassNames;
|
|
|
|
}
|
|
|
|
if ( $iconType !== 'before' ) {
|
|
|
|
$this->attributes['role'] = 'button';
|
2021-09-09 15:56:42 +00:00
|
|
|
}
|
2019-11-07 21:46:55 +00:00
|
|
|
return $this;
|
2019-05-14 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-07 21:46:55 +00:00
|
|
|
* Set the menu entry title
|
|
|
|
* @param Message $message Title message
|
|
|
|
* @return $this
|
2019-05-14 10:40:56 +00:00
|
|
|
*/
|
2019-11-07 21:46:55 +00:00
|
|
|
public function setTitle( Message $message ): self {
|
|
|
|
$this->attributes['title'] = $message->escaped();
|
|
|
|
return $this;
|
2019-05-14 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-07 21:46:55 +00:00
|
|
|
* Set the Menu entry ID html attribute
|
|
|
|
* @param string $nodeID
|
|
|
|
* @return $this
|
2019-05-14 10:40:56 +00:00
|
|
|
*/
|
2019-11-07 21:46:55 +00:00
|
|
|
public function setNodeID( $nodeID ): self {
|
|
|
|
$this->attributes['id'] = $nodeID;
|
|
|
|
return $this;
|
2019-05-14 10:40:56 +00:00
|
|
|
}
|
2019-11-07 18:04:21 +00:00
|
|
|
|
2020-01-07 20:29:35 +00:00
|
|
|
/**
|
|
|
|
* Mark entry as JS only.
|
|
|
|
*/
|
|
|
|
public function setJSOnly() {
|
|
|
|
$this->isJSOnly = true;
|
|
|
|
}
|
2019-05-14 10:40:56 +00:00
|
|
|
}
|