comparison core/misc/autocomplete.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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
8 (function ($, Drupal) {
9 var autocomplete = void 0;
10
11 function autocompleteSplitValues(value) {
12 var result = [];
13 var quote = false;
14 var current = '';
15 var valueLength = value.length;
16 var character = void 0;
17
18 for (var i = 0; i < valueLength; i++) {
19 character = value.charAt(i);
20 if (character === '"') {
21 current += character;
22 quote = !quote;
23 } else if (character === ',' && !quote) {
24 result.push(current.trim());
25 current = '';
26 } else {
27 current += character;
28 }
29 }
30 if (value.length > 0) {
31 result.push($.trim(current));
32 }
33
34 return result;
35 }
36
37 function extractLastTerm(terms) {
38 return autocomplete.splitValues(terms).pop();
39 }
40
41 function searchHandler(event) {
42 var options = autocomplete.options;
43
44 if (options.isComposing) {
45 return false;
46 }
47
48 var term = autocomplete.extractLastTerm(event.target.value);
49
50 if (term.length > 0 && options.firstCharacterBlacklist.indexOf(term[0]) !== -1) {
51 return false;
52 }
53
54 return term.length >= options.minLength;
55 }
56
57 function sourceData(request, response) {
58 var elementId = this.element.attr('id');
59
60 if (!(elementId in autocomplete.cache)) {
61 autocomplete.cache[elementId] = {};
62 }
63
64 function showSuggestions(suggestions) {
65 var tagged = autocomplete.splitValues(request.term);
66 var il = tagged.length;
67 for (var i = 0; i < il; i++) {
68 var index = suggestions.indexOf(tagged[i]);
69 if (index >= 0) {
70 suggestions.splice(index, 1);
71 }
72 }
73 response(suggestions);
74 }
75
76 function sourceCallbackHandler(data) {
77 autocomplete.cache[elementId][term] = data;
78
79 showSuggestions(data);
80 }
81
82 var term = autocomplete.extractLastTerm(request.term);
83
84 if (autocomplete.cache[elementId].hasOwnProperty(term)) {
85 showSuggestions(autocomplete.cache[elementId][term]);
86 } else {
87 var options = $.extend({ success: sourceCallbackHandler, data: { q: term } }, autocomplete.ajax);
88 $.ajax(this.element.attr('data-autocomplete-path'), options);
89 }
90 }
91
92 function focusHandler() {
93 return false;
94 }
95
96 function selectHandler(event, ui) {
97 var terms = autocomplete.splitValues(event.target.value);
98
99 terms.pop();
100
101 terms.push(ui.item.value);
102
103 event.target.value = terms.join(', ');
104
105 return false;
106 }
107
108 function renderItem(ul, item) {
109 return $('<li>').append($('<a>').html(item.label)).appendTo(ul);
110 }
111
112 Drupal.behaviors.autocomplete = {
113 attach: function attach(context) {
114 var $autocomplete = $(context).find('input.form-autocomplete').once('autocomplete');
115 if ($autocomplete.length) {
116 var blacklist = $autocomplete.attr('data-autocomplete-first-character-blacklist');
117 $.extend(autocomplete.options, {
118 firstCharacterBlacklist: blacklist || ''
119 });
120
121 $autocomplete.autocomplete(autocomplete.options).each(function () {
122 $(this).data('ui-autocomplete')._renderItem = autocomplete.options.renderItem;
123 });
124
125 $autocomplete.on('compositionstart.autocomplete', function () {
126 autocomplete.options.isComposing = true;
127 });
128 $autocomplete.on('compositionend.autocomplete', function () {
129 autocomplete.options.isComposing = false;
130 });
131 }
132 },
133 detach: function detach(context, settings, trigger) {
134 if (trigger === 'unload') {
135 $(context).find('input.form-autocomplete').removeOnce('autocomplete').autocomplete('destroy');
136 }
137 }
138 };
139
140 autocomplete = {
141 cache: {},
142
143 splitValues: autocompleteSplitValues,
144 extractLastTerm: extractLastTerm,
145
146 options: {
147 source: sourceData,
148 focus: focusHandler,
149 search: searchHandler,
150 select: selectHandler,
151 renderItem: renderItem,
152 minLength: 1,
153
154 firstCharacterBlacklist: '',
155
156 isComposing: false
157 },
158 ajax: {
159 dataType: 'json'
160 }
161 };
162
163 Drupal.autocomplete = autocomplete;
164 })(jQuery, Drupal);