1: <?php
2: /**
3: * TbModal class file.
4: * @author Antonio Ramirez <[email protected]>
5: * @copyright Copyright © Christoffer Niska 2013-
6: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7: * @package bootstrap.widgets
8: */
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11: Yii::import('bootstrap.helpers.TbHtml');
12:
13: /**
14: * Bootstrap typeahead widget.
15: */
16: class TbTypeAhead extends CInputWidget
17: {
18: /**
19: * @var mixed the data source to query against. May be an array of strings or a function. The function is passed
20: * two arguments, the query value in the input field and the process callback. The function may be used synchronously
21: * by returning the data source directly or asynchronously via the process callback's single argument.
22: */
23: public $source = array();
24:
25: /**
26: * @var int the max number of items to display in the dropdown. Defaults to 8.
27: */
28: public $items = 8;
29:
30: /**
31: * @var int the minimum character length needed before triggering autocomplete suggestions
32: */
33: public $minLength = 1;
34:
35: /**
36: * @var string javascript function the method used to determine if a query matches an item. Accepts a single argument, the item
37: * against which to test the query. Access the current query with this.query. Return a boolean true if query is a
38: * match. Case insensitive.
39: */
40: public $matcher;
41:
42: /**
43: * @var string javascript function method used to sort autocomplete results. Accepts a single argument items and has
44: * the scope of the typeahead instance. Reference the current query with this.query. Exact match, case sensitive,
45: * case insensitive
46: */
47: public $sorter;
48:
49: /**
50: * @var string javascript the method used to return selected item. Accepts a single argument, the item and has the
51: * scope of the typeahead instance. Returns selected item.
52: */
53: public $updater;
54:
55: /**
56: * @var string javascript method used to highlight autocomplete results. Accepts a single argument item and has the
57: * scope of the typeahead instance. Should return html. Highlights all default matches
58: */
59: public $highlighter;
60:
61: /**
62: * @var array the plugin options
63: */
64: protected $pluginOptions = array();
65:
66: /**
67: * Widget's initialization method.
68: */
69: public function init()
70: {
71: $this->attachBehavior('TbWidget', new TbWidget);
72: $this->initOptions();
73: }
74:
75: /**
76: * Initializes the plugin options
77: */
78: public function initOptions()
79: {
80: $options = array();
81: foreach (array('matcher', 'sorter', 'updater', 'highlighter') as $fn) {
82: if ($this->$fn !== null) {
83: if ($this->$fn instanceof CJavaScriptExpression) {
84: $options[$fn] = $this->$fn;
85: } else {
86: $options[$fn] = new CJavaScriptExpression($this->$fn);
87: }
88: }
89: }
90:
91: $this->pluginOptions = TbArray::merge(
92: array(
93: 'source' => $this->source,
94: 'items' => $this->items,
95: 'minLength' => $this->minLength
96: ),
97: $options
98: );
99: }
100:
101: /**
102: * Widget's run method.
103: */
104: public function run()
105: {
106: $this->renderField();
107: $this->registerClientScript();
108: }
109:
110: /**
111: * Renders 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: // by using TbHtml we support all bootstrap options
121: if ($this->hasModel()) {
122: echo TbHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
123: } else {
124: echo TbHtml::textField($name, $this->value, $this->htmlOptions);
125: }
126: }
127:
128: /**
129: * Register required scripts.
130: */
131: public function registerClientScript()
132: {
133: /** @var TbApi $api */
134: $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
135: $this->registerPlugin(TbApi::PLUGIN_TYPEAHEAD, $selector, $this->pluginOptions);
136: }
137: }