Completed
Push — master ( 1d3c0c...8bf0fe )
by Song
03:06
created

DropdownActions::prependDefaultActions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Actions\RowAction;
6
use Encore\Admin\Admin;
7
use Encore\Admin\Grid\Actions\Delete;
8
use Encore\Admin\Grid\Actions\Edit;
9
use Encore\Admin\Grid\Actions\Show;
10
11
class DropdownActions extends Actions
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $custom = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $default = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $defaultClass = [Edit::class, Show::class, Delete::class];
27
28
    /**
29
     *
30
     */
31
    protected function script()
32
    {
33
        $script = <<<SCRIPT
34
(function ($) {
35
    $('.table-responsive').on('show.bs.dropdown', function () {
36
         $('.table-responsive').css( "overflow", "inherit" );
37
    });
38
    
39
    $('.table-responsive').on('hide.bs.dropdown', function () {
40
         $('.table-responsive').css( "overflow", "auto" );
41
    })
42
})(jQuery);
43
SCRIPT;
44
45
        Admin::script($script);
46
    }
47
48
    /**
49
     * @param RowAction $action
50
     *
51
     * @return $this
52
     */
53
    public function add(RowAction $action)
54
    {
55
        $this->prepareAction($action);
56
57
        array_push($this->custom, $action);
58
59
        return $this;
60
    }
61
62
    protected function prependDefaultActions()
63
    {
64
        foreach ($this->defaultClass as $class) {
65
            /** @var RowAction $action */
66
            $action = new $class();
67
68
            $this->prepareAction($action);
69
70
            array_push($this->default, $action);
71
        }
72
    }
73
74
    /**
75
     * @param RowAction $action
76
     */
77
    protected function prepareAction(RowAction $action)
78
    {
79
        $action->setGrid($this->grid)
80
            ->setColumn($this->column)
81
            ->setRow($this->row);
82
    }
83
84
    /**
85
     * Disable view action.
86
     *
87
     * @return $this
88
     */
89 View Code Duplication
    public function disableView(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        if ($disable) {
92
            array_delete($this->default, Show::class);
93
        } elseif (!in_array(Show::class, $this->default)) {
94
            array_push($this->default, Show::class);
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * Disable delete.
102
     *
103
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
104
     */
105 View Code Duplication
    public function disableDelete(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
106
    {
107
        if ($disable) {
108
            array_delete($this->default, Delete::class);
109
        } elseif (!in_array(Delete::class, $this->default)) {
110
            array_push($this->default, Delete::class);
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * Disable edit.
118
     *
119
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
120
     */
121 View Code Duplication
    public function disableEdit(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
122
    {
123
        if ($disable) {
124
            array_delete($this->default, Edit::class);
125
        } elseif (!in_array(Edit::class, $this->default)) {
126
            array_push($this->default, Edit::class);
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param null|\Closure $callback
134
     *
135
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
136
     */
137
    public function display($callback = null)
138
    {
139
        $this->script();
140
141
        if ($callback instanceof \Closure) {
142
            $callback->call($this, $this);
143
        }
144
145
        $this->prependDefaultActions();
146
147
        $actions = [
148
            'default' => $this->default,
149
            'custom'  => $this->custom,
150
        ];
151
152
        return view('admin::grid.dropdown-actions', $actions);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin::grid.dropdown-actions', $actions); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 152 which is incompatible with the return type of the parent method Encore\Admin\Grid\Displayers\Actions::display of type string.
Loading history...
153
    }
154
}