All files / src/libs/Modals ChangeStatusModal.ts

0% Statements 0/47
0% Branches 0/31
0% Functions 0/9
0% Lines 0/47

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                                                                                                                                                                                                                                                                                 
import { Setting } from 'obsidian';
import API from 'src/classes/API';
import Lng from 'src/classes/Lng';
import type { IApp } from 'src/interfaces/IApp';
import { ILogger_ } from 'src/interfaces/ILogger';
import type IMetadataCache from 'src/interfaces/IMetadataCache';
import { IPrj } from 'src/interfaces/IPrj';
import { FileType } from 'src/libs/FileType/FileType';
import { IPrjTaskManagementData } from 'src/models/Data/interfaces/IPrjTaskManagementData';
import PrjBaseData from 'src/models/Data/PrjBaseData';
import { PrjTaskManagementModel } from 'src/models/PrjTaskManagementModel';
import type {
    ICustomModal,
    ICustomModal_,
} from './CustomModal/interfaces/ICustomModal';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { Resolve } from '../DependencyInjection/functions/Resolve';
import { StatusTypes } from '../StatusType/interfaces/IStatusType';
 
/**
 * Represents a modal to change the status of a project.
 */
export default class ChangeStatusModal {
    @Inject('IApp')
    protected readonly _IApp!: IApp;
    @Inject('IMetadataCache')
    private readonly _IMetadataCache!: IMetadataCache;
    @Inject('ICustomModal_')
    private readonly _ICustomModal_!: ICustomModal_;
 
    private readonly _customModal: ICustomModal = new this._ICustomModal_();
 
    private _selectedStatus: StatusTypes;
    private _activeModel: PrjTaskManagementModel<
        IPrjTaskManagementData & PrjBaseData<unknown>
    >;
 
    /**
     * Creates and opens a Change Status modal.
     */
    constructor() {
        this._customModal
            .setBackgroundDimmed(true)
            .setDraggableEnabled(false)
            .setShouldOpen(this.shouldOpen.bind(this))
            .setOnOpen(this._onOpen.bind(this))
            .open();
    }
 
    /**
     * Checks if the active file is a project file
     * @returns True if the active file is a project file
     */
    private shouldOpen(): boolean {
        const workspace = this._IApp.workspace;
        const activeFile = workspace.getActiveFile();
 
        Iif (!activeFile) {
            return false;
        }
        const activeFileMetadata = this._IMetadataCache.getEntry(activeFile);
        const type = activeFileMetadata?.metadata.frontmatter?.type;
 
        Iif (!FileType.isValidOf(type, ['Topic', 'Project', 'Task'])) {
            return false;
        }
 
        const model =
            API.prjTaskManagementModel.getCorospondingModel(activeFile);
 
        Iif (!model) {
            return false;
        }
        this._activeModel = model;
 
        return true;
    }
 
    /**
     * Build the content of the change status modal
     */
    private _onOpen(): void {
        const contentEl = this._customModal.content;
        this._customModal.setTitle(Lng.gt('Change Status'));
 
        new Setting(contentEl)
            .setName(Lng.gt('New Status'))
            .addDropdown((cb) => {
                cb.addOption('Active', Lng.gt('StatusActive'));
                cb.addOption('Waiting', Lng.gt('StatusWaiting'));
                cb.addOption('Later', Lng.gt('StatusLater'));
                cb.addOption('Someday', Lng.gt('StatusSomeday'));
                cb.addOption('Done', Lng.gt('StatusDone'));
 
                cb.setValue(
                    this._activeModel.data.status?.toString() ?? 'Active',
                );
 
                cb.onChange((value) => {
                    this._selectedStatus = value as StatusTypes;
                });
            });
 
        new Setting(contentEl).addButton((btn) =>
            btn
                .setButtonText(Lng.gt('Change Status'))
                .setCta()
                .onClick(() => {
                    this._activeModel.changeStatus(this._selectedStatus);
                    this._customModal.close();
                }),
        );
    }
 
    /**
     * Registers the command to open the modal
     */
    public static registerCommand(): void {
        const plugin = Resolve<IPrj>('IPrj');
 
        const logger = Resolve<ILogger_>('ILogger_', false)?.getLogger(
            'ChangeStatusModal',
        );
 
        try {
            plugin.addCommand({
                id: 'change-prj-status',
                name: Lng.gt('Change Status'),
                callback: () => new ChangeStatusModal(),
            });
            logger?.trace("Registered 'Change Status' command successfully");
        } catch (error) {
            logger?.error("Failed to register 'Change Status' command", error);
        }
    }
}
 
Zur TypeDoc-Dokumentation