|
1 | | -import { fetchAutocompleteJSON } from 'spotlight/admin/search_typeahead'; |
| 1 | +import { fetchAutocompleteJSON } from "spotlight/admin/search_typeahead" |
2 | 2 |
|
3 | | -(function ($){ |
| 3 | +;(function () { |
4 | 4 | SirTrevor.BlockMixins.Autocompleteable = { |
5 | 5 | mixinName: "Autocompleteable", |
6 | 6 | preload: true, |
7 | 7 |
|
8 | | - initializeAutocompleteable: function() { |
9 | | - this.on("onRender", this.addAutocompletetoSirTrevorForm); |
| 8 | + initializeAutocompleteable: function () { |
| 9 | + this.on("onRender", this.addAutocompletetoSirTrevorForm) |
10 | 10 |
|
11 | | - if (this['autocomplete_url'] === undefined) { |
12 | | - this.autocomplete_url = function() { return $('form[data-autocomplete-url]').data('autocomplete-url'); }; |
| 11 | + if (this["autocomplete_url"] === undefined) { |
| 12 | + this.autocomplete_url = function () { |
| 13 | + const form = document.querySelector("form[data-autocomplete-url]") |
| 14 | + return form ? form.dataset.autocompleteUrl : null |
| 15 | + } |
13 | 16 | } |
14 | 17 |
|
15 | | - if (this['autocomplete_fetch'] === undefined) { |
16 | | - this.autocomplete_fetch = this.fetchAutocompleteResults; |
| 18 | + if (this["autocomplete_fetch"] === undefined) { |
| 19 | + this.autocomplete_fetch = this.fetchAutocompleteResults |
17 | 20 | } |
18 | 21 |
|
19 | | - if (this['transform_autocomplete_results'] === undefined) { |
20 | | - this.transform_autocomplete_results = (val) => val |
| 22 | + if (this["transform_autocomplete_results"] === undefined) { |
| 23 | + this.transform_autocomplete_results = val => val |
21 | 24 | } |
22 | 25 |
|
23 | | - if (this['highlight'] === undefined) { |
24 | | - this.highlight = function(value) { |
25 | | - if (!value) return ''; |
26 | | - const queryValue = this.getQueryValue().trim(); |
27 | | - return queryValue ? value.replace(new RegExp(queryValue, 'gi'), '<strong>$&</strong>') : value; |
| 26 | + if (this["highlight"] === undefined) { |
| 27 | + this.highlight = function (value) { |
| 28 | + if (!value) return "" |
| 29 | + const queryValue = this.getQueryValue().trim() |
| 30 | + return queryValue |
| 31 | + ? value.replace(new RegExp(queryValue, "gi"), "<strong>$&</strong>") |
| 32 | + : value |
28 | 33 | } |
29 | 34 | } |
30 | 35 |
|
31 | | - if (this['autocomplete_control'] === undefined) { |
32 | | - this.autocomplete_control = function() { |
33 | | - const autocompleteID = this.autocompleteID(); |
| 36 | + if (this["autocomplete_control"] === undefined) { |
| 37 | + this.autocomplete_control = function () { |
| 38 | + const autocompleteID = this.autocompleteID() |
34 | 39 | return ` |
35 | 40 | <auto-complete src="${this.autocomplete_url()}" for="${autocompleteID}-popup" fetch-on-empty> |
36 | 41 | <input type="text" name="${autocompleteID}" placeholder="${i18n.t("blocks:autocompleteable:placeholder")}" data-default-typeahead> |
37 | 42 | <ul id="${autocompleteID}-popup"></ul> |
38 | 43 | <div id="${autocompleteID}-popup-feedback" class="visually-hidden"></div> |
39 | 44 | </auto-complete> |
40 | | - ` }; |
| 45 | + ` |
| 46 | + } |
41 | 47 | } |
42 | 48 |
|
43 | | - if (this['autocomplete_element_template'] === undefined) { |
44 | | - this.autocomplete_element_template = function(item) { |
| 49 | + if (this["autocomplete_element_template"] === undefined) { |
| 50 | + this.autocomplete_element_template = function (item) { |
45 | 51 | return `<li role="option" data-autocomplete-value="${item.id}">${this.autocomplete_template(item)}</li>` |
46 | 52 | } |
47 | 53 | } |
48 | 54 | }, |
49 | 55 |
|
50 | | - queryTokenizer: function(query) { |
51 | | - return query.trim().toLowerCase().split(/\s+/).filter(Boolean); |
| 56 | + queryTokenizer: function (query) { |
| 57 | + return query.trim().toLowerCase().split(/\s+/).filter(Boolean) |
52 | 58 | }, |
53 | 59 |
|
54 | | - filterResults: function(data, query) { |
55 | | - const queryStrings = this.queryTokenizer(query); |
| 60 | + filterResults: function (data, query) { |
| 61 | + const queryStrings = this.queryTokenizer(query) |
56 | 62 | return data.filter(item => { |
57 | | - const lowerTitle = item.title.toLowerCase(); |
58 | | - return queryStrings.some(queryString => lowerTitle.includes(queryString)); |
59 | | - }); |
| 63 | + const lowerTitle = item.title.toLowerCase() |
| 64 | + return queryStrings.some(queryString => |
| 65 | + lowerTitle.includes(queryString) |
| 66 | + ) |
| 67 | + }) |
60 | 68 | }, |
61 | 69 |
|
62 | | - fetchAutocompleteResults: async function(url) { |
63 | | - const result = await fetchAutocompleteJSON(url); |
64 | | - const transformed = this.transform_autocomplete_results(result); |
65 | | - this.fetchedData = {}; |
66 | | - transformed.map(item => this.fetchedData[item.id] = item); |
67 | | - return transformed.map(item => this.autocomplete_element_template(item)).join(''); |
| 70 | + fetchAutocompleteResults: async function (url) { |
| 71 | + const result = await fetchAutocompleteJSON(url) |
| 72 | + const transformed = this.transform_autocomplete_results(result) |
| 73 | + this.fetchedData = {} |
| 74 | + transformed.map(item => (this.fetchedData[item.id] = item)) |
| 75 | + return transformed |
| 76 | + .map(item => this.autocomplete_element_template(item)) |
| 77 | + .join("") |
68 | 78 | }, |
69 | 79 |
|
70 | | - fetchOnceAndFilterLocalResults: async function(url) { |
| 80 | + fetchOnceAndFilterLocalResults: async function (url) { |
71 | 81 | if (this.fetchedData === undefined) { |
72 | | - await this.fetchAutocompleteResults(url); |
| 82 | + await this.fetchAutocompleteResults(url) |
73 | 83 | } |
74 | | - const query = url.searchParams.get('q'); |
75 | | - const data = Object.values(this.fetchedData); |
76 | | - const filteredData = query ? this.filterResults(data, query) : data; |
77 | | - return filteredData.map(item => this.autocomplete_element_template(item)).join(''); |
| 84 | + const query = url.searchParams.get("q") |
| 85 | + const data = Object.values(this.fetchedData) |
| 86 | + const filteredData = query ? this.filterResults(data, query) : data |
| 87 | + return filteredData |
| 88 | + .map(item => this.autocomplete_element_template(item)) |
| 89 | + .join("") |
78 | 90 | }, |
79 | 91 |
|
80 | | - autocompleteID: function() { |
81 | | - return this.blockID + '-autocomplete'; |
| 92 | + autocompleteID: function () { |
| 93 | + return this.blockID + "-autocomplete" |
82 | 94 | }, |
83 | 95 |
|
84 | | - getQueryValue: function() { |
85 | | - const completer = this.inner.querySelector("auto-complete > input"); |
86 | | - return completer.value; |
| 96 | + getQueryValue: function () { |
| 97 | + const completer = this.inner.querySelector("auto-complete > input") |
| 98 | + return completer.value |
87 | 99 | }, |
88 | 100 |
|
89 | | - addAutocompletetoSirTrevorForm: function() { |
90 | | - const completer = this.inner.querySelector("auto-complete"); |
91 | | - completer.fetchResult = this.autocomplete_fetch.bind(this); |
92 | | - completer.addEventListener('auto-complete-change', (e) => { |
93 | | - const data = this.fetchedData[e.relatedTarget.value]; |
| 101 | + addAutocompletetoSirTrevorForm: function () { |
| 102 | + const completer = this.inner.querySelector("auto-complete") |
| 103 | + completer.fetchResult = this.autocomplete_fetch.bind(this) |
| 104 | + completer.addEventListener("auto-complete-change", e => { |
| 105 | + const data = this.fetchedData[e.relatedTarget.value] |
94 | 106 | if (e.relatedTarget.value && data) { |
95 | | - e.value = e.relatedTarget.value = ''; |
96 | | - this.createItemPanel({ ...data, display: "true" }); |
| 107 | + e.value = e.relatedTarget.value = "" |
| 108 | + this.createItemPanel({ ...data, display: "true" }) |
97 | 109 | } |
98 | | - }); |
99 | | - }, |
100 | | - }, |
101 | | - |
| 110 | + }) |
| 111 | + } |
| 112 | + } |
102 | 113 |
|
103 | | - SirTrevor.Block.prototype.availableMixins.push("autocompleteable"); |
104 | | -})(jQuery); |
| 114 | + SirTrevor.Block.prototype.availableMixins.push("autocompleteable") |
| 115 | +})() |
0 commit comments