Completed
Push — master ( 5cbd8c...10b8c5 )
by Song
02:23
created

DropdownActions::disableView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 10
loc 10
rs 9.9332
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
     * Add JS script into pages.
30
     *
31
     * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." 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...
32
     */
33
    protected function addScript()
34
    {
35
        $script = <<<'SCRIPT'
36
(function ($) {
37
    $('.table-responsive').on('show.bs.dropdown', function () {
38
         $('.table-responsive').css("overflow", "inherit" );
39
    });
40
    
41
    $('.table-responsive').on('hide.bs.dropdown', function () {
42
         $('.table-responsive').css("overflow", "auto");
43
    })
44
})(jQuery);
45
SCRIPT;
46
47
        Admin::script($script);
48
    }
49
50
    /**
51
     * @param RowAction $action
52
     *
53
     * @return $this
54
     */
55
    public function add(RowAction $action)
56
    {
57
        $this->prepareAction($action);
58
59
        array_push($this->custom, $action);
60
61
        return $this;
62
    }
63
64
    /**
65
     * Prepend default `edit` `view` `delete` actions.
66
     */
67
    protected function prependDefaultActions()
68
    {
69
        foreach ($this->defaultClass as $class) {
70
            /** @var RowAction $action */
71
            $action = new $class();
72
73
            $this->prepareAction($action);
74
75
            array_push($this->default, $action);
76
        }
77
    }
78
79
    /**
80
     * @param RowAction $action
81
     */
82
    protected function prepareAction(RowAction $action)
83
    {
84
        $action->setGrid($this->grid)
85
            ->setColumn($this->column)
86
            ->setRow($this->row);
87
    }
88
89
    /**
90
     * Disable view action.
91
     *
92
     * @param bool $disable
93
     *
94
     * @return $this
95
     */
96 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...
97
    {
98
        if ($disable) {
99
            array_delete($this->default, Show::class);
100
        } elseif (!in_array(Show::class, $this->default)) {
101
            array_push($this->default, Show::class);
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * Disable delete.
109
     *
110
     * @param bool $disable
111
     *
112
     * @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...
113
     */
114 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...
115
    {
116
        if ($disable) {
117
            array_delete($this->default, Delete::class);
118
        } elseif (!in_array(Delete::class, $this->default)) {
119
            array_push($this->default, Delete::class);
120
        }
121
122
        return $this;
123
    }
124
125
    /**
126
     * Disable edit.
127
     *
128
     * @param bool $disable
129
     *
130
     * @return $this
131
     */
132 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...
133
    {
134
        if ($disable) {
135
            array_delete($this->default, Edit::class);
136
        } elseif (!in_array(Edit::class, $this->default)) {
137
            array_push($this->default, Edit::class);
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param null|\Closure $callback
145
     *
146
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
147
     */
148
    public function display($callback = null)
149
    {
150
        $this->addScript();
151
152
        if ($callback instanceof \Closure) {
153
            $callback->call($this, $this);
154
        }
155
156
        $this->prependDefaultActions();
157
158
        $actions = [
159
            'default' => $this->default,
160
            'custom'  => $this->custom,
161
        ];
162
163
        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 163 which is incompatible with the return type of the parent method Encore\Admin\Grid\Displayers\Actions::display of type string.
Loading history...
164
    }
165
}
166