| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
namespace MediaWiki\Minerva\Menu\Entries; |
| 19 |
|
| 20 |
use MediaWiki\Message\Message; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class SingleMenuEntry implements IMenuEntry { |
| 26 |
private string $name; |
| 27 |
private array $attributes; |
| 28 |
private bool $isJSOnly = false; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function __construct( string $name, string $text, string $url, $className = '', bool $isInterface = true ) { |
| 42 |
$this->name = $name; |
| 43 |
$menuClass = 'menu__item--' . $name; |
| 44 |
|
| 45 |
$this->attributes = [ |
| 46 |
'data-icon' => [ |
| 47 |
'icon' => null, |
| 48 |
], |
| 49 |
'data-event-name' => null, |
| 50 |
'title' => null, |
| 51 |
'id' => null, |
| 52 |
'text' => $text, |
| 53 |
'href' => $url, |
| 54 |
'role' => '', |
| 55 |
'class' => is_array( $className ) ? |
| 56 |
implode( ' ', $className + [ $menuClass ] ) : |
| 57 |
ltrim( $className . ' ' . $menuClass ), |
| 58 |
]; |
| 59 |
if ( $isInterface ) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
$this->attributes['data-mw'] = 'interface'; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function overrideIcon( string $icon ): self { |
| 75 |
$this->setIcon( $icon ); |
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function overrideText( string $text ): self { |
| 86 |
$this->attributes['text'] = $text; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static function create( $name, $text, $url, $className = '', $icon = null, $trackable = false ) { |
| 102 |
$entry = new static( $name, $text, $url, $className ); |
| 103 |
if ( $trackable ) { |
| 104 |
$entry->trackClicks( $name ); |
| 105 |
} |
| 106 |
if ( $icon === null ) { |
| 107 |
$icon = $name; |
| 108 |
} |
| 109 |
$entry->setIcon( $icon ); |
| 110 |
return $entry; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getName(): string { |
| 117 |
return $this->name; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function getCSSClasses(): array { |
| 124 |
return $this->isJSOnly ? [ 'skin-minerva-list-item-jsonly' ] : []; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function getComponents(): array { |
| 131 |
$attrs = []; |
| 132 |
foreach ( [ 'id', 'href', 'data-event-name', 'data-mw', 'role' ] as $key ) { |
| 133 |
$value = $this->attributes[$key] ?? null; |
| 134 |
if ( $value ) { |
| 135 |
$attrs[] = [ |
| 136 |
'key' => $key, |
| 137 |
'value' => $value, |
| 138 |
]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$btn = [ |
| 143 |
'tag-name' => 'a', |
| 144 |
'label' => $this->attributes['text'], |
| 145 |
'array-attributes' => $attrs, |
| 146 |
'classes' => $this->attributes['class'], |
| 147 |
'data-icon' => $this->attributes['data-icon'], |
| 148 |
]; |
| 149 |
|
| 150 |
return [ $btn ]; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function trackClicks( $eventName ): self { |
| 159 |
$this->attributes['data-event-name'] = 'menu.' . $eventName; |
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function setIcon( $iconName ): self { |
| 169 |
if ( $iconName !== null ) { |
| 170 |
$this->attributes['data-icon']['icon'] = $iconName; |
| 171 |
} else { |
| 172 |
$this->attributes['data-icon'] = null; |
| 173 |
} |
| 174 |
return $this; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function setTitle( Message $message ): self { |
| 183 |
$this->attributes['title'] = $message->escaped(); |
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function setNodeID( $nodeID ): self { |
| 193 |
$this->attributes['id'] = $nodeID; |
| 194 |
return $this; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function setJSOnly() { |
| 201 |
$this->isJSOnly = true; |
| 202 |
} |
| 203 |
} |