diff --git a/core/misc/autocomplete.js b/core/misc/autocomplete.js index dc58b19..51de595 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -1,336 +1,163 @@ -(function ($) { +(function ($, Drupal) { "use strict"; -/** - * Attaches the autocomplete behavior to all required fields. - */ -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]); - }); - } -}; - -/** - * 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; -}; +var autocomplete; /** - * An AutoComplete object. + * Helper splitting terms from the autocomplete value. + * + * @param {String} value + * + * @return {Array} */ -Drupal.jsAC = function ($input, db) { - var ac = this; - this.input = $input[0]; - this.ariaLive = $('#' + this.input.id + '-autocomplete-aria-live'); - this.db = db; +function autocompleteSplitValues(value) { + // We will match the value against comma-seperated, quoted terms. + return value.match(/("[^"]+")|(\b\w+\b)/g) || []; +} - $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. + * Returns the last value of an multi-value textfield. + * + * @param {String} terms + * + * @return {String} */ -Drupal.jsAC.prototype.onkeydown = function (input, e) { - if (!e) { - e = window.event; - } - switch (e.keyCode) { - case 40: // down arrow. - e.preventDefault(); - this.selectDown(); - break; - case 38: // up arrow. - e.preventDefault(); - this.selectUp(); - break; - default: // All other keys. - return true; - } -}; +function extractLastTerm(terms) { + return autocomplete.splitValues(terms).pop(); +} /** - * Handler for the "keyup" event. + * The search handler is called before a search is performed. + * + * @param {Object} event + * + * @return {Boolean} */ -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); - } - return true; - } -}; +function searchHandler(event) { + // Only search when the term is two characters or larger. + var term = autocomplete.extractLastTerm(event.target.value); + return term.length >= autocomplete.minLength; +} /** - * Puts the currently highlighted suggestion into the autocomplete field. + * jQuery UI autocomplete source callback. + * + * @param {Object} request + * @param {Function} response */ -Drupal.jsAC.prototype.select = function (node) { - this.input.value = $(node).data('autocompleteValue'); -}; +function sourceData(request, response) { + // Get the desired term and construct the autocomplete URL for it. + var term = autocomplete.extractLastTerm(request.term); -/** - * 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)); + /** + * Transforms the data object into an array and update autocomplete results. + * + * @param {Object} data + */ + function sourceCallbackHandler(data) { + // Drupal returns an object, we need an array. + var terms = []; + for (var i in data) { + if (data.hasOwnProperty(i)) { + terms.push(data[i]); + } } - } -}; + autocomplete.cache[term] = terms; -/** - * Highlights the previous suggestion. - */ -Drupal.jsAC.prototype.selectUp = function () { - if (this.selected && this.selected.previousSibling) { - this.highlight(this.selected.previousSibling); + // Send the new string array of terms to the jQuery UI list. + response(terms); } -}; - -/** - * 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'); + // Check if the term is already cached. + if (autocomplete.cache.hasOwnProperty(term)) { + response(autocomplete.cache[term]); } - $(node).addClass('selected'); - this.selected = node; - $(this.ariaLive).html($(this.selected).html()); -}; + else { + // Construct the AJAX request from the provided defaults. + var ajax = Drupal.ajax; + var url = this.element.data('autocompletePath') + '/' + encodeURIComponent(term); + ajax.success = sourceCallbackHandler; + // Make the AJAX request. + $.ajax(url, ajax); + } +} /** - * Unhighlights a suggestion. + * Handles an autocompletefocus event. + * + * @return {Boolean} */ -Drupal.jsAC.prototype.unhighlight = function (node) { - $(node).removeClass('selected'); - this.selected = false; - $(this.ariaLive).empty(); -}; +function focusHandler() { + return false; +} /** - * Hides the autocomplete suggestions. + * Handles an autocompleteselect event. + * + * @param {Object} event + * @param {Object} ui + * + * @return {Boolean} */ -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'); +function selectHandler(event, ui) { + var terms = autocomplete.splitValues(event.target.value); + // Remove the current input. + terms.pop(); + // Add the selected item. + if (ui.item.value.search(",") > 0) { + terms.push('"' + ui.item.value + '"'); } - // Hide popup. - var popup = this.popup; - if (popup) { - this.popup = null; - $(popup).fadeOut('fast', function () { $(popup).remove(); }); + else { + terms.push(ui.item.value); } - 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); -}; + event.target.value = terms.join(', '); + // Return false to tell jQuery UI that we've filled in the value already. + return false; +} /** - * Fills the suggestion popup with any matches received. + * Attaches the autocomplete behavior to all required fields. */ -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 = $(''); - var ac = this; - for (var key in matches) { - if (matches.hasOwnProperty(key)) { - $('
  • ') - .html($('
    ').html(matches[key])) - .mousedown(function () { ac.select(this); }) - .mouseover(function () { ac.highlight(this); }) - .mouseout(function () { ac.unhighlight(this); }) - .data('autocompleteValue', key) - .appendTo(ul); - } - } - - // Show popup with matches, if any. - if (this.popup) { - if (ul.children().length) { - $(this.popup).empty().append(ul).show(); - $(this.ariaLive).html(Drupal.t('Autocomplete popup')); +Drupal.behaviors.autocomplete = { + attach: function (context) { + // Act on textfields with the "form-autocomplete" class. + var $autocomplete = $(context).find('input.form-autocomplete').once('autocomplete'); + if ($autocomplete.length) { + // Use jQuery UI Autocomplete on the textfield. + $autocomplete.autocomplete(autocomplete.options); } - else { - $(this.popup).css({ visibility: 'hidden' }); - this.hidePopup(); + }, + detach: function (context, settings, trigger) { + if (trigger === 'unload') { + $(context).find('input.form-autocomplete') + .removeOnce('autocomplete') + .autocomplete('destroy'); } } }; -Drupal.jsAC.prototype.setStatus = function (status) { - switch (status) { - case 'begin': - $(this.input).addClass('throbbing'); - $(this.ariaLive).html(Drupal.t('Searching for matches...')); - break; - case 'cancel': - case 'error': - case 'found': - $(this.input).removeClass('throbbing'); - break; - } -}; - /** - * An AutoComplete DataBase object. + * The autocomplete object, allowing overriding by contrib. */ -Drupal.ACDB = function (uri) { - this.uri = uri; - this.delay = 300; - this.cache = {}; -}; - -/** - * Performs a cached and delayed search. - */ -Drupal.ACDB.prototype.search = function (searchString) { - var db = this; - this.searchString = searchString; - - // See if this string needs to be searched for anyway. - searchString = searchString.replace(/^\s+|\s+$/, ''); - if (searchString.length <= 0 || - searchString.charAt(searchString.length - 1) === ',') { - return; - } - - // See if this key has been searched for before. - if (this.cache[searchString]) { - return this.owner.found(this.cache[searchString]); +autocomplete = { + cache: {}, + // Exposes methods to allow overriding by contrib. + minLength: 2, + splitValues: autocompleteSplitValues, + extractLastTerm: extractLastTerm, + // jQuery UI autocomplete options. + options: { + source: sourceData, + focus: focusHandler, + search: searchHandler, + select: selectHandler + }, + ajax: { + dataType: 'json' } - - // Initiate delayed search. - if (this.timer) { - clearTimeout(this.timer); - } - this.timer = setTimeout(function () { - db.owner.setStatus('begin'); - - // Ajax GET request for autocompletion. We use Drupal.encodePath instead of - // encodeURIComponent to allow autocomplete search terms to contain slashes. - $.ajax({ - type: 'GET', - url: db.uri + '/' + Drupal.encodePath(searchString), - dataType: 'json', - success: function (matches) { - if (typeof matches.status === 'undefined' || matches.status !== 0) { - db.cache[searchString] = matches; - // Verify if these are still the matches the user wants to see. - if (db.searchString === searchString) { - db.owner.found(matches); - } - db.owner.setStatus('found'); - } - }, - error: function (xmlhttp) { - alert(Drupal.ajaxError(xmlhttp, db.uri)); - } - }); - }, this.delay); }; -/** - * Cancels the current autocomplete request. - */ -Drupal.ACDB.prototype.cancel = function () { - if (this.owner) { - this.owner.setStatus('cancel'); - } - if (this.timer) { - clearTimeout(this.timer); - } - this.searchString = ''; -}; +Drupal.autocomplete = autocomplete; -})(jQuery); +})(jQuery, Drupal); diff --git a/core/modules/system/system.base-rtl.css b/core/modules/system/system.base-rtl.css index d01792e..73b80fb 100644 --- a/core/modules/system/system.base-rtl.css +++ b/core/modules/system/system.base-rtl.css @@ -1,4 +1,3 @@ - /** * @file * Generic theme-independent base styles. @@ -11,7 +10,7 @@ .js input.form-autocomplete { background-position: 0% 2px; } -.js input.throbbing { +.js input.form-autocomplete.ui-autocomplete-loading { background-position: 0% -18px; } diff --git a/core/modules/system/system.base.css b/core/modules/system/system.base.css index f8a7726..f7c4a3a 100644 --- a/core/modules/system/system.base.css +++ b/core/modules/system/system.base.css @@ -1,40 +1,15 @@ - /** * @file * Generic theme-independent base styles. */ -/** - * Autocomplete. - * - * @see autocomplete.js - */ -/* Suggestion list */ -#autocomplete { - border: 1px solid; - overflow: hidden; - position: absolute; - z-index: 100; -} -#autocomplete ul { - list-style: none; - list-style-image: none; - margin: 0; - padding: 0; -} -#autocomplete li { - background: #fff; - color: #000; - cursor: default; - white-space: pre; -} /* Animated throbber */ .js input.form-autocomplete { background-image: url(../../misc/throbber.gif); background-position: 100% 2px; /* LTR */ background-repeat: no-repeat; } -.js input.throbbing { +.js input.form-autocomplete.ui-autocomplete-loading { background-position: 100% -18px; /* LTR */ } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index f78f123..b066879 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1336,6 +1336,7 @@ function system_library_info() { array('system', 'jquery'), array('system', 'drupal'), array('system', 'drupal.ajax'), + array('system', 'jquery.ui.autocomplete'), ), ); diff --git a/core/themes/bartik/css/style-rtl.css b/core/themes/bartik/css/style-rtl.css index 90638eb..111c764 100644 --- a/core/themes/bartik/css/style-rtl.css +++ b/core/themes/bartik/css/style-rtl.css @@ -225,7 +225,7 @@ ul.action-links li a { .js input.form-autocomplete { background-position: 1% 4px; } -.js input.throbbing { +.js input.form-autocomplete.ui-autocomplete-loading { background-position: 1% -16px; } diff --git a/core/themes/bartik/css/style.css b/core/themes/bartik/css/style.css index 7532a4a..0871991 100644 --- a/core/themes/bartik/css/style.css +++ b/core/themes/bartik/css/style.css @@ -1,4 +1,3 @@ - /* ---------- Overall Specifications ---------- */ body { @@ -1283,7 +1282,7 @@ input.form-button-disabled:active, .js input.form-autocomplete { background-position: 100% 4px; /* LTR */ } -.js input.throbbing { +.js input.form-autocomplete.ui-autocomplete-loading { background-position: 100% -16px; /* LTR */ } diff --git a/core/themes/seven/style.css b/core/themes/seven/style.css index 80a5874..1b5976c 100644 --- a/core/themes/seven/style.css +++ b/core/themes/seven/style.css @@ -1,4 +1,3 @@ - /** * Generic elements. */ @@ -554,7 +553,6 @@ div.teaser-checkbox .form-item, .form-item label.option input { vertical-align: middle; } -.form-disabled input.form-autocomplete, .form-disabled input.form-text, .form-disabled input.form-tel, .form-disabled input.form-email, @@ -645,7 +643,6 @@ input.form-button-disabled:active { text-shadow: none; color: #999; } -input.form-autocomplete, input.form-text, input.form-tel, input.form-email, @@ -681,12 +678,6 @@ select.form-select:focus { box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(220, 220, 220, 0.4); outline-color: rgba(0, 116, 189, 0.5); } -.js input.form-autocomplete { - background-position: 100% 4px; -} -.js input.throbbing { - background-position: 100% -16px; -} ul.action-links { margin: 1em 0; padding: 0 20px 0 20px; /* LTR */