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

  • WhTypeAhead
 1 <?php
 2 /**
 3  * WhTypeAhead widget class
 4  *
 5  * @see https://github.com/twitter/typeahead.js
 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.typeahead
11  * @uses YiiStrap.helpers.TbArray
12  */
13 Yii::import('bootstrap.helpers.TbArray');
14 
15 class WhTypeAhead extends CInputWidget
16 {
17 
18     /**
19      * @var array the plugin options
20      * @see https://github.com/twitter/typeahead.js
21      */
22     public $pluginOptions;
23 
24     /**
25      * @var bool whether to display minified versions of the files or not
26      */
27     public $debugMode = false;
28 
29     /**
30      * Initializes the widget.
31      */
32     public function init()
33     {
34         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
35     }
36 
37     /**
38      * Runs the widget.
39      */
40     public function run()
41     {
42         $this->renderField();
43         $this->registerClientScript();
44     }
45 
46     /**
47      * Renders the typeahead field
48      */
49     public function renderField()
50     {
51         list($name, $id) = $this->resolveNameID();
52 
53         TbArray::defaultValue('id', $id, $this->htmlOptions);
54         TbArray::defaultValue('name', $name, $this->htmlOptions);
55 
56         TbHtml::addCssClass('form-control', $this->htmlOptions);
57 
58         if ($this->hasModel()) {
59             echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
60         } else {
61             echo CHtml::textField($this->name, $this->value, $this->htmlOptions);
62         }
63     }
64 
65     /**
66      * Registers required client script for bootstrap typeahead. It is not used through bootstrap->registerPlugin
67      * in order to attach events if any
68      */
69     public function registerClientScript()
70     {
71         /* publish assets dir */
72         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
73         $assetsUrl = $this->getAssetsUrl($path);
74 
75         /* @var $cs CClientScript */
76         $cs = Yii::app()->getClientScript();
77 
78         $min = $this->debugMode
79             ? '.min'
80             : '';
81 
82         $cs->registerCssFile($assetsUrl . '/css/typeahead' . $min . '.css');
83         $cs->registerScriptFile($assetsUrl . '/js/typeahead' . $min . '.js', CClientScript::POS_END);
84 
85         /* initialize plugin */
86         $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
87 
88         $this->getApi()->registerPlugin('typeahead', $selector, $this->pluginOptions);
89     }
90 }
YiiWheels API documentation generated by ApiGen 2.8.0