var baseUrl = 'https://live.webb.uu.se/completers-all?q=%QUERY'; function addAutoComplete(inputId, outputSelector, name, langCode, extra) { if (typeof Bloodhound == 'undefined') { console.error("Can not add autocomplete for " + inputId + ". Bloodhound is missing. (It always is in decorated, see the component Typeahead Bloodhound.)"); return; } if (typeof accessibleAutocomplete == 'undefined') { console.error("Can not add autocomplete for " + inputId + ". The function accessibleAutocomplete is missing."); return; } jQuery(outputSelector + ' .search-progressive-input').remove(); var suggestionUrl = baseUrl + '&language_code=' + langCode; if (extra && extra.searchParams) { if (extra.searchParams.collection) { suggestionUrl += '&collection=' + extra.searchParams.collection; } if (extra.searchParams.type) { suggestionUrl += '&type=' + extra.searchParams.type; } if (extra.searchParams.subdomain) { suggestionUrl += '&subdomain=' + extra.searchParams.subdomain; } if (extra.searchParams.facetsQuery) { suggestionUrl += '&' + extra.searchParams.facetsQuery.replaceAll('&', '&'); } if (extra.searchParams.educations_category_code_filter) { suggestionUrl += '&educations_category_code_filter=' + extra.searchParams.educations_category_code_filter; } else if (extra.searchParams.type === "1.educations") { suggestionUrl += '&educations_category_code_filter=na&educations_category_code_filter=ib&educations_category_code_filter=im'; } if (extra.searchParams.educations_organisation_code_filter) { var organisation_codes = extra.searchParams.educations_organisation_code_filter.split(","); for (var i = 0; i < organisation_codes.length; i++) { suggestionUrl += '&educations_organisation_code_filter=' + organisation_codes[i]; } } if (extra.searchParams.limit) { suggestionUrl += '&hits=' + (extra.searchParams.limit + 5); } } var completer = new Bloodhound({ datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.value); }, queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: suggestionUrl, filter: function (list) { var labels = jQuery.map(list.suggestions, function (word) { return word.label; }); var filteredLabels = labels.filter(function(label) { var queryStringArray = list.queryString.split(" "); for (i = 0; i < queryStringArray.length; i++) { if (queryStringArray[i]) { var regexp = new RegExp(queryStringArray[i], 'ig'); if (!regexp.test(label)) return false; } } return true; }); return filteredLabels.slice(0, extra.searchParams.limit); }, prepare: function(query, settings) { settings.url = settings.url.replace('%QUERY', query.trim()); settings.dataType = 'json'; return settings; } } }); completer.initialize().then(function() { var autocompleteOptions = { element: document.querySelector(outputSelector), id: inputId, autoselect: false, confirmOnBlur: false, onConfirm: function() { var inputElement = document.querySelector('#' + inputId); setTimeout(function() { inputElement.form.submit(); }, 1); }, displayMenu: 'overlay', name: name, experimentalAllowAnyInput: true, source: function(query, callback) { return completer.ttAdapter()(query, callback, callback); }, placeholder: extra ? extra.placeholder : undefined, defaultValue: extra ? extra.defaultValue : undefined, showNoOptionsFound: false, minLength: 2, templates: { inputValue: function(suggestion) { return suggestion }, suggestion: function(suggestion) { var html = suggestion; var inputElement = document.querySelector('#' + inputId); if (inputElement) { var currentSearchQuery = inputElement.value; if (currentSearchQuery) { var currentSearchQueryArray = currentSearchQuery.split(" "); for (i = 0; i < currentSearchQueryArray.length; i++) { if (currentSearchQueryArray[i]) html = html.replace(new RegExp('(' + currentSearchQueryArray[i] + ')', 'ig'), '<<<$1>>>'); } html = html.replaceAll('<<<', '<strong>'); html = html.replaceAll('>>>', '</strong>'); } } return html; } } }; if (langCode == 'sv') { autocompleteOptions.tStatusQueryTooShort = function (minQueryLength) { return 'Skriv in minst ' + minQueryLength + ' tecken för att välja en sökning.'; }; autocompleteOptions.tStatusNoResults = function () { return 'Inga sökförslag.'; }; autocompleteOptions.tStatusSelectedOption = function (selectedOption, length, index) { return selectedOption + ' (' + (index + 1) + ' av ' + length + ') är markerad.'; }; autocompleteOptions.tStatusResults = function (length, contentSelectedOption) { return length + ' sökförslag finns. ' + contentSelectedOption; }; autocompleteOptions.tNoResults = function () { return 'Inga sökförslag'; }; autocompleteOptions.tAssistiveHint = function() { return 'När det finns sökförslag, använd up- och ner-pilarna för att välja ett alternativ. Använd enter för att välja ett av dessa. Användare med pekskärmar, svep och peka för att välja ett alternativ.';; }; } accessibleAutocomplete(autocompleteOptions); }); }