diff --git a/app/assets/javascripts/alchemy/admin.js b/app/assets/javascripts/alchemy/admin.js index 718079aee3..43484bdaec 100644 --- a/app/assets/javascripts/alchemy/admin.js +++ b/app/assets/javascripts/alchemy/admin.js @@ -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 diff --git a/app/assets/javascripts/alchemy/alchemy.autocomplete.js.coffee b/app/assets/javascripts/alchemy/alchemy.autocomplete.js.coffee deleted file mode 100644 index 3444c991eb..0000000000 --- a/app/assets/javascripts/alchemy/alchemy.autocomplete.js.coffee +++ /dev/null @@ -1,30 +0,0 @@ -window.Alchemy = {} if typeof(window.Alchemy) is 'undefined' - -Alchemy.Autocomplete = - - tags: (scope) -> - field = $('[data-autocomplete]', scope) - url = field.data('autocomplete') - field.select2 - tags: true - tokenSeparators: [","] - minimumInputLength: 1 - openOnEnter: false - createSearchChoice: @_createResultItem - ajax: - url: url - dataType: 'json' - data: (term, page) -> term: term - results: (data, page) -> results: data - initSelection: @_initializeSelection - - _createResultItem: (term, data) -> - if $(data).filter(-> @text.localeCompare(term) is 0).length is 0 - id: term - text: term - - _initializeSelection: (element, callback) -> - data = [] - $(element.val().split(",")).each -> - data.push id: $.trim(this), text: this - callback(data) diff --git a/app/javascript/alchemy_admin/autocomplete.js b/app/javascript/alchemy_admin/autocomplete.js new file mode 100644 index 0000000000..d6fe0f59e0 --- /dev/null +++ b/app/javascript/alchemy_admin/autocomplete.js @@ -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 + }) +} diff --git a/app/javascript/alchemy_admin/gui.js b/app/javascript/alchemy_admin/gui.js index 9d4af85334..dac86af115 100644 --- a/app/javascript/alchemy_admin/gui.js +++ b/app/javascript/alchemy_admin/gui.js @@ -1,3 +1,5 @@ +import Autocomplete from "alchemy_admin/autocomplete" + function init(scope) { Alchemy.SelectBox(scope) Alchemy.Datepicker(scope && scope.selector) @@ -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) })