YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • 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     /**
 71      * Initializes the widget.
 72      * @throws CException
 73      */
 74     public function init()
 75     {
 76         if (!in_array($this->inputType, array('radio', 'checkbox'))) {
 77             throw new CException(Yii::t('zii', '"inputType" attribute must be of type "radio" or "checkbox"'));
 78         }
 79         if (!in_array($this->size, array('mini', 'small', 'normal', 'large'))) {
 80             throw new CException(Yii::t('zii', 'Unknown value for attribute "size".'));
 81         }
 82         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 83 
 84         if (!$this->animated) {
 85             $this->htmlOptions['data-animate'] = 'false';
 86         }
 87         $this->htmlOptions['data-on-text'] = $this->onLabel;
 88         $this->htmlOptions['data-off-text'] = $this->offLabel;
 89         $this->htmlOptions['data-label-text'] = $this->textLabel;
 90         $this->htmlOptions['data-on-color'] = $this->onColor;
 91         $this->htmlOptions['data-off-color'] = $this->offColor;
 92         $this->htmlOptions['data-size'] = $this->size;
 93     }
 94 
 95     /**
 96      * Runs the widget.
 97      */
 98     public function run()
 99     {
100         $this->renderField();
101         $this->registerClientScript();
102     }
103 
104     /**
105      * Renders the typeahead field
106      */
107     public function renderField()
108     {
109         list($name, $id) = $this->resolveNameID();
110 
111         TbArray::defaultValue('id', $id, $this->htmlOptions);
112         TbArray::defaultValue('name', $name, $this->htmlOptions);
113 
114         if ($this->hasModel()) {
115             echo $this->inputType == 'radio'
116                 ? CHtml::activeRadioButton($this->model, $this->attribute, $this->htmlOptions)
117                 : CHtml::activeCheckBox($this->model, $this->attribute, $this->htmlOptions);
118         } else {
119             echo $this->inputType == 'radio'
120                 ? CHtml::radioButton($this->name, $this->value, $this->htmlOptions)
121                 : CHtml::checkBox($this->name, $this->value, $this->htmlOptions);
122         }
123     }
124 
125     /**
126      * Registers required client script for bootstrap typeahead. It is not used through bootstrap->registerPlugin
127      * in order to attach events if any
128      */
129     public function registerClientScript()
130     {
131         /* publish assets dir */
132         $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
133         $assetsUrl = $this->getAssetsUrl($path);
134         $id = TbArray::getValue('id', $this->htmlOptions);
135 
136         /* @var $cs CClientScript */
137         $cs = Yii::app()->getClientScript();
138 
139         $min = $this->debugMode
140             ? '.min'
141             : '';
142 
143         $cs->registerCssFile($assetsUrl . '/css/bootstrap-switch.css');
144         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-switch' . $min . '.js', CClientScript::POS_END);
145         $selector = '#' . $id;
146 
147         $this->getApi()->registerPlugin('bootstrapSwitch', $selector);
148         $this->getApi()->registerEvents($selector, $this->events);
149 
150     }
151 }
YiiWheels API documentation generated by ApiGen 2.8.0