All files / src/libs/BlockRenderComponents DocumentBlockRenderComponent.ts

0% Statements 0/148
0% Branches 0/79
0% Functions 0/23
0% Lines 0/146

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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import API from 'src/classes/API';
import Lng from 'src/classes/Lng';
import { Logging } from 'src/classes/Logging';
import { DocumentModel } from 'src/models/DocumentModel';
import DocumentComponents from './InnerComponents/DocumentComponents';
import FilterButton from './InnerComponents/FilterButton';
import GeneralComponents from './InnerComponents/GeneralComponents';
import MaxShownModelsInput from './InnerComponents/MaxShownModelsInput';
import SearchInput from './InnerComponents/SearchInput';
import TableBlockRenderComponent, {
    BlockRenderSettings,
} from './TableBlockRenderComponent';
import { IProcessorSettings } from '../../interfaces/IProcessorSettings';
import { HelperGeneral } from '../Helper/General';
import { FileMetadata } from '../MetadataCache';
import Table, { Row, TableHeader } from '../Table';
import { Tags } from '../Tags/Tags';
 
/**
 * Document block render component class for `TableBlockRenderComponent`.
 * @remarks This class provides methods to create and manage a document block render component.
 * @see {@link create} for details about creating a document block render component.
 */
export default class DocumentBlockRenderComponent extends TableBlockRenderComponent<DocumentModel> {
    private _filterButtonDebounceTimer: NodeJS.Timeout;
    protected _settings: BlockRenderSettings = {
        tags: [],
        reactOnActiveFile: false,
        filter: ['Documents'],
        maxDocuments: this._IPrjSettings.defaultMaxShow,
        search: undefined,
        searchText: undefined,
        batchSize: 8,
        sleepBetweenBatches: 10,
    };
 
    /**
     * The table headers.
     * @remarks The table headers are used to create the table.
     */
    protected _tableHeaders: TableHeader[] = [
        {
            text: Lng.gt('DocumentType'),
            headerClass: [],
            columnClass: ['dont-decorate-link', 'font-medium'],
        },
        { text: Lng.gt('Date'), headerClass: [], columnClass: ['font-xsmall'] },
        { text: Lng.gt('Subject'), headerClass: [], columnClass: [] },
        {
            text: Lng.gt('SendRecip'),
            headerClass: [],
            columnClass: ['font-xsmall'],
        },
        {
            text: Lng.gt('Content description'),
            headerClass: [],
            columnClass: ['font-xsmall'],
        },
        {
            text: Lng.gt('DeliveryDate'),
            headerClass: [],
            columnClass: ['font-xsmall'],
        },
        { text: Lng.gt('Tags'), headerClass: [], columnClass: ['tags'] },
    ];
 
    /**
     * Creates a new document block render component.
     * @param settings The settings of the document block render component.
     */
    constructor(settings: IProcessorSettings) {
        super(settings);
        this.parseSettings();
    }
 
    /**
     * Run the {@link TableBlockRenderComponent.build} method.
     * @returns A promise which is resolved when the documents basic components are built.
     */
    public build(): Promise<void> {
        return super.build();
    }
 
    /**
     * Draws the component and adds the documents to the table.
     * @returns A promise which is resolved when the documents are added to the table.
     * @remarks - Calls the `super.draw` method.
     * - Calls the `buildTable` method.
     * - Calls the `buildHeader` method.
     * - Calls the `addDocumentsToTable` method.
     */
    protected async draw(): Promise<void> {
        const startTime = Date.now();
 
        const documentsPromise = super.getModels(
            ['Metadata'],
            this._settings.tags,
            (metadata: FileMetadata) => new DocumentModel(metadata.file),
        );
        await super.draw();
        await this.buildTable();
        await this.buildHeader();
        this.grayOutHeader();
        this._models = await documentsPromise;
 
        API.documentModel.sortDocumentsByDateDesc(this._models);
        await this.addDocumentsToTable();
        this.normalizeHeader();
        const endTime = Date.now();
 
        this._logger?.debug(
            `Redraw Documents (for ${this._models.length} Docs.) runs for ${endTime - startTime}ms`,
        );
    }
 
