Row.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Dcat\Admin\Show;
  3. use Dcat\Admin\Show;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Support\Collection;
  6. class Row implements Renderable
  7. {
  8. /**
  9. * Callback for add field to current row.s.
  10. *
  11. * @var \Closure
  12. */
  13. protected $callback;
  14. /**
  15. * Parent show.
  16. *
  17. * @var Show
  18. */
  19. protected $show;
  20. /**
  21. * @var \Illuminate\Support\Collection
  22. */
  23. protected $fields;
  24. /**
  25. * Default field width for appended field.
  26. *
  27. * @var int
  28. */
  29. protected $defaultFieldWidth = 12;
  30. /**
  31. * Row constructor.
  32. *
  33. * @param \Closure $callback
  34. * @param Show $show
  35. */
  36. public function __construct(\Closure $callback, Show $show)
  37. {
  38. $this->callback = $callback;
  39. $this->show = $show;
  40. $this->fields = new Collection();
  41. call_user_func($this->callback, $this);
  42. }
  43. /**
  44. * Render the row.
  45. *
  46. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  47. */
  48. public function render()
  49. {
  50. return view('admin::show.row', ['fields' => $this->fields]);
  51. }
  52. /**
  53. * @return \Illuminate\Support\Collection
  54. */
  55. public function getFields()
  56. {
  57. return $this->fields;
  58. }
  59. /**
  60. * Set width for a incomming field.
  61. *
  62. * @param int $width
  63. *
  64. * @return $this
  65. */
  66. public function width($width = 12)
  67. {
  68. $this->defaultFieldWidth = $width;
  69. return $this;
  70. }
  71. /**
  72. * @param $name
  73. * @param string $label
  74. *
  75. * @return \Dcat\Admin\Show\Field
  76. */
  77. public function field($name, $label = '')
  78. {
  79. $field = $this->show->field($name, $label);
  80. $this->pushField($field);
  81. return $field;
  82. }
  83. /**
  84. * Add field.
  85. * @param $name
  86. *
  87. * @return \Dcat\Admin\Show\Field|\Illuminate\Support\Collection
  88. */
  89. public function __get($name)
  90. {
  91. $field = $this->show->__get($name);
  92. $this->pushField($field);
  93. return $field;
  94. }
  95. /**
  96. * @param $method
  97. * @param $arguments
  98. *
  99. * @return \Dcat\Admin\Show\Field
  100. */
  101. public function __call($method, $arguments)
  102. {
  103. $field = $this->show->__call($method, $arguments);
  104. $this->pushField($field);
  105. return $field;
  106. }
  107. /**
  108. * @param $field
  109. */
  110. public function pushField($field)
  111. {
  112. $this->fields->push([
  113. 'width' => $this->defaultFieldWidth,
  114. 'element' => $field,
  115. ]);
  116. }
  117. }