From 8f5099a85d22adc8c9c3b4249ec49d1a739c184b Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 30 Nov 2023 14:57:31 -0700 Subject: [PATCH] Select account: sort options --- api/js/etemplate/Et2Select/SearchMixin.ts | 2 +- .../etemplate/Et2Select/SelectAccountMixin.ts | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/api/js/etemplate/Et2Select/SearchMixin.ts b/api/js/etemplate/Et2Select/SearchMixin.ts index 4db9d90bba..a928316ff3 100644 --- a/api/js/etemplate/Et2Select/SearchMixin.ts +++ b/api/js/etemplate/Et2Select/SearchMixin.ts @@ -455,7 +455,7 @@ export const Et2WithSearchMixin = dedupeMixin( { await this.updateComplete; const moreCount = this._total_result_count - this.select?.querySelectorAll("sl-option.match").length; - if(this._total_result_count == 0 || moreCount == 0 || !this.select) + if(this._total_result_count <= 0 || moreCount == 0 || !this.select) { return nothing; } diff --git a/api/js/etemplate/Et2Select/SelectAccountMixin.ts b/api/js/etemplate/Et2Select/SelectAccountMixin.ts index 03b0756bf9..28cfa4813e 100644 --- a/api/js/etemplate/Et2Select/SelectAccountMixin.ts +++ b/api/js/etemplate/Et2Select/SelectAccountMixin.ts @@ -74,6 +74,14 @@ export const SelectAccountMixin = >(superclass } } + /** + * OVerridden to do nothing, we handle it differently in _find_options() + * @param {string} newValueElement + * @protected + */ + protected _missingOption(newValueElement : string) + {} + _find_options(val) { for(let id of val) @@ -100,6 +108,7 @@ export const SelectAccountMixin = >(superclass option.label = title || ''; this.requestUpdate(); + this.account_options.sort(this.optionSort); // Directly update if it's already there const slOption = this.select?.querySelector('[value="' + id + '"]'); if(slOption) @@ -111,6 +120,7 @@ export const SelectAccountMixin = >(superclass }); } } + this.account_options.sort(this.optionSort); } get value() @@ -121,13 +131,41 @@ export const SelectAccountMixin = >(superclass get select_options() { return [...new Map([...this.account_options, ...(super.select_options || [])].map(item => - [item.value, item])).values()]; + [item.value, item])).values()].sort(this.optionSort); } set select_options(value : SelectOption[]) { super.select_options = value; } + + /** + * Sort options + * @param a + * @param b + * @returns {number} + * @protected + */ + protected optionSort(a : SelectOption, b : SelectOption) + { + // Sort accounts before groups, then by label + let int_a = 0; + let int_b = 0; + if(typeof a.value === "string") + { + int_a = parseInt(a.value) ?? 0; + } + if(typeof b.value === "string") + { + int_b = parseInt(b.value) ?? 0; + } + if(int_a < 0 && int_b < 0 || int_a > 0 && int_b > 0) + { + return ('' + a.label).localeCompare(b.label); + } + // Accounts before groups + return int_b - int_a; + } } return SelectAccount as unknown as Constructor & T;