All files / src/libs/EditableDataView/Components DropdownComponent.ts

0% Statements 0/63
0% Branches 0/18
0% Functions 0/20
0% Lines 0/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { Component } from 'obsidian';
import BaseComponent from './BaseComponent';
 
/**
 * Represents a dropdown component that extends the base component.
 */
export default class DropdownComponent extends BaseComponent {
    //#region base properties
    protected _isEditable = false;
    onEnableEditCallback: () => void;
    onDisableEditCallback: () => void;
    onSaveCallback: () => Promise<void>;
    onFirstEdit: () => void;
    onFinalize: () => void;
    //#endregion
    //#region extended properties
    private _onPresentation: (value: string) => Promise<void> | undefined;
    private _onSave: (value: string) => Promise<void>;
    private _value: string;
    private _options: { value: string; text: string }[];
    /**
     * Gets the selected option of the dropdown component.
     */
    private get _selectedOption(): { value: string; text: string } {
        const selectedOption = this._options.find(
            (o) => o.value === this._value,
        );
 
        Iif (selectedOption) return selectedOption;
 
        return { value: this._value, text: this._value };
    }
    private _title: string;
    //#endregion
    //#region HTML Elements
    private _presentationSpan: HTMLElement;
    private _select: HTMLSelectElement;
    //#endregion
 
    /**
     * Creates a new instance of the DropdownComponent class.
     * @param component The component to attach the dropdown to.
     */
    constructor(component: Component) {
        super(component);
        this.onFinalize = this.build;
        this.onFirstEdit = this.buildInput;
        this.onEnableEditCallback = this.enableEdit;
        this.onSaveCallback = this.save;
        this.onDisableEditCallback = this.disableEdit;
    }
 
    //#region Configuration methods
    /**
     * Enables the editability of the component.
     * @returns The component itself.
     */
    public enableEditability(): this {
        this._isEditable = true;
 
        return this;
    }
 
    /**
     * Sets the value of the component.
     * @param value The value to set.
     * @returns The component itself.
     */
    public setValue(value: string): this {
        this._value = value;
 
        return this;
    }
 
    /**
     * Sets the options of the component.
     * @param options The options to set.
     * @returns The component itself.
     */
    public setOptions(options: { value: string; text: string }[]): this {
        this._options = options;
 
        return this;
    }
 
    /**
     * Sets the title of the component.
     * @param title The title to set.
     * @returns The component itself.
     */
    public setTitle(title: string): this {
        this._title = title;
 
        return this;
    }
 
    /**
     * Sets the formator of the component.
     * @param formator The formator to set.
     * @returns The component itself.
     * @remarks - The formator is called when the component change in `not-edit` mode.
     * - `value` is the value of the selected option. (Not the text!)
     */
    public setFormator(
        formator: (value: string) => { text: string; html?: DocumentFragment },
    ): this {
        /**
         * Sets the presentation of the component.
         * @param value The value to set.
         */
        this._onPresentation = async (value: string): Promise<void> => {
            const { text, html } = formator(value);
 
            if (html) {
                this._presentationSpan.innerHTML = '';
                this._presentationSpan.appendChild(html);
            } else {
                this._presentationSpan.textContent = text;
            }
        };
 
        return this;
    }
 
    /**
     * Sets the saver of the component.
     * @param callback The saver to set.
     * @returns The component itself.
     * @remarks - The saver is called when the component save button is clicked.
     * - `value` is the value of the selected option. (Not the text!)
     */
    public onSave(callback: (value: string) => Promise<void>): this {
        this._onSave = callback;
 
        return this;
    }
    //#endregion
 
    /**
     * Enables the options of the dropdown component.
     */
    private enableOptions(): void {
        const optionFound = this._options.find((o) => o.value === this._value);
 
        Iif (!optionFound) {
            const optionElement = document.createElement('option');
            optionElement.value = this._value;
            optionElement.textContent = `${this._value} (not in options)`;
            this._select.appendChild(optionElement);
        }
 
        this._options.forEach((option) => {
            const optionElement = document.createElement('option');
            optionElement.value = option.value;
            optionElement.textContent = option.text;
            this._select.appendChild(optionElement);
        });
    }
 
    /**
     * Disables the options of the dropdown component.
     */
    private disableOptions(): void {
        this._select.innerHTML = '';
    }
 
    //#region Base Callbacks
    /**
     * Builds the presentation of the dropdown component.
     */
    private build(): void {
        this._presentationSpan = document.createElement('span');
        this._presentationContainer.appendChild(this._presentationSpan);
 
        this._presentationSpan.title = this._title;
        this._presentationSpan.classList.add('editable-data-view');
        this._presentationSpan.classList.add('text-presentation');
        this._onPresentation?.(this._selectedOption.value);
        //this.presentationSpan.textContent = this._onPresentation ? this._onPresentation(this._selectedOption.value) : this._selectedOption.text;
    }
 
    /**
     * Builds the input of the dropdown component.
     */
    private buildInput(): void {
        this._select = document.createElement('select');
        this._dataInputContainer.appendChild(this._select);
        this._select.title = this._title;
        this._select.classList.add('editable-data-view');
        this._select.classList.add('select-input');
    }
 
    /**
     * Enables the edit mode of the dropdown component.
     */
    private enableEdit(): void {
        this.enableOptions();
        this._select.value = this._value ? this._value : '';
        this._select.focus();
    }
 
    /**
     * Disables the edit mode of the dropdown component.
     */
    private disableEdit(): void {
        this._onPresentation?.(this._selectedOption.value);
        //this.presentationSpan.textContent = this._onPresentation ? this._onPresentation(this._selectedOption.value) : this._selectedOption.text;
        this.disableOptions();
    }
 
    /**
     * Saves the value of the dropdown component.
     */
    private async save(): Promise<void> {
        this._value = this._select.value;
        await this._onSave?.(this._value);
    }
    //#endregion
}
 
Zur TypeDoc-Dokumentation