-
Notifications
You must be signed in to change notification settings - Fork 9
/
Toggle.php
108 lines (92 loc) · 2.93 KB
/
Toggle.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* @link https://github.com/loveorigami/yii2-bootstrap-toggle
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace lo\widgets;
use yii\helpers\Html;
use yii\widgets\InputWidget;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* Toogle renders a checkbox type toggle switch control. For example:
*
* ```
* <?= \lo\widgets\Toggle::widget([
* 'name' => 'Test',
* 'options' => [
* 'data-size' => 'large',
* ]
* ]);?>
* ```
*
* @author Andrey Lukyanov <[email protected]>
* @link http://www.loveorigami.info
* @package lo\widgets\Toogle
*/
class Toggle extends InputWidget
{
/**
* @var bool specifies whether the checkbox should be checked or unchecked, when not used with a model. If [[items]],
* [[$checked]] will specify the value to select.
*/
public $checked = false;
/**
* @var array the options for the Bootstrap Toogle plugin.
* Please refer to the Bootstrap Toogle plugin Web page for possible options.
* @see http://www.bootstraptoggle.com
*/
public $options = [];
/**
* @var array the default options for the widget.
*/
protected $woptions = [
'data-toggle' => 'toggle',
'data-onstyle' => 'success',
'data-offstyle' => 'danger',
'label' => false,
];
/**
* @var array the event handlers for the underlying Bootstrap Toggle input JS plugin.
* Please refer to the [Bootstrap Toggle](http://www.bootstraptoggle.com/#events) plugin
* Web page for possible events.
*/
public $clientEvents = [];
/**
* @var string the DOM element selector
*/
protected $selector;
/**
* @var bool whether to display the label inline or not. Defaults to true.
*/
public $inlineLabel = true;
/**
* Registers Bootstrap Switch plugin and related events
*/
public function registerClientScript()
{
$view = $this->view;
ToggleAsset::register($view);
//$this->clientOptions['animate'] = ArrayHelper::getValue($this->clientOptions, 'animate', true);
$options = Json::encode($this->options);
$js[] = "jQuery('$this->selector').bootstrapToggle($options);";
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('$this->selector').on('$event', $handler);";
}
}
$view->registerJs(implode("\n", $js));
}
public function run()
{
$this->options = ArrayHelper::merge($this->woptions, $this->options);
if ($this->hasModel()) {
$input = Html::activeCheckbox($this->model, $this->attribute, $this->options);
} else {
$input = Html::checkbox($this->name, $this->checked, $this->options);
}
echo $this->inlineLabel ? $input : Html::tag('div', $input);
$this->selector = "#{$this->options['id']}";
$this->registerClientScript();
}
}