Modal.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Support\RemoteRenderable;
  6. use Illuminate\Support\Str;
  7. class Modal extends AbstractDisplayer
  8. {
  9. protected $title;
  10. protected $renderable;
  11. public function title(string $title)
  12. {
  13. $this->title = $title;
  14. }
  15. protected function generateElementId()
  16. {
  17. $key = Str::random(8);
  18. return 'grid-modal-'.$this->grid->getName().$key;
  19. }
  20. protected function addRenderableModalScript(string $modalId, string $url)
  21. {
  22. $script = <<<JS
  23. (function () {
  24. var modal = $('#{$modalId}');
  25. modal.on('show.bs.modal', function (e) {
  26. modal.find('.modal-body').html('<div style="min-height:150px"></div>');
  27. modal.find('.modal-body').loading();
  28. $.ajax('{$url}').then(function (data) {
  29. modal.find('.modal-body').html(data);
  30. });
  31. })
  32. })();
  33. JS;
  34. Admin::script($script);
  35. }
  36. protected function setUpRemoteRenderable(string $modalId, $renderable)
  37. {
  38. /* @var RemoteRenderable $renderable */
  39. if (is_string($renderable)) {
  40. $renderable = $renderable::make($this->getKey());
  41. }
  42. $this->addRenderableModalScript($modalId, $renderable->getUrl());
  43. $renderable::collectAssets();
  44. }
  45. public function display($callback = null)
  46. {
  47. $title = $this->trans('title');
  48. if (func_num_args() == 2) {
  49. [$title, $callback] = func_get_args();
  50. }
  51. $title = $this->title ?: $title;
  52. $html = $this->value;
  53. $id = $this->generateElementId();
  54. if ($callback instanceof \Closure) {
  55. $html = Helper::render(
  56. $callback->call($this->row, $this)
  57. );
  58. } elseif (is_string($callback) && is_subclass_of($callback, RemoteRenderable::class)) {
  59. $html = '';
  60. $this->setUpRemoteRenderable($id, $callback);
  61. } elseif ($callback instanceof RemoteRenderable) {
  62. $html = '';
  63. $this->setUpRemoteRenderable($id, $callback->setKey($this->getKey()));
  64. }
  65. return <<<EOT
  66. <span class="grid-expand" data-toggle="modal" data-target="#{$id}">
  67. <a href="javascript:void(0)"><i class="fa fa-clone"></i>&nbsp;&nbsp;{$this->value}</a>
  68. </span>
  69. <div class="modal fade" id="{$id}" role="dialog">
  70. <div class="modal-dialog modal-lg">
  71. <div class="modal-content">
  72. <div class="modal-header">
  73. <h4 class="modal-title">{$title}</h4>
  74. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  75. </div>
  76. <div class="modal-body">
  77. {$html}
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. EOT;
  83. }
  84. }