Modal.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Support\Str;
  5. class Modal extends AbstractDisplayer
  6. {
  7. protected $title;
  8. public function title(string $title)
  9. {
  10. $this->title = $title;
  11. }
  12. protected function generateElementId()
  13. {
  14. $key = Str::random(8);
  15. return 'grid-modal-'.$this->grid->getName().$key;
  16. }
  17. public function display($callback = null)
  18. {
  19. $title = $this->trans('title');
  20. if (func_num_args() == 2) {
  21. [$title, $callback] = func_get_args();
  22. }
  23. $html = $this->value;
  24. if ($callback instanceof \Closure) {
  25. $html = Helper::render(
  26. $callback->call($this->row, $this)
  27. );
  28. }
  29. $title = $this->title ?: $title;
  30. $id = $this->generateElementId();
  31. return <<<EOT
  32. <span class="grid-expand" data-toggle="modal" data-target="#{$id}">
  33. <a href="javascript:void(0)"><i class="fa fa-clone"></i>&nbsp;&nbsp;{$this->value}</a>
  34. </span>
  35. <div class="modal fade" id="{$id}" role="dialog">
  36. <div class="modal-dialog modal-lg">
  37. <div class="modal-content">
  38. <div class="modal-header">
  39. <h4 class="modal-title">{$title}</h4>
  40. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  41. </div>
  42. <div class="modal-body">
  43. {$html}
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. EOT;
  49. }
  50. }