Row.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\SelectResource selectResource($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->form = $form;
  97. call_user_func($this->callback, $this);
  98. }
  99. /**
  100. * Get fields of this row.
  101. *
  102. * @return array
  103. */
  104. public function fields()
  105. {
  106. return $this->fields;
  107. }
  108. /**
  109. * Set width for a incomming field.
  110. *
  111. * @param int $width
  112. *
  113. * @return $this
  114. */
  115. public function width($width = 12)
  116. {
  117. $this->defaultFieldWidth = $width;
  118. return $this;
  119. }
  120. /**
  121. * Render the row.
  122. *
  123. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  124. */
  125. public function render()
  126. {
  127. return view('admin::form.row', ['fields' => $this->fields]);
  128. }
  129. /**
  130. * Add field.
  131. *
  132. * @param string $method
  133. * @param array $arguments
  134. *
  135. * @return Field|void
  136. */
  137. public function __call($method, $arguments)
  138. {
  139. $field = $this->form->__call($method, $arguments);
  140. $field->disableHorizontal();
  141. $this->fields[] = [
  142. 'width' => $this->defaultFieldWidth,
  143. 'element' => $field,
  144. ];
  145. return $field;
  146. }
  147. }