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 if ($this->hasModel()) {
57 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
58 } else {
59 echo CHtml::textField($this->name, $this->value, $this->htmlOptions);
60 }
61 }
62
63 64 65 66
67 public function registerClientScript()
68 {
69
70 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
71 $assetsUrl = $this->getAssetsUrl($path);
72
73
74 $cs = Yii::app()->getClientScript();
75
76 $min = $this->debugMode
77 ? '.min'
78 : '';
79
80 $cs->registerCssFile($assetsUrl . '/css/typeahead' . $min . '.css');
81 $cs->registerScriptFile($assetsUrl . '/js/typeahead' . $min . '.js', CClientScript::POS_END);
82
83
84 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
85
86 $this->getApi()->registerPlugin('typeahead', $selector, $this->pluginOptions);
87 }
88 }