Row.php 5.7 KB

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