Panel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Encore\Admin\Show;
  3. use Encore\Admin\Show;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Support\Collection;
  6. class Panel implements Renderable
  7. {
  8. /**
  9. * The view to be rendered.
  10. *
  11. * @var string
  12. */
  13. protected $view = 'admin::show.panel';
  14. /**
  15. * The fields that this panel holds.
  16. *
  17. * @var Collection
  18. */
  19. protected $fields;
  20. /**
  21. * Variables in the view.
  22. *
  23. * @var array
  24. */
  25. protected $data;
  26. /**
  27. * Parent show instance.
  28. *
  29. * @var Show
  30. */
  31. protected $parent;
  32. /**
  33. * Panel constructor.
  34. */
  35. public function __construct(Show $show)
  36. {
  37. $this->parent = $show;
  38. $this->initData();
  39. }
  40. /**
  41. * Initialize view data.
  42. */
  43. protected function initData()
  44. {
  45. $this->data = [
  46. 'fields' => new Collection(),
  47. 'tools' => new Tools($this),
  48. 'style' => 'info',
  49. 'title' => trans('admin.detail'),
  50. ];
  51. }
  52. /**
  53. * Set parent container.
  54. *
  55. * @param Show $show
  56. *
  57. * @return $this
  58. */
  59. public function setParent(Show $show)
  60. {
  61. $this->parent = $show;
  62. return $this;
  63. }
  64. /**
  65. * Get parent container.
  66. *
  67. * @return Show
  68. */
  69. public function getParent()
  70. {
  71. return $this->parent;
  72. }
  73. /**
  74. * Set style for this panel.
  75. *
  76. * @param string $style
  77. *
  78. * @return $this
  79. */
  80. public function style($style = 'info')
  81. {
  82. $this->data['style'] = $style;
  83. return $this;
  84. }
  85. /**
  86. * Set title for this panel.
  87. *
  88. * @param string $title
  89. *
  90. * @return $this
  91. */
  92. public function title($title)
  93. {
  94. $this->data['title'] = $title;
  95. return $this;
  96. }
  97. /**
  98. * Set view for this panel to render.
  99. *
  100. * @param string $view
  101. *
  102. * @return $this
  103. */
  104. public function view($view)
  105. {
  106. $this->view = $view;
  107. return $this;
  108. }
  109. /**
  110. * Build panel tools.
  111. *
  112. * @param $callable
  113. */
  114. public function tools($callable)
  115. {
  116. call_user_func($callable, $this->data['tools']);
  117. }
  118. /**
  119. * Fill fields to panel.
  120. *
  121. * @param []Field $fields
  122. *
  123. * @return $this
  124. */
  125. public function fill($fields)
  126. {
  127. $this->data['fields'] = $fields;
  128. return $this;
  129. }
  130. /**
  131. * Render this panel.
  132. *
  133. * @return string
  134. */
  135. public function render()
  136. {
  137. return view($this->view, $this->data)->render();
  138. }
  139. }