Pjax   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 9
dl 0
loc 81
ccs 0
cts 71
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A run() 0 11 3
A registerClientScript() 0 29 3
A addBreadcrumbs() 0 28 4
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use Yii;
14
use yii\helpers\Html;
15
use yii\helpers\Json;
16
use yii\web\JsExpression;
17
use yii\widgets\Breadcrumbs;
18
19
class Pjax extends \yii\widgets\Pjax
20
{
21
    public function init()
22
    {
23
        parent::init();
24
        if (!isset($this->clientOptions['skipOuterContainers'])) {
25
            $this->clientOptions['skipOuterContainers'] = true;
26
        }
27
    }
28
29
    public function run()
30
    {
31
        if ($this->requiresPjax()) {
32
            Alert::widget();
33
            /// We do render breadcrumbs only for the main outer PJAX block
34
            if ($this->id === Yii::$app->params['pjax']['id']) {
35
                $this->addBreadcrumbs();
36
            }
37
        }
38
        parent::run();
39
    }
40
41
    public function registerClientScript()
42
    {
43
        $id = $this->options['id'];
44
        $this->clientOptions['push'] = $this->enablePushState;
45
        $this->clientOptions['replace'] = $this->enableReplaceState;
46
        $this->clientOptions['timeout'] = $this->timeout;
47
        $this->clientOptions['scrollTo'] = $this->scrollTo;
48
        $options = Json::encode($this->clientOptions);
49
        $linkSelector = Json::encode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
50
        $formSelector = Json::encode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
51
        $view = $this->getView();
52
        PjaxAsset::register($view);
53
        $js = "jQuery(document).pjax($linkSelector, \"#$id\", $options);";
54
        $js .= "\njQuery(document).on('submit', $formSelector, function (event) {
55
            var options = $options;
56
57
            var \$form = $(event.target);
58
            if (\$form.data('pjaxPush') !== undefined) {
59
                options.push = \$form.data('pjaxPush')
60
            }
61
            jQuery.pjax.submit(event, '#$id', options);
62
        });";
63
        $js .= "\njQuery(document).on('pjax:beforeReplace', function () {
64
            $('.modal-backdrop').remove();
65
            /// Dirty crutch for https://github.com/twbs/bootstrap/issues/16320
66
        });";
67
        $view->registerJs($js);
68
        $view->registerJs('$.pjax.defaults.timeout = 0;');
69
    }
70
71
    public function addBreadcrumbs()
72
    {
73
        $view = Yii::$app->getView();
74
75
        // No need to add breadcrumbs, if they are completely empty
76
        if (!isset($view->params['breadcrumbs'])) {
77
            return null;
78
        }
79
80
        $header      = Html::tag('h1', $view->title . ($view->params['subtitle'] ? Html::tag('small', $view->params['subtitle']) : ''));
81
        $breadcrumbs = Breadcrumbs::widget([
82
            'homeLink'     => [
83
                'label' => '<i class="fa fa-dashboard"></i> ' . Yii::t('hipanel', 'Home'),
84
                'url'   => '/',
85
            ],
86
            'encodeLabels' => false,
87
            'tag'          => 'ol',
88
            'links'        => isset($view->params['breadcrumbs']) ? $view->params['breadcrumbs'] : [],
89
        ]);
90
        $content = Json::htmlEncode(Html::tag('section', $header . $breadcrumbs, ['class' => 'content-header']));
91
        Yii::$app->getView()->registerJs(new JsExpression(<<< JS
92
            $('.content-header li a').on('click', function (event) {
93
                $.pjax.click(event, {container: '#{$this->id}'});
94
            });
95
            $('.content-header').replaceWith($content);
96
JS
97
        ), \yii\web\View::POS_READY);
98
    }
99
}
100