Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

168 lines
4.7KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\base\InvalidConfigException;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * Progress renders a bootstrap progress bar component.
  13. *
  14. * For example,
  15. *
  16. * ```php
  17. * // default with label
  18. * echo Progress::widget([
  19. * 'percent' => 60,
  20. * 'label' => 'test',
  21. * ]);
  22. *
  23. * // styled
  24. * echo Progress::widget([
  25. * 'percent' => 65,
  26. * 'barOptions' => ['class' => 'progress-bar-danger']
  27. * ]);
  28. *
  29. * // striped
  30. * echo Progress::widget([
  31. * 'percent' => 70,
  32. * 'barOptions' => ['class' => 'progress-bar-warning'],
  33. * 'options' => ['class' => 'progress-striped']
  34. * ]);
  35. *
  36. * // striped animated
  37. * echo Progress::widget([
  38. * 'percent' => 70,
  39. * 'barOptions' => ['class' => 'progress-bar-success'],
  40. * 'options' => ['class' => 'active progress-striped']
  41. * ]);
  42. *
  43. * // stacked bars
  44. * echo Progress::widget([
  45. * 'bars' => [
  46. * ['percent' => 30, 'options' => ['class' => 'progress-bar-danger']],
  47. * ['percent' => 30, 'label' => 'test', 'options' => ['class' => 'progress-bar-success']],
  48. * ['percent' => 35, 'options' => ['class' => 'progress-bar-warning']],
  49. * ]
  50. * ]);
  51. * ```
  52. * @see http://getbootstrap.com/components/#progress
  53. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  54. * @author Alexander Makarov <sam@rmcreative.ru>
  55. * @since 2.0
  56. */
  57. class Progress extends Widget
  58. {
  59. /**
  60. * @var string the button label.
  61. */
  62. public $label;
  63. /**
  64. * @var integer the amount of progress as a percentage.
  65. */
  66. public $percent = 0;
  67. /**
  68. * @var array the HTML attributes of the bar.
  69. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  70. */
  71. public $barOptions = [];
  72. /**
  73. * @var array a set of bars that are stacked together to form a single progress bar.
  74. * Each bar is an array of the following structure:
  75. *
  76. * ```php
  77. * [
  78. * // required, the amount of progress as a percentage.
  79. * 'percent' => 30,
  80. * // optional, the label to be displayed on the bar
  81. * 'label' => '30%',
  82. * // optional, array, additional HTML attributes for the bar tag
  83. * 'options' => [],
  84. * ]
  85. * ```
  86. */
  87. public $bars;
  88. /**
  89. * Initializes the widget.
  90. * If you override this method, make sure you call the parent implementation first.
  91. */
  92. public function init()
  93. {
  94. parent::init();
  95. Html::addCssClass($this->options, 'progress');
  96. }
  97. /**
  98. * Renders the widget.
  99. */
  100. public function run()
  101. {
  102. BootstrapAsset::register($this->getView());
  103. return implode("\n", [
  104. Html::beginTag('div', $this->options),
  105. $this->renderProgress(),
  106. Html::endTag('div')
  107. ]) . "\n";
  108. }
  109. /**
  110. * Renders the progress.
  111. * @return string the rendering result.
  112. * @throws InvalidConfigException if the "percent" option is not set in a stacked progress bar.
  113. */
  114. protected function renderProgress()
  115. {
  116. if (empty($this->bars)) {
  117. return $this->renderBar($this->percent, $this->label, $this->barOptions);
  118. }
  119. $bars = [];
  120. foreach ($this->bars as $bar) {
  121. $label = ArrayHelper::getValue($bar, 'label', '');
  122. if (!isset($bar['percent'])) {
  123. throw new InvalidConfigException("The 'percent' option is required.");
  124. }
  125. $options = ArrayHelper::getValue($bar, 'options', []);
  126. $bars[] = $this->renderBar($bar['percent'], $label, $options);
  127. }
  128. return implode("\n", $bars);
  129. }
  130. /**
  131. * Generates a bar
  132. * @param integer $percent the percentage of the bar
  133. * @param string $label, optional, the label to display at the bar
  134. * @param array $options the HTML attributes of the bar
  135. * @return string the rendering result.
  136. */
  137. protected function renderBar($percent, $label = '', $options = [])
  138. {
  139. $defaultOptions = [
  140. 'role' => 'progressbar',
  141. 'aria-valuenow' => $percent,
  142. 'aria-valuemin' => 0,
  143. 'aria-valuemax' => 100,
  144. 'style' => "width:{$percent}%",
  145. ];
  146. $options = array_merge($defaultOptions, $options);
  147. Html::addCssClass($options, 'progress-bar');
  148. $out = Html::beginTag('div', $options);
  149. $out .= $label;
  150. $out .= Html::tag('span', \Yii::t('yii', '{percent}% Complete', ['percent' => $percent]), [
  151. 'class' => 'sr-only'
  152. ]);
  153. $out .= Html::endTag('div');
  154. return $out;
  155. }
  156. }