|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
-
-
- namespace yii\bootstrap;
-
- use yii\base\InvalidConfigException;
- use yii\helpers\ArrayHelper;
-
-
- class Progress extends Widget
- {
-
-
- public $label;
-
-
- public $percent = 0;
-
-
- public $barOptions = [];
-
-
- public $bars;
-
-
-
-
- public function init()
- {
- parent::init();
- Html::addCssClass($this->options, ['widget' => 'progress']);
- }
-
-
-
- public function run()
- {
- BootstrapAsset::register($this->getView());
- return implode("\n", [
- Html::beginTag('div', $this->options),
- $this->renderProgress(),
- Html::endTag('div')
- ]) . "\n";
- }
-
-
-
- protected function renderProgress()
- {
- if (empty($this->bars)) {
- return $this->renderBar($this->percent, $this->label, $this->barOptions);
- }
- $bars = [];
- foreach ($this->bars as $bar) {
- $label = ArrayHelper::getValue($bar, 'label', '');
- if (!isset($bar['percent'])) {
- throw new InvalidConfigException("The 'percent' option is required.");
- }
- $options = ArrayHelper::getValue($bar, 'options', []);
- $bars[] = $this->renderBar($bar['percent'], $label, $options);
- }
-
- return implode("\n", $bars);
- }
-
-
-
- protected function renderBar($percent, $label = '', $options = [])
- {
- $defaultOptions = [
- 'role' => 'progressbar',
- 'aria-valuenow' => $percent,
- 'aria-valuemin' => 0,
- 'aria-valuemax' => 100,
- 'style' => "width:{$percent}%",
- ];
- $options = array_merge($defaultOptions, $options);
- Html::addCssClass($options, ['widget' => 'progress-bar']);
-
- $out = Html::beginTag('div', $options);
- $out .= $label;
- $out .= Html::tag('span', \Yii::t('yii', '{percent}% Complete', ['percent' => $percent]), [
- 'class' => 'sr-only'
- ]);
- $out .= Html::endTag('div');
-
- return $out;
- }
- }
|