diff --git a/core/includes/form.inc b/core/includes/form.inc index c94bc62..ea4b5df 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -3832,20 +3832,8 @@ function form_process_autocomplete($element, &$form_state) { if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) { $element['#attributes']['class'][] = 'form-autocomplete'; $element['#attached']['library'][] = array('system', 'drupal.autocomplete'); - // Provide a hidden element for the JavaScript behavior to bind to. Since - // this element is for client-side functionality only, and we don't want to - // collect any input from it, use #theme='hidden' instead of #type='hidden'. - // @todo Refactor autocomplete.js to accept Drupal.settings instead of - // requiring extraneous markup. - $element['autocomplete'] = array( - '#theme' => 'hidden', - '#attributes' => array( - 'id' => $element['#id'] . '-autocomplete', - 'value' => url($element['#autocomplete_path'], array('absolute' => TRUE)), - 'class' => array('autocomplete'), - 'disabled' => 'disabled', - ), - ); + // Provide a data attribute for the JavaScript behavior to bind to. + $element['#attributes']['data-autocomplete-path'] = url($element['#autocomplete_path'], array('absolute' => TRUE)); } return $element; } diff --git a/core/misc/autocomplete.js b/core/misc/autocomplete.js index 0ffe404..c07de2a 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -7,328 +7,65 @@ */ Drupal.behaviors.autocomplete = { attach: function (context, settings) { - var acdb = []; - $(context).find('input.autocomplete').once('autocomplete', function () { - var uri = this.value; - if (!acdb[uri]) { - acdb[uri] = new Drupal.ACDB(uri); - } - var $input = $('#' + this.id.substr(0, this.id.length - 13)) - .attr('autocomplete', 'OFF') - .attr('aria-autocomplete', 'list'); - $($input[0].form).submit(Drupal.autocompleteSubmit); - $input.parent() - .attr('role', 'application') - .append($('') - .attr('id', $input[0].id + '-autocomplete-aria-live') - ); - new Drupal.jsAC($input, acdb[uri]); + // Act on textfields with the "form-autocomplete" class. + $('input.form-autocomplete', context).once('autocomplete', function() { + // Use jQuery UI Autocomplete on the textfield. + $(this).autocomplete({ + // The autcomplete source is provided by the data-autocomplete-path + // attribute within the element itself. + source: function(request, response) { + // Get the desired term and construct the autocomplete URL for it. + var term = Drupal.autocompleteextractLast(request.term); + var url = $(this.element).data('autocompletePath') + '/' + encodeURIComponent(term); + // Make the AJAX request. + $.getJSON(url, function(data) { + // Drupal returns an object array, but we need a string array. + var terms = new Array(); + $.each(data, function(key, value) { + terms.push(value); + }); + // Send the new string array of terms to the jQuery UI list. + response(terms); + }); + }, + search: function() { + // Only search if there has been more then two characters in there. + var term = Drupal.autocompleteextractLast(this.value); + if (term.length < 1) { + return false; + } + }, + focus: function() { + // Prevent value inserted on focus. + return false; + }, + // It will select the search term based on the last item in the list. + select: function(event, ui) { + var terms = Drupal.autocompletesplit(this.value); + // Remove the current input. + terms.pop(); + // Add the selected item. + terms.push(ui.item.value); + this.value = terms.join(", "); + return false; + } + }); }); - } -}; - -/** - * Prevents the form from submitting if the suggestions popup is open - * and closes the suggestions popup when doing so. - */ -Drupal.autocompleteSubmit = function () { - var $autocomplete = $('#autocomplete'); - if ($autocomplete.length !== 0) { - $autocomplete[0].owner.hidePopup(); - } - return $autocomplete.length === 0; -}; - -/** - * An AutoComplete object. - */ -Drupal.jsAC = function ($input, db) { - var ac = this; - this.input = $input[0]; - this.ariaLive = $('#' + this.input.id + '-autocomplete-aria-live'); - this.db = db; - - $input - .keydown(function (event) { return ac.onkeydown(this, event); }) - .keyup(function (event) { ac.onkeyup(this, event); }) - .blur(function () { ac.hidePopup(); ac.db.cancel(); }); -}; - -/** - * Handler for the "keydown" event. - */ -Drupal.jsAC.prototype.onkeydown = function (input, e) { - if (!e) { - e = window.event; - } - switch (e.keyCode) { - case 40: // down arrow. - this.selectDown(); - return false; - case 38: // up arrow. - this.selectUp(); - return false; - default: // All other keys. - return true; - } -}; - -/** - * Handler for the "keyup" event. - */ -Drupal.jsAC.prototype.onkeyup = function (input, e) { - if (!e) { - e = window.event; - } - switch (e.keyCode) { - case 16: // Shift. - case 17: // Ctrl. - case 18: // Alt. - case 20: // Caps lock. - case 33: // Page up. - case 34: // Page down. - case 35: // End. - case 36: // Home. - case 37: // Left arrow. - case 38: // Up arrow. - case 39: // Right arrow. - case 40: // Down arrow. - return true; - case 9: // Tab. - case 13: // Enter. - case 27: // Esc. - this.hidePopup(e.keyCode); - return true; - - default: // All other keys. - if (input.value.length > 0 && !input.readOnly) { - this.populatePopup(); - } - else { - this.hidePopup(e.keyCode); + // Pop up the autocomplete if tab was entered. + $(this).bind('keydown', function(event) { + if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) { + event.preventDefault(); } - return true; - } -}; - -/** - * Puts the currently highlighted suggestion into the autocomplete field. - */ -Drupal.jsAC.prototype.select = function (node) { - this.input.value = $(node).data('autocompleteValue'); -}; - -/** - * Highlights the next suggestion. - */ -Drupal.jsAC.prototype.selectDown = function () { - if (this.selected && this.selected.nextSibling) { - this.highlight(this.selected.nextSibling); - } - else if (this.popup) { - var lis = $(this.popup).find('li'); - if (lis.length > 0) { - this.highlight(lis.get(0)); - } - } -}; - -/** - * Highlights the previous suggestion. - */ -Drupal.jsAC.prototype.selectUp = function () { - if (this.selected && this.selected.previousSibling) { - this.highlight(this.selected.previousSibling); - } -}; - -/** - * Highlights a suggestion. - */ -Drupal.jsAC.prototype.highlight = function (node) { - // Unhighlights a suggestion for "keyup" and "keydown" events. - if (this.selected !== false) { - $(this.selected).removeClass('selected'); - } - $(node).addClass('selected'); - this.selected = node; - $(this.ariaLive).html($(this.selected).html()); -}; - -/** - * Unhighlights a suggestion. - */ -Drupal.jsAC.prototype.unhighlight = function (node) { - $(node).removeClass('selected'); - this.selected = false; - $(this.ariaLive).empty(); -}; - -/** - * Hides the autocomplete suggestions. - */ -Drupal.jsAC.prototype.hidePopup = function (keycode) { - // Select item if the right key or mousebutton was pressed. - if (this.selected && ((keycode && keycode !== 46 && keycode !== 8 && keycode !== 27) || !keycode)) { - this.input.value = $(this.selected).data('autocompleteValue'); - } - // Hide popup. - var popup = this.popup; - if (popup) { - this.popup = null; - $(popup).fadeOut('fast', function () { $(popup).remove(); }); - } - this.selected = false; - $(this.ariaLive).empty(); -}; - -/** - * Positions the suggestions popup and starts a search. - */ -Drupal.jsAC.prototype.populatePopup = function () { - var $input = $(this.input); - var position = $input.position(); - // Show popup. - if (this.popup) { - $(this.popup).remove(); - } - this.selected = false; - this.popup = $('
')[0]; - this.popup.owner = this; - $(this.popup).css({ - top: parseInt(position.top + this.input.offsetHeight, 10) + 'px', - left: parseInt(position.left, 10) + 'px', - width: $input.innerWidth() + 'px', - display: 'none' - }); - $input.before(this.popup); - - // Do search. - this.db.owner = this; - this.db.search(this.input.value); -}; - -/** - * Fills the suggestion popup with any matches received. - */ -Drupal.jsAC.prototype.found = function (matches) { - // If no value in the textfield, do not show the popup. - if (!this.input.value.length) { - return false; - } - - // Prepare matches. - var ul = $('