Completed
Push — master ( 26a43a...8af7c3 )
by Song
02:21
created

HasHotKeys::addHotKeyScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
use Encore\Admin\Admin;
6
7
trait HasHotKeys
8
{
9
    protected function addHotKeyScript()
10
    {
11
        $filterID = $this->getFilter()->getFilterID();
0 ignored issues
show
Bug introduced by
It seems like getFilter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
12
13
        $refreshMessage = __('admin.refresh_succeeded');
14
15
        $script = <<<SCRIPT
16
17
$(document).off('keydown').keydown(function(e) {
18
    var tag = e.target.tagName.toLowerCase();
19
    
20
    if (tag == 'input' || tag == 'textarea') {
21
        return;
22
    }
23
24
    var \$box = $("#{$this->tableID}").closest('.box');
0 ignored issues
show
Bug introduced by
The property tableID does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    var \$current_page = \$box.find('.pagination .page-item.active');
26
27
    switch(e.which) {
28
        case 82: // `r` for reload
29
            $.admin.reload();
30
            $.admin.toastr.success('{$refreshMessage}', '', {positionClass:"toast-top-center"});
31
            break;
32
        case 83: // `s` for search
33
            \$box.find('input.grid-quick-search').trigger('focus');
34
            break; 
35
        case 70: // `f` for open filter
36
            \$box.find('#{$filterID}').toggleClass('hide');
37
            break;
38
        case 67: // `c` go to create page 
39
            \$box.find('.grid-create-btn>a').trigger('click');
40
            break; 
41
        case 37: // `left` for go to prev page
42
            \$current_page.prev().find('a').trigger('click');
43
            break;
44
        case 39: // `right` for go to next page
45
            \$current_page.next().find('a').trigger('click');
46
            break;
47
        default: return;
48
    }
49
    e.preventDefault();
50
});
51
52
SCRIPT;
53
54
        Admin::script($script);
55
    }
56
57
    public function enableHotKeys()
58
    {
59
        $this->addHotKeyScript();
60
61
        return $this;
62
    }
63
}