1
|
|
|
<?php |
2
|
|
|
namespace Ajax\ui\components; |
3
|
|
|
|
4
|
|
|
use Ajax\JsUtils; |
5
|
|
|
use Ajax\ui\properties\Position; |
6
|
|
|
use Ajax\common\components\SimpleComponent; |
7
|
|
|
use Ajax\service\JString; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* JQuery UI Autocomplete component |
11
|
|
|
* |
12
|
|
|
* @author jc |
13
|
|
|
* @version 1.0.2 |
14
|
|
|
*/ |
15
|
|
|
class Autocomplete extends SimpleComponent { |
16
|
|
|
|
17
|
|
|
public function __construct(JsUtils $js) { |
18
|
|
|
parent::__construct($js); |
19
|
|
|
$this->uiName = "autocomplete"; |
20
|
|
|
$this->setParam("minLength", 3); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Define source property with an ajax request based on $url |
25
|
|
|
* $url must return a JSON array of values |
26
|
|
|
* |
27
|
|
|
* @param String $url |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
public function setAjaxSource($url) { |
31
|
|
|
if (JString::startsWith($url, "/")) { |
32
|
|
|
$url = $this->js->getUrl($url); |
33
|
|
|
} |
34
|
|
|
$ajax = "%function (request, response) { |
35
|
|
|
$.ajax({ |
36
|
|
|
url: '{$url}', |
37
|
|
|
dataType: 'jsonp', |
38
|
|
|
data: {q : request.term}, |
39
|
|
|
success: function(data) {response(data);} |
40
|
|
|
}); |
41
|
|
|
}%"; |
42
|
|
|
return $this->setParam("source", $ajax); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Define the source property |
47
|
|
|
* with a JSON Array of values |
48
|
|
|
* Example : ["Bordeaux","Alsace","Bourgogne"] |
49
|
|
|
* Example : [{value : "BO", label : "Bordeaux"}, {value : "AL", label : "Alsace"}, {value : "BOU", label : "Bourgogne"}] |
50
|
|
|
* |
51
|
|
|
* @param String $source |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setSource($source) { |
55
|
|
|
$source = str_ireplace(array( |
56
|
|
|
"\"", |
57
|
|
|
"'" |
58
|
|
|
), "%quote%", $source); |
59
|
|
|
return $this->setParam("source", "%" . $source . "%"); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If set to true the first item will automatically be focused when the menu is shown. |
64
|
|
|
* default : false |
65
|
|
|
* |
66
|
|
|
* @param Boolean $value |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setAutofocus($value) { |
70
|
|
|
return $this->setParamCtrl("autoFocus", $value, "is_bool"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The delay in milliseconds between when a keystroke occurs and when a search is performed. |
75
|
|
|
* A zero-delay makes sense for local data (more responsive), but can produce a lot of load for remote data, |
76
|
|
|
* while being less responsive. |
77
|
|
|
* default : 300 |
78
|
|
|
* |
79
|
|
|
* @param int $value |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setDelay($value) { |
83
|
|
|
return $this->setParamCtrl("delay", $value, "is_int"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Disables the autocomplete if set to true. |
88
|
|
|
* |
89
|
|
|
* @param Boolean $value |
90
|
|
|
* default : false |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setDisabled($value) { |
94
|
|
|
return $this->setParamCtrl("disabled", $value, "is_bool"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The minimum number of characters a user must type before a search is performed. |
99
|
|
|
* Zero is useful for local data with just a few items, |
100
|
|
|
* but a higher value should be used when a single character search could match a few thousand items. |
101
|
|
|
* |
102
|
|
|
* @param int $value |
103
|
|
|
* default : 1 |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setMinLength($value) { |
107
|
|
|
return $this->setParamCtrl("minLength", $value, "is_int"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Identifies the position of the suggestions menu in relation to the associated input element. |
112
|
|
|
* The of option defaults to the input element, but you can specify another element to position against. |
113
|
|
|
* You can refer to the jQuery UI Position utility for more details about the various options. |
114
|
|
|
* |
115
|
|
|
* @param int $position |
116
|
|
|
* default : { my: "left top", at: "left bottom", collision: "none" } |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setPosition(Position $position) { |
120
|
|
|
return $this->setParam("position", $position->getParams()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Triggered when the field is blurred, if the value has changed. |
125
|
|
|
* |
126
|
|
|
* @param string $jsCode |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function onChange($jsCode) { |
130
|
|
|
return $this->addEvent("change", $jsCode); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Triggered when the menu is hidden. |
135
|
|
|
* Not every close event will be accompanied by a change event. |
136
|
|
|
* |
137
|
|
|
* @param string $jsCode |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function onClose($jsCode) { |
141
|
|
|
return $this->addEvent("close", $jsCode); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Triggered when focus is moved to an item (not selecting). |
146
|
|
|
* The default action is to replace the text field's value with the value of the focused item, |
147
|
|
|
* though only if the event was triggered by a keyboard interaction. |
148
|
|
|
* Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused. |
149
|
|
|
* |
150
|
|
|
* @param string $jsCode |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function onFocus($jsCode) { |
154
|
|
|
return $this->addEvent("focus", $jsCode); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Triggered when the suggestion menu is opened or updated. |
159
|
|
|
* |
160
|
|
|
* @param string $jsCode |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function onOpen($jsCode) { |
164
|
|
|
return $this->addEvent("open", $jsCode); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Triggered after a search completes, before the menu is shown. |
169
|
|
|
* Useful for local manipulation of suggestion data, where a custom source option callback is not required. |
170
|
|
|
* This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled. |
171
|
|
|
* |
172
|
|
|
* @param string $jsCode |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function onResponse($jsCode) { |
176
|
|
|
return $this->addEvent("response", $jsCode); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Triggered before a search is performed, after minLength and delay are met. |
181
|
|
|
* If canceled, then no request will be started and no items suggested. |
182
|
|
|
* |
183
|
|
|
* @param string $jsCode |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function onSearch($jsCode) { |
187
|
|
|
return $this->addEvent("search", $jsCode); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Triggered when an item is selected from the menu. |
192
|
|
|
* The default action is to replace the text field's value with the value of the selected item. |
193
|
|
|
* Canceling this event prevents the value from being updated, but does not prevent the menu from closing. |
194
|
|
|
* |
195
|
|
|
* @param string $jsCode |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function onSelect($jsCode) { |
199
|
|
|
return $this->addEvent("select", $jsCode); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|