Expand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace UCore\DcatAdmin\Show;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Contracts\LazyRenderable;
  5. use Dcat\Admin\Grid\Displayers\AbstractDisplayer;
  6. use Dcat\Admin\Show\AbstractField;
  7. use Dcat\Admin\Support\Helper;
  8. use Illuminate\Support\Str;
  9. class Expand extends AbstractField
  10. {
  11. protected $button;
  12. protected static $counter = 0;
  13. public $callbackOrButton;
  14. public function button($button)
  15. {
  16. $this->button = $button;
  17. }
  18. public function callback($callbackOrButton)
  19. {
  20. $this->callbackOrButton = $callbackOrButton;
  21. return $this;
  22. }
  23. public function render($callbackOrButton =null)
  24. {
  25. return $this->display($callbackOrButton);
  26. }
  27. public function display($callbackOrButton = null)
  28. {
  29. $html = $this->value;
  30. $remoteUrl = '';
  31. if ($callbackOrButton && $callbackOrButton instanceof \Closure) {
  32. $callbackOrButton = $callbackOrButton->call($this->model, $this);
  33. if (!$callbackOrButton instanceof LazyRenderable) {
  34. $html = Helper::render($callbackOrButton);
  35. $callbackOrButton = null;
  36. }
  37. }
  38. if ($callbackOrButton instanceof LazyRenderable) {
  39. $html = '<div style="min-height: 150px"></div>';
  40. $remoteUrl = $callbackOrButton->getUrl();
  41. } elseif (is_string($callbackOrButton) && is_subclass_of($callbackOrButton, LazyRenderable::class)) {
  42. $html = '<div style="min-height: 150px"></div>';
  43. $renderable = $callbackOrButton::make();
  44. $remoteUrl = $renderable->getUrl();
  45. } elseif ($callbackOrButton && is_string($callbackOrButton)) {
  46. $this->button = $callbackOrButton;
  47. }
  48. $button = is_null($this->button) ? $this->value : $this->button;
  49. $data = [
  50. 'key' => $this->getKey(),
  51. 'url' => $remoteUrl,
  52. 'button' => $button,
  53. 'html' => $html,
  54. 'dataKey' => $this->getDataKey(),
  55. ];
  56. return Admin::view('admin_core.core.show.expand',$data);
  57. }
  58. protected function getKey()
  59. {
  60. return $this->value;
  61. }
  62. protected function getDataKey()
  63. {
  64. $key = $this->value ?: Str::random(8);
  65. static::$counter++;
  66. return $this->makeName($key . '-' . static::$counter);
  67. }
  68. public function makeName($string)
  69. {
  70. return 'expand' . $string;
  71. }
  72. }