Skip to content

Commit

Permalink
Convert autocomplete from CoffeeScript to Javascript
Browse files Browse the repository at this point in the history
Move the file also into the app/javascript folder.
  • Loading branch information
sascha-karnatz committed Aug 3, 2023
1 parent 3e63053 commit a379f29
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/alchemy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//= require handlebars
//= require alchemy/templates
//= require alchemy/alchemy.base
//= require alchemy/alchemy.autocomplete
//= require alchemy/alchemy.dialog
//= require alchemy/alchemy.char_counter
//= require alchemy/alchemy.confirm_dialog
Expand Down
30 changes: 0 additions & 30 deletions app/assets/javascripts/alchemy/alchemy.autocomplete.js.coffee

This file was deleted.

46 changes: 46 additions & 0 deletions app/javascript/alchemy_admin/autocomplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function createSearchChoice(term, data) {
if (
$(data).filter(function () {
return this.text.localeCompare(term) === 0
}).length === 0
) {
return {
id: term,
text: term
}
}
}

function initSelection(element, callback) {
const data = []
$(element.val().split(",")).each(function () {
data.push({
id: $.trim(this),
text: this
})
})
return callback(data)
}

export default function Autocomplete(scope) {
const field = $("[data-autocomplete]", scope)
const url = field.data("autocomplete")
field.select2({
tags: true,
tokenSeparators: [","],
minimumInputLength: 1,
openOnEnter: false,
createSearchChoice,
ajax: {
url,
dataType: "json",
data: (term, page) => {
return { term }
},
results: (data, page) => {
return { results: data }
}
},
initSelection
})
}
4 changes: 3 additions & 1 deletion app/javascript/alchemy_admin/gui.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Autocomplete from "alchemy_admin/autocomplete"

function init(scope) {
Alchemy.SelectBox(scope)
Alchemy.Datepicker(scope && scope.selector)
Expand All @@ -8,7 +10,7 @@ function init(scope) {
}
Alchemy.Hotkeys(scope)
Alchemy.ListFilter(scope)
Alchemy.Autocomplete.tags(scope)
Autocomplete(scope)
$("[data-alchemy-char-counter]", scope).each(function () {
new Alchemy.CharCounter(this)
})
Expand Down

0 comments on commit a379f29

Please sign in to comment.