Alert.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 = 'light';
  22. /**
  23. * @var string
  24. */
  25. protected $icon;
  26. /**
  27. * @var bool
  28. */
  29. protected $showCloseBtn = false;
  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. *
  48. * @return $this
  49. */
  50. public function title($title)
  51. {
  52. $this->title = $title;
  53. return $this;
  54. }
  55. /**
  56. * Set contents.
  57. *
  58. * @param string|\Closure|Renderable $content
  59. *
  60. * @return $this
  61. */
  62. public function content($content)
  63. {
  64. $this->content = $this->toString($content);
  65. return $this;
  66. }
  67. /**
  68. * Set info style.
  69. *
  70. * @return $this
  71. */
  72. public function info()
  73. {
  74. return $this->style('info')->icon('fa fa-info');
  75. }
  76. /**
  77. * Set success style.
  78. *
  79. * @return $this
  80. */
  81. public function success()
  82. {
  83. return $this->style('success')->icon('fa fa-check');
  84. }
  85. /**
  86. * Set warning style.
  87. *
  88. * @return $this
  89. */
  90. public function warning()
  91. {
  92. return $this->style('warning')->icon('fa fa-warning');
  93. }
  94. /**
  95. * Set warning style.
  96. *
  97. * @return $this
  98. */
  99. public function danger()
  100. {
  101. return $this->style('danger')->icon('fa fa-ban');
  102. }
  103. /**
  104. * Set light style.
  105. *
  106. * @return $this
  107. */
  108. public function light()
  109. {
  110. return $this->style('light');
  111. }
  112. /**
  113. * Show close button.
  114. *
  115. * @param bool $value
  116. *
  117. * @return $this
  118. */
  119. public function removable(bool $value = true)
  120. {
  121. $this->showCloseBtn = $value;
  122. return $this;
  123. }
  124. /**
  125. * Add style.
  126. *
  127. * @param string $style
  128. *
  129. * @return $this
  130. */
  131. public function style($style = 'info')
  132. {
  133. $this->style = $style;
  134. return $this;
  135. }
  136. /**
  137. * Add icon.
  138. *
  139. * @param string $icon
  140. *
  141. * @return $this
  142. */
  143. public function icon($icon)
  144. {
  145. $this->icon = $icon;
  146. return $this;
  147. }
  148. /**
  149. * @return array
  150. */
  151. public function variables()
  152. {
  153. $this->class("alert alert-{$this->style} alert-dismissable");
  154. return [
  155. 'title' => $this->title,
  156. 'content' => $this->content,
  157. 'icon' => $this->icon,
  158. 'attributes' => $this->formatHtmlAttributes(),
  159. 'showCloseBtn' => $this->showCloseBtn,
  160. ];
  161. }
  162. }