From 03b2d3d1a461ffe46f94f2063d4005b0cbf1522b Mon Sep 17 00:00:00 2001 From: Ivymaster Date: Mon, 29 Jun 2026 14:02:17 +0200 Subject: [PATCH] extracted some code related to tags from component to helper --- .../Stv/Studentenverwaltung/List.js | 70 +++++++------------ .../tabulator/filters/extendedHeaderFilter.js | 55 +++++++++++++++ 2 files changed, 79 insertions(+), 46 deletions(-) diff --git a/public/js/components/Stv/Studentenverwaltung/List.js b/public/js/components/Stv/Studentenverwaltung/List.js index 34079dcf6..83629a8aa 100644 --- a/public/js/components/Stv/Studentenverwaltung/List.js +++ b/public/js/components/Stv/Studentenverwaltung/List.js @@ -3,9 +3,10 @@ import ListNew from "./List/New.js"; import CoreTag from "../../Tag/Tag.js"; import { buildTagHeaderFilterExpression, - buildTagOptionsFromRows, customTagFilter, setTagHeaderFilterValue, + syncSelectedTagOptionsWithHeaderFilters, + syncTagHeaderFilterOptions, tagHeaderFilter, } from "../../../tabulator/filters/extendedHeaderFilter.js"; import { @@ -438,8 +439,10 @@ export default { this.count = 0; this.allPrestudents = []; } - this.refreshTagHeaderFilterInitialOptions( + syncTagHeaderFilterOptions( Array.isArray(data) ? data : [], + this.tagFilterState.initialOptions, + this.tagFilterState.selectedOptions, ); }, }, @@ -447,7 +450,11 @@ export default { event: "dataFiltered", handler: (filters, rows) => { this.filteredcount = rows.length; - this.syncSelectedTagOptionsWithHeaderFilters(filters); + syncSelectedTagOptionsWithHeaderFilters( + filters, + this.tagFilterState.selectedOptions, + this.tagsEnabled, + ); }, }, { @@ -594,7 +601,6 @@ export default { this.$refs.table.tabulator, combinedFilterStatement, ); - this.$emit("filterActive", selectedOptions.length > 0); }, deep: true, }, @@ -866,38 +872,27 @@ export default { //methods tags addedTag(addedTag) { addTagInTable(addedTag, this.allRows, "prestudent_id"); - this.refreshTagHeaderFilterInitialOptions(); + syncTagHeaderFilterOptions( + this.$refs.table?.tabulator?.getData() || [], + this.tagFilterState.initialOptions, + this.tagFilterState.selectedOptions, + ); }, deletedTag(deletedTag) { deleteTagInTable(deletedTag, this.allRows); - this.refreshTagHeaderFilterInitialOptions(); + syncTagHeaderFilterOptions( + this.$refs.table?.tabulator?.getData() || [], + this.tagFilterState.initialOptions, + this.tagFilterState.selectedOptions, + ); }, updatedTag(updatedTag) { updateTagInTable(updatedTag, this.allRows); - this.refreshTagHeaderFilterInitialOptions(); - }, - refreshTagHeaderFilterInitialOptions(rows) { - const data = rows || this.$refs.table?.tabulator?.getData() || []; - - const options = buildTagOptionsFromRows(data); - this.tagFilterState.initialOptions.splice( - 0, - this.tagFilterState.initialOptions.length, - ...options, + syncTagHeaderFilterOptions( + this.$refs.table?.tabulator?.getData() || [], + this.tagFilterState.initialOptions, + this.tagFilterState.selectedOptions, ); - - const availableValues = new Set(options.map((option) => option.value)); - for ( - let i = this.tagFilterState.selectedOptions.length - 1; - i >= 0; - i-- - ) { - if ( - !availableValues.has(this.tagFilterState.selectedOptions[i].value) - ) { - this.tagFilterState.selectedOptions.splice(i, 1); - } - } }, getAllRows() { this.allRows = this.$refs.table.tabulator.getRows(); @@ -913,23 +908,6 @@ export default { this.tagFilterState.selectedOptions.length, ); }, - syncSelectedTagOptionsWithHeaderFilters(filters) { - if (!this.tagsEnabled || !this.tagFilterState.selectedOptions.length) { - return; - } - - const hasActiveTagFilter = filters.some( - (filter) => - filter.field === "tags" && - filter.value !== "" && - filter.value !== null && - filter.value !== undefined, - ); - - if (!hasActiveTagFilter) { - this.clearSelectedTagHeaderFilters(); - } - }, handleHeaderFilter(filterActive) { this.headerFilterActive = filterActive; }, diff --git a/public/js/tabulator/filters/extendedHeaderFilter.js b/public/js/tabulator/filters/extendedHeaderFilter.js index cbbc13875..6e58758a2 100644 --- a/public/js/tabulator/filters/extendedHeaderFilter.js +++ b/public/js/tabulator/filters/extendedHeaderFilter.js @@ -82,6 +82,61 @@ export function buildTagOptionsFromRows(rows = []) { return tags; } +export function syncTagHeaderFilterOptions( + rows = [], + initialOptions = [], + selectedOptions = [], +) { + const options = buildTagOptionsFromRows(rows) || []; + + if (Array.isArray(initialOptions)) { + initialOptions.splice( + 0, + initialOptions.length, + ...options, + ); + } + + if (Array.isArray(selectedOptions)) { + const availableValues = new Set(options.map((option) => option.value)); + + for (let i = selectedOptions.length - 1; i >= 0; i--) { + if (!availableValues.has(selectedOptions[i].value)) { + selectedOptions.splice(i, 1); + } + } + } + + return options; +} + +export function syncSelectedTagOptionsWithHeaderFilters( + filters = [], + selectedOptions = [], + tagsEnabled = true, + field = "tags", +) { + if (!tagsEnabled || !Array.isArray(selectedOptions) || !selectedOptions.length) { + return false; + } + + const hasActiveTagFilter = filters.some( + (filter) => + filter.field === field && + filter.value !== "" && + filter.value !== null && + filter.value !== undefined, + ); + + if (hasActiveTagFilter) { + return false; + } + + selectedOptions.splice(0, selectedOptions.length); + + return true; +} + export function getRawTagValuesFromRows(rows = []) { let tags = [];