Annotate depth for recursive hierarchies

This commit is contained in:
Jeremy Stretch 2024-02-07 14:41:12 -05:00
parent 6926b3709e
commit 50f995e7ba
4 changed files with 20 additions and 4 deletions

Binary file not shown.

Binary file not shown.

View File

@ -34,8 +34,8 @@ export class DynamicTomSelect extends TomSelect {
// Set the null option (if any) // Set the null option (if any)
const nullOption = this.input.getAttribute('data-null-option'); const nullOption = this.input.getAttribute('data-null-option');
if (nullOption) { if (nullOption) {
let valueField = user_settings.valueField || 'value'; let valueField = this.settings.valueField;
let labelField = user_settings.labelField || 'text'; let labelField = this.settings.labelField;
this.nullOption = {} this.nullOption = {}
this.nullOption[valueField] = 'null'; this.nullOption[valueField] = 'null';
this.nullOption[labelField] = nullOption; this.nullOption[labelField] = nullOption;

View File

@ -1,6 +1,19 @@
import { getElements } from '../util'; import { getElements } from '../util';
import { DynamicTomSelect } from './classes/dynamicTomSelect' import { DynamicTomSelect } from './classes/dynamicTomSelect'
const VALUE_FIELD = 'id';
const LABEL_FIELD = 'display';
// Render the HTML for a dropdown option
function renderOption(data: any, escape: Function) {
// If the object has a `_depth` property, indent its display text
if (typeof data._depth === 'number' && data._depth > 0) {
return `<div>${'─'.repeat(data._depth)} ${escape(data[LABEL_FIELD])}</div>`;
}
return `<div>${escape(data[LABEL_FIELD])}</div>`;
}
// Initialize <select> elements which are populated via a REST API call // Initialize <select> elements which are populated via a REST API call
export function initDynamicSelects(): void { export function initDynamicSelects(): void {
@ -8,8 +21,8 @@ export function initDynamicSelects(): void {
for (const select of getElements<HTMLSelectElement>('select.api-select')) { for (const select of getElements<HTMLSelectElement>('select.api-select')) {
new DynamicTomSelect(select, { new DynamicTomSelect(select, {
plugins: ['clear_button'], plugins: ['clear_button'],
valueField: 'id', valueField: VALUE_FIELD,
labelField: 'display', labelField: LABEL_FIELD,
searchField: [], searchField: [],
disabledField: select.getAttribute('disabled-indicator') || undefined, disabledField: select.getAttribute('disabled-indicator') || undefined,
copyClassesToDropdown: false, copyClassesToDropdown: false,
@ -17,6 +30,9 @@ export function initDynamicSelects(): void {
controlInput: '<input>', controlInput: '<input>',
preload: 'focus', preload: 'focus',
maxOptions: 100, maxOptions: 100,
render: {
option: renderOption
}
}); });
} }