1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Illuminate\Contracts\Support\Renderable; |
7
|
|
|
|
8
|
|
|
class Expand extends AbstractDisplayer |
9
|
|
|
{ |
10
|
|
|
protected $renderable; |
11
|
|
|
|
12
|
|
|
public function display($callback = null, $isExpand = false) |
13
|
|
|
{ |
14
|
|
View Code Duplication |
if (is_subclass_of($callback, Renderable::class)) { |
|
|
|
|
15
|
|
|
$html = <<<HTML |
16
|
|
|
<div class="loading text-center" style="padding: 20px 0px;"> |
17
|
|
|
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> |
18
|
|
|
</div> |
19
|
|
|
HTML; |
20
|
|
|
$this->renderable = $callback; |
21
|
|
|
$this->addRenderableModalScript(); |
22
|
|
|
} else { |
23
|
|
|
$callback = $callback->bindTo($this->row); |
24
|
|
|
|
25
|
|
|
$html = call_user_func_array($callback, [$this->row]); |
26
|
|
|
|
27
|
|
|
$this->addScript($isExpand); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$key = $this->column->getName().'-'.$this->getKey(); |
31
|
|
|
|
32
|
|
|
return <<<EOT |
33
|
|
|
<span class="{$this->getElementClass()}" data-inserted="0" data-pk="{$this->getKey()}" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}"> |
34
|
|
|
<a href="javascript:void(0)"><i class="fa fa-angle-double-down"></i> {$this->value}</a> |
35
|
|
|
</span> |
36
|
|
|
<template class="grid-expand-{$key}"> |
37
|
|
|
<div id="grid-collapse-{$key}" class="collapse"> |
38
|
|
|
<div style="padding: 10px 10px 0 10px;" class="html">$html</div> |
39
|
|
|
</div> |
40
|
|
|
</template> |
41
|
|
|
EOT; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $multiple |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function getLoadUrl() |
50
|
|
|
{ |
51
|
|
|
$renderable = str_replace('\\', '_', $this->renderable); |
52
|
|
|
|
53
|
|
|
return route('admin.handle-renderable', compact('renderable')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function addRenderableModalScript() |
57
|
|
|
{ |
58
|
|
|
$script = <<<SCRIPT |
59
|
|
|
$('.{$this->getElementClass()}').on('click', function () { |
60
|
|
|
var target = $(this); |
61
|
|
|
if (target.data('inserted') == '0') { |
62
|
|
|
var pk = target.data('pk'); |
63
|
|
|
var key = $(this).data('key'); |
64
|
|
|
var row = $(this).closest('tr'); |
65
|
|
|
var html = $('template.grid-expand-'+key).html(); |
66
|
|
|
|
67
|
|
|
row.after("<tr style='background-color: #ecf0f5;'><td colspan='"+(row.find('td').length)+"' style='padding:0 !important; border:0;'>"+html+"</td></tr>"); |
68
|
|
|
|
69
|
|
|
$(this).data('inserted', 1); |
70
|
|
|
|
71
|
|
|
$.get('{$this->getLoadUrl()}'+'&key='+pk, function (data) { |
72
|
|
|
$('#grid-collapse-'+key).find('.html').html(data); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$("i", this).toggleClass("fa-angle-double-down fa-angle-double-up"); |
77
|
|
|
}); |
78
|
|
|
SCRIPT; |
79
|
|
|
|
80
|
|
|
Admin::script($script); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function addScript($isExpand) |
84
|
|
|
{ |
85
|
|
|
$script = <<<SCRIPT |
86
|
|
|
$('.{$this->getElementClass()}').on('click', function () { |
87
|
|
|
|
88
|
|
|
if ($(this).data('inserted') == '0') { |
89
|
|
|
|
90
|
|
|
var key = $(this).data('key'); |
91
|
|
|
var row = $(this).closest('tr'); |
92
|
|
|
var html = $('template.grid-expand-'+key).html(); |
93
|
|
|
|
94
|
|
|
row.after("<tr style='background-color: #ecf0f5;'><td colspan='"+(row.find('td').length)+"' style='padding:0 !important; border:0;'>"+html+"</td></tr>"); |
95
|
|
|
|
96
|
|
|
$(this).data('inserted', 1); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$("i", this).toggleClass("fa-angle-double-down fa-angle-double-up"); |
100
|
|
|
}); |
101
|
|
|
SCRIPT; |
102
|
|
|
|
103
|
|
|
if ($isExpand) { |
104
|
|
|
$script .= "$('.{$this->getElementClass()}').trigger('click');"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
Admin::script($script); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function getElementClass() |
114
|
|
|
{ |
115
|
|
|
return "grid-expand-{$this->grid->getGridRowName()}"; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|