| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace Dcat\Admin\Form;
- use Dcat\Admin\Form;
- use Dcat\Admin\Layout\Column;
- use Dcat\Admin\Widgets\Form as WidgetForm;
- class Layout
- {
- /**
- * @var Form|WidgetForm
- */
- protected $form;
- /**
- * @var Column[]
- */
- protected $columns = [];
- /**
- * @var array
- */
- protected $currentFields = [];
- protected $hasBlock = false;
- /**
- * @var bool
- */
- protected $hasColumn = false;
- public function __construct($form)
- {
- $this->form = $form;
- }
- public function addField(Field $field)
- {
- $this->currentFields[] = $field;
- }
- public function hasBlocks()
- {
- return $this->hasBlock;
- }
- public function hasColumns()
- {
- return $this->hasColumn;
- }
- /**
- * 列布局.
- *
- * @param int $width 1~12
- * @param mixed $content
- */
- public function onlyColumn($width, $content)
- {
- $width = (int) ($width < 1 ? round(12 * $width) : $width);
- $this->hasColumn = true;
- $this->resetCurrentFields();
- $column = $this->column($width, $content);
- foreach ($this->currentFields as $field) {
- $column->append($field);
- }
- }
- /**
- * 增加列.
- *
- * @param int $width 1~12
- * @param mixed $content
- *
- * @return Column
- */
- public function column(int $width, $content)
- {
- return $this->columns[] = new Column($content, $width);
- }
- /**
- * block布局.
- *
- * @param int $width
- * @param \Closure $callback
- */
- public function block(int $width, \Closure $callback)
- {
- $this->hasBlock = true;
- $this->column($width, function (Column $column) use ($callback) {
- $this->form->layoutColumn = $column;
- $column->row(function (\Dcat\Admin\Layout\Row $row) use ($callback) {
- $form = $this->form();
- $form->layoutRow = $row;
- $row->column(12, $form);
- $callback($form);
- });
- });
- }
- /**
- * @param int $width
- * @param mixed $content
- */
- public function prepend(int $width, $content)
- {
- $column = new Column($content, $width);
- array_unshift($this->columns, $column);
- }
- /**
- * @param \Closure|null $callback
- *
- * @return BlockForm
- */
- public function form(\Closure $callback = null)
- {
- $form = new Form\BlockForm($this->form);
- $form->disableResetButton();
- $form->disableSubmitButton();
- $form->disableFormTag();
- $form->ajax(false);
- if ($callback) {
- $callback($form);
- }
- return $form;
- }
- /**
- * Build html of content.
- *
- * @return string
- */
- public function build()
- {
- $html = '<div class="row">';
- foreach ($this->columns as $column) {
- $html .= $column->render();
- }
- return $html.'</div>';
- }
- protected function resetCurrentFields()
- {
- $this->currentFields = [];
- }
- }
|