選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

149 行
3.8KB

  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;
  9. use yii\helpers\ArrayHelper;
  10. /**
  11. * Alert renders an alert bootstrap component.
  12. *
  13. * For example,
  14. *
  15. * ```php
  16. * echo Alert::widget([
  17. * 'options' => [
  18. * 'class' => 'alert-info',
  19. * ],
  20. * 'body' => 'Say hello...',
  21. * ]);
  22. * ```
  23. *
  24. * The following example will show the content enclosed between the [[begin()]]
  25. * and [[end()]] calls within the alert box:
  26. *
  27. * ```php
  28. * Alert::begin([
  29. * 'options' => [
  30. * 'class' => 'alert-warning',
  31. * ],
  32. * ]);
  33. *
  34. * echo 'Say hello...';
  35. *
  36. * Alert::end();
  37. * ```
  38. *
  39. * @see http://getbootstrap.com/components/#alerts
  40. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  41. * @since 2.0
  42. */
  43. class Alert extends Widget
  44. {
  45. /**
  46. * @var string the body content in the alert component. Note that anything between
  47. * the [[begin()]] and [[end()]] calls of the Alert widget will also be treated
  48. * as the body content, and will be rendered before this.
  49. */
  50. public $body;
  51. /**
  52. * @var array the options for rendering the close button tag.
  53. * The close button is displayed in the header of the modal window. Clicking
  54. * on the button will hide the modal window. If this is false, no close button will be rendered.
  55. *
  56. * The following special options are supported:
  57. *
  58. * - tag: string, the tag name of the button. Defaults to 'button'.
  59. * - label: string, the label of the button. Defaults to '&times;'.
  60. *
  61. * The rest of the options will be rendered as the HTML attributes of the button tag.
  62. * Please refer to the [Alert documentation](http://getbootstrap.com/components/#alerts)
  63. * for the supported HTML attributes.
  64. */
  65. public $closeButton = [];
  66. /**
  67. * Initializes the widget.
  68. */
  69. public function init()
  70. {
  71. parent::init();
  72. $this->initOptions();
  73. echo Html::beginTag('div', $this->options) . "\n";
  74. echo $this->renderBodyBegin() . "\n";
  75. }
  76. /**
  77. * Renders the widget.
  78. */
  79. public function run()
  80. {
  81. echo "\n" . $this->renderBodyEnd();
  82. echo "\n" . Html::endTag('div');
  83. $this->registerPlugin('alert');
  84. }
  85. /**
  86. * Renders the close button if any before rendering the content.
  87. * @return string the rendering result
  88. */
  89. protected function renderBodyBegin()
  90. {
  91. return $this->renderCloseButton();
  92. }
  93. /**
  94. * Renders the alert body (if any).
  95. * @return string the rendering result
  96. */
  97. protected function renderBodyEnd()
  98. {
  99. return $this->body . "\n";
  100. }
  101. /**
  102. * Renders the close button.
  103. * @return string the rendering result
  104. */
  105. protected function renderCloseButton()
  106. {
  107. if (($closeButton = $this->closeButton) !== false) {
  108. $tag = ArrayHelper::remove($closeButton, 'tag', 'button');
  109. $label = ArrayHelper::remove($closeButton, 'label', '&times;');
  110. if ($tag === 'button' && !isset($closeButton['type'])) {
  111. $closeButton['type'] = 'button';
  112. }
  113. return Html::tag($tag, $label, $closeButton);
  114. } else {
  115. return null;
  116. }
  117. }
  118. /**
  119. * Initializes the widget options.
  120. * This method sets the default values for various options.
  121. */
  122. protected function initOptions()
  123. {
  124. Html::addCssClass($this->options, ['alert', 'fade', 'in']);
  125. if ($this->closeButton !== false) {
  126. $this->closeButton = array_merge([
  127. 'data-dismiss' => 'alert',
  128. 'aria-hidden' => 'true',
  129. 'class' => 'close',
  130. ], $this->closeButton);
  131. }
  132. }
  133. }