Relation.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Dcat\Admin\Show;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Show;
  5. use Illuminate\Support\Fluent;
  6. class Relation extends Field
  7. {
  8. /**
  9. * Relation name.
  10. *
  11. * @var string
  12. */
  13. protected $name;
  14. /**
  15. * Relation panel builder.
  16. *
  17. * @var \Closure
  18. */
  19. protected $builder;
  20. /**
  21. * Relation panel title.
  22. *
  23. * @var string
  24. */
  25. protected $title;
  26. /**
  27. * Parent model.
  28. *
  29. * @var Fluent
  30. */
  31. protected $model;
  32. /**
  33. * Relation constructor.
  34. *
  35. * @param string $name
  36. * @param \Closure $builder
  37. * @param string $title
  38. */
  39. public function __construct($name, $builder, $title = '')
  40. {
  41. $this->name = $name;
  42. $this->builder = $builder;
  43. $this->title = $this->formatLabel($title);
  44. }
  45. /**
  46. * Set parent model for relation.
  47. *
  48. * @param Fluent $model
  49. *
  50. * @return $this
  51. */
  52. public function setModel(Fluent $model)
  53. {
  54. $this->model = $model;
  55. return $this;
  56. }
  57. /**
  58. * Render this relation panel.
  59. *
  60. * @return string
  61. */
  62. public function render()
  63. {
  64. $view = call_user_func($this->builder, $this->model);
  65. if ($view instanceof Show) {
  66. return $this->renderTitle().$view->render();
  67. }
  68. if (!$view instanceof Grid) {
  69. return $this->renderTitle().$view;
  70. }
  71. $view->setName($this->name)
  72. ->disableFilterButton()
  73. ->disableBatchDelete()
  74. ->disableFilter();
  75. $filter = $view->getFilter()
  76. ->expand()
  77. ->withoutInputBorder()
  78. ->hiddenResetButtonText()
  79. ->expand()
  80. ->style('padding:0 0 5px;left:-5px;');
  81. $filter = "<div class='row'><div class='col-md-12'>{$filter->render()}</div></div>";
  82. return $this->renderTitle().$filter.$view->render();
  83. }
  84. /**
  85. * @return string
  86. */
  87. protected function renderTitle()
  88. {
  89. return <<<EOF
  90. <div class="row">
  91. <div class="col-md-12" style="margin-bottom:.75rem;font-size:18px;text-transform:uppercase">
  92. <span class="show-relation-grid-title">{$this->title}</span>
  93. </div>
  94. </div>
  95. EOF;
  96. }
  97. }