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
|
|
|
|
2024-05-19 12:45:56 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2019-05-14 10:40:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for a simple menu entries with label and icon
|
|
|
|
*/
|
|
|
|
class SingleMenuEntry implements IMenuEntry {
|
2024-03-28 19:15:41 +00:00
|
|
|
private string $name;
|
|
|
|
private array $attributes;
|
|
|
|
private bool $isJSOnly = false;
|
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.
|
2023-10-21 15:34:52 +00:00
|
|
|
* @param bool $isInterface If true, the menu element is provided with data-mw='interface'
|
|
|
|
* and is treated as a standard part of the interface (ie. MediaWiki Core might bind to
|
|
|
|
* the menu element)
|
2019-05-14 10:40:56 +00:00
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function __construct( string $name, string $text, string $url, $className = '', bool $isInterface = true ) {
|
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' => '',
|
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
|
|
|
];
|
2023-10-21 15:34:52 +00:00
|
|
|
if ( $isInterface ) {
|
|
|
|
// This is needed when Minerva uses a standard MediaWiki button (such as the
|
|
|
|
// watchstar) for a different purpose than MediaWiki usually uses it for. Not setting
|
|
|
|
// data-mw interface will prevent MediaWiki Core from binding to the element and
|
|
|
|
// potentially triggering its own actions. See T344925 for an example bug report.
|
|
|
|
$this->attributes['data-mw'] = 'interface';
|
|
|
|
}
|
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
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function overrideIcon( string $icon ): self {
|
2023-09-05 17:07:13 +00:00
|
|
|
$this->setIcon( $icon );
|
2022-01-18 23:36:30 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the text used in the home menu entry.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return $this
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function overrideText( string $text ): self {
|
2022-01-18 23:36:30 +00:00
|
|
|
$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
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function getName(): string {
|
2019-11-07 21:46:55 +00:00
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getCSSClasses(): array {
|
2023-12-14 19:03:26 +00:00
|
|
|
return $this->isJSOnly ? [ 'skin-minerva-list-item-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 ) {
|
2023-10-21 15:34:52 +00:00
|
|
|
$value = $this->attributes[$key] ?? null;
|
2023-08-08 23:33:28 +00:00
|
|
|
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
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function trackClicks( $eventName ): self {
|
2019-11-07 18:04:21 +00:00
|
|
|
$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
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return $this
|
2019-11-07 18:04:21 +00:00
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function setIcon( $iconName ): self {
|
2023-08-11 17:14:11 +00:00
|
|
|
if ( $iconName !== null ) {
|
2023-09-05 17:07:13 +00:00
|
|
|
$this->attributes['data-icon']['icon'] = $iconName;
|
2023-08-11 17:14:11 +00:00
|
|
|
} else {
|
|
|
|
$this->attributes['data-icon'] = null;
|
|
|
|
}
|
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
|
|
|
}
|