4 * Attaches the autocomplete behavior to all required fields.
6 Drupal
.behaviors
.autocomplete
= {
7 attach: function (context
, settings
) {
9 $('input.autocomplete', context
).once('autocomplete', function () {
12 acdb
[uri
] = new Drupal
.ACDB(uri
);
14 var $input
= $('#' + this.id
.substr(0, this.id
.length
- 13))
15 .attr('autocomplete', 'OFF')
16 .attr('aria-autocomplete', 'list');
17 $($input
[0].form
).submit(Drupal
.autocompleteSubmit
);
19 .attr('role', 'application')
20 .append($('<span class="element-invisible" aria-live="assertive"></span>')
21 .attr('id', $input
.attr('id') + '-autocomplete-aria-live')
23 new Drupal
.jsAC($input
, acdb
[uri
]);
29 * Prevents the form from submitting if the suggestions popup is open
30 * and closes the suggestions popup when doing so.
32 Drupal
.autocompleteSubmit = function () {
33 return $('#autocomplete').each(function () {
34 this.owner
.hidePopup();
39 * An AutoComplete object.
41 Drupal
.jsAC = function ($input
, db
) {
43 this.input
= $input
[0];
44 this.ariaLive
= $('#' + this.input
.id
+ '-autocomplete-aria-live');
48 .keydown(function (event
) { return ac
.onkeydown(this, event
); })
49 .keyup(function (event
) { ac
.onkeyup(this, event
); })
50 .blur(function () { ac
.hidePopup(); ac
.db
.cancel(); });
55 * Handler for the "keydown" event.
57 Drupal
.jsAC
.prototype.onkeydown = function (input
, e
) {
62 case 40: // down arrow.
68 default: // All other keys.
74 * Handler for the "keyup" event.
76 Drupal
.jsAC
.prototype.onkeyup = function (input
, e
) {
84 case 20: // Caps lock.
86 case 34: // Page down.
89 case 37: // Left arrow.
91 case 39: // Right arrow.
92 case 40: // Down arrow.
98 this.hidePopup(e
.keyCode
);
101 default: // All other keys.
102 if (input
.value
.length
> 0 && !input
.readOnly
) {
103 this.populatePopup();
106 this.hidePopup(e
.keyCode
);
113 * Puts the currently highlighted suggestion into the autocomplete field.
115 Drupal
.jsAC
.prototype.select = function (node
) {
116 this.input
.value
= $(node
).data('autocompleteValue');
117 $(this.input
).trigger('autocompleteSelect', [node
]);
121 * Highlights the next suggestion.
123 Drupal
.jsAC
.prototype.selectDown = function () {
124 if (this.selected
&& this.selected
.nextSibling
) {
125 this.highlight(this.selected
.nextSibling
);
127 else if (this.popup
) {
128 var lis
= $('li', this.popup
);
129 if (lis
.length
> 0) {
130 this.highlight(lis
.get(0));
136 * Highlights the previous suggestion.
138 Drupal
.jsAC
.prototype.selectUp = function () {
139 if (this.selected
&& this.selected
.previousSibling
) {
140 this.highlight(this.selected
.previousSibling
);
145 * Highlights a suggestion.
147 Drupal
.jsAC
.prototype.highlight = function (node
) {
149 $(this.selected
).removeClass('selected');
151 $(node
).addClass('selected');
152 this.selected
= node
;
153 $(this.ariaLive
).html($(this.selected
).html());
157 * Unhighlights a suggestion.
159 Drupal
.jsAC
.prototype.unhighlight = function (node
) {
160 $(node
).removeClass('selected');
161 this.selected
= false;
162 $(this.ariaLive
).empty();
166 * Hides the autocomplete suggestions.
168 Drupal
.jsAC
.prototype.hidePopup = function (keycode
) {
169 // Select item if the right key or mousebutton was pressed.
170 if (this.selected
&& ((keycode
&& keycode
!= 46 && keycode
!= 8 && keycode
!= 27) || !keycode
)) {
171 this.select(this.selected
);
174 var popup
= this.popup
;
177 $(popup
).fadeOut('fast', function () { $(popup
).remove(); });
179 this.selected
= false;
180 $(this.ariaLive
).empty();
184 * Positions the suggestions popup and starts a search.
186 Drupal
.jsAC
.prototype.populatePopup = function () {
187 var $input
= $(this.input
);
188 var position
= $input
.position();
191 $(this.popup
).remove();
193 this.selected
= false;
194 this.popup
= $('<div id="autocomplete"></div>')[0];
195 this.popup
.owner
= this;
197 top
: parseInt(position
.top
+ this.input
.offsetHeight
, 10) + 'px',
198 left
: parseInt(position
.left
, 10) + 'px',
199 width
: $input
.innerWidth() + 'px',
202 $input
.before(this.popup
);
205 this.db
.owner
= this;
206 this.db
.search(this.input
.value
);
210 * Fills the suggestion popup with any matches received.
212 Drupal
.jsAC
.prototype.found = function (matches
) {
213 // If no value in the textfield, do not show the popup.
214 if (!this.input
.value
.length
) {
219 var ul
= $('<ul></ul>');
221 for (key
in matches
) {
223 .html($('<div></div>').html(matches
[key
]))
224 .mousedown(function () { ac
.hidePopup(this); })
225 .mouseover(function () { ac
.highlight(this); })
226 .mouseout(function () { ac
.unhighlight(this); })
227 .data('autocompleteValue', key
)
231 // Show popup with matches, if any.
233 if (ul
.children().length
) {
234 $(this.popup
).empty().append(ul
).show();
235 $(this.ariaLive
).html(Drupal
.t('Autocomplete popup'));
238 $(this.popup
).css({ visibility
: 'hidden' });
244 Drupal
.jsAC
.prototype.setStatus = function (status
) {
247 $(this.input
).addClass('throbbing');
248 $(this.ariaLive
).html(Drupal
.t('Searching for matches...'));
253 $(this.input
).removeClass('throbbing');
259 * An AutoComplete DataBase object.
261 Drupal
.ACDB = function (uri
) {
268 * Performs a cached and delayed search.
270 Drupal
.ACDB
.prototype.search = function (searchString
) {
272 this.searchString
= searchString
;
274 // See if this string needs to be searched for anyway. The pattern ../ is
275 // stripped since it may be misinterpreted by the browser.
276 searchString
= searchString
.replace(/^\s+|\.{2,}\/|\s+$/g, '');
277 // Skip empty search strings, or search strings ending with a comma, since
278 // that is the separator between search terms.
279 if (searchString
.length
<= 0 ||
280 searchString
.charAt(searchString
.length
- 1) == ',') {
284 // See if this key has been searched for before.
285 if (this.cache
[searchString
]) {
286 return this.owner
.found(this.cache
[searchString
]);
289 // Initiate delayed search.
291 clearTimeout(this.timer
);
293 this.timer
= setTimeout(function () {
294 db
.owner
.setStatus('begin');
296 // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
297 // encodeURIComponent to allow autocomplete search terms to contain slashes.
300 url
: db
.uri
+ '/' + Drupal
.encodePath(searchString
),
302 success: function (matches
) {
303 if (typeof matches
.status
== 'undefined' || matches
.status
!= 0) {
304 db
.cache
[searchString
] = matches
;
305 // Verify if these are still the matches the user wants to see.
306 if (db
.searchString
== searchString
) {
307 db
.owner
.found(matches
);
309 db
.owner
.setStatus('found');
312 error: function (xmlhttp
) {
313 Drupal
.displayAjaxError(Drupal
.ajaxError(xmlhttp
, db
.uri
));
320 * Cancels the current autocomplete request.
322 Drupal
.ACDB
.prototype.cancel = function () {
323 if (this.owner
) this.owner
.setStatus('cancel');
324 if (this.timer
) clearTimeout(this.timer
);
325 this.searchString
= '';