123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace Dcat\Admin\Grid\Displayers;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Support\RemoteRenderable;
- use Illuminate\Support\Str;
- class Modal extends AbstractDisplayer
- {
- protected $title;
- protected $renderable;
- public function title(string $title)
- {
- $this->title = $title;
- }
- protected function generateElementId()
- {
- $key = Str::random(8);
- return 'grid-modal-'.$this->grid->getName().$key;
- }
- protected function addRenderableModalScript(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(string $modalId, RemoteRenderable $renderable)
- {
- $renderable->with('key', $this->getKey());
- $this->addRenderableModalScript($modalId, $renderable->getUrl());
- $renderable::collectAssets();
- }
- public function display($callback = null)
- {
- $title = $this->value ?: $this->trans('title');
- if (func_num_args() == 2) {
- [$title, $callback] = func_get_args();
- }
- $html = $this->value;
- $id = $this->generateElementId();
- if ($callback instanceof \Closure) {
- $html = Helper::render(
- $callback->call($this->row, $this)
- );
- } elseif (is_string($callback) && is_subclass_of($callback, RemoteRenderable::class)) {
- $html = '';
- $this->setUpRemoteRenderable($id, $callback::make());
- } elseif ($callback instanceof RemoteRenderable) {
- $html = '';
- $this->setUpRemoteRenderable($id, $callback);
- }
- $title = $this->title ?: $title;
- return <<<EOT
- <span class="grid-expand" data-toggle="modal" data-target="#{$id}">
- <a href="javascript:void(0)"><i class="fa fa-clone"></i> {$this->value}</a>
- </span>
- <div class="modal fade" id="{$id}" role="dialog">
- <div class="modal-dialog modal-lg">
- <div class="modal-content">
- <div class="modal-header">
- <h4 class="modal-title">{$title}</h4>
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- </div>
- <div class="modal-body">
- {$html}
- </div>
- </div>
- </div>
- </div>
- EOT;
- }
- }
|