|
@@ -3,6 +3,7 @@
|
|
|
namespace Dcat\Admin\Grid\Displayers;
|
|
|
|
|
|
use Dcat\Admin\Admin;
|
|
|
+use Dcat\Admin\Support\RemoteRenderable;
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
@@ -20,9 +21,44 @@ class Expand extends AbstractDisplayer
|
|
|
$this->button = $button;
|
|
|
}
|
|
|
|
|
|
+ protected function generateElementId()
|
|
|
+ {
|
|
|
+ $key = Str::random(8);
|
|
|
+
|
|
|
+ return 'grid-modal-'.$this->grid->getName().$key;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function addRenderableScript(string $modalId, string $url)
|
|
|
+ {
|
|
|
+ $script = <<<JS
|
|
|
+(function () {
|
|
|
+ var modal = $('#{$modalId}');
|
|
|
+
|
|
|
+ modal.on('show.bs.modal', function (e) {
|
|
|
+ modal.find('.modal-body').html('<div style="min-height:150px"></div>');
|
|
|
+
|
|
|
+ modal.find('.modal-body').loading();
|
|
|
+
|
|
|
+ $.ajax('{$url}').then(function (data) {
|
|
|
+ modal.find('.modal-body').html(data);
|
|
|
+ });
|
|
|
+ })
|
|
|
+})();
|
|
|
+JS;
|
|
|
+
|
|
|
+ Admin::script($script);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function setUpRemoteRenderable(RemoteRenderable $renderable)
|
|
|
+ {
|
|
|
+ $renderable::collectAssets();
|
|
|
+ }
|
|
|
+
|
|
|
public function display($callbackOrButton = null)
|
|
|
{
|
|
|
$html = $this->value;
|
|
|
+ $remoteUrl = '';
|
|
|
+
|
|
|
if ($callbackOrButton && $callbackOrButton instanceof \Closure) {
|
|
|
$callback = $callbackOrButton->bindTo($this->row);
|
|
|
|
|
@@ -32,16 +68,28 @@ class Expand extends AbstractDisplayer
|
|
|
}
|
|
|
} elseif ($callbackOrButton && is_string($callbackOrButton)) {
|
|
|
$this->button = $callbackOrButton;
|
|
|
+ } elseif ($callbackOrButton instanceof RemoteRenderable) {
|
|
|
+ $html = '<div style="min-height: 150px"></div>';
|
|
|
+
|
|
|
+ $this->setUpRemoteRenderable($callbackOrButton);
|
|
|
+
|
|
|
+ $remoteUrl = $callbackOrButton->getUrl();
|
|
|
+ } elseif (is_string($callbackOrButton) && is_subclass_of($callbackOrButton, RemoteRenderable::class)) {
|
|
|
+ $html = '<div style="min-height: 150px"></div>';
|
|
|
+
|
|
|
+ $this->setUpRemoteRenderable($renderable = $callbackOrButton::make());
|
|
|
+
|
|
|
+ $remoteUrl = $renderable->getUrl();
|
|
|
}
|
|
|
|
|
|
- $this->setupScript();
|
|
|
+ $this->addScript($remoteUrl);
|
|
|
|
|
|
$key = $this->getDataKey();
|
|
|
|
|
|
$button = is_null($this->button) ? $this->value : $this->button;
|
|
|
|
|
|
return <<<EOT
|
|
|
-<span class="grid-expand" data-inserted="0" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}">
|
|
|
+<span class="grid-expand" data-inserted="0" data-id="{$this->getKey()}" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}">
|
|
|
<a href="javascript:void(0)"><i class="feather icon-chevrons-right"></i> $button</a>
|
|
|
</span>
|
|
|
<template class="grid-expand-{$key}">
|
|
@@ -62,11 +110,11 @@ EOT;
|
|
|
return $this->grid->getName().$key.'-'.static::$counter;
|
|
|
}
|
|
|
|
|
|
- protected function setupScript()
|
|
|
+ protected function addScript(?string $remoteUrl)
|
|
|
{
|
|
|
- $script = <<<'JS'
|
|
|
+ $script = <<<JS
|
|
|
$('.grid-expand').off('click').on('click', function () {
|
|
|
- var _th = $(this);
|
|
|
+ var _th = $(this), url = "{$remoteUrl}";
|
|
|
|
|
|
if ($(this).data('inserted') == '0') {
|
|
|
|
|
@@ -74,10 +122,21 @@ $('.grid-expand').off('click').on('click', function () {
|
|
|
var row = _th.closest('tr');
|
|
|
var html = $('template.grid-expand-'+key).html();
|
|
|
var id = 'expand-'+key+Dcat.helpers.random(10);
|
|
|
+ var rowKey = _th.data('id');
|
|
|
|
|
|
$(this).attr('data-expand', '#'+id);
|
|
|
|
|
|
row.after("<tr id="+id+"><td colspan='"+(row.find('td').length)+"' style='padding:0 !important; border:0;height:0;'>"+html+"</td></tr>");
|
|
|
+
|
|
|
+ if (url) {
|
|
|
+ var collapse = $('#grid-collapse-'+key);
|
|
|
+ collapse.find('div').loading();
|
|
|
+ $('.dcat-loading').css({position: 'inherit', 'padding-top': '70px'});
|
|
|
+
|
|
|
+ $.ajax(url+'&key='+rowKey).then(function (data) {
|
|
|
+ collapse.html(data);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
$(this).data('inserted', 1);
|
|
|
} else {
|