All files / src/libs/BlockRenderComponents/InnerComponents MaxShownModelsInput.ts

0% Statements 0/56
0% Branches 0/16
0% Functions 0/14
0% Lines 0/56

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { Component, setIcon } from 'obsidian';
import Lng from 'src/classes/Lng';
import { Logging } from 'src/classes/Logging';
 
/**
 * Max shown models input component class for `TableBlockRenderComponent`.
 *
 * This class provides methods to create and manage a max shown models input component.
 * It includes functionality for setting up the max shown models input and handling input events.
 * @see {@link create} for details about creating a max shown models input component.
 * @see {@link MaxShownModelsCallback} for details about the max shown models callback.
 */
export default class MaxShownModelsInput {
    /**
     * Creates a new max shown models input component.
     * @param component The component to register the events to.
     * @param defaultValue The default value of the max shown models number.
     * @param batchSize The batch size to add or subtract.
     * @param onChange The callback to call when the max shown models number changes. Passes the max shown models number. You can return a new max shown models number.
     * @returns The created max shown models input component as `DocumentFragment`.
     * @remarks - The max shown models input component consists of a container, a minus symbol, a presentation span and a plus symbol.
     * - CSS classes:
     * - `filter-max-models` - The container of the max shown models input component.
     * - `filter-max-number` - The presentation span of the max shown models input component.
     * - `minus-batch-button` - The minus symbol of the max shown models input component.
     * - `plus-batch-button` - The plus symbol of the max shown models input component.
     */
    public static create(
        component: Component,
        defaultValue: number,
        batchSize: number,
        onChange: MaxShownModelsCallback,
    ): DocumentFragment {
        const headerItemContainer = document.createDocumentFragment();
        const logger = Logging.getLogger('MaxShownModelsInput');
        let debounceTimer: NodeJS.Timeout;
 
        const maxShownModels: MaxShownModelNumber = {
            maxShownModels:
                !isNaN(parseFloat(defaultValue as unknown as string)) &&
                isFinite(defaultValue)
                    ? Number(defaultValue)
                    : 0,
        };
 
        const filterMaxModelsContainer = document.createElement('div');
        headerItemContainer.appendChild(filterMaxModelsContainer);
        filterMaxModelsContainer.classList.add('filter-max-models');
 
        const number = this.createNumberPresentation(
            maxShownModels,
            component,
            batchSize,
        );
 
        /**
         * Debounces the `onChange` callback.
         */
        const debounceOnChange = (): void => {
            clearTimeout(debounceTimer);
 
            debounceTimer = setTimeout(() => {
                Promise.resolve(onChange(maxShownModels.maxShownModels))
                    .then((newMaxShownModels) => {
                        maxShownModels.maxShownModels =
                            newMaxShownModels ?? maxShownModels.maxShownModels;
                    })
                    .catch((error) => {
                        logger.error(
                            'The `onChange` callback threw an error!',
                            error,
                        );
                    })
                    .finally(() => {
                        number.number.textContent =
                            maxShownModels.maxShownModels.toString();
                    });
            }, 500);
        };
 
        MaxShownModelsInput.createNumberPresentationEvent(
            component,
            number.number,
            maxShownModels,
            batchSize,
            debounceOnChange,
        );
 
        const minus = this.createSymbol(
            'minus',
            number.number,
            component,
            maxShownModels,
            batchSize,
            debounceOnChange,
        );
 
        const plus = this.createSymbol(
            'plus',
            number.number,
            component,
            maxShownModels,
            batchSize,
            debounceOnChange,
        );
 
        filterMaxModelsContainer.appendChild(minus);
        filterMaxModelsContainer.appendChild(number.container);
        filterMaxModelsContainer.appendChild(plus);
 
        return headerItemContainer;
    }
 
    /**
     * Create the presentation span for the max shown models number.
     * @param maxShownModels The container for the max shown models number.
     * @param component The component to register the events to.
     * @param batchSize The batch size to add or subtract.
     * @returns - The created presentation span as `DocumentFragment`
     * - The created presentation span as `HTMLSpanElement`
     * - The span element has the class `filter-max-number`.
     */
    private static createNumberPresentation(
        maxShownModels: MaxShownModelNumber,
        component: Component,
        batchSize: number,
    ): { container: DocumentFragment; number: HTMLSpanElement } {
        const filterMaxModelsContainer = document.createDocumentFragment();
 
        const maxShownNumber = document.createElement('span');
        filterMaxModelsContainer.appendChild(maxShownNumber);
        maxShownNumber.classList.add('filter-max-number');
        maxShownNumber.title = Lng.gt('MaxShownEntrys');
        maxShownNumber.textContent = maxShownModels.maxShownModels.toString();
 
        return { container: filterMaxModelsContainer, number: maxShownNumber };
    }
 
