Completed
Push — master ( b511be...229cbe )
by Song
02:32
created

Expand::display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 15
Ratio 48.39 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 15
loc 31
rs 9.424
c 0
b 0
f 0
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)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Contracts\Support\Renderable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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>&nbsp;&nbsp;{$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
0 ignored issues
show
Bug introduced by
There is no parameter named $multiple. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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