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');
120 * Highlights the next suggestion.
122 Drupal
.jsAC
.prototype.selectDown = function () {
123 if (this.selected
&& this.selected
.nextSibling
) {
124 this.highlight(this.selected
.nextSibling
);
126 else if (this.popup
) {
127 var lis
= $('li', this.popup
);
128 if (lis
.length
> 0) {
129 this.highlight(lis
.get(0));
135 * Highlights the previous suggestion.
137 Drupal
.jsAC
.prototype.selectUp = function () {
138 if (this.selected
&& this.selected
.previousSibling
) {
139 this.highlight(this.selected
.previousSibling
);
144 * Highlights a suggestion.
146 Drupal
.jsAC
.prototype.highlight = function (node
) {
148 $(this.selected
).removeClass('selected');
150 $(node
).addClass('selected');
151 this.selected
= node
;
152 $(this.ariaLive
).html($(this.selected
).html());
156 * Unhighlights a suggestion.
158 Drupal
.jsAC
.prototype.unhighlight = function (node
) {
159 $(node
).removeClass('selected');
160 this.selected
= false;
161 $(this.ariaLive
).empty();
165 * Hides the autocomplete suggestions.
167 Drupal
.jsAC
.prototype.hidePopup = function (keycode
) {
168 // Select item if the right key or mousebutton was pressed.
169 if (this.selected
&& ((keycode
&& keycode
!= 46 && keycode
!= 8 && keycode
!= 27) || !keycode
)) {
170 this.input
.value
= $(this.selected
).data('autocompleteValue');
173 var popup
= this.popup
;
176 $(popup
).fadeOut('fast', function () { $(popup
).remove(); });
178 this.selected
= false;
179 $(this.ariaLive
).empty();
183 * Positions the suggestions popup and starts a search.
185 Drupal
.jsAC
.prototype.populatePopup = function () {
186 var $input
= $(this.input
);
187 var position
= $input
.position();
190 $(this.popup
).remove();
192 this.selected
= false;
193 this.popup
= $('<div id="autocomplete"></div>')[0];
194 this.popup
.owner
= this;
196 top
: parseInt(position
.top
+ this.input
.offsetHeight
, 10) + 'px',
197 left
: parseInt(position
.left
, 10) + 'px',
198 width
: $input
.innerWidth() + 'px',
201 $input
.before(this.popup
);
204 this.db
.owner
= this;
205 this.db
.search(this.input
.value
);
209 * Fills the suggestion popup with any matches received.
211 Drupal
.jsAC
.prototype.found = function (matches
) {
212 // If no value in the textfield, do not show the popup.
213 if (!this.input
.value
.length
) {
218 var ul
= $('<ul></ul>');
220 for (key
in matches
) {
222 .html($('<div></div>').html(matches
[key
]))
223 .mousedown(function () { ac
.select(this); })
224 .mouseover(function () { ac
.highlight(this); })
225 .mouseout(function () { ac
.unhighlight(this); })
226 .data('autocompleteValue', key
)
230 // Show popup with matches, if any.
232 if (ul
.children().length
) {
233 $(this.popup
).empty().append(ul
).show();
234 $(this.ariaLive
).html(Drupal
.t('Autocomplete popup'));
237 $(this.popup
).css({ visibility
: 'hidden' });
243 Drupal
.jsAC
.prototype.setStatus = function (status
) {
246 $(this.input
).addClass('throbbing');
247 $(this.ariaLive
).html(Drupal
.t('Searching for matches...'));
252 $(this.input
).removeClass('throbbing');
258 * An AutoComplete DataBase object.
260 Drupal
.ACDB = function (uri
) {
267 * Performs a cached and delayed search.
269 Drupal
.ACDB
.prototype.search = function (searchString
) {
271 this.searchString
= searchString
;
273 // See if this string needs to be searched for anyway.
274 searchString
= searchString
.replace(/^\s+|\s+$/, '');
275 if (searchString
.length
<= 0 ||
276 searchString
.charAt(searchString
.length
- 1) == ',') {
280 // See if this key has been searched for before.
281 if (this.cache
[searchString
]) {
282 return this.owner
.found(this.cache
[searchString
]);
285 // Initiate delayed search.
287 clearTimeout(this.timer
);
289 this.timer
= setTimeout(function () {
290 db
.owner
.setStatus('begin');
292 // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
293 // encodeURIComponent to allow autocomplete search terms to contain slashes.
296 url
: db
.uri
+ '/' + Drupal
.encodePath(searchString
),
298 success: function (matches
) {
299 if (typeof matches
.status
== 'undefined' || matches
.status
!= 0) {
300 db
.cache
[searchString
] = matches
;
301 // Verify if these are still the matches the user wants to see.
302 if (db
.searchString
== searchString
) {
303 db
.owner
.found(matches
);
305 db
.owner
.setStatus('found');
308 error: function (xmlhttp
) {
309 alert(Drupal
.ajaxError(xmlhttp
, db
.uri
));
316 * Cancels the current autocomplete request.
318 Drupal
.ACDB
.prototype.cancel = function () {
319 if (this.owner
) this.owner
.setStatus('cancel');
320 if (this.timer
) clearTimeout(this.timer
);
321 this.searchString
= '';