| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Generic widget for buttons. |
| 7 |
*/ |
| 8 |
class ButtonWidget extends Widget { |
| 9 |
use ButtonElement; |
| 10 |
use IconElement; |
| 11 |
use IndicatorElement; |
| 12 |
use LabelElement; |
| 13 |
use TitledElement; |
| 14 |
use FlaggedElement; |
| 15 |
use TabIndexedElement; |
| 16 |
use AccessKeyedElement; |
| 17 |
|
| 18 |
/* Static Properties */ |
| 19 |
|
| 20 |
/** @var string */ |
| 21 |
public static $tagName = 'span'; |
| 22 |
|
| 23 |
/* Properties */ |
| 24 |
|
| 25 |
/** |
| 26 |
* Whether button is active. |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
protected $active = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Hyperlink to visit when clicked. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $href = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* Target to open hyperlink in. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
protected $target = null; |
| 45 |
|
| 46 |
/** |
| 47 |
* Search engine traversal hint. |
| 48 |
* |
| 49 |
* True if search engines should avoid following this hyperlink. |
| 50 |
* |
| 51 |
* @var bool |
| 52 |
*/ |
| 53 |
protected $noFollow = true; |
| 54 |
|
| 55 |
/** |
| 56 |
* Relationship attributes, such as the noFollow field above, or noopener for the hyperlink. |
| 57 |
* |
| 58 |
* @var string[] |
| 59 |
*/ |
| 60 |
protected $rel = []; |
| 61 |
|
| 62 |
/** |
| 63 |
* @param array $config Configuration options |
| 64 |
* - bool $config['active'] Whether button should be shown as active (default: false) |
| 65 |
* - string $config['href'] Hyperlink to visit when clicked |
| 66 |
* - string $config['target'] Target to open hyperlink in |
| 67 |
* - bool $config['noFollow'] Search engine traversal hint (default: true) |
| 68 |
* - string|string[] $config['rel'] Relationship attributes for the hyperlink |
| 69 |
*/ |
| 70 |
public function __construct( array $config = [] ) { |
| 71 |
// Parent constructor |
| 72 |
parent::__construct( $config ); |
| 73 |
|
| 74 |
// Traits |
| 75 |
$this->initializeButtonElement( $config ); |
| 76 |
$this->initializeIconElement( $config ); |
| 77 |
$this->initializeIndicatorElement( $config ); |
| 78 |
$this->initializeLabelElement( $config ); |
| 79 |
$this->initializeTitledElement( |
| 80 |
array_merge( [ 'titled' => $this->button ], $config ) |
| 81 |
); |
| 82 |
$this->initializeFlaggedElement( $config ); |
| 83 |
$this->initializeTabIndexedElement( |
| 84 |
array_merge( [ 'tabIndexed' => $this->button ], $config ) |
| 85 |
); |
| 86 |
$this->initializeAccessKeyedElement( |
| 87 |
array_merge( [ 'accessKeyed' => $this->button ], $config ) |
| 88 |
); |
| 89 |
|
| 90 |
// Initialization |
| 91 |
$this->button->appendContent( $this->icon, $this->label, $this->indicator ); |
| 92 |
$this |
| 93 |
->addClasses( [ 'oo-ui-buttonWidget' ] ) |
| 94 |
->appendContent( $this->button ); |
| 95 |
|
| 96 |
$this->setActive( $config['active'] ?? false ); |
| 97 |
$this->setHref( $config['href'] ?? null ); |
| 98 |
$this->setTarget( $config['target'] ?? null ); |
| 99 |
$rel = [ 'nofollow' ]; |
| 100 |
if ( isset( $config['rel'] ) ) { |
| 101 |
$rel = $config['rel']; |
| 102 |
} elseif ( isset( $config[ 'noFollow' ] ) && $config[ 'noFollow' ] === false ) { |
| 103 |
$rel = []; |
| 104 |
} |
| 105 |
|
| 106 |
$this->setRel( $rel ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get hyperlink location. |
| 111 |
* |
| 112 |
* @return string Hyperlink location |
| 113 |
*/ |
| 114 |
public function getHref() { |
| 115 |
return $this->href; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get hyperlink target. |
| 120 |
* |
| 121 |
* @return string Hyperlink target |
| 122 |
*/ |
| 123 |
public function getTarget() { |
| 124 |
return $this->target; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get search engine traversal hint. |
| 129 |
* |
| 130 |
* @return bool Whether search engines should avoid traversing this hyperlink |
| 131 |
*/ |
| 132 |
public function getNoFollow() { |
| 133 |
return $this->noFollow; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get the relationship attribute of the hyperlink. |
| 138 |
* |
| 139 |
* @return string[] Relationship attributes that apply to the hyperlink |
| 140 |
*/ |
| 141 |
public function getRel() { |
| 142 |
return $this->rel; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Set hyperlink location. |
| 147 |
* |
| 148 |
* @param string|null $href Hyperlink location, null to remove |
| 149 |
* @return $this |
| 150 |
*/ |
| 151 |
public function setHref( $href ) { |
| 152 |
$this->href = is_string( $href ) ? $href : null; |
| 153 |
|
| 154 |
$this->updateHref(); |
| 155 |
|
| 156 |
return $this; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Update the href attribute, in case of changes to href or disabled |
| 161 |
* state. |
| 162 |
* |
| 163 |
* @return $this |
| 164 |
*/ |
| 165 |
public function updateHref() { |
| 166 |
if ( $this->href !== null && !$this->isDisabled() ) { |
| 167 |
$this->button->setAttributes( [ 'href' => $this->href ] ); |
| 168 |
} else { |
| 169 |
$this->button->removeAttributes( [ 'href' ] ); |
| 170 |
} |
| 171 |
return $this; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Set hyperlink target. |
| 176 |
* |
| 177 |
* @param string|null $target Hyperlink target, null to remove |
| 178 |
* @return $this |
| 179 |
*/ |
| 180 |
public function setTarget( $target ) { |
| 181 |
$this->target = is_string( $target ) ? $target : null; |
| 182 |
|
| 183 |
if ( $this->target !== null ) { |
| 184 |
$this->button->setAttributes( [ 'target' => $target ] ); |
| 185 |
} else { |
| 186 |
$this->button->removeAttributes( [ 'target' ] ); |
| 187 |
} |
| 188 |
|
| 189 |
return $this; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Set search engine traversal hint. |
| 194 |
* |
| 195 |
* @param bool $noFollow True if search engines should avoid traversing this hyperlink |
| 196 |
* @return $this |
| 197 |
*/ |
| 198 |
public function setNoFollow( $noFollow ) { |
| 199 |
if ( $noFollow !== $this->noFollow ) { |
| 200 |
if ( $noFollow ) { |
| 201 |
$rel = array_merge( $this->rel, [ 'nofollow' ] ); |
| 202 |
} else { |
| 203 |
$rel = array_diff( $this->rel, [ 'nofollow' ] ); |
| 204 |
} |
| 205 |
$this->setRel( $rel ); |
| 206 |
} |
| 207 |
|
| 208 |
return $this; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Set the relationship attribute of the hyperlink. |
| 213 |
* |
| 214 |
* @param string|string[] $rel Relationship attributes for the hyperlink |
| 215 |
* @return $this |
| 216 |
*/ |
| 217 |
public function setRel( $rel ) { |
| 218 |
$this->rel = $rel === '' ? [] : (array)$rel; |
| 219 |
// For backwards compatibility |
| 220 |
$this->noFollow = in_array( 'nofollow', $this->rel ); |
| 221 |
|
| 222 |
$value = implode( ' ', $this->rel ); |
| 223 |
if ( $value !== '' ) { |
| 224 |
$this->button->setAttributes( [ 'rel' => $value ] ); |
| 225 |
} else { |
| 226 |
$this->button->removeAttributes( [ 'rel' ] ); |
| 227 |
} |
| 228 |
|
| 229 |
return $this; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Toggle active state. |
| 234 |
* |
| 235 |
* A button should be marked as active when clicking it would only refresh the page. |
| 236 |
* |
| 237 |
* @param bool|null $active Make button active |
| 238 |
* @return $this |
| 239 |
*/ |
| 240 |
public function setActive( $active = null ) { |
| 241 |
$this->active = (bool)$active; |
| 242 |
$this->toggleClasses( [ 'oo-ui-buttonElement-active' ], $this->active ); |
| 243 |
return $this; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Check if button is active. |
| 248 |
* |
| 249 |
* @return bool Button is active |
| 250 |
*/ |
| 251 |
public function isActive() { |
| 252 |
return $this->active; |
| 253 |
} |
| 254 |
|
| 255 |
/** @inheritDoc */ |
| 256 |
public function getConfig( &$config ) { |
| 257 |
if ( $this->active !== false ) { |
| 258 |
$config['active'] = $this->active; |
| 259 |
} |
| 260 |
if ( $this->href !== null ) { |
| 261 |
$config['href'] = $this->href; |
| 262 |
} |
| 263 |
if ( $this->target !== null ) { |
| 264 |
$config['target'] = $this->target; |
| 265 |
} |
| 266 |
if ( $this->noFollow !== true ) { |
| 267 |
$config['noFollow'] = $this->noFollow; |
| 268 |
} |
| 269 |
if ( $this->rel !== [] ) { |
| 270 |
$config['rel'] = $this->rel; |
| 271 |
} |
| 272 |
return parent::getConfig( $config ); |
| 273 |
} |
| 274 |
} |