Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Centralize list-selector/autocomplete JS logic.

Kailash Nadhb7e3344416e0
diff --git a/static/admin/partials/list-selector.html b/static/admin/partials/list-selector.html
index 4a76f32b..0b0141e2 100644
--- a/static/admin/partials/list-selector.html
+++ b/static/admin/partials/list-selector.html
@@ -5,7 +5,7 @@
<label data-field>
{{ $root.L.T .Label }}
<ot-taginput x-model="{{ $model }}">
- <input list="{{ $listID }}" placeholder="{{ $root.L.T .Placeholder }}" oninput="_listAutocomplete(this)" onpointerdown="_listAutocomplete(this)" onfocus="_listAutocomplete(this)" />
+ <input list="{{ $listID }}" placeholder="{{ $root.L.T .Placeholder }}" @input="listAutocomplete($el)" @pointerdown="listAutocomplete($el)" @focus="listAutocomplete($el)" />
<datalist id="{{ $listID }}"></datalist>
</ot-taginput>
<small data-hint>{{ $root.L.T "subscribers.listsHelp" }}</small>
diff --git a/static/admin/src/js/main.js b/static/admin/src/js/main.js
index 0426f29f..735437a4 100644
--- a/static/admin/src/js/main.js
+++ b/static/admin/src/js/main.js
@@ -24,6 +24,7 @@ function setupAlpine() {
window.Alpine.data('adminApp', () => ({
isLoading,
copyToClipboard: u.copyToClipboard,
+ listAutocomplete,
}));
}
@@ -53,6 +54,34 @@ u.setI18n(i18n);
// ========
// Public functions.
+// ListTag is the object used in the <ot-taginput> list selector.
+export class ListTag {
+ constructor(list) {
+ Object.assign(this, list);
+ }
+
+ toString() {
+ return this.name;
+ }
+}
+
+// listAutocomplete populates the <ot-taginput> list selector's suggestions with lists.
+export function listAutocomplete(el) {
+ const ti = el.closest('ot-taginput');
+ const chosen = new Set((ti ? ti.value : []).map((l) => l.id));
+ const q = el.value.toLowerCase();
+
+ el.list.replaceChildren(...(window._lists || [])
+ .filter((l) => !chosen.has(l.id) && l.name.toLowerCase().includes(q))
+ // Cap suggestions (so focusing with an empty input still shows a usable list of items).
+ .slice(0, 10)
+ .map((l) => {
+ const o = new Option(l.name);
+ o.data = new ListTag(l);
+ return o;
+ }));
+}
+
// Returns true if the given named api() call is currently loading.
export function isLoading(name) {
return getStore().loading[name] === true;
diff --git a/static/admin/src/js/views/import.js b/static/admin/src/js/views/import.js
index 211cd64c..970005e4 100644
--- a/static/admin/src/js/views/import.js
+++ b/static/admin/src/js/views/import.js
@@ -1,36 +1,10 @@
import {
urls,
i18n,
+ ListTag,
} from '../main.js';
import * as u from '../utils.js';
-// ListTag is the object used in the <ot-taginput> list selector.
-class ListTag {
- constructor(list) {
- Object.assign(this, list);
- }
-
- toString() {
- return this.name;
- }
-}
-
-// _listAutocomplete populates the <ot-taginput> listselector's suggestions with lists.
-window._listAutocomplete = (el) => {
- const ti = el.closest('ot-taginput');
- const chosen = new Set((ti ? ti.value : []).map((l) => l.id));
- const q = el.value.toLowerCase();
-
- el.list.replaceChildren(...(window._lists || [])
- .filter((l) => !chosen.has(l.id) && l.name.toLowerCase().includes(q))
- .slice(0, 10)
- .map((l) => {
- const o = new Option(l.name);
- o.data = new ListTag(l);
- return o;
- }));
-};
-
// Regexp for splitting a log line into [timestamp] [file] [message].
// 2021/05/01 00:00:00:00 init.go:99: reading config: config.toml
const reLine = /^([0-9\s:/]+\.[0-9]{6}) (.+?\.go:[0-9]+|\*):\s(.+)$/;
diff --git a/static/admin/src/js/views/subscribers.js b/static/admin/src/js/views/subscribers.js
index fd869fcd..384417c8 100644
--- a/static/admin/src/js/views/subscribers.js
+++ b/static/admin/src/js/views/subscribers.js
@@ -2,37 +2,10 @@ import {
api,
urls,
i18n,
+ ListTag,
} from '../main.js';
import * as u from '../utils.js';
-// ListTag is the object used in <ot-taginput> list selector.
-class ListTag {
- constructor(list) {
- Object.assign(this, list);
- }
-
- toString() {
- return this.name;
- }
-}
-
-// _listAutocomplete populates the <ot-taginput> listselector's suggestions with lists.
-window._listAutocomplete = (el) => {
- const ti = el.closest('ot-taginput');
- const chosen = new Set((ti ? ti.value : []).map((l) => l.id));
- const q = el.value.toLowerCase();
-
- el.list.replaceChildren(...(window._lists || [])
- .filter((l) => !chosen.has(l.id) && l.name.toLowerCase().includes(q))
- // Cap suggestions (so focusing with an empty input still shows a usable list of items).
- .slice(0, 10)
- .map((l) => {
- const o = new Option(l.name);
- o.data = new ListTag(l);
- return o;
- }));
-};
-
function component(sub = null) {
// The current view's filters are injected by the template.
const filters = window._filters || {};