| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
class ProgressBarWidget extends Widget { |
| 6 |
/** |
| 7 |
* @var bool|int |
| 8 |
*/ |
| 9 |
protected $progress; |
| 10 |
|
| 11 |
/** |
| 12 |
* @var bool |
| 13 |
*/ |
| 14 |
protected $inline; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var Tag |
| 18 |
*/ |
| 19 |
protected $bar; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param array $config Configuration options |
| 23 |
* - bool|int $config['progress'] Numeric value between 0 and 100 (the percent complete) |
| 24 |
* for determinate progress bar, or `false` for indeterminate progress bar |
| 25 |
* (default: false) |
| 26 |
* - bool $config['inline'] Use a smaller inline variant on the progress bar |
| 27 |
* (default: false) |
| 28 |
*/ |
| 29 |
public function __construct( array $config = [] ) { |
| 30 |
parent::__construct( $config ); |
| 31 |
|
| 32 |
$this->bar = new Tag( 'div' ); |
| 33 |
$this->bar->addClasses( [ 'oo-ui-progressBarWidget-bar' ] ); |
| 34 |
|
| 35 |
$this->setProgress( array_key_exists( 'progress', $config ) ? $config['progress'] : false ); |
| 36 |
|
| 37 |
$this |
| 38 |
->setAttributes( [ |
| 39 |
'role' => 'progressbar', |
| 40 |
'aria-valuemin' => 0, |
| 41 |
'aria-valuemax' => 100, |
| 42 |
] ) |
| 43 |
->addClasses( [ 'oo-ui-progressBarWidget' ] ) |
| 44 |
->appendContent( $this->bar ); |
| 45 |
|
| 46 |
$this->inline = $config['inline'] ?? false; |
| 47 |
|
| 48 |
if ( $this->inline ) { |
| 49 |
$this->addClasses( [ 'oo-ui-progressBarWidget-inline' ] ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return bool|int |
| 55 |
*/ |
| 56 |
public function getProgress() { |
| 57 |
return $this->progress; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param bool|int $progress Numeric value between 0 and 100 (the percent complete) |
| 62 |
* for determinate progress bar, or `false` for indeterminate progress bar (default: false) |
| 63 |
*/ |
| 64 |
public function setProgress( $progress ) { |
| 65 |
$this->progress = $progress; |
| 66 |
|
| 67 |
if ( $progress !== false ) { |
| 68 |
$this->bar->setAttributes( [ 'style' => 'width: ' . $this->progress . '%;' ] ); |
| 69 |
$this->setAttributes( [ 'aria-valuenow' => $this->progress ] ); |
| 70 |
} else { |
| 71 |
$this->removeAttributes( [ 'aria-valuenow' ] ); |
| 72 |
} |
| 73 |
$this->toggleClasses( [ 'oo-ui-progressBarWidget-indeterminate' ], $progress === false ); |
| 74 |
} |
| 75 |
|
| 76 |
/** @inheritDoc */ |
| 77 |
public function getConfig( &$config ) { |
| 78 |
if ( $this->progress !== null ) { |
| 79 |
$config['progress'] = $this->progress; |
| 80 |
} |
| 81 |
if ( $this->inline ) { |
| 82 |
$config['inline'] = $this->inline; |
| 83 |
} |
| 84 |
return parent::getConfig( $config ); |
| 85 |
} |
| 86 |
} |