Expand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Contracts\LazyRenderable;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Support\Str;
  7. class Expand extends AbstractDisplayer
  8. {
  9. protected $button;
  10. /**
  11. * @var array
  12. */
  13. protected static $counter = 0;
  14. public function button($button)
  15. {
  16. $this->button = $button;
  17. }
  18. public function display($callbackOrButton = null)
  19. {
  20. $html = $this->value;
  21. $remoteUrl = '';
  22. if ($callbackOrButton && $callbackOrButton instanceof \Closure) {
  23. $callbackOrButton = $callbackOrButton->call($this->row, $this);
  24. if (! $callbackOrButton instanceof LazyRenderable) {
  25. $html = Helper::render($callbackOrButton);
  26. $callbackOrButton = null;
  27. }
  28. }
  29. if ($callbackOrButton instanceof LazyRenderable) {
  30. $html = '<div style="min-height: 150px"></div>';
  31. $remoteUrl = $callbackOrButton->getUrl();
  32. } elseif (is_string($callbackOrButton) && is_subclass_of($callbackOrButton, LazyRenderable::class)) {
  33. $html = '<div style="min-height: 150px"></div>';
  34. $renderable = $callbackOrButton::make();
  35. $remoteUrl = $renderable->getUrl();
  36. } elseif ($callbackOrButton && is_string($callbackOrButton)) {
  37. $this->button = $callbackOrButton;
  38. }
  39. $this->addScript();
  40. $key = $this->getDataKey();
  41. $button = is_null($this->button) ? $this->value : $this->button;
  42. return <<<EOT
  43. <span class="grid-expand" data-url="$remoteUrl" data-inserted="0" data-id="{$this->getKey()}" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}">
  44. <a href="javascript:void(0)"><i class="feather icon-chevrons-right"></i> $button</a>
  45. </span>
  46. <template class="grid-expand-{$key}">
  47. <div id="grid-collapse-{$key}" class="collapse">$html</div>
  48. </template>
  49. EOT;
  50. }
  51. /**
  52. * @return string
  53. */
  54. protected function getDataKey()
  55. {
  56. $key = $this->getKey() ?: Str::random(8);
  57. static::$counter++;
  58. return $this->grid->getName().$key.'-'.static::$counter;
  59. }
  60. protected function addScript()
  61. {
  62. $script = <<<'JS'
  63. $('.grid-expand').off('click').on('click', function () {
  64. var _th = $(this), url = _th.data('url');
  65. if ($(this).data('inserted') == '0') {
  66. var key = _th.data('key');
  67. var row = _th.closest('tr');
  68. var html = $('template.grid-expand-'+key).html();
  69. var id = 'expand-'+key+Dcat.helpers.random(10);
  70. var rowKey = _th.data('id');
  71. $(this).attr('data-expand', '#'+id);
  72. row.after("<tr id="+id+"><td colspan='"+(row.find('td').length)+"' style='padding:0 !important; border:0;height:0;'>"+html+"</td></tr>");
  73. if (url) {
  74. var collapse = $('#grid-collapse-'+key);
  75. collapse.find('div').loading();
  76. $('.dcat-loading').css({position: 'inherit', 'padding-top': '70px'});
  77. Dcat.helpers.asyncRender(url+'&key='+rowKey, function (html) {
  78. collapse.html(html);
  79. })
  80. }
  81. $(this).data('inserted', 1);
  82. } else {
  83. if ($("i", this).hasClass('icon-chevrons-right')) {
  84. $(_th.data('expand')).show();
  85. } else {
  86. setTimeout(function() {
  87. $(_th.data('expand')).hide();
  88. }, 250);
  89. }
  90. }
  91. $("i", this).toggleClass("icon-chevrons-right icon-chevrons-down");
  92. });
  93. JS;
  94. Admin::script($script);
  95. }
  96. }