forked from kartik-v/yii2-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchInput.php
150 lines (134 loc) · 4.71 KB
/
SwitchInput.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2013
* @package yii2-widgets
* @version 3.2.0
*/
namespace kartik\widgets;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
/**
* Switch widget is a Yii2 wrapper for the Bootstrap Switch plugin by Mattia, Peter, & Emanuele.
* This input widget is a jQuery based replacement for checkboxes and radio buttons and converts
* them to toggle switches.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
* @see http:https://www.bootstrap-switch.org/
*/
class SwitchInput extends InputWidget
{
const CHECKBOX = 1;
const RADIO = 2;
/**
* @var integer the input type - one
* of the constants above.
*/
public $type = self::CHECKBOX;
/**
* @var array the list of items for radio input
* (applicable only if `type` = 2). The following
* keys could be setup:
* - label: string the label of each radio item. If this is
* set to false or null, the label will not be displayed.
* - value: string the value of each radio item
* - options: HTML attributes for the radio item
* - labelOptions: HTML attributes for each radio item label
*/
public $items = [];
/**
* @var boolean whether label is aligned on same line. Defaults to true.
* If set to false, the label and input will be on separate lines.
*/
public $inlineLabel = true;
/**
* @var array default HTML attributes for each radio item
* (applicable only if `type` = 2)
*/
public $itemOptions = [];
/**
* @var array default HTML attributes for each radio item label
*/
public $labelOptions = [];
/**
* @var string the separator content between each radio item
* (applicable only if `type` = 2)
*/
public $separator = " ";
/**
* @var array HTML attributes for the radio group container
* (applicable only if `type` = 2)
*/
public $containerOptions = [];
/**
* Initializes the widget
*
* @throw InvalidConfigException
*/
public function init()
{
parent::init();
if (empty($this->type) && $this->type !== self::CHECKBOX && $this->type !== self::RADIO) {
throw new InvalidConfigException("You must define a valid 'type' which must be either 1 (for checkbox) or 2 (for radio).");
}
if ($this->type == self::RADIO) {
if (empty($this->items) || !is_array($this->items)) {
throw new InvalidConfigException("You must setup the 'items' array for the 'radio' type.");
}
}
$this->registerAssets();
echo $this->renderInput();
}
/**
* Renders the source Input for the Switch plugin.
* Graceful fallback to a normal HTML checkbox or radio input
* in case JQuery is not supported by the browser
*/
protected function renderInput()
{
if ($this->type == self::CHECKBOX) {
if (empty($this->options['label'])) {
$this->options['label'] = null;
}
$input = $this->getInput('checkbox');
return ($this->inlineLabel) ? $input : Html::tag('div', $input);
}
$output = '';
foreach ($this->items as $item) {
if (!is_array($item)) {
continue;
}
$label = ArrayHelper::getValue($item, 'label', false);
$options = ArrayHelper::merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
$labelOptions = ArrayHelper::merge($this->labelOptions, ArrayHelper::getValue($item, 'labelOptions', []));
$value = ArrayHelper::getValue($item, 'value', null);
$options['value'] = $value;
$input = Html::radio($this->name, false, $options);
$output .= Html::label($label, $this->name, $labelOptions) . "\n" .
(($this->inlineLabel) ? $input : Html::tag('div', $input)) . "\n" .
$this->separator;
}
if (empty($this->containerOptions['class'])) {
$this->containerOptions['class'] = 'form-group';
}
return Html::tag('div', $output, $this->containerOptions) . "\n";
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
SwitchInputAsset::register($view);
if (!isset($this->pluginOptions['animate'])) {
$this->pluginOptions['animate'] = true;
}
if ($this->type == self::RADIO) {
$this->registerPlugin('bootstrapSwitch', 'jQuery("[name = \'' . $this->name . '\']")');
} else {
$this->registerPlugin('bootstrapSwitch');
}
}
}