| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  *## TbTypeahead class file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Amr Bedair <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @since v4.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @todo add support of bloodhound datasets, and remote ajax  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @see <https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#bloodhound-integration> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *## Twitter typeahead widget. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @see https://github.com/twitter/typeahead.js | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @since 4.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @package booster.widgets.forms.inputs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  | Yii::import('booster.widgets.TbBaseInputWidget'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class TbTypeahead extends TbBaseInputWidget { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var array the options for the twitter typeahead widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @see <https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#options> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	public $options = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @var array the datasets for the twitter typeahead widget  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @see <https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#datasets> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public $datasets = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * Initializes the widget. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  | 	public function init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  | 		if(empty($this->options)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  | 			$this->options['minLength'] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  | 		$this->registerClientScript(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  | 		if(!isset($this->htmlOptions['class']) || empty($this->htmlOptions['class'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  | 			$this->htmlOptions['class'] = 'typeahead'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  | 			$this->htmlOptions['class'] .= ' typeahead'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  | 		parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * Runs the widget. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  | 	public function run() { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  | 		list($name, $id) = $this->resolveNameID(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  | 		if (isset($this->htmlOptions['id'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 			$id = $this->htmlOptions['id']; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  | 			$this->htmlOptions['id'] = $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1 |  | 		if (isset($this->htmlOptions['name'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 			$name = $this->htmlOptions['name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  | 		if ($this->hasModel()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  | 			echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 			echo CHtml::textField($name, $this->value, $this->htmlOptions); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  | 		if (isset($this->datasets['source'])) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  | 			$this->datasets = array($this->datasets); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  | 		$datasets_js = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  | 		foreach ($this->datasets as $i => $dataset) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 			if (!isset($dataset['source'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 				throw new CException('The source for a Typeahead dataset was not set'); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			if (isset($dataset['source']['name'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 				$name = preg_replace('/[^\da-z]/i', '_', $dataset['source']['name']); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 				$bloodhound_id = $this->id .'_bloodhound_'. $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 				$dataset['source'] = 'js:'. $bloodhound_id .'.ttAdapter()'; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 			} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 				$dataset['source'] = 'js:substringMatcher(_'. $this->id .'_source_list_'. $i .')'; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			$this->datasets[$i] = $dataset; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			$datasets_js[] = CJavaScript::encode($dataset); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  | 		$options = CJavaScript::encode($this->options); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  | 		$datasets = implode(', ', $datasets_js); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 1 |  | 		Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').typeahead({$options}, {$datasets});"); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  | 	function registerClientScript() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  | 		$booster = Booster::getBooster(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  | 		$booster->registerPackage('typeahead'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  | 		$datasets = $this->datasets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  | 		if (isset($this->datasets['source'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  | 			$datasets = array($this->datasets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  | 		Yii::app()->clientScript->registerScript(__CLASS__ . '#substringMatcher', ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			var substringMatcher = function(strs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				return function findMatches(q, cb) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 					var matches, substringRegex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 					// an array that will be populated with substring matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 					matches = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 					// regex used to determine if a string contains the substring `q` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 					substrRegex = new RegExp(q, "i"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 					// iterate through the pool of strings and for any string that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 					// contains the substring `q`, add it to the `matches` array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 					$.each(strs, function(i, str) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 						if (substrRegex.test(str)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 							// the typeahead jQuery plugin expects suggestions to a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 							// JavaScript object, refer to typeahead docs for more info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 							matches.push({ value: str }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 					}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 					cb(matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 				}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  | 		', CClientScript::POS_HEAD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  | 		if (isset($this->datasets['source'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 1 |  | 			$this->datasets = array($this->datasets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  | 		foreach ($datasets as $i => $dataset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			if (isset($dataset['source']['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 				$name = preg_replace('/[^\da-z]/i', '_', $dataset['source']['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				$bloodhound_id = $this->id .'_bloodhound_'. $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				$bloodhound_config = CJavaScript::encode($dataset['source']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 				Yii::app()->clientScript->registerScript(__CLASS__ .'_'. $bloodhound_id, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 					var $bloodhound_id = new Bloodhound($bloodhound_config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 					$bloodhound_id.initialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 				", CClientScript::POS_HEAD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 				$source_list = CJavaScript::encode($dataset['source']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 				Yii::app()->clientScript->registerScript(__CLASS__ .'#source_list#'. $i, ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 					var _'.$this->id.'_source_list_'. $i .' = '.$source_list.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 				', CClientScript::POS_HEAD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.