1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhToggleButton extends CInputWidget
14 {
15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 public $onChange;
35
36 37 38
39 public $width = 100;
40
41 42 43
44 public $height = 25;
45
46 47 48
49 public $animated = true;
50
51 52 53
54 public $transitionSpeed;
55
56 57 58
59 public $enabledLabel = 'ON';
60
61 62 63
64 public $disabledLabel = 'OFF';
65
66 67 68 69
70 public $enabledStyle = 'primary';
71
72 73 74 75
76 public $disabledStyle = null;
77
78 79 80 81 82 83 84 85 86 87 88 89
90 public $customEnabledStyle = array();
91
92 93 94 95 96 97 98 99 100 101 102 103
104 public $customDisabledStyle = array();
105
106 107 108
109 public $tagName = 'div';
110
111 112 113
114 public function init()
115 {
116 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
117 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
118 }
119
120 121 122
123 public function run()
124 {
125 $this->renderField();
126 $this->registerClientScript();
127 }
128
129 130 131
132 public function renderField()
133 {
134 list($name, $id) = $this->resolveNameID();
135
136 echo CHtml::openTag($this->tagName, array('id' => 'wrapper-' . $id));
137
138 if ($this->hasModel()) {
139 echo CHtml::activeCheckBox($this->model, $this->attribute, $this->htmlOptions);
140 } else {
141 echo CHtml::checkBox($name, $this->value, $this->htmlOptions);
142 }
143
144 echo CHtml::closeTag($this->tagName);
145 }
146
147 148 149
150 protected function registerClientScript()
151 {
152
153 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
154 $assetsUrl = $this->getAssetsUrl($path);
155
156
157 $cs = Yii::app()->clientScript;
158
159 $cs->registerCoreScript('jquery');
160
161 $cs->registerCssFile($assetsUrl . '/css/bootstrap-toggle-buttons.css');
162 $cs->registerScriptFile($assetsUrl . '/js/jquery.toggle.buttons.js');
163
164
165 $selector = '#wrapper-' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
166
167 $this->getApi()->registerPlugin(
168 'toggleButtons',
169 $selector,
170 $this->getConfiguration(),
171 CClientScript::POS_READY
172 );
173
174 }
175
176 177 178
179 protected function getConfiguration()
180 {
181 if ($this->onChange !== null) {
182 if ((!$this->onChange instanceof CJavaScriptExpression) && strpos($this->onChange, 'js:') !== 0) {
183 $onChange = new CJavaScriptExpression($this->onChange);
184 } else {
185 $onChange = $this->onChange;
186 }
187 } else {
188 $onChange = 'js:$.noop';
189 }
190
191 $config = array(
192 'onChange' => $onChange,
193 'width' => $this->width,
194 'height' => $this->height,
195 'animated' => $this->animated,
196 'transitionSpeed' => $this->transitionSpeed,
197 'label' => array(
198 'enabled' => $this->enabledLabel,
199 'disabled' => $this->disabledLabel
200 ),
201 'style' => array()
202 );
203 if (!empty($this->enabledStyle)) {
204 $config['style']['enabled'] = $this->enabledStyle;
205 }
206 if (!empty($this->disabledStyle)) {
207 $config['style']['disabled'] = $this->disabledStyle;
208 }
209 if (!empty($this->customEnabledStyle)) {
210 $config['style']['custom'] = array('enabled' => $this->customEnabledStyle);
211 }
212 if (!empty($this->customDisabledStyle)) {
213 if (isset($config['style']['custom'])) {
214 $config['style']['custom']['disabled'] = $this->customDisabledStyle;
215 } else {
216 $config['style']['custom'] = array('disabled' => $this->customDisabledStyle);
217 }
218 }
219 foreach ($config as $key => $element) {
220 if (empty($element)) {
221 unset($config[$key]);
222 }
223 }
224 return $config;
225 }
226 }
227