2
0
mirror of https://github.com/moebooru/moebooru synced 2025-08-28 20:47:42 +00:00

Javascript Autocomplete (cleanup)

This commit is contained in:
nanaya 2025-01-21 02:37:04 +09:00
parent 6dfa49262a
commit 55fc74a0fa

View File

@ -1,77 +1,58 @@
var $, Autocomplete, autocompleterMap;
import autocompleter from 'autocompleter'; import autocompleter from 'autocompleter';
import TagCompletion from './tag_completion';
import TagCompletionBox from './tag_completion_box'; import TagCompletionBox from './tag_completion_box';
$ = jQuery; const $ = window.jQuery;
autocompleterMap = (match) => { function autocompleterMap (match) {
return { return {
label: match, label: match,
value: match value: match
}; };
}; }
export default Autocomplete = class Autocomplete { export default class Autocomplete {
constructor (tagCompletionInstance) { constructor (tagCompletionInstance) {
this._genericCompletion = this._genericCompletion.bind(this);
this._genericCompletionAll = this._genericCompletionAll.bind(this);
this._tagCompletion = this._tagCompletion.bind(this);
this.tagCompletionInstance = tagCompletionInstance; this.tagCompletionInstance = tagCompletionInstance;
$(() => { $(() => {
this._genericCompletionAll(); this.genericCompletionAll();
return this._tagCompletion(); this.tagCompletion();
}); });
} }
_genericCompletion(input) { genericCompletion (input) {
var url; const url = input.dataset.autocomplete;
url = input.dataset.autocomplete;
return autocompleter({ autocompleter({
input: input, input,
fetch: (text, update) => { fetch: (text, update) => {
return $.ajax(url, { $.ajax(url, {
data: { data: { term: text },
term: text
},
dataType: 'json' dataType: 'json'
}).done((matches) => { }).done((matches) => {
return update(matches.map(autocompleterMap)); update(matches.map(autocompleterMap));
}); });
}, },
onSelect: (match) => { onSelect: (match) => {
return input.value = match.value; input.value = match.value;
}, },
preventSubmit: 2 // OnSelect preventSubmit: 2 // OnSelect
}); });
} }
_genericCompletionAll() { genericCompletionAll () {
var i, input, len, ref, results; for (const input of document.querySelectorAll('[data-autocomplete]')) {
ref = document.querySelectorAll('[data-autocomplete]'); this.genericCompletion(input);
results = [];
for (i = 0, len = ref.length; i < len; i++) {
input = ref[i];
results.push(this._genericCompletion(input));
} }
return results;
} }
_tagCompletion() { tagCompletion () {
var editForm, tags; const editForm = document.querySelector('#edit-form');
editForm = document.querySelector('#edit-form'); if (editForm == null) return;
if (editForm == null) {
return;
}
tags = document.querySelector('.ac-tags');
if (tags == null) {
return;
}
new TagCompletionBox(tags);
return this.tagCompletionInstance.observe_tag_changes_on_submit(editForm, tags);
}
}; const tags = document.querySelector('.ac-tags');
if (tags == null) return;
new TagCompletionBox(tags); // eslint-disable-line no-new
this.tagCompletionInstance.observe_tag_changes_on_submit(editForm, tags);
}
}