Layout.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Layout\Column;
  5. use Dcat\Admin\Widgets\Form as WidgetForm;
  6. class Layout
  7. {
  8. /**
  9. * @var Form|WidgetForm
  10. */
  11. protected $form;
  12. /**
  13. * @var Column[]
  14. */
  15. protected $columns = [];
  16. /**
  17. * @var array
  18. */
  19. protected $currentFields = [];
  20. protected $hasBlock = false;
  21. /**
  22. * @var bool
  23. */
  24. protected $hasColumn = false;
  25. public function __construct($form)
  26. {
  27. $this->form = $form;
  28. }
  29. public function addField(Field $field)
  30. {
  31. $this->currentFields[] = $field;
  32. }
  33. public function appendToLastColumn($content)
  34. {
  35. if ($end = end($this->columns)) {
  36. foreach (is_array($content) ? $content : [$content] as $value) {
  37. $end->append($value);
  38. }
  39. }
  40. }
  41. public function hasBlocks()
  42. {
  43. return $this->hasBlock;
  44. }
  45. public function hasColumns()
  46. {
  47. return $this->hasColumn;
  48. }
  49. /**
  50. * 列布局.
  51. *
  52. * @param int $width 1~12
  53. * @param mixed $content
  54. */
  55. public function onlyColumn($width, $content)
  56. {
  57. $width = (int) ($width < 1 ? round(12 * $width) : $width);
  58. $this->hasColumn = true;
  59. $this->resetCurrentFields();
  60. $column = $this->column($width, $content);
  61. foreach ($this->currentFields as $field) {
  62. $column->append($field);
  63. }
  64. }
  65. /**
  66. * 增加列.
  67. *
  68. * @param int $width 1~12
  69. * @param mixed $content
  70. * @return Column
  71. */
  72. public function column(int $width, $content)
  73. {
  74. return $this->columns[] = new Column($content, $width);
  75. }
  76. /**
  77. * block布局.
  78. *
  79. * @param int $width
  80. * @param \Closure $callback
  81. */
  82. public function block(int $width, \Closure $callback)
  83. {
  84. $this->hasBlock = true;
  85. $this->column($width, function (Column $column) use ($callback) {
  86. $this->form->layoutColumn = $column;
  87. $column->row(function (\Dcat\Admin\Layout\Row $row) use ($callback) {
  88. $form = $this->form();
  89. $form->layoutRow = $row;
  90. $row->column(12, $form);
  91. $callback($form);
  92. });
  93. });
  94. }
  95. /**
  96. * @param int $width
  97. * @param mixed $content
  98. */
  99. public function prepend(int $width, $content)
  100. {
  101. $column = new Column($content, $width);
  102. array_unshift($this->columns, $column);
  103. }
  104. /**
  105. * @param \Closure|null $callback
  106. * @return BlockForm
  107. */
  108. public function form(\Closure $callback = null)
  109. {
  110. $form = new Form\BlockForm($this->form);
  111. $form->disableResetButton();
  112. $form->disableSubmitButton();
  113. $form->useFormTag(false);
  114. $form->ajax(false);
  115. if ($callback) {
  116. $callback($form);
  117. }
  118. return $form;
  119. }
  120. /**
  121. * Build html of content.
  122. *
  123. * @param string $add
  124. * @return string
  125. */
  126. public function build($add = null)
  127. {
  128. $html = '<div class="row">';
  129. foreach ($this->columns as $column) {
  130. $html .= $column->render();
  131. }
  132. return $html.'</div>'.$add;
  133. }
  134. public function getColumns()
  135. {
  136. return $this->columns;
  137. }
  138. public function setColumns(array $columns)
  139. {
  140. $this->columns = $columns;
  141. return $this;
  142. }
  143. public function reset()
  144. {
  145. $this->hasColumn = false;
  146. $this->resetCurrentFields();
  147. $this->setColumns([]);
  148. }
  149. protected function resetCurrentFields()
  150. {
  151. $this->currentFields = [];
  152. }
  153. }