Completed
Push — master ( ce3da7...ddbc6d )
by Song
02:21
created

ColumnSelector::ignore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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 ColumnSelector extends AbstractTool
10
{
11
    const SELECT_COLUMN_NAME = '_columns_';
12
13
    /**
14
     * @var Grid
15
     */
16
    protected $grid;
17
18
    /**
19
     * @var array
20
     */
21
    protected static $ignoredColumns = [
22
        Grid\Column::SELECT_COLUMN_NAME,
23
        Grid\Column::ACTION_COLUMN_NAME
24
    ];
25
26
    /**
27
     * Create a new Export button instance.
28
     *
29
     * @param Grid $grid
30
     */
31
    public function __construct(Grid $grid)
32
    {
33
        $this->grid = $grid;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @return string
40
     */
41
    public function render()
42
    {
43
        if (!$this->grid->showColumnSelector()) {
44
            return '';
45
        }
46
47
        $show = $this->grid->visibleColumnNames();
48
49
        $lists = $this->getGridColumns()->map(function ($label, $key) use ($show) {
50
            if (empty($show)) {
51
                $checked = 'checked';
52
            } else {
53
                $checked = in_array($key, $show) ? 'checked' : '';
54
            }
55
56
            return <<<HTML
57
<li class="checkbox icheck" style="margin: 0;">
58
    <label style="width: 100%;padding: 3px;">
59
        <input type="checkbox" class="column-select-item" value="{$key}" {$checked}/>&nbsp;&nbsp;&nbsp;{$label}
60
    </label>
61
</li>
62
HTML;
63
        })->implode("\r\n");
64
65
        $btns = [
66
            'all'    => __('admin.all'),
67
            'submit' => __('admin.submit'),
68
        ];
69
70
        $this->setupScript();
71
72
        return <<<EOT
73
74
<div class="dropdown pull-right column-selector" style="margin-right: 10px">
75
    <button type="button" class="btn btn-sm btn-instagram dropdown-toggle" data-toggle="dropdown">
76
        <i class="fa fa-table"></i>
77
        &nbsp;
78
        <span class="caret"></span>
79
    </button>
80
    <ul class="dropdown-menu" role="menu" style="padding: 10px;">
81
        <li>
82
            <ul style='padding: 0;'>
83
                {$lists}
84
            </ul>
85
        </li>
86
        <li class="divider"></li>
87
        <li class="text-right">
88
            <button class="btn btn-sm btn-default column-select-all">{$btns['all']}</button>&nbsp;&nbsp;
89
            <button class="btn btn-sm btn-primary column-select-submit">{$btns['submit']}</button>
90
        </li>
91
    </ul>
92
</div>
93
EOT;
94
    }
95
96
    /**
97
     * @return Collection
98
     */
99
    protected function getGridColumns()
100
    {
101
        return $this->grid->columns()->map(function (Grid\Column $column) {
102
            $name = $column->getName();
103
104
            if ($this->isColumnIgnored($name)) {
105
                return;
106
            }
107
108
            return [$name => $column->getLabel()];
109
        })->filter()->collapse();
110
    }
111
112
    /**
113
     * Is column ignored in column selector.
114
     *
115
     * @param string $name
116
     * @return bool
117
     */
118
    protected function isColumnIgnored($name)
119
    {
120
        return in_array($name, static::$ignoredColumns);
121
    }
122
123
    /**
124
     * Ignore a column to display in column selector.
125
     *
126
     * @param string|array $name
127
     */
128
    public static function ignore($name)
129
    {
130
        static::$ignoredColumns = array_merge(static::$ignoredColumns, (array) $name);
131
    }
132
133
    /**
134
     * Setup script.
135
     */
136
    protected function setupScript()
137
    {
138
        $defaults = json_encode($this->grid->getDefaultVisibleColumnNames());
139
140
        $script = <<<SCRIPT
141
142
$('.column-select-submit').on('click', function () {
143
    
144
    var defaults = $defaults;
145
    var selected = [];
146
    
147
    $('.column-select-item:checked').each(function () {
148
        selected.push($(this).val());
149
    });
150
151
    if (selected.length == 0) {
152
        return;
153
    }
154
155
    var url = new URL(location);
156
    
157
    if (selected.sort().toString() == defaults.sort().toString()) {
158
        url.searchParams.delete('_columns_');
159
    } else {
160
        url.searchParams.set('_columns_', selected.join());
161
    }
162
163
    $.pjax({container:'#pjax-container', url: url.toString()});
164
});
165
166
$('.column-select-all').on('click', function () {
167
    $('.column-select-item').iCheck('check');
168
    return false;
169
});
170
171
$('.column-select-item').iCheck({
172
    checkboxClass:'icheckbox_minimal-blue'
173
});
174
175
SCRIPT;
176
177
        Admin::script($script);
178
    }
179
}
180