From 2f4ffd5d86aa695afaffed9c6780f695d0bfde9d Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Tue, 10 Oct 2023 15:18:12 +0100 Subject: Improve group selector styling to use Select2 --- public/js/modules/group-linker.js | 90 +++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 22 deletions(-) (limited to 'public/js/modules/group-linker.js') diff --git a/public/js/modules/group-linker.js b/public/js/modules/group-linker.js index ca5e159..a21fec3 100644 --- a/public/js/modules/group-linker.js +++ b/public/js/modules/group-linker.js @@ -5,23 +5,87 @@ function eventGroupLinker() { eventGroupEditToken: "", groups: [], }, + manualGroupInputVisible: false, + eventGroupOptionTemplate(state) { + if (!state.id) { + return state.text; + } + if (!this.data.groups.length) { + return state.text; + } + const group = this.data.groups.find( + (group) => group.id === state.id, + ); + if (!group) { + return state.text; + } + const template = ` + + +
+ ${group.name} +

${group.description}

+
+
`; + return $(template); + }, async init() { + this.select2 = $(this.$refs.eventGroupSelect).select2({ + placeholder: "No group selected", + templateResult: this.eventGroupOptionTemplate.bind(this), + templateSelection: this.eventGroupOptionTemplate.bind(this), + selectionCssClass: "group-select-dropdown", + }); + this.select2.on("select2:select", (event) => { + this.selectGroup(event); + }); + this.select2.on("select2:unselect", () => { + this.data.eventGroupID = ""; + this.data.eventGroupEditToken = ""; + }); this.$watch("data.eventGroupID", () => { this.$dispatch( "event-group-id-changed", this.data.eventGroupID, ); + const matchingGroup = this.data.groups.find( + (group) => + group.id === this.data.eventGroupID && + group.editToken === this.data.eventGroupEditToken, + ); + if (matchingGroup) { + this.select2.val(matchingGroup.id).trigger("change"); + } else { + this.resetGroupSelector(); + } }); this.$watch("data.eventGroupEditToken", () => { this.$dispatch( "event-group-edit-token-changed", this.data.eventGroupEditToken, ); + const matchingGroup = this.data.groups.find( + (group) => + group.id === this.data.eventGroupID && + group.editToken === this.data.eventGroupEditToken, + ); + if (matchingGroup) { + this.select2.val(matchingGroup.id).trigger("change"); + } else { + this.resetGroupSelector(); + } }); - if (window.eventData && window.eventData.eventGroupID !== "") { + this.$watch("data.groups", () => { + this.select2.val(this.data.eventGroupID).trigger("change"); + }); + if (window.eventData && !!window.eventData.eventGroupID) { this.data.eventGroupID = window.eventData.eventGroupID; } - if (window.eventData && window.eventGroupEditToken !== "") { + if (window.eventData && !!window.eventGroupEditToken) { this.data.eventGroupEditToken = window.eventData.eventGroupEditToken; } @@ -42,7 +106,7 @@ function eventGroupLinker() { if (!response.ok) { return; } - const json = await (await response).json(); + const json = await response.json(); this.data.groups = json; } catch (e) { return false; @@ -60,26 +124,8 @@ function eventGroupLinker() { this.data.eventGroupID = group.id; this.data.eventGroupEditToken = group.editToken; }, - showGroupPreview() { - return ( - this.data.eventGroupID !== "" && - this.data.groups.some( - (group) => - group.id === this.data.eventGroupID && - group.editToken === this.data.eventGroupEditToken, - ) - ); - }, - groupPreview() { - if (!this.showGroupPreview()) { - return {}; - } - return this.data.groups.find( - (group) => group.id === this.data.eventGroupID, - ); - }, resetGroupSelector() { - this.$refs.eventGroupSelect.value = ""; + this.select2.val(null).trigger("change"); }, }; } -- cgit v1.2.3