Panel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Dcat\Admin\Show;
  3. use Dcat\Admin\Show;
  4. use Dcat\Admin\Widgets\Box;
  5. use Dcat\Admin\Widgets\Card;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\Collection;
  8. class Panel implements Renderable
  9. {
  10. /**
  11. * The view to be rendered.
  12. *
  13. * @var string
  14. */
  15. protected $view = 'admin::show.panel';
  16. /**
  17. * The fields that this panel holds.
  18. *
  19. * @var Collection
  20. */
  21. protected $fields;
  22. /**
  23. * Variables in the view.
  24. *
  25. * @var array
  26. */
  27. protected $data;
  28. /**
  29. * Parent show instance.
  30. *
  31. * @var Show
  32. */
  33. protected $parent;
  34. /**
  35. * @var \Closure
  36. */
  37. protected $wrapper;
  38. /**
  39. * Panel constructor.
  40. */
  41. public function __construct(Show $show)
  42. {
  43. $this->parent = $show;
  44. $this->initData();
  45. }
  46. /**
  47. * Initialize view data.
  48. */
  49. protected function initData()
  50. {
  51. $this->data = [
  52. 'fields' => new Collection(),
  53. 'tools' => new Tools($this),
  54. 'style' => 'default',
  55. 'title' => trans('admin.detail'),
  56. ];
  57. }
  58. /**
  59. * Set parent container.
  60. *
  61. * @param Show $show
  62. *
  63. * @return $this
  64. */
  65. public function setParent(Show $show)
  66. {
  67. $this->parent = $show;
  68. return $this;
  69. }
  70. /**
  71. * Get parent container.
  72. *
  73. * @return Show
  74. */
  75. public function getParent()
  76. {
  77. return $this->parent;
  78. }
  79. /**
  80. * Set style for this panel.
  81. *
  82. * @param string $style
  83. *
  84. * @return $this
  85. */
  86. public function style($style = 'info')
  87. {
  88. $this->data['style'] = $style;
  89. return $this;
  90. }
  91. /**
  92. * Set title for this panel.
  93. *
  94. * @param string $title
  95. *
  96. * @return $this
  97. */
  98. public function title($title)
  99. {
  100. $this->data['title'] = $title;
  101. return $this;
  102. }
  103. /**
  104. * Set view for this panel to render.
  105. *
  106. * @param string $view
  107. *
  108. * @return $this
  109. */
  110. public function setView($view)
  111. {
  112. $this->view = $view;
  113. return $this;
  114. }
  115. /**
  116. * Add variables to show view.
  117. *
  118. * @param array $variables
  119. * @return $this
  120. */
  121. public function with(array $variables = [])
  122. {
  123. $this->data = array_merge($this->data, $variables);
  124. return $this;
  125. }
  126. /**
  127. * @return $this
  128. */
  129. public function wrap(\Closure $wrapper)
  130. {
  131. $this->wrapper = $wrapper;
  132. return $this;
  133. }
  134. /**
  135. * @return bool
  136. */
  137. public function hasWrapper()
  138. {
  139. return $this->wrapper ? true : false;
  140. }
  141. /**
  142. * Build panel tools.
  143. *
  144. * @param $callable
  145. */
  146. public function tools($callable)
  147. {
  148. call_user_func($callable, $this->data['tools']);
  149. }
  150. /**
  151. * @return Tools
  152. */
  153. public function getTools()
  154. {
  155. return $this->data['tools'];
  156. }
  157. /**
  158. * Fill fields to panel.
  159. *
  160. * @param []Field $fields
  161. *
  162. * @return $this
  163. */
  164. public function fill($fields)
  165. {
  166. $this->data['fields'] = $fields;
  167. return $this;
  168. }
  169. /**
  170. * Render this panel.
  171. *
  172. * @return string
  173. */
  174. public function render()
  175. {
  176. return $this->doWrap();
  177. }
  178. /**
  179. * @return string
  180. */
  181. protected function doWrap()
  182. {
  183. $view = view($this->view, $this->data);
  184. if (!$wrapper = $this->wrapper) {
  185. return "<div class='card material'>{$view->render()}</div>";
  186. }
  187. return $wrapper($view);
  188. }
  189. }