Layout.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 hasBlocks()
  34. {
  35. return $this->hasBlock;
  36. }
  37. public function hasColumns()
  38. {
  39. return $this->hasColumn;
  40. }
  41. /**
  42. * 列布局.
  43. *
  44. * @param int $width 1~12
  45. * @param mixed $content
  46. */
  47. public function onlyColumn($width, $content)
  48. {
  49. $width = (int) ($width < 1 ? round(12 * $width) : $width);
  50. $this->hasColumn = true;
  51. $this->resetCurrentFields();
  52. $column = $this->column($width, $content);
  53. foreach ($this->currentFields as $field) {
  54. $column->append($field);
  55. }
  56. }
  57. /**
  58. * 增加列.
  59. *
  60. * @param int $width 1~12
  61. * @param mixed $content
  62. *
  63. * @return Column
  64. */
  65. public function column(int $width, $content)
  66. {
  67. return $this->columns[] = new Column($content, $width);
  68. }
  69. /**
  70. * block布局.
  71. *
  72. * @param int $width
  73. * @param \Closure $callback
  74. */
  75. public function block(int $width, \Closure $callback)
  76. {
  77. $this->hasBlock = true;
  78. $this->column($width, function (Column $column) use ($callback) {
  79. $this->form->layoutColumn = $column;
  80. $column->row(function (\Dcat\Admin\Layout\Row $row) use ($callback) {
  81. $form = $this->form();
  82. $form->layoutRow = $row;
  83. $row->column(12, $form);
  84. $callback($form);
  85. });
  86. });
  87. }
  88. /**
  89. * @param int $width
  90. * @param mixed $content
  91. */
  92. public function prepend(int $width, $content)
  93. {
  94. $column = new Column($content, $width);
  95. array_unshift($this->columns, $column);
  96. }
  97. /**
  98. * @param \Closure|null $callback
  99. *
  100. * @return BlockForm
  101. */
  102. public function form(\Closure $callback = null)
  103. {
  104. $form = new Form\BlockForm($this->form);
  105. $form->disableResetButton();
  106. $form->disableSubmitButton();
  107. $form->disableFormTag();
  108. $form->ajax(false);
  109. if ($callback) {
  110. $callback($form);
  111. }
  112. return $form;
  113. }
  114. /**
  115. * Build html of content.
  116. *
  117. * @return string
  118. */
  119. public function build()
  120. {
  121. $html = '<div class="row">';
  122. foreach ($this->columns as $column) {
  123. $html .= $column->render();
  124. }
  125. return $html.'</div>';
  126. }
  127. protected function resetCurrentFields()
  128. {
  129. $this->currentFields = [];
  130. }
  131. }