autocomplete.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. var autocomplete = void 0;
  9. function autocompleteSplitValues(value) {
  10. var result = [];
  11. var quote = false;
  12. var current = '';
  13. var valueLength = value.length;
  14. var character = void 0;
  15. for (var i = 0; i < valueLength; i++) {
  16. character = value.charAt(i);
  17. if (character === '"') {
  18. current += character;
  19. quote = !quote;
  20. } else if (character === ',' && !quote) {
  21. result.push(current.trim());
  22. current = '';
  23. } else {
  24. current += character;
  25. }
  26. }
  27. if (value.length > 0) {
  28. result.push($.trim(current));
  29. }
  30. return result;
  31. }
  32. function extractLastTerm(terms) {
  33. return autocomplete.splitValues(terms).pop();
  34. }
  35. function searchHandler(event) {
  36. var options = autocomplete.options;
  37. if (options.isComposing) {
  38. return false;
  39. }
  40. var term = autocomplete.extractLastTerm(event.target.value);
  41. if (term.length > 0 && options.firstCharacterBlacklist.indexOf(term[0]) !== -1) {
  42. return false;
  43. }
  44. return term.length >= options.minLength;
  45. }
  46. function sourceData(request, response) {
  47. var elementId = this.element.attr('id');
  48. if (!(elementId in autocomplete.cache)) {
  49. autocomplete.cache[elementId] = {};
  50. }
  51. function showSuggestions(suggestions) {
  52. var tagged = autocomplete.splitValues(request.term);
  53. var il = tagged.length;
  54. for (var i = 0; i < il; i++) {
  55. var index = suggestions.indexOf(tagged[i]);
  56. if (index >= 0) {
  57. suggestions.splice(index, 1);
  58. }
  59. }
  60. response(suggestions);
  61. }
  62. var term = autocomplete.extractLastTerm(request.term);
  63. function sourceCallbackHandler(data) {
  64. autocomplete.cache[elementId][term] = data;
  65. showSuggestions(data);
  66. }
  67. if (autocomplete.cache[elementId].hasOwnProperty(term)) {
  68. showSuggestions(autocomplete.cache[elementId][term]);
  69. } else {
  70. var options = $.extend({ success: sourceCallbackHandler, data: { q: term } }, autocomplete.ajax);
  71. $.ajax(this.element.attr('data-autocomplete-path'), options);
  72. }
  73. }
  74. function focusHandler() {
  75. return false;
  76. }
  77. function selectHandler(event, ui) {
  78. var terms = autocomplete.splitValues(event.target.value);
  79. terms.pop();
  80. terms.push(ui.item.value);
  81. event.target.value = terms.join(', ');
  82. return false;
  83. }
  84. function renderItem(ul, item) {
  85. return $('<li>').append($('<a>').html(item.label)).appendTo(ul);
  86. }
  87. Drupal.behaviors.autocomplete = {
  88. attach: function attach(context) {
  89. var $autocomplete = $(context).find('input.form-autocomplete').once('autocomplete');
  90. if ($autocomplete.length) {
  91. var blacklist = $autocomplete.attr('data-autocomplete-first-character-blacklist');
  92. $.extend(autocomplete.options, {
  93. firstCharacterBlacklist: blacklist || ''
  94. });
  95. $autocomplete.autocomplete(autocomplete.options).each(function () {
  96. $(this).data('ui-autocomplete')._renderItem = autocomplete.options.renderItem;
  97. });
  98. $autocomplete.on('compositionstart.autocomplete', function () {
  99. autocomplete.options.isComposing = true;
  100. });
  101. $autocomplete.on('compositionend.autocomplete', function () {
  102. autocomplete.options.isComposing = false;
  103. });
  104. }
  105. },
  106. detach: function detach(context, settings, trigger) {
  107. if (trigger === 'unload') {
  108. $(context).find('input.form-autocomplete').removeOnce('autocomplete').autocomplete('destroy');
  109. }
  110. }
  111. };
  112. autocomplete = {
  113. cache: {},
  114. splitValues: autocompleteSplitValues,
  115. extractLastTerm: extractLastTerm,
  116. options: {
  117. source: sourceData,
  118. focus: focusHandler,
  119. search: searchHandler,
  120. select: selectHandler,
  121. renderItem: renderItem,
  122. minLength: 1,
  123. firstCharacterBlacklist: '',
  124. isComposing: false
  125. },
  126. ajax: {
  127. dataType: 'json'
  128. }
  129. };
  130. Drupal.autocomplete = autocomplete;
  131. })(jQuery, Drupal);