Row.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Illuminate\Contracts\Support\Renderable;
  5. /**
  6. * Class Row.
  7. *
  8. * @method Field\Text text($column, $label = '')
  9. * @method Field\Checkbox checkbox($column, $label = '')
  10. * @method Field\Radio radio($column, $label = '')
  11. * @method Field\Select select($column, $label = '')
  12. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  13. * @method Field\Textarea textarea($column, $label = '')
  14. * @method Field\Hidden hidden($column, $label = '')
  15. * @method Field\Id id($column, $label = '')
  16. * @method Field\Ip ip($column, $label = '')
  17. * @method Field\Url url($column, $label = '')
  18. * @method Field\Email email($column, $label = '')
  19. * @method Field\Mobile mobile($column, $label = '')
  20. * @method Field\Slider slider($column, $label = '')
  21. * @method Field\Map map($latitude, $longitude, $label = '')
  22. * @method Field\Editor editor($column, $label = '')
  23. * @method Field\Date date($column, $label = '')
  24. * @method Field\Datetime datetime($column, $label = '')
  25. * @method Field\Time time($column, $label = '')
  26. * @method Field\Year year($column, $label = '')
  27. * @method Field\Month month($column, $label = '')
  28. * @method Field\DateRange dateRange($start, $end, $label = '')
  29. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  30. * @method Field\TimeRange timeRange($start, $end, $label = '')
  31. * @method Field\Number number($column, $label = '')
  32. * @method Field\Currency currency($column, $label = '')
  33. * @method Field\SwitchField switch ($column, $label = '')
  34. * @method Field\Display display($column, $label = '')
  35. * @method Field\Rate rate($column, $label = '')
  36. * @method Field\Divide divider()
  37. * @method Field\Password password($column, $label = '')
  38. * @method Field\Decimal decimal($column, $label = '')
  39. * @method Field\Html html($html, $label = '')
  40. * @method Field\Tags tags($column, $label = '')
  41. * @method Field\Icon icon($column, $label = '')
  42. * @method Field\Embeds embeds($column, $label = '')
  43. * @method Field\Captcha captcha()
  44. * @method Field\Listbox listbox($column, $label = '')
  45. * @method Field\SelectResource selectResource($column, $label = '')
  46. * @method Field\File file($column, $label = '')
  47. * @method Field\Image image($column, $label = '')
  48. * @method Field\MultipleFile multipleFile($column, $label = '')
  49. * @method Field\MultipleImage multipleImage($column, $label = '')
  50. * @method Field\HasMany hasMany($column, $labelOrCallback, $callback = null)
  51. * @method Field\Tree tree($column, $label = '')
  52. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  53. * @method Field\ListField list($column, $label = '')
  54. * @method Field\Timezone timezone($column, $label = '')
  55. * @method Field\KeyValue keyValue($column, $label = '')
  56. * @method Field\Tel tel($column, $label = '')
  57. * @method Field\Markdown markdown($column, $label = '')
  58. */
  59. class Row implements Renderable {
  60. /**
  61. * Callback for add field to current row.s.
  62. *
  63. * @var \Closure
  64. */
  65. protected $callback;
  66. /**
  67. * Parent form.
  68. *
  69. * @var Form
  70. */
  71. protected $form;
  72. /**
  73. * Fields in this row.
  74. *
  75. * @var array
  76. */
  77. protected $fields = [];
  78. /**
  79. * Default field width for appended field.
  80. *
  81. * @var int
  82. */
  83. protected $defaultFieldWidth = 12;
  84. /**
  85. * Row constructor.
  86. *
  87. * @param \Closure $callback
  88. * @param Form $form
  89. */
  90. public function __construct(\Closure $callback, Form $form) {
  91. $this->callback = $callback;
  92. $this->form = $form;
  93. call_user_func($this->callback, $this);
  94. }
  95. /**
  96. * Get fields of this row.
  97. *
  98. * @return array
  99. */
  100. public function getFields() {
  101. return $this->fields;
  102. }
  103. /**
  104. * Set width for a incomming field.
  105. *
  106. * @param int $width
  107. *
  108. * @return $this
  109. */
  110. public function width($width = 12) {
  111. $this->defaultFieldWidth = $width;
  112. return $this;
  113. }
  114. /**
  115. * Render the row.
  116. *
  117. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  118. */
  119. public function render() {
  120. return view('admin::form.row', ['fields' => $this->fields]);
  121. }
  122. /**
  123. * Add field.
  124. *
  125. * @param string $method
  126. * @param array $arguments
  127. *
  128. * @return Field|void
  129. */
  130. public function __call($method, $arguments) {
  131. $field = $this->form->__call($method, $arguments);
  132. $field->disableHorizontal();
  133. $this->fields[] = [
  134. 'width' => $this->defaultFieldWidth,
  135. 'element' => $field,
  136. ];
  137. return $field;
  138. }
  139. }