Alert.php 2.7 KB

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