All files / src/libs/BlockRenderComponents TableBlockRenderComponent.ts

0% Statements 0/148
0% Branches 0/98
0% Functions 0/21
0% Lines 0/143

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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { Component, setIcon } from 'obsidian';
import Lng from 'src/classes/Lng';
import type { IApp } from 'src/interfaces/IApp';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import type IMetadataCache from 'src/interfaces/IMetadataCache';
import { FileType } from 'src/libs/FileType/FileType';
import { FileTypes } from 'src/libs/FileType/interfaces/IFileType';
import IPrjModel from 'src/models/interfaces/IPrjModel';
import type { IPrjSettings } from 'src/types/PrjSettings';
import RedrawableBlockRenderComponent from './RedrawableBlockRenderComponent';
import { IProcessorSettings } from '../../interfaces/IProcessorSettings';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { HelperGeneral } from '../Helper/General';
import { FileMetadata } from '../MetadataCache';
import { ISearch } from '../Search/interfaces/ISearch';
import { Search } from '../Search/Search';
import Table, { RowsState, TableHeader } from '../Table';
import { Tags } from '../Tags/Tags';
 
/**
 * Represents the base class for table block render components.
 */
export default abstract class TableBlockRenderComponent<
    T extends IPrjModel<unknown>,
> implements RedrawableBlockRenderComponent
{
    //#region General properties
    @Inject('IPrjSettings')
    protected _IPrjSettings: IPrjSettings;
    @Inject('ILogger_', (x: ILogger_) =>
        x.getLogger('TableBlockRenderComponent'),
    )
    protected _logger?: ILogger;
    @Inject('IMetadataCache')
    protected _IMetadataCache: IMetadataCache;
    @Inject('IApp')
    protected _IApp: IApp;
 
    private _activeFileDebounceTimer: NodeJS.Timeout;
    //#endregion
    //#region Component properties
    protected _processorSettings: IProcessorSettings;
    protected _component: Component;
    protected _settings: BlockRenderSettings;
    //#endregion
    //#region Models
    protected _models: T[];
    //#endregion
    //#region HTML properties
    protected _table: Table;
    protected _tableHeaders: TableHeader[];
    protected _headerContainer: HTMLElement;
    protected _tableContainer: HTMLElement;
    //#endregion
 
    /**
     * Creates a new TableBlockRenderComponent instance.
     * @param settings The processor settings.
     * @param logger The logger to use. Defaults to the default logger `TableBlockRenderComponent`.
     */
    constructor(settings: IProcessorSettings, logger?: ILogger) {
        this._processorSettings = settings;
        this._component = settings.component;
        this.onActiveFileDebounce = this.onActiveFileDebounce.bind(this);
        //this.parseSettings();
    }
 
    /**
     * Builds the component first time.
     * @returns A promise that resolves when the component is drawn.
     * @remarks Calls the `draw` method.
     */
    public async build(): Promise<void> {
        return this.draw();
    }
 
    /**
     * Builds the `tableContainer` and `headerContainer` elements.
     * @remarks - Call this method to build the base structure first.
     * - Override this method to build the other elements.
     * @remarks - Build the `tableContainer` and `headerContainer` elements.
     * - Build the `controle block` => add a refresh button which calls the `redraw` method.
     */
    protected async draw(): Promise<void> {
        //Create header container
        this._headerContainer = document.createElement('div');
        this._processorSettings.container.appendChild(this._headerContainer);
        this._headerContainer.classList.add('header-container');
 
        //Create controle block
        const blockControle = document.createElement('div');
        this._headerContainer.appendChild(blockControle);
        blockControle.classList.add('block-controle');
 
        //Create refresh Button
        const refreshButton = document.createElement('a');
        blockControle.appendChild(refreshButton);
        refreshButton.classList.add('refresh-button');
        refreshButton.title = Lng.gt('Refresh');
        refreshButton.href = '#';
        setIcon(refreshButton, 'refresh-cw');
 
        this._component.registerDomEvent(
            refreshButton,
            'click',
            async (event: MouseEvent) => {
                event.preventDefault();
                this.redraw();
            },
        );
 
        this._tableContainer = document.createElement('div');
        this._processorSettings.container.appendChild(this._tableContainer);
        this._tableContainer.classList.add('table-container');
    }
 
    /**
     * Redraws the component on request. Clears the container and calls the `draw` method.
     * @returns A promise that resolves when the component is redrawn.
     * @remarks This methode clears the container and calls the `draw` methode.
     */
    public async redraw(): Promise<void> {
        this._processorSettings.container.innerHTML = '';
 
        return this.draw();
    }
 
    /**
     * Normalizes the header.
     * @remarks - Removes the `disable` class from the header.
     * - The header is not grayed out anymore.
     */
    protected normalizeHeader(): void {
        this._headerContainer.removeClass('disable');
    }
 
    /**
     * Grays out the header.
     * @remarks - Adds the `disable` class to the header.
     * - The header is grayed out.
     */
    protected grayOutHeader(): void {
        this._headerContainer.addClass('disable');
    }
 
    /**
     * Parses the settings given by the user per YAML in code block.
     * @remarks The settings are parsed and saved in the `settings` property.
     * @remarks Settings:
     * - `tags`: Can be `all`, `this`, `activeFile` or a list of tags.
     * `this` means the tags of the current document.
     * `activeFile` means the tags of the active file.
     * - `maxDocuments`: The maximum number of documents to show on same time.
     * - `filter`: Must be an array. The values present the document types.
     * All values that are in the array are shown.
     */
    protected parseSettings(): void {
        this._processorSettings.options.forEach((option) => {
            switch (option.label) {
                case 'tags':
                    if (option.value === 'all') {
                        this._settings.tags = [];
                    } else if (option.value === 'this') {
                        const tags = this._processorSettings?.frontmatter?.tags;
 
                        if (Array.isArray(tags)) {
                            this._settings.tags.push(...tags);
                        } else if (tags) {
                            this._settings.tags.push(tags);
                        } else {
                            this._settings.tags = ['NOTAGSNODATA'];
                        }
                    } else if (option.value === 'activeFile') {
                        // Register event to update the tags when the active file changes
                        this._component.registerEvent(
                            this._IApp.workspace.on('active-leaf-change', () =>
                                this.onActiveFileChange.bind(this)(),
                            ),
                        );
                        this._settings.reactOnActiveFile = true;
                    } else {
                        this._settings.tags = option.value;
                    }
                    break;
                case 'maxDocuments':
                    this._settings.maxDocuments = option.value;
                    break;
                case 'filter':
                    this._settings.filter = option.value;
                    break;
                default:
                    break;
            }
        });
    }
 
    /**
     * Handles the event when the active file changes.
     * If the active file is not in the "Ressourcen/Panels/" path,
     * it updates the tags in the settings based on the metadata of the active file.
     * @private
     */
    private onActiveFileChange(): void {
        const activeFile = this._IApp.workspace.getActiveFile();
 
        Iif (activeFile && !activeFile.path.contains('Ressourcen/Panels/')) {
            this._logger?.trace('Active file changed: ', activeFile.path);
 
            const tags =
                this._IMetadataCache.getEntry(activeFile)?.metadata?.frontmatter
                    ?.tags;
            let newTags: string[] = [];
 
            if (Array.isArray(tags)) {
                newTags = tags;
            } else Iif (tags && this) {
                newTags = [tags];
            }
 
            /**
             * Checks if the tags are different.
             * @param tags1 The first array to compare.
             * @param tags2 The second array to compare.
             * @returns True if the tags are different, false otherwise.
             */
            const areTagsDifferent = (
                tags1: string[],
                tags2: string[],
            ): boolean => {
                Iif (tags1.length !== tags2.length) return true;
 
                const sortedTags1 = [...tags1].sort();
                const sortedTags2 = [...tags2].sort();
 
                for (let i = 0; i < sortedTags1.length; i++) {
                    Iif (sortedTags1[i] !== sortedTags2[i]) return true;
                }
 
                return false;
            };
 
            Iif (areTagsDifferent(newTags, this._settings.tags)) {
                this._settings.tags = newTags;
                this.onActiveFileDebounce();
            }
        }
    }
 
    /**
     * Debounces the active file change event and triggers a redraw after a delay.
     */
    private onActiveFileDebounce(): void {
        this._logger?.trace('Active file changed: Debouncing');
        clearTimeout(this._activeFileDebounceTimer);
 
        this._activeFileDebounceTimer = setTimeout(() => {
            this.onActiveFileFilter();
        }, 750);
    }
 
    /**
     * Handles the event when the active file changes.
     */
    abstract onActiveFileFilter(): void;
 
    /**
     * Gets the unique identifier for the model.
     * @param model The model to get the unique identifier for.
     * @returns The unique identifier per model.
     */
    protected getUID(model: T): string {
        return HelperGeneral.generateUID(model.file.path);
    }
 
    /**
     * Retrieves models based on the specified file types, tags, and model factory.
     * @param types - The file types to filter by.
     * @param tags - The tags to filter by.
     * @param modelFactory - The factory function to create models from file metadata.
     * @returns A promise that resolves to an array of models.
     */
    protected getModels(
        types: FileTypes[],
        tags: string[],
        modelFactory: (metadata: FileMetadata) => T | undefined,
    ): Promise<T[]> {
        const templateFolder = this._IPrjSettings.templateFolder;
 
        // Create an instance of the Tags class for the provided tags
        const filterTags = new Tags(tags);
 
        const allDocumentFiles = this._IMetadataCache.cache.filter((file) => {
            const typeFilter = FileType.isValidOf(
                file.metadata.frontmatter?.type,
                types,
            );
 
            const thisFileAndTemplateFilter =
                file.file.path !== this._processorSettings.source &&
                !file.file.path.startsWith(templateFolder);
 
            Iif (tags.length > 0) {
                const fileTags = new Tags(file.metadata.frontmatter?.tags);
 
                const tagFilter = fileTags.contains(filterTags);
 
                return thisFileAndTemplateFilter && typeFilter && tagFilter;
            }
 
            return thisFileAndTemplateFilter && typeFilter;
        });
 
        const models: T[] = [];
 
        for (const file of allDocumentFiles) {
            const model = modelFactory(file);
 
            Iif (model) models.push(model);
        }
 
        return Promise.resolve(models);
    }
 
    /**
     * This method is called when the search box is used.
     * @param searchQuery The search text.
     * @param key The key that was pressed.
     * @returns The search text.
     * @remarks - If the `Enter` key was pressed, the search is applied.
     * - If the `Escape` key was pressed, the search is reset.
     * - After the search is applied, the {@link onFilter} method is called.
     */
    protected async onSearch(
        searchQuery: string,
        key: string,
    ): Promise<string> {
        if (key === 'Enter') {
            if (searchQuery !== '') {
                this._settings.searchText = searchQuery;
                this._settings.search = new Search(searchQuery);
                this._settings.search.parse();
                this.onFilter();
            } else {
                this._settings.searchText = undefined;
                this._settings.search = undefined;
                this.onFilter();
            }
        } else Iif (key === 'Escape') {
            this._settings.searchText = undefined;
            this._settings.search = undefined;
            this.onFilter();
 
            return '';
        }
 
        return searchQuery;
    }
 
    /**
     * Filters the models and shows/hides them in the table.
     * @remarks - The models are filtered by the `filter` setting,
     * searched by the `search` setting
     * and the number of documents is limited by the `maxDocuments` if no search is applied.
     */
    protected async onFilter(): Promise<void> {
        this.grayOutHeader();
        const batchSize = this._settings.batchSize;
        const sleepBetweenBatches = this._settings.sleepBetweenBatches;
        let sleepPromise = Promise.resolve();
        const documentsLength = this._models.length;
        const rows: RowsState[] = [];
        let visibleRows = 0;
 
        for (let i = 0; i < documentsLength; i++) {
            const document = this._models[i];
 
            const rowUid = this.getUID(document);
            let hide = this.getHideState(document, undefined);
            this._logger?.trace(`Model ${rowUid} is hidden by state: ${hide}`);
 
            this._logger?.trace(
                `Visible rows: ${visibleRows}; Max shown Models: ${this._settings.maxDocuments}`,
            );
 
            Iif (visibleRows >= this._settings.maxDocuments) {
                hide = true;
            }
 
            this._logger?.trace(
                `Model ${rowUid} is hidden by max counts: ${hide}`,
            );
 
            if (hide) {
                rows.push({ rowUid, hidden: true });
            } else {
                visibleRows++;
                rows.push({ rowUid, hidden: false });
            }
 
            Iif ((i !== 0 && i % batchSize === 0) || i === documentsLength - 1) {
                await sleepPromise;
 
                this._logger?.trace(
                    `Batchsize reached. Change rows: ${rows.length}`,
                );
                await this._table.changeShowHideStateRows(rows);
                rows.length = 0;
                sleepPromise = HelperGeneral.sleep(sleepBetweenBatches);
            }
        }
 
        this.normalizeHeader();
    }
 
    /**
     * Gets the hide state for the document.
     * @param model - The document to get the hide state for.
     * @param maxVisibleRows - The maximum number of visible rows.
     * @returns True if the document should be hidden, false otherwise.
     */
    protected abstract getHideState(
        model: T,
        maxVisibleRows: number | undefined,
    ): boolean;
}
 
export type BlockRenderSettings = {
    /**
     * The tags associated with the documents.
     * Can be `all`, `this` or a list of specific tags.
     * `all` includes all documents regardless of their tags.
     * `this` includes documents that have the same tags as the current document.
     */
    tags: string[];
 
    /**
     * Whether to react to the active file change event.
     */
    reactOnActiveFile: boolean;
 
    /**
     * Filter for the model types to display.
     * Only the types listed in the array will be shown.
     */
    filter: unknown[];
 
    /**
     * The maximum number of models to show at the same time.
     */
    maxDocuments: number;
 
    /**
     * Search terms array used to filter the models.
     * If undefined, no search filter is applied.
     */
    search: ISearch | undefined;
 
    /**
     * The search text.
     */
    searchText: string | undefined;
 
    /**
     * The number of models to process in one batch.
     */
    batchSize: number;
 
    /**
     * The time to wait (in milliseconds) between processing batches of models.
     */
    sleepBetweenBatches: number;
};
 
Zur TypeDoc-Dokumentation