Column.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Contracts\Support\Renderable;
  7. class Column implements Renderable
  8. {
  9. /**
  10. * grid system prefix width.
  11. *
  12. * @var array
  13. */
  14. protected $width = [];
  15. /**
  16. * @var array
  17. */
  18. protected $contents = [];
  19. /**
  20. * Column constructor.
  21. *
  22. * @param $content
  23. * @param int $width
  24. */
  25. public function __construct($content, $width = 12)
  26. {
  27. $width = $this->normalizeWidth($width);
  28. if ($content instanceof \Closure) {
  29. call_user_func($content, $this);
  30. } else {
  31. $this->append($content);
  32. }
  33. ///// set width.
  34. // if null, or $this->width is empty array, set as "md" => "12"
  35. if (is_null($width) || (is_array($width) && count($width) === 0)) {
  36. $this->width['md'] = 12;
  37. }
  38. // $this->width is number(old version), set as "md" => $width
  39. elseif (is_numeric($width)) {
  40. $this->width['md'] = $width;
  41. } else {
  42. $this->width = $width;
  43. }
  44. }
  45. protected function normalizeWidth($width)
  46. {
  47. return (int) ($width < 1 ? round(12 * $width) : $width);
  48. }
  49. /**
  50. * Append content to column.
  51. *
  52. * @param $content
  53. *
  54. * @return $this
  55. */
  56. public function append($content)
  57. {
  58. $this->contents[] = $content;
  59. return $this;
  60. }
  61. /**
  62. * Add a row for column.
  63. *
  64. * @param $content
  65. *
  66. * @return Column
  67. */
  68. public function row($content)
  69. {
  70. if (! $content instanceof \Closure) {
  71. $row = new Row($content);
  72. } else {
  73. $row = new Row();
  74. call_user_func($content, $row);
  75. }
  76. return $this->append($row);
  77. }
  78. /**
  79. * Build column html.
  80. *
  81. * @return string
  82. */
  83. public function render()
  84. {
  85. $html = $this->startColumn();
  86. foreach ($this->contents as $content) {
  87. if ($content instanceof Grid && $content->isAsyncRequest()) {
  88. Admin::prevent($content->render());
  89. continue;
  90. }
  91. $html .= Helper::render($content);
  92. }
  93. return $html.$this->endColumn();
  94. }
  95. /**
  96. * Start column.
  97. *
  98. * @return string
  99. */
  100. protected function startColumn()
  101. {
  102. // get class name using width array
  103. $classnName = collect($this->width)->map(function ($value, $key) {
  104. return $value == 0 ? "col-$key" : "col-$key-$value";
  105. })->implode(' ');
  106. return "<div class=\"{$classnName}\">";
  107. }
  108. /**
  109. * End column.
  110. *
  111. * @return string
  112. */
  113. protected function endColumn()
  114. {
  115. return '</div>';
  116. }
  117. }