HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbTypeahead.php
Go to the documentation of this file.
1 <?php
22 Yii::import('booster.widgets.TbBaseInputWidget');
23 
25 
30  public $options = array();
31 
36  public $datasets = array();
37 
41  public function init() {
42 
43  if(!isset($this->datasets['source']))
44  $this->datasets['source'] = array();
45 
46  // @todo: which one is more correct?
47  // if(!isset($this->datasets['source']) || empty($this->datasets['source']))
48  // throw new CException('you must provide datasets["source"] option');
49 
50  if(empty($this->options))
51  $this->options['minLength'] = 1;
52  $this->registerClientScript();
53 
54  if(!isset($this->htmlOptions['class']) || empty($this->htmlOptions['class']))
55  $this->htmlOptions['class'] = 'typeahead';
56  else
57  $this->htmlOptions['class'] .= ' typeahead';
58 
59  parent::init();
60  }
61 
65  public function run() {
66  // print_r($this->htmlOptions); //typeahead
67  list($name, $id) = $this->resolveNameID();
68 
69  if (isset($this->htmlOptions['id'])) {
70  $id = $this->htmlOptions['id'];
71  } else {
72  $this->htmlOptions['id'] = $id;
73  }
74 
75  if (isset($this->htmlOptions['name'])) {
76  $name = $this->htmlOptions['name'];
77  }
78 
79  if ($this->hasModel()) {
80  echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
81  } else {
82  echo CHtml::textField($name, $this->value, $this->htmlOptions);
83  }
84 
85  $this->datasets['source'] = 'js:substringMatcher(_'.$this->id.'_source_list)';
86 
87  $options = CJavaScript::encode($this->options);
88  $datasets = CJavaScript::encode($this->datasets);
89 
90  Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').typeahead({$options}, {$datasets});");
91 
92  }
93 
98  function registerClientScript() {
99 
100  $booster = Booster::getBooster();
101  $booster->registerPackage('typeahead');
102 
103  if(empty($this->datasets) || !isset($this->datasets['source']) || !is_array($this->datasets['source']))
104  return;
105 
106  Yii::app()->clientScript->registerScript(__CLASS__ . '#substringMatcher', '
107  var substringMatcher = function(strs) {
108  return function findMatches(q, cb) {
109  var matches, substringRegex;
110 
111  // an array that will be populated with substring matches
112  matches = [];
113 
114  // regex used to determine if a string contains the substring `q`
115  substrRegex = new RegExp(q, "i");
116 
117  // iterate through the pool of strings and for any string that
118  // contains the substring `q`, add it to the `matches` array
119  $.each(strs, function(i, str) {
120  if (substrRegex.test(str)) {
121  // the typeahead jQuery plugin expects suggestions to a
122  // JavaScript object, refer to typeahead docs for more info
123  matches.push({ value: str });
124  }
125  });
126 
127  cb(matches);
128  };
129  };
130  ', CClientScript::POS_HEAD);
131 
132  $source_list = !empty($this->options) ? CJavaScript::encode($this->datasets['source']) : '';
133  Yii::app()->clientScript->registerScript(__CLASS__ . '#source_list#'.$this->id, '
134  var _'.$this->id.'_source_list = '.$source_list.';
135  ', CClientScript::POS_HEAD);
136  }
137 }