Box.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * @return $this
  37. */
  38. public function content($content)
  39. {
  40. if ($content instanceof LazyGrid) {
  41. $content->simple();
  42. }
  43. $this->content = $this->formatRenderable($content);
  44. return $this;
  45. }
  46. /**
  47. * Set box title.
  48. *
  49. * @param string $title
  50. * @return $this
  51. */
  52. public function title($title)
  53. {
  54. $this->title = $title;
  55. return $this;
  56. }
  57. /**
  58. * Set box as collapsable.
  59. *
  60. * @return $this
  61. */
  62. public function collapsable()
  63. {
  64. $this->tools[] =
  65. '<button class="border-0 bg-white" data-action="collapse"><i class="feather icon-minus"></i></button>';
  66. return $this;
  67. }
  68. /**
  69. * Set box as removable.
  70. *
  71. * @return $this
  72. */
  73. public function removable()
  74. {
  75. $this->tools[] =
  76. '<button class="border-0 bg-white" data-action="remove"><i class="feather icon-x"></i></button>';
  77. return $this;
  78. }
  79. /**
  80. * Set box style.
  81. *
  82. * @param string $styles
  83. * @return $this|Box
  84. */
  85. public function style($styles)
  86. {
  87. $styles = array_map(function ($style) {
  88. return 'box-'.$style;
  89. }, (array) $styles);
  90. $this->class = $this->class.' '.implode(' ', $styles);
  91. return $this;
  92. }
  93. /**
  94. * @param string|Renderable|\Closure $content
  95. * @return $this
  96. */
  97. public function tool($content)
  98. {
  99. $this->tools[] = $this->toString($content);
  100. return $this;
  101. }
  102. /**
  103. * Add `box-solid` class to box.
  104. *
  105. * @return $this
  106. */
  107. public function solid()
  108. {
  109. return $this->style('solid');
  110. }
  111. /**
  112. * Variables in view.
  113. *
  114. * @return array
  115. */
  116. public function defaultVariables()
  117. {
  118. return [
  119. 'title' => $this->title,
  120. 'content' => $this->toString($this->content),
  121. 'tools' => $this->tools,
  122. 'attributes' => $this->formatHtmlAttributes(),
  123. 'padding' => $this->padding,
  124. ];
  125. }
  126. }