YiiWheels
  • Package
  • Class
  • Tree

Packages

  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • editable
      • fileupload
      • fileuploader
      • formhelpers
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhToggleAction
  • WhToggleButton
  • WhToggleColumn
  1 <?php
  2 /**
  3  * WhToggleButton widget class
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @copyright Copyright &copy; 2amigos.us 2013-
  7  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  8  * @package YiiWheels.widgets.toggle
  9  * @uses YiiStrap.helpers.TbArray
 10  */
 11 Yii::import('bootstrap.helpers.TbArray');
 12 
 13 class WhToggleButton extends CInputWidget
 14 {
 15 
 16     /**
 17      * @var string the javascript function
 18      *
 19      * The function signature is <code>function($el, status, e)</code>
 20      * <ul>
 21      * <li><code>$el</code> the toggle element changed. </li>
 22      * <li><code>status</code> the status of the element (true=on | false=off) </li>
 23      * <li><code>e</code> the event object </li>
 24      * </ul>
 25      *
 26      * Example:
 27      * <pre>
 28      *  array(
 29      *     class'=>'WhToggleColumn',
 30      *     'onChange'=>'js:function($el, status, e){ console.log($el, status, e); }',
 31      *  ),
 32      * </pre>
 33      */
 34     public $onChange;
 35 
 36     /**
 37      * @var int the width of the toggle button
 38      */
 39     public $width = 100;
 40 
 41     /**
 42      * @var int the height of the toggle button
 43      */
 44     public $height = 25;
 45 
 46     /**
 47      * @var bool whether to use animation or not
 48      */
 49     public $animated = true;
 50 
 51     /**
 52      * @var mixed the transition speed (toggle movement)
 53      */
 54     public $transitionSpeed; //accepted values: float or percent [1, 0.5, '150%']
 55 
 56     /**
 57      * @var string the label to display on the enabled side
 58      */
 59     public $enabledLabel = 'ON';
 60 
 61     /**
 62      * @var string the label to display on the disabled side
 63      */
 64     public $disabledLabel = 'OFF';
 65 
 66     /**
 67      * @var string the style of the toggle button enable style
 68      * Accepted values ["primary", "danger", "info", "success", "warning"] or nothing
 69      */
 70     public $enabledStyle = 'primary';
 71 
 72     /**
 73      * @var string the style of the toggle button disabled style
 74      * Accepted values ["primary", "danger", "info", "success", "warning"] or nothing
 75      */
 76     public $disabledStyle = null;
 77 
 78     /**
 79      * @var array a custom style for the enabled option. Format
 80      * <pre>
 81      *  ...
 82      *  'customEnabledStyle'=>array(
 83      *      'background'=>'#FF00FF',
 84      *      'gradient'=>'#D300D3',
 85      *      'color'=>'#FFFFFF'
 86      *  ),
 87      *  ...
 88      * </pre>
 89      */
 90     public $customEnabledStyle = array();
 91 
 92     /**
 93      * @var array a custom style for the disabled option. Format
 94      * <pre>
 95      *  ...
 96      *  'customDisabledStyle'=>array(
 97      *      'background'=>'#FF00FF',
 98      *      'gradient'=>'#D300D3',
 99      *      'color'=>'#FFFFFF'
100      *  ),
101      *  ...
102      * </pre>
103      */
104     public $customDisabledStyle = array();
105 
106     /**
107      * @var string the tag name. Defaults to 'div'.
108      */
109     public $tagName = 'div';
110 
111     /**
112      * Widget's initialization method
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      * Widget's run function
122      */
123     public function run()
124     {
125         $this->renderField();
126         $this->registerClientScript();
127     }
128 
129     /**
130      * Renders the input field
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      * Registers client scripts
149      */
150     protected function registerClientScript()
151     {
152 
153         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
154         $assetsUrl = $this->getAssetsUrl($path);
155 
156         /* @var $cs CClientScript */
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         /* initialize plugin */
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      * @return array the configuration of the plugin
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 
YiiWheels API documentation generated by ApiGen 2.8.0