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

  • WhSwitch
  1 <?php
  2 /**
  3  * WhSwitch widget class
  4  *
  5  * @see https://github.com/nostalgiaz/bootstrap-switch
  6  *
  7  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  8  * @copyright Copyright &copy; 2amigos.us 2013-
  9  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 10  * @package YiiWheels.widgets.switch
 11  * @uses YiiStrap.helpers.TbArray
 12  * @uses YiiStrap.helpers.TbHtml
 13  */
 14 Yii::import('bootstrap.helpers.TbArray');
 15 Yii::import('bootstrap.helpers.TbHtml');
 16 
 17 class WhSwitch extends CInputWidget
 18 {
 19     /**
 20      * @var string input type. It can be 'radio' or 'checkbox'
 21      */
 22     public $inputType = 'checkbox';
 23 
 24     /**
 25      * @var string size of switch button. Supports 'large', 'small' or 'mini'
 26      */
 27     public $size = 'small';
 28 
 29     /**
 30      * @var string color when is on. It can be any of bootstrap button states. Defaults to 'primary'.
 31      */
 32     public $onColor = 'primary';
 33 
 34     /**
 35      * @var string color when is off. It can be any of bootstrap button states. Defaults to 'warning'.
 36      */
 37     public $offColor = 'warning';
 38 
 39     /**
 40      * @var bool whether the slide is animated or not.
 41      */
 42     public $animated = true;
 43 
 44     /**
 45      * @var string the label when is on. Defaults to 'On'.
 46      */
 47     public $onLabel;
 48 
 49     /**
 50      * @var string the label when is off. Defaults to 'Off'.
 51      */
 52     public $offLabel;
 53 
 54     /**
 55      * @var string the text label. Defaults to null.
 56      */
 57     public $textLabel;
 58 
 59     /**
 60      * @var string[] the JavaScript event handlers.
 61      */
 62     public $events = array();
 63 
 64     /**
 65      * @var bool whether to display minified versions of the files or not
 66      */
 67     public $debugMode = false;
 68 
 69     /**
 70      * @var array the switch plugin options.
 71      */
 72     protected $pluginOptions = array();
 73 
 74 
 75     /**
 76      * Initializes the widget.
 77      * @throws CException
 78      */
 79     public function init()
 80     {
 81         if (!in_array($this->inputType, array('radio', 'checkbox'))) {
 82             throw new CException(Yii::t('zii', '"inputType" attribute must be of type "radio" or "checkbox"'));
 83         }
 84         if (!in_array($this->size, array('mini', 'small', 'large'))) {
 85             throw new CException(Yii::t('zii', 'Unknown value for attribute "size".'));
 86         }
 87         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 88 
 89         TbHtml::addCssClass('make-switch', $this->pluginOptions);
 90         TbHtml::addCssClass('switch-' . $this->size, $this->pluginOptions);
 91         if(!$this->animated) {
 92             $this->pluginOptions['data-animated'] = 'false';
 93         }
 94         $this->pluginOptions['data-on-label'] = $this->onLabel;
 95         $this->pluginOptions['data-off-label'] = $this->offLabel;
 96         $this->pluginOptions['data-text-label'] = $this->textLabel;
 97         $this->pluginOptions['data-on'] = $this->onColor;
 98         $this->pluginOptions['data-off'] = $this->offColor;
 99     }
100 
101     /**
102      * Runs the widget.
103      */
104     public function run()
105     {
106         $this->renderField();
107         $this->registerClientScript();
108     }
109 
110     /**
111      * Renders the typeahead field
112      */
113     public function renderField()
114     {
115         list($name, $id) = $this->resolveNameID();
116 
117         TbArray::defaultValue('id', $id, $this->htmlOptions);
118         TbArray::defaultValue('name', $name, $this->htmlOptions);
119 
120         $this->pluginOptions['id'] = $this->htmlOptions['id'] . '_switch';
121         echo CHtml::openTag('div', $this->pluginOptions);
122         if ($this->hasModel()) {
123                 echo $this->inputType == 'radio'
124                     ? CHtml::activeRadioButton($this->model, $this->attribute, $this->htmlOptions)
125                     : CHtml::activeCheckBox($this->model, $this->attribute, $this->htmlOptions);
126         } else {
127             echo $this->inputType == 'radio'
128                 ? CHtml::radioButton($this->name, $this->value, $this->htmlOptions)
129                 : CHtml::checkBox($this->name, $this->value, $this->htmlOptions);
130         }
131         echo CHtml::closeTag('div');
132     }
133 
134     /**
135      * Registers required client script for bootstrap typeahead. It is not used through bootstrap->registerPlugin
136      * in order to attach events if any
137      */
138     public function registerClientScript()
139     {
140         /* publish assets dir */
141         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
142         $assetsUrl = $this->getAssetsUrl($path);
143 
144         /* @var $cs CClientScript */
145         $cs = Yii::app()->getClientScript();
146 
147         $min = $this->debugMode
148             ? '.min'
149             : '';
150 
151         $cs->registerCssFile($assetsUrl . '/css/bootstrap-switch.css');
152         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-switch' . $min . '.js', CClientScript::POS_END);
153 
154         if($this->events)
155         {
156             /* initialize plugin */
157             $selector = '#' . TbArray::getValue('id', $this->pluginOptions);
158 
159             $this->getApi()->registerEvents($selector, $this->events);
160         }
161     }
162 }
YiiWheels API documentation generated by ApiGen 2.8.0