Alert.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Alert extends Widget
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $view = 'admin::widgets.alert';
  10. /**
  11. * @var string|\Symfony\Component\Translation\TranslatorInterface
  12. */
  13. protected $title = '';
  14. /**
  15. * @var string
  16. */
  17. protected $content = '';
  18. /**
  19. * @var string
  20. */
  21. protected $style = 'danger';
  22. /**
  23. * @var string
  24. */
  25. protected $icon = 'ban';
  26. /**
  27. * @var bool
  28. */
  29. protected $showCloseBtn = true;
  30. /**
  31. * Alert constructor.
  32. *
  33. * @param mixed $content
  34. * @param string $title
  35. * @param string $style
  36. */
  37. public function __construct($content = '', $title = null, $style = 'danger')
  38. {
  39. $this->content($content);
  40. $this->title($title);
  41. $this->style($style);
  42. }
  43. /**
  44. * Set title.
  45. *
  46. * @param string $title
  47. * @return $this
  48. */
  49. public function title($title)
  50. {
  51. $this->title = $title;
  52. return $this;
  53. }
  54. /**
  55. * Set contents.
  56. *
  57. * @param string|\Closure|Renderable $content
  58. * @return $this
  59. */
  60. public function content($content)
  61. {
  62. $this->content = $this->toString($content);
  63. return $this;
  64. }
  65. /**
  66. * Set info style.
  67. *
  68. * @return $this
  69. */
  70. public function info()
  71. {
  72. return $this->style('info')->icon('fa fa-info');
  73. }
  74. /**
  75. * Set success style.
  76. *
  77. * @return $this
  78. */
  79. public function success()
  80. {
  81. return $this->style('success')->icon('fa fa-check');
  82. }
  83. /**
  84. * Set warning style.
  85. *
  86. * @return $this
  87. */
  88. public function warning()
  89. {
  90. return $this->style('warning')->icon('fa fa-warning');
  91. }
  92. /**
  93. * Disable close button.
  94. *
  95. * @param bool $value
  96. * @return $this
  97. */
  98. public function disableCloseButton(bool $value = true)
  99. {
  100. $this->showCloseBtn = ! $value;
  101. return $this;
  102. }
  103. /**
  104. * Add style.
  105. *
  106. * @param string $style
  107. *
  108. * @return $this
  109. */
  110. public function style($style = 'info')
  111. {
  112. $this->style = $style;
  113. return $this;
  114. }
  115. /**
  116. * Add icon.
  117. *
  118. * @param string $icon
  119. *
  120. * @return $this
  121. */
  122. public function icon($icon)
  123. {
  124. $this->icon = $icon;
  125. return $this;
  126. }
  127. /**
  128. * @return array
  129. */
  130. public function variables()
  131. {
  132. $this->class("alert alert-{$this->style} alert-dismissable");
  133. return [
  134. 'title' => $this->title,
  135. 'content' => $this->content,
  136. 'icon' => $this->icon,
  137. 'attributes' => $this->formatHtmlAttributes(),
  138. 'showCloseBtn' => $this->showCloseBtn,
  139. ];
  140. }
  141. }