Completed
Push — master ( 403071...badb46 )
by Klochok
13:49
created

OwlCarousel::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace hiqdev\themes\obaju\widgets;
4
5
use hiqdev\themes\obaju\OwlCarouselAsset;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
use yii\web\JsExpression;
9
use yii\web\View;
10
11
class OwlCarousel extends Widget
12
{
13
    /**
14
     * Items (div, a, img, span, li etc.)
15
     * @var array
16
     */
17
    public $items = [];
18
19
    /**
20
     * Container element (div, ul etc.)
21
     * @var string
22
     */
23
    public $containerTag = 'div';
24
25
    /**
26
     * Call the plugin function and your carousel is ready.
27
     * @var string
28
     */
29
    public $pluginInit = '$(".owl-carousel").owlCarousel();';
30
31
    /**
32
     * Only the class owl-carousel is mandatory to apply proper styles
33
     * NOTE: The owl-theme class is optional, but without it, you will need to style navigation features on your own.
34
     * @var array
35
     */
36
    protected $defaultContainerOptions = ['class' => 'owl-carousel owl-theme'];
37
38
    public $containerOptions = [];
39
40
    public function run()
41
    {
42
        $this->initCarouselJs();
43
        $out = Html::beginTag($this->containerTag, array_merge($this->containerOptions, $this->defaultContainerOptions));
44
        foreach ($this->items as $item) {
45
            $out .= $item;
46
        }
47
        $out .= Html::endTag($this->containerTag);
48
49
        return $out;
50
    }
51
52
    public function initCarouselJs()
53
    {
54
        OwlCarouselAsset::register($this->view);
55
        $this->view->registerJs(new JsExpression($this->pluginInit), View::POS_READY);
56
    }
57
}
58