1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 Yii::import('bootstrap.helpers.TbArray');
14
15 class WhTypeAhead extends CInputWidget
16 {
17
18 19 20 21
22 public $pluginOptions;
23
24 25 26
27 public $debugMode = false;
28
29 30 31
32 public function init()
33 {
34 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
35 }
36
37 38 39
40 public function run()
41 {
42 $this->renderField();
43 $this->registerClientScript();
44 }
45
46 47 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 67 68
69 public function registerClientScript()
70 {
71
72 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
73 $assetsUrl = $this->getAssetsUrl($path);
74
75
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
86 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
87
88 $this->getApi()->registerPlugin('typeahead', $selector, $this->pluginOptions);
89 }
90 }