This repository has been archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBootstrapSelectAsset.php
64 lines (54 loc) · 1.81 KB
/
BootstrapSelectAsset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace cakebake\bootstrap\select;
use yii\web\AssetBundle;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
class BootstrapSelectAsset extends AssetBundle
{
public $sourcePath = '@vendor/bootstrap-select/bootstrap-select/dist';
public $css = [
'css/bootstrap-select.min.css',
];
public $js = [
'js/bootstrap-select.js',
];
public $depends = [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
];
/**
* @param View $view
* @param array $options
* @return static the registered asset bundle instance
* @see http://silviomoreto.github.io/bootstrap-select/3/#options
*/
public static function register($view, $options = [])
{
$o = ArrayHelper::merge([
'selector' => 'select',
'menuArrow' => true,
'tickIcon' => true,
'selectpickerOptions' => [
'style' => 'btn-default form-control',
],
], $options);
if (!is_string($o['selector']) || empty($o['selector']))
return false;
$js = '';
if ($o['menuArrow']) {
$js .= '$("' . $o['selector'] . '").addClass("show-menu-arrow");' . PHP_EOL;
}
if ($o['tickIcon']) {
$js .= '$("' . $o['selector'] . '").addClass("show-tick");' . PHP_EOL;
}
//Enable Bootstrap-Select for $o['selector']
$js .= '$("' . $o['selector'] . '").selectpicker(' . Json::htmlEncode($o['selectpickerOptions']) . ');' . PHP_EOL;
//Update Bootstrap-Select by :reset click
$js .= '$(":reset").click(function(){
$(this).closest("form").trigger("reset");
$("' . $o['selector'] . '").selectpicker("refresh");
});';
parent::register($view);
$view->registerJs($js);
}
}