FloatHeaderBehavior   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 55
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A registerClientScript() 0 23 4
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-grid-view-library project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\grid\behaviors;
11
12
use dosamigos\grid\bundles\FloatTableHeadAsset;
13
use dosamigos\grid\contracts\RegistersClientScriptInterface;
14
use yii\base\Behavior;
15
use yii\grid\GridView;
16
use yii\helpers\Json;
17
18
/**
19
 * Notes: If you use this behavior, add this meta tag into your header to placate IE:
20
 * <meta http-equiv="X-UA-Compatible" content="IE=10; IE=9; IE=8; IE=7; IE=EDGE" />
21
 */
22
class FloatHeaderBehavior extends Behavior implements RegistersClientScriptInterface
23
{
24
    /**
25
     * @var array $clientOptions the options for the underlying floatThead jquery plugin. The default offset is set to
26
     *            `50` assuming a fixed bootstrap navbar on top.
27
     *
28
     * @see http://mkoryak.github.io/floatThead#options
29
     */
30
    public $clientOptions = [
31
        'top' => 50
32
    ];
33
    /**
34
     * @var array $clientEvents the events of the underlying floatThead jquery plugin. For example, to set the
35
     *            floatThead event:
36
     *
37
     * ```
38
     * 'floatThead' => new JsExpression('function(e, isFloated, $floatContainer) {
39
     *      if(isFloated) {
40
     *          $floatContainer.addClass("floated");
41
     *      }
42
     *
43
     * }')
44
     * ```
45
     *
46
     * @see http://mkoryak.github.io/floatThead/#options
47
     */
48
    public $clientEvents = [];
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function registerClientScript()
54
    {
55
        /** @var GridView $owner */
56
        $owner = $this->owner;
57
58
        $view = $owner->getView();
59
60
        FloatTableHeadAsset::register($view);
61
62
        $options = !empty($this->clientOptions)
63
            ? Json::encode($this->clientOptions)
64
            : '';
65
66
        $js = [];
67
        $id = $owner->getId();
68
        $js[] = ";jQuery('#$id > table.da-grid-view-table').floatThead($options);";
69
        if (!empty($this->clientEvents)) {
70
            foreach ($this->clientEvents as $event => $handler) {
71
                $js[] = "jQuery('#$id > table.da-grid-view-table').on('$event', $handler);";
72
            }
73
        }
74
        $view->registerJs(implode("\n", $js));
75
    }
76
}
77