| 1 | import { saveSettingsDebounced } from '../script.js'; |
| 2 | import { power_user } from './power-user.js'; |
| 3 | import { isValidUrl } from './utils.js'; |
| 4 | |
| 5 | /** |
| 6 | * @param {{ term: string; }} request |
| 7 | * @param {function} resolve |
| 8 | * @param {string} serverLabel |
| 9 | */ |
| 10 | function findServers(request, resolve, serverLabel) { |
| 11 | if (!power_user.servers) { |
| 12 | power_user.servers = []; |
| 13 | } |
| 14 | |
| 15 | const needle = request.term.toLowerCase(); |
| 16 | const result = power_user.servers.filter(x => x.label == serverLabel).sort((a, b) => b.lastConnection - a.lastConnection).map(x => x.url).slice(0, 5); |
| 17 | const hasExactMatch = result.findIndex(x => x.toLowerCase() == needle) !== -1; |
| 18 | |
| 19 | if (request.term && !hasExactMatch) { |
| 20 | result.unshift(request.term); |
| 21 | } |
| 22 | |
| 23 | resolve(result); |
| 24 | } |
| 25 | |
| 26 | function selectServer(event, ui, serverLabel) { |
| 27 | // unfocus the input |
| 28 | $(event.target).val(ui.item.value).trigger('input').trigger('blur'); |
| 29 | |
| 30 | $('[data-server-connect]').each(function () { |
| 31 | const serverLabels = String($(this).data('server-connect')).split(','); |
| 32 | |
| 33 | if (serverLabels.includes(serverLabel)) { |
| 34 | $(this).trigger('click'); |
| 35 | } |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | function createServerAutocomplete() { |
| 40 | const inputElement = $(this); |
| 41 | const serverLabel = inputElement.data('server-history'); |
| 42 | |
| 43 | inputElement |
| 44 | .autocomplete({ |
| 45 | source: (i, o) => findServers(i, o, serverLabel), |
| 46 | select: (e, u) => selectServer(e, u, serverLabel), |
| 47 | minLength: 0, |
| 48 | }) |
| 49 | .on('focus', onInputFocus); // <== show tag list on click |
| 50 | } |
| 51 | |
| 52 | function onInputFocus() { |
| 53 | $(this).autocomplete('search', $(this).val()); |
| 54 | } |
| 55 | |
| 56 | function onServerConnectClick() { |
| 57 | const serverLabels = String($(this).data('server-connect')).split(','); |
| 58 | |
| 59 | serverLabels.forEach(serverLabel => { |
| 60 | if (!power_user.servers) { |
| 61 | power_user.servers = []; |
| 62 | } |
| 63 | |
| 64 | const value = String($(`[data-server-history="${serverLabel}"]`).val()).toLowerCase().trim(); |
| 65 | |
| 66 | // Don't save empty values or invalid URLs |
| 67 | if (!value || !isValidUrl(value)) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | const server = power_user.servers.find(x => x.url === value && x.label === serverLabel); |
| 72 | |
| 73 | if (!server) { |
| 74 | power_user.servers.push({ label: serverLabel, url: value, lastConnection: Date.now() }); |
| 75 | } else { |
| 76 | server.lastConnection = Date.now(); |
| 77 | } |
| 78 | |
| 79 | saveSettingsDebounced(); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | export function initServerHistory() { |
| 84 | $('[data-server-history]').each(createServerAutocomplete); |
| 85 | $(document).on('click', '[data-server-connect]', onServerConnectClick); |
| 86 | } |