Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Progress.php 4.7KB

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