    /**
     * Creates the mouse wheel events for the max shown models number presentation span.
     * @param component The component to register the events to.
     * @param maxShownNumber The presentation span of the max shown models number.
     * @param maxShownModels The container for the max shown models number.
     * @param batchSize The batch size to add or subtract.
     * @param onMaxShownModelsChange The callback to call when the max shown models number changes.
     */
    private static createNumberPresentationEvent(
        component: Component,
        maxShownNumber: HTMLSpanElement,
        maxShownModels: MaxShownModelNumber,
        batchSize: number,
        onMaxShownModelsChange: () => void,
    ): void {
        component.registerDomEvent(
            maxShownNumber,
            'wheel',
            async (event: WheelEvent) => {
                event.preventDefault();
 
                if (event.deltaY > 0) {
                    this.changeValue(
                        'minus',
                        maxShownModels,
                        batchSize,
                        maxShownNumber,
                        onMaxShownModelsChange,
                    );
                } else {
                    this.changeValue(
                        'plus',
                        maxShownModels,
                        batchSize,
                        maxShownNumber,
                        onMaxShownModelsChange,
                    );
                }
            },
            { passive: false },
        );
    }
 
    /**
     * Creates a symbol for the max shown models number.
     * @param type The type of the symbol. Can be `plus` or `minus`.
     * @param maxShownNumber The presentation span of the max shown models number.
     * @param component The component to register the events to.
     * @param maxShownModels The container for the max shown models number.
     * @param batchSize The batch size to add or subtract.
     * @param onMaxShownModelsChange The callback to call when the max shown models number changes.
     * @returns The created symbol as `DocumentFragment`.
     * @remarks - The symbol is a `HTMLAnchorElement`.
     * - The symbol has the class `plus-batch-button` or `minus-batch-button`.
     */
    private static createSymbol(
        type: 'plus' | 'minus',
        maxShownNumber: HTMLSpanElement,
        component: Component,
        maxShownModels: MaxShownModelNumber,
        batchSize: number,
        onMaxShownModelsChange: () => void,
    ): DocumentFragment {
        const filterMaxModelsContainer = document.createDocumentFragment();
 
        const maxShownDocMinus = document.createElement('a');
        filterMaxModelsContainer.appendChild(maxShownDocMinus);
        maxShownDocMinus.classList.add(`${type}-batch-button`);
        maxShownDocMinus.title = type;
        maxShownDocMinus.href = '#';
        setIcon(maxShownDocMinus, type);
 
        component.registerDomEvent(
            maxShownDocMinus,
            'click',
            async (event: MouseEvent) => {
                this.changeValue(
                    type,
                    maxShownModels,
                    batchSize,
                    maxShownNumber,
                    onMaxShownModelsChange,
                );
            },
        );
 
        return filterMaxModelsContainer;
    }
 
    /**
     * Changes the max shown models number.
     * @param type The type of the symbol. Can be `plus` or `minus`.
     * @param maxShownModels The container for the max shown models number.
     * @param batchSize The batch size to add or subtract.
     * @param maxShownNumber The presentation span of the max shown models number.
     * @param onMaxShownModelsChange The callback to call when the max shown models number changes.
     */
    private static changeValue(
        type: string,
        maxShownModels: MaxShownModelNumber,
        batchSize: number,
        maxShownNumber: HTMLSpanElement,
        onMaxShownModelsChange: () => void,
    ): void {
        if (type === 'minus') {
            if (maxShownModels.maxShownModels >= batchSize) {
                // Subtracts either the remainder (to arrive at the next multiple of `batchSize`) or `batchSize` itself
                maxShownModels.maxShownModels -=
                    maxShownModels.maxShownModels % batchSize || batchSize;
            } else {
                maxShownModels.maxShownModels = 0;
            }
        } else {
            // Adds either the remainder (to arrive at the next multiple of `batchSize`) or `batchSize` itself
            maxShownModels.maxShownModels +=
                batchSize - (maxShownModels.maxShownModels % batchSize);
        }
        maxShownNumber.textContent = maxShownModels.maxShownModels.toString();
        onMaxShownModelsChange();
    }
}
 
/**
 * The callback for a change of the max shown models number.
 * @param value The current max shown models number.
 * @returns - The new max shown models number.
 * - If you return `undefined` the max shown models number is not changed.
 * @remarks The callback is called when the max shown models number is changed.
 */
type MaxShownModelsCallback = (value: number) => Promise<number | undefined>;
 
/**
 * A container for the max shown models number.
 * @param maxShownModels The max shown models number.
 * @remarks The container is used to pass the max shown models number by reference.
 */
type MaxShownModelNumber = { maxShownModels: number };
 
Zur TypeDoc-Dokumentation