    /**
     * Builds the header.
     * @remarks - The header is saved in the `headerContainer` property.
     * - Creates the `filter container`.
     * - Creates the `filter label`.
     * - Creates the `filter buttons`: `Documents`, `HideDocuments` and `Cluster`.
     * - Creates the `max documents input`.
     * - Creates the `search box`.
     */
    private async buildHeader(): Promise<void> {
        // Filter container
        const headerFilterButtons = document.createElement('div');
        this._headerContainer.appendChild(headerFilterButtons);
        headerFilterButtons.classList.add('header-item');
        headerFilterButtons.classList.add('filter-symbols');
 
        // Filter label
        const filterLabelContainer = document.createElement('div');
        headerFilterButtons.appendChild(filterLabelContainer);
        const filterLabel = document.createElement('span');
        filterLabelContainer.appendChild(filterLabel);
        filterLabel.classList.add('filter-symbol');
        filterLabel.textContent = Lng.gt('Filter');
 
        const documentFilterButton = FilterButton.create(
            this._component,
            'Documents',
            this._IPrjSettings.documentSettings.symbol,
            this._settings.filter.includes('Documents'),
            this.onFilterButton.bind(this),
        );
        headerFilterButtons.appendChild(documentFilterButton);
 
        const hideDocumentFilterButton = FilterButton.create(
            this._component,
            'HideDocuments',
            this._IPrjSettings.documentSettings.hideSymbol,
            this._settings.filter.includes('HideDocuments'),
            this.onFilterButton.bind(this),
        );
        headerFilterButtons.appendChild(hideDocumentFilterButton);
 
        const clusterFilterButton = FilterButton.create(
            this._component,
            'Cluster',
            this._IPrjSettings.documentSettings.clusterSymbol,
            this._settings.filter.includes('Cluster'),
            this.onFilterButton.bind(this),
        );
        headerFilterButtons.appendChild(clusterFilterButton);
 
        const maxDocuments = MaxShownModelsInput.create(
            this._component,
            this._settings.maxDocuments,
            this._IPrjSettings.defaultMaxShow,
            this.onMaxDocumentsChange.bind(this),
        );
        headerFilterButtons.appendChild(maxDocuments);
 
        const searchBox = SearchInput.create(
            this._component,
            this.onSearch.bind(this),
            this._settings.searchText,
        );
        headerFilterButtons.appendChild(searchBox);
    }
 
    /**
     * This method is called when the filter button is clicked.
     * @param type The type of the filter button.
     * @param status The new status of the filter button.
     * @remarks Runs the `onFilter` method.
     */
    private async onFilterButton(type: string, status: boolean): Promise<void> {
        if (this._settings.filter.includes(type as FilteredDocument)) {
            this._settings.filter = this._settings.filter.filter(
                (v) => v !== type,
            );
        } else {
            this._settings.filter.push(type as FilteredDocument);
        }
        await this.onFilterDebounce();
    }
 
    /**
     * This method debounces the `onFilter` method.
     * @returns A promise which is resolved when Timer is set.
     */
    private async onFilterDebounce(): Promise<void> {
        clearTimeout(this._filterButtonDebounceTimer);
 
        this._filterButtonDebounceTimer = setTimeout(() => {
            this.onFilter();
        }, 750);
    }
 
    /**
     * This method is called when the max documents input is changed.
     * @param maxDocuments The new max documents value.
     * @returns A promise which is resolved when the documents are filtered.
     * @remarks Runs the `onFilter` method.
     */
    private async onMaxDocumentsChange(
        maxDocuments: number,
    ): Promise<undefined> {
        this._settings.maxDocuments = maxDocuments;
        this.onFilter();
 
        return undefined;
    }
 
