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