Completed
Push — master ( 8bf0fe...b37564 )
by Song
05:18 queued 03:01
created

FixColumns::apply()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid;
7
use Illuminate\Support\Collection;
8
9
class FixColumns
10
{
11
    /**
12
     * @var Grid
13
     */
14
    protected $grid;
15
16
    /**
17
     * @var integer
18
     */
19
    protected $head;
20
21
    /**
22
     * @var integer
23
     */
24
    protected $tail;
25
26
    /**
27
     * @var Collection
28
     */
29
    protected $left;
30
31
    /**
32
     * @var Collection
33
     */
34
    protected $right;
35
36
    /**
37
     * @var string
38
     */
39
    protected $view = 'admin::grid.fixed-table';
40
41
    /**
42
     * FixColumns constructor.
43
     *
44
     * @param Grid $grid
45
     * @param integer $head
46
     * @param integer $tail
47
     */
48
    public function __construct(Grid $grid, $head, $tail = -1)
49
    {
50
        $this->grid = $grid;
51
        $this->head = $head;
52
        $this->tail = $tail;
53
54
        $this->left = Collection::make();
55
        $this->right = Collection::make();
56
    }
57
58
    /**
59
     * @return Collection
60
     */
61
    public function leftColumns()
62
    {
63
        return $this->left;
64
    }
65
66
    /**
67
     * @return Collection
68
     */
69
    public function rightColumns()
70
    {
71
        return $this->right;
72
    }
73
74
    /**
75
     * @return \Closure
76
     */
77
    public function apply()
78
    {
79
        $this->grid->setView($this->view);
80
81
        return function (Grid $grid) {
82
            if ($this->head > 0) {
83
                $this->left = $grid->visibleColumns()->slice(0, $this->head);
0 ignored issues
show
Bug introduced by
The method slice does only exist in Illuminate\Support\Collection, but not in Encore\Admin\Grid\Concerns\CanHidesColumns.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            }
85
86
            if ($this->tail < 0) {
87
                $this->right = $grid->visibleColumns()->slice($this->tail);
88
            }
89
90
            $this->addStyle()->addScript();
91
        };
92
    }
93
94
    /**
95
     * @return $this
96
     */
97
    protected function addScript()
98
    {
99
        $rowName = $this->grid->getGridRowName();
100
101
        $script = <<<SCRIPT
102
103
(function () {
104
    var theadHeight = $('.table-main thead tr').outerHeight();
105
    $('.table-fixed thead tr').outerHeight(theadHeight);
106
    
107
    var tfootHeight = $('.table-main tfoot tr').outerHeight();
108
    $('.table-fixed tfoot tr').outerHeight(tfootHeight);
109
    
110
        $('.table-main tbody tr').each(function(i, obj) {
111
            var height = $(obj).outerHeight();
112
    
113
            $('.table-fixed-left tbody tr').eq(i).outerHeight(height);
114
            $('.table-fixed-right tbody tr').eq(i).outerHeight(height);
115
        });
116
    
117
    if ($('.table-main').width() >= $('.table-main').prop('scrollWidth')) {
118
        $('.table-fixed').hide();
119
    }
120
    
121
    $('.table-wrap tbody tr').on('mouseover', function () {
122
        var index = $(this).index();
123
        
124
        $('.table-main tbody tr').eq(index).addClass('active');
125
        $('.table-fixed-left tbody tr').eq(index).addClass('active');
126
        $('.table-fixed-right tbody tr').eq(index).addClass('active');
127
    });
128
    
129
    $('.table-wrap tbody tr').on('mouseout', function () {
130
        var index = $(this).index();
131
        
132
        $('.table-main tbody tr').eq(index).removeClass('active');
133
        $('.table-fixed-left tbody tr').eq(index).removeClass('active');
134
        $('.table-fixed-right tbody tr').eq(index).removeClass('active');
135
    });
136
    
137
    $('.{$rowName}-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
138
    
139
        var id = $(this).data('id');
140
        var index = $(this).closest('tr').index();
141
    
142
        if (this.checked) {
143
            \$.admin.grid.select(id);
144
            $('.table-main tbody tr').eq(index).css('background-color', '#ffffd5');
145
            $('.table-fixed-left tbody tr').eq(index).css('background-color', '#ffffd5');
146
            $('.table-fixed-right tbody tr').eq(index).css('background-color', '#ffffd5');
147
        } else {
148
            \$.admin.grid.unselect(id);
149
            $('.table-main tbody tr').eq(index).css('background-color', '');
150
            $('.table-fixed-left tbody tr').eq(index).css('background-color', '');
151
            $('.table-fixed-right tbody tr').eq(index).css('background-color', '');
152
        }
153
    });
154
})();
155
156
SCRIPT;
157
158
        Admin::script($script);
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return $this
165
     */
166
    protected function addStyle()
167
    {
168
        $style = <<<STYLE
169
.tables-container {
170
    position:relative;
171
}
172
173
.tables-container table {
174
    margin-bottom: 0px !important;
175
}
176
177
.tables-container table th, .tables-container table td {
178
    white-space:nowrap;
179
}
180
181
.table-wrap table tr .active {
182
    background': #f5f5f5;
183
}
184
185
.table-main {
186
    overflow-x: auto;
187
    width: 100%;
188
}
189
190
.table-fixed {
191
    position:absolute;
192
	top: 0px;
193
	background:#ffffff;
194
	z-index:10;
195
}
196
197
.table-fixed-left {
198
	left:0;
199
	box-shadow: 7px 0 5px -5px rgba(0,0,0,.12);
200
}
201
202
.table-fixed-right {
203
	right:0;
204
	box-shadow: -5px 0 5px -5px rgba(0,0,0,.12);
205
}
206
STYLE;
207
208
        Admin::style($style);
209
210
        return $this;
211
    }
212
}