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