All files / src/models TaskModel.ts

0% Statements 0/40
0% Branches 0/51
0% Functions 0/11
0% Lines 0/38

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                                                                                                                                                                                                                                                                     
import { TFile } from 'obsidian';
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { Path } from 'src/classes/Path';
import type { IDIContainer } from 'src/libs/DependencyInjection/interfaces/IDIContainer';
import { HelperGeneral } from 'src/libs/Helper/General';
import { Lifecycle } from 'src/libs/LifecycleManager/decorators/Lifecycle';
import { ILifecycleObject } from 'src/libs/LifecycleManager/interfaces/ILifecycleObject';
import { StatusTypes } from 'src/libs/StatusType/interfaces/IStatusType';
import { Tags } from 'src/libs/Tags/Tags';
import PrjTaskData from './Data/PrjTaskData';
import { PrjTaskManagementModel } from './PrjTaskManagementModel';
// @import-me
/**
 * Represents a task.
 */
@Lifecycle()
@ImplementsStatic<ILifecycleObject>()
export class TaskModel extends PrjTaskManagementModel<PrjTaskData> {
    /**
     * Initializes the model.
     */
    public static onLoad(): void {
        TaskModel.registerThisModelFactory();
    }
 
    /**
     * Registers the TaskModel at the PrjTaskManagementModel.
     */
    private static registerThisModelFactory(): void {
        // eslint-disable-next-line no-console
        console.debug('Registering `TaskModel` at `PrjTaskManagementModel`');
 
        PrjTaskManagementModel.registerModelFactory(
            'Task',
            (file: TFile) => new TaskModel(file),
        );
    }
 
    /**
     * The related tasks.
     * @deprecated This method is deprecated and will be removed in a future version.
     */
    public get relatedTasks(): TaskModel[] {
        // eslint-disable-next-line deprecation/deprecation
        this._relatedTasks = this._relatedTasks ?? this.getRelatedTasks();
 
        return this._relatedTasks;
    }
 
    private _relatedTasks: TaskModel[] | null | undefined = undefined;
 
    /**
     * Creates a new instance of the TaskModel.
     * @param file The file to create the TaskModel from.
     * @param dependencies The optional dependencies to use.
     */
    constructor(file: TFile | undefined, dependencies?: IDIContainer) {
        super(file, PrjTaskData, dependencies);
    }
 
    /**
     * Returns the related tasks.
     * @param status A delegate to filter the tasks by status.
     * @returns The related tasks.
     * @deprecated This method is deprecated and will be removed in a future version.
     */
    private getRelatedTasks(
        status?: (status: StatusTypes | undefined) => boolean,
    ): TaskModel[] {
        const filesWithSameTags = this._IMetadataCache.cache.filter((file) => {
            const fileTags = new Tags(file.metadata?.frontmatter?.tags);
            const thisTags = this.data.tags;
 
            return (
                fileTags.some((tag) => thisTags?.includes(tag) ?? false) &&
                file.file.basename !== this.file.basename &&
                file.metadata?.frontmatter?.type === 'Task' &&
                (status ? status(file.metadata?.frontmatter?.status) : true)
            );
        });
 
        const relatedTasks: TaskModel[] = [];
 
        filesWithSameTags.forEach((file) => {
            Iif (file instanceof TFile) {
                relatedTasks.push(new TaskModel(file));
            }
        });
 
        return relatedTasks;
    }
 
    /**
     * Returns the acronym for the task.
     * @returns The acronym for the task:
     * - `t` as prefix.
     * - The first three characters of the title.
     */
    public override getAcronym(): string {
        return HelperGeneral.generateAcronym(this.data.title as string, 4, 't');
    }
 
    /**
     * Returns the automatic filename for the task.
     * @returns The automatic filename for the task:
     */
    public override getAutomaticFilename(): string | undefined {
        Iif (!this.data.title && this.data.description) {
            const firstDescriptionLine = this.data.description.split('\n')[0];
            this.data.title = firstDescriptionLine.replace(/\.*$/, '');
        }
        const automaticFilename = super.getAutomaticFilename();
        const history = this.data.history?.first();
 
        Iif (!history) {
            return automaticFilename;
        }
 
        const date = HelperGeneral.formatDate(
            history.date,
            this._IPrjSettings.dateFormat,
        );
 
        const newFileName = Path.sanitizeFilename(
            `${date} - ${automaticFilename}`,
        );
 
        return newFileName;
    }
}
 
Zur TypeDoc-Dokumentation