    /**
     * Adds the documents to the table.
     * @param batchSize The batch size.
     * @param sleepBetweenBatches The sleep time between the batches.
     * @returns A promise which is resolved when the documents are added to the table.
     * @remarks - The documents are added to the table in batches.
     * - The batch size is defined in the `batchSize` parameter. Default is `settings.batchSize`.
     * - The sleep time between the batches is defined in the `sleepBetweenBatches` parameter. Default is `settings.sleepBetweenBatches`.
     */
    private async addDocumentsToTable(
        batchSize = this._settings.batchSize,
        sleepBetweenBatches = this._settings.sleepBetweenBatches,
    ): Promise<void> {
        let sleepPromise = Promise.resolve();
        const documentsLength = this._models.length;
        const rows: Row[] = [];
        let rowPromise: Promise<Row> | undefined = undefined;
 
        if (documentsLength > 0) {
            rowPromise = this.generateTableRow(this._models[0]);
        } else {
            return;
        }
 
        let visibleRows = 0;
 
        for (let i = 0; i < documentsLength; i++) {
            const document =
                i + 1 < documentsLength ? this._models[i + 1] : null;
 
            const row = await rowPromise;
            rowPromise = document ? this.generateTableRow(document) : undefined;
 
            Iif (row && !row.hidden) {
                if (visibleRows < this._settings.maxDocuments) {
                    visibleRows++;
                } else {
                    row.hidden = true;
                }
            }
 
            Iif (row) rows.push(row);
 
            Iif ((i !== 0 && i % batchSize === 0) || i === documentsLength - 1) {
                await sleepPromise;
                this._table.addRows(rows);
                rows.length = 0;
                sleepPromise = HelperGeneral.sleep(sleepBetweenBatches);
            }
        }
    }
 
    /**
     * Generates a table row for the given document.
     * @param documentModel The document to generate the table row for.
     * @returns The generated table row.
     */
    private async generateTableRow(documentModel: DocumentModel): Promise<Row> {
        const rowClassList: string[] = [];
        const rowData: DocumentFragment[] = [];
        const rowUid = this.getUID(documentModel);
 
        // Row 0 -- Metadata Link
        const metadataLink = document.createDocumentFragment();
        rowData.push(metadataLink);
 
        DocumentComponents.createCellMetadatalink(
            metadataLink,
            this._component,
            documentModel,
        );
 
        // Row 1 -- Date
        const date = document.createDocumentFragment();
        rowData.push(date);
 
        GeneralComponents.createCellDate(
            date,
            this._component,
            Lng.gt('DocumentDate'),
            this._IPrjSettings.dateFormat,
            () => documentModel.data.date ?? 'na',
            (value: string) => (documentModel.data.date = value),
        );
 
        // Row 2 -- File Link
        const fileLink = document.createDocumentFragment();
        rowData.push(fileLink);
 
        DocumentComponents.createCellFileLink(
            fileLink,
            this._component,
            documentModel,
        );
 
        // Row 3 -- Sender Recipient
        const senderRecipient = DocumentComponents.createCellSenderRecipient(
            documentModel,
            this._component,
            this._models,
        );
        rowData.push(senderRecipient);
 
        // Row 4 -- Summary & Related Files
        const summaryRelatedFiles = document.createDocumentFragment();
        rowData.push(summaryRelatedFiles);
 
        DocumentComponents.createCellSummary(
            documentModel,
            this._component,
            summaryRelatedFiles,
        );
 
        DocumentComponents.createRelatedFilesList(
            summaryRelatedFiles,
            this._component,
            documentModel,
            this._IPrjSettings.noneSymbol,
            this._IPrjSettings.dateFormatShort,
        );
 
        // Row 5 -- Date of delivery
        const deliveryDate = document.createDocumentFragment();
        rowData.push(deliveryDate);
 
        GeneralComponents.createCellDate(
            deliveryDate,
            this._component,
            Lng.gt('DeliveryDate'),
            this._IPrjSettings.dateFormat,
            () => documentModel.data.dateOfDelivery ?? 'na',
            (value: string) => (documentModel.data.dateOfDelivery = value),
        );
 
        // Row 6 -- Tags
        const tags = document.createDocumentFragment();
        rowData.push(tags);
 
        GeneralComponents.createCellTags(
            tags,
            this._component,
            documentModel.data.tags?.primitiveOf() ?? [],
        );
 
        const hide = this.getHideState(documentModel, undefined);
 
        const row = {
            rowUid,
            rowData,
            rowClassList,
            hidden: hide,
        };
 
        return row;
    }
 
    /**
     * Gets the hide state of the document.
     * @param model The document model to get the hide state for.
     * @param maxVisibleRows The maximum visible rows.
     * @returns If the document should be hidden returns `true`, else `false`.
     */
    protected getHideState(
        model: DocumentModel,
        maxVisibleRows: number | undefined,
    ): boolean {
        let searchResult = false;
        let maxRows = false;
 
        Iif (this._settings.search) {
            const text = model.data.toString?.();
            searchResult = this._settings.search.applySearchLogic(text ?? '');
        }
 
        Iif (maxVisibleRows && maxVisibleRows > 0) {
            const rowStats = this._table.getRowStats();
            maxRows = rowStats.visibleRows >= maxVisibleRows;
        }
 
        const hide =
            this.determineHideState(model) || this.determineTagHideState(model);
 
        if (searchResult && !hide) {
            return false; // Shows the document, if it is not hidden and the search was successful
        } else if (this._settings.search) {
            return true; // Hide the document, if the search was not successful and aplicable
        } else Iif (!searchResult) {
            return maxRows || hide; // Else the document is hidden, if the max rows are reached or the document type is hidden
        }
 
        return hide; // Standard-Verhalten
    }
 
    /**
     * Determines if the document should be hidden.
     * @param document The document to check.
     * @returns If the document should be hidden returns `true`, else `false`.
     * @remarks - The document is hidden if the `filter` setting not includes the document type.
     */
    private determineHideState(document: DocumentModel): boolean {
        Iif (
            this._settings.filter.includes('Documents') &&
            document.data.hide !== true &&
            document.data.subType !== 'Cluster'
        ) {
            return false;
        }
 
        Iif (
            this._settings.filter.includes('Cluster') &&
            document.data.subType === 'Cluster'
        ) {
            return false;
        }
 
        Iif (
            this._settings.filter.includes('HideDocuments') &&
            document.data.hide === true
        ) {
            return false;
        }
 
        return true;
    }
 
    /**
     * Determines if the document should be hidden based on the tags in the settings.
     * @param document The document to check.
     * @returns If the document should be  shown returns `true`, else `false`.
     */
    private determineTagHideState(document: DocumentModel): boolean {
        const settingTags = new Tags(this._settings.tags);
 
        return this._settings.reactOnActiveFile
            ? !document.data.tags?.contains(settingTags)
            : false;
    }
 
    /**
     * This method runs the {@link TableBlockRenderComponent.filter} method.
     */
    public onActiveFileFilter(): void {
        this.onFilter();
    }
 
    /**
     * Builds the table.
     * @remarks - The table is saved in the `table` property.
     * - The table is appended to the `tableContainer`.
     * - The table has the CSS class `document-table`.
     * - The table has the headers from the `tableHeaders` property.
     */
    private async buildTable(): Promise<void> {
        this._table = new Table(
            this._tableHeaders,
            'document-table',
            undefined,
            Logging.getLogger('DocumentBlockRenderComponent/Table'),
        );
        this._tableContainer.appendChild(this._table.data.table);
    }
 
    /**
     * This method runs the {@link TableBlockRenderComponent.redraw} method.
     * @returns A promise which is resolved when the documents are redrawn.
     */
    public redraw(): Promise<void> {
        return super.redraw();
    }
}
 
/**
 * The types of documents to show.
 * @remarks - `Documents` - Show documents.
 * - `HideDocuments` - Show hidden documents.
 * - `Cluster` - Show clusters.
 */
type FilteredDocument = 'Documents' | 'HideDocuments' | 'Cluster';
 
Zur TypeDoc-Dokumentation