Dominus77 /
yii2-light-slider-widget
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace dominus77\lightslider; |
||
| 4 | |||
| 5 | use yii\base\Widget; |
||
| 6 | use yii\web\JsExpression; |
||
| 7 | use yii\helpers\Html; |
||
| 8 | use yii\helpers\ArrayHelper; |
||
| 9 | use dominus77\lightslider\assets\SliderAsset; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class Slider |
||
| 13 | * Renders a JQuery LightSlider widget for Yii2. |
||
| 14 | * @package dominus77\lightslider |
||
| 15 | * @see http://sachinchoolur.github.io/lightslider/ |
||
| 16 | * |
||
| 17 | * <?= Slider::widget([ |
||
| 18 | * 'items' => ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4', 'Slide 5', 'Slide 6', 'Slide 7', '...'], |
||
| 19 | * 'clientOptions' => [ |
||
| 20 | * 'item' => 3, |
||
| 21 | * 'autoWidth' => false, |
||
| 22 | * 'slideMove' => 1, // slidemove will be 1 if loop is true |
||
| 23 | * 'slideMargin' => 10, |
||
| 24 | * //... |
||
| 25 | * ], |
||
| 26 | * ]); ?> |
||
| 27 | */ |
||
| 28 | class Slider extends Widget |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string|integer |
||
| 32 | * 'id' => 'myId', |
||
| 33 | */ |
||
| 34 | public $id = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Items |
||
| 38 | * @var array |
||
| 39 | * |
||
| 40 | * [ |
||
| 41 | * '<h1>Slide 1</h1><p>Text 1</p>', |
||
| 42 | * '<h1>Slide 2</h1><p>Text 2</p>', |
||
| 43 | * '...', |
||
| 44 | * ] |
||
| 45 | */ |
||
| 46 | public $items = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | * @see http://sachinchoolur.github.io/lightslider/ |
||
| 51 | */ |
||
| 52 | public $clientOptions = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * List Options |
||
| 56 | * @var array |
||
| 57 | * 'listOptions' => ['class' => 'myListCssClass'] |
||
| 58 | */ |
||
| 59 | public $listOptions = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Items options |
||
| 63 | * @var array |
||
| 64 | * 'itemOptions' => ['class' => 'myItemsCssClass'] |
||
| 65 | */ |
||
| 66 | public $itemOptions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var integer|string |
||
| 70 | */ |
||
| 71 | private $_id; |
||
|
0 ignored issues
–
show
Comprehensibility
introduced
by
Loading history...
|
|||
| 72 | |||
| 73 | /** |
||
| 74 | * Initializes the widget. |
||
| 75 | */ |
||
| 76 | 2 | public function init() |
|
| 77 | { |
||
| 78 | 2 | parent::init(); |
|
| 79 | 2 | $this->_id = $this->id ? $this->id : $this->getId(); |
|
| 80 | 2 | $this->listOptions['id'] = $this->_id; |
|
| 81 | 2 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Renders widget |
||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | 1 | public function run() |
|
| 88 | { |
||
| 89 | 1 | if ($this->items) { |
|
|
0 ignored issues
–
show
The expression
$this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 90 | 1 | $this->registerAssets(); |
|
| 91 | 1 | echo Html::beginTag('ul', $this->listOptions) . PHP_EOL; |
|
| 92 | 1 | foreach ($this->items as $key => $item) { |
|
| 93 | 1 | echo Html::tag('li', $item, $this->itemOptions) . PHP_EOL; |
|
| 94 | } |
||
| 95 | 1 | echo Html::endTag('ul') . PHP_EOL; |
|
| 96 | } |
||
| 97 | 1 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Set client options |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 2 | public function getOptions() |
|
| 104 | { |
||
| 105 | 2 | $options = []; |
|
| 106 | 2 | $options = ArrayHelper::merge($options, $this->clientOptions); |
|
| 107 | 2 | return json_encode($options); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Register resource |
||
| 112 | */ |
||
| 113 | 1 | public function registerAssets() |
|
| 114 | { |
||
| 115 | 1 | $options = $this->getOptions(); |
|
| 116 | 1 | $view = $this->getView(); |
|
| 117 | 1 | SliderAsset::register($view); |
|
| 118 | 1 | $script = new JsExpression(" |
|
| 119 | 1 | $('#{$this->_id}').lightSlider({$options}); |
|
| 120 | "); |
||
| 121 | 1 | $view->registerJs($script, $view::POS_READY); |
|
| 122 | 1 | } |
|
| 123 | } |
||
| 124 |