Card.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Grid\LazyRenderable as LazyGrid;
  4. use Dcat\Admin\Traits\LazyWidget;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\Str;
  7. class Card extends Widget
  8. {
  9. protected $view = 'admin::widgets.card';
  10. protected $title;
  11. protected $content;
  12. protected $footer;
  13. protected $tools = [];
  14. protected $divider = false;
  15. protected $padding;
  16. public function __construct($title = '', $content = null)
  17. {
  18. if ($content === null) {
  19. $content = $title;
  20. $title = '';
  21. }
  22. $this->title($title);
  23. $this->content($content);
  24. $this->class('card');
  25. $this->id('card-'.Str::random(8));
  26. }
  27. /**
  28. * @return $this
  29. */
  30. public function withHeaderBorder()
  31. {
  32. $this->divider = true;
  33. return $this;
  34. }
  35. /**
  36. * 设置卡片间距.
  37. *
  38. * @param string $padding
  39. */
  40. public function padding(string $padding)
  41. {
  42. $this->padding = 'padding:'.$padding;
  43. return $this;
  44. }
  45. public function noPadding()
  46. {
  47. return $this->padding('0');
  48. }
  49. /**
  50. * @param string|\Closure|Renderable|LazyWidget $content
  51. *
  52. * @return $this
  53. */
  54. public function content($content)
  55. {
  56. if ($content instanceof LazyGrid) {
  57. $content->simple();
  58. }
  59. $this->content = $this->formatRenderable($content);
  60. return $this;
  61. }
  62. /**
  63. * @param string $content
  64. *
  65. * @return $this
  66. */
  67. public function footer($content)
  68. {
  69. $this->footer = $content;
  70. return $this;
  71. }
  72. /**
  73. * @param string $title
  74. *
  75. * @return $this
  76. */
  77. public function title($title)
  78. {
  79. $this->title = $title;
  80. return $this;
  81. }
  82. /**
  83. * @param string|Renderable|\Closure $content
  84. *
  85. * @return $this
  86. */
  87. public function tool($content)
  88. {
  89. $this->tools[] = $this->toString($content);
  90. return $this;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function defaultVariables()
  96. {
  97. return [
  98. 'title' => $this->title,
  99. 'content' => $this->toString($this->content),
  100. 'footer' => $this->toString($this->footer),
  101. 'tools' => $this->tools,
  102. 'attributes' => $this->formatHtmlAttributes(),
  103. 'padding' => $this->padding,
  104. 'divider' => $this->divider,
  105. ];
  106. }
  107. }