Box.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Grid\LazyRenderable as LazyGrid;
  4. use Illuminate\Contracts\Support\Renderable;
  5. class Box extends Widget
  6. {
  7. protected $view = 'admin::widgets.box';
  8. protected $title = 'Box header';
  9. protected $content = 'here is the box content.';
  10. protected $tools = [];
  11. protected $padding;
  12. public function __construct($title = '', $content = '')
  13. {
  14. if ($title) {
  15. $this->title($title);
  16. }
  17. if ($content) {
  18. $this->content($content);
  19. }
  20. $this->class('box');
  21. }
  22. /**
  23. * Set content padding.
  24. *
  25. * @param string $padding
  26. */
  27. public function padding(string $padding)
  28. {
  29. $this->padding = 'padding:'.$padding;
  30. return $this;
  31. }
  32. /**
  33. * Set box content.
  34. *
  35. * @param string $content
  36. *
  37. * @return $this
  38. */
  39. public function content($content)
  40. {
  41. if ($content instanceof LazyGrid) {
  42. $content->simple();
  43. }
  44. $this->content = $this->formatRenderable($content);
  45. return $this;
  46. }
  47. /**
  48. * Set box title.
  49. *
  50. * @param string $title
  51. *
  52. * @return $this
  53. */
  54. public function title($title)
  55. {
  56. $this->title = $title;
  57. return $this;
  58. }
  59. /**
  60. * Set box as collapsable.
  61. *
  62. * @return $this
  63. */
  64. public function collapsable()
  65. {
  66. $this->tools[] =
  67. '<button class="border-0 bg-white" data-action="collapse"><i class="feather icon-minus"></i></button>';
  68. return $this;
  69. }
  70. /**
  71. * Set box as removable.
  72. *
  73. * @return $this
  74. */
  75. public function removable()
  76. {
  77. $this->tools[] =
  78. '<button class="border-0 bg-white" data-action="remove"><i class="feather icon-x"></i></button>';
  79. return $this;
  80. }
  81. /**
  82. * Set box style.
  83. *
  84. * @param string $styles
  85. *
  86. * @return $this|Box
  87. */
  88. public function style($styles)
  89. {
  90. $styles = array_map(function ($style) {
  91. return 'box-'.$style;
  92. }, (array) $styles);
  93. $this->class = $this->class.' '.implode(' ', $styles);
  94. return $this;
  95. }
  96. /**
  97. * @param string|Renderable|\Closure $content
  98. *
  99. * @return $this
  100. */
  101. public function tool($content)
  102. {
  103. $this->tools[] = $this->toString($content);
  104. return $this;
  105. }
  106. /**
  107. * Add `box-solid` class to box.
  108. *
  109. * @return $this
  110. */
  111. public function solid()
  112. {
  113. return $this->style('solid');
  114. }
  115. /**
  116. * Variables in view.
  117. *
  118. * @return array
  119. */
  120. public function defaultVariables()
  121. {
  122. return [
  123. 'title' => $this->title,
  124. 'content' => $this->toString($this->content),
  125. 'tools' => $this->tools,
  126. 'attributes' => $this->formatHtmlAttributes(),
  127. 'padding' => $this->padding,
  128. ];
  129. }
  130. }