Skip to content

Commit

Permalink
v1.0.0 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
mhunesi committed Jul 2, 2020
1 parent 6139c82 commit c62c2c7
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 7 deletions.
93 changes: 93 additions & 0 deletions PerfectScrollbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace mhunesi\scrollbar;

use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;

/**
* This is just an example.
*/
class PerfectScrollbar extends \yii\base\Widget
{
/**
* @var int
*/
public $height = 250;

/**
* @var int
*/
public $width = 250;

/**
* @var string
*/
public $unit = 'px';

/**
* @var array the HTML attributes for the widget container tag. The following special options are recognized:
*
* - `tag`: string, the tag name for the container. Defaults to `div`
* This option is available since version 2.0.7.
* See also [[\yii\helpers\Html::tag()]].
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];

/**
* @var array the options for the underlying Select2 JS plugin.
* Please refer to the plugin Web page for possible options.
*
* @see https://github.com/mdbootstrap/perfect-scrollbar#options
*/
public $clientOptions = [];

public function init()
{
parent::init();
ob_start();

$style = array_merge([
'position' => 'relative',
'overflow' => 'auto',
'width' => $this->width . $this->unit,
'height' => $this->height . $this->unit,
],ArrayHelper::getValue($this->options,'style',[]));

$this->options = array_merge($this->options,[
'id' => $this->id,
'style' => $style
]);

$tag = ArrayHelper::remove($this->options, 'tag', 'div');
echo Html::beginTag($tag, $this->options);

}

public function run()
{
echo Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div'));

$content = ob_get_clean();

$this->registerClientScript();

return $content;
}

public function registerClientScript()
{
PerfectScrollbarAsset::register($this->view);

$id = $this->options['id'];

$jsonConfig = Json::encode($this->clientOptions);

$js = "var ps_{$id} = new PerfectScrollbar('#{$id}', $jsonConfig);";

$this->view->registerJs($js);
}
}
19 changes: 19 additions & 0 deletions PerfectScrollbarAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace mhunesi\scrollbar;

class PerfectScrollbarAsset extends \yii\web\AssetBundle
{
public $sourcePath = '@npm/perfect-scrollbar';

public $css = [
'css/perfect-scrollbar.css',
];

public $js = [
'dist/perfect-scrollbar.min.js',
];

public $jsOptions = array(
'position' => \yii\web\View::POS_HEAD
);
}
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
php composer.phar require --prefer-dist mhunesi/yii2-perfect-scrollbar "*"
composer require mhunesi/yii2-perfect-scrollbar "*"
```

or add
Expand All @@ -28,9 +28,19 @@ Usage
Once the extension is installed, simply use it in your code by :

```php
<?= \mhunesi\scrollbar\PerfectScrollbar::begin(); ?>```

content

```php
<?= \mhunesi\scrollbar\PerfectScrollbar::end(); ?>```
<?= \mhunesi\scrollbar\PerfectScrollbar::begin([
'width' => 200,
'height' => 200,
'options' => [
'class' => 'p-2 mb-5',
],
'clientOptions' => [
'wheelPropagation' => true
]
]) ?>

<p> HTML Content </p>

<?= \mhunesi\scrollbar\PerfectScrollbar::end() ?>

```
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "mhunesi/yii2-perfect-scrollbar",
"description": "Yii2 Perfect Scrollbar Extension",
"type": "yii2-extension",
"keywords": ["yii2","extension","perfect-scrollbar","scrollbar"],
"license": "MIT",
"authors": [
{
"name": "Mustafa Hayri ÜNEŞİ",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "~2.0.0",
"npm-asset/perfect-scrollbar": "^1.5.0"
},
"autoload": {
"psr-4": {
"mhunesi\\scrollbar\\": ""
}
}
}

0 comments on commit c62c2c7

Please sign in to comment.