Row.php 5.9 KB

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