# All files / js app.ts

0% Statements 0/88
0% Branches 0/48
0% Functions 0/27
0% Lines 0/83

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

     import '../css/app.css';
    import 'vue-sonner/style.css';
    import 'flatpickr/dist/flatpickr.min.css';

    import { createInertiaApp, router } from '@inertiajs/vue3';
    import DragHandleIcon from '@/components/DragHandleIcon.vue';
    import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
    import FlashToasts from '@/components/FlashToasts.vue';
    import { Toaster } from 'vue-sonner';
    import type { DefineComponent } from 'vue';
    import { Fragment, createApp, h } from 'vue';
    import flatpickr from 'flatpickr';
    import { initializeTheme } from './composables/useAppearance';
    import { buildDateTimeOptions, type DateTimeFormatProps } from './composables/useDateTimeFormat';

    const appName = import.meta.env.VITE_APP_NAME || 'JobCardOnline';
    type DateMethod = 'toLocaleDateString' | 'toLocaleTimeString' | 'toLocaleString';
    const originalDateMethods: Partial<Record<DateMethod, Date[DateMethod]>> = {};
    let activeDateTimeFormat: DateTimeFormatProps | undefined;
    let dateInputMutationObserver: MutationObserver | null = null;
    let dateInputRefreshTimer: number | null = null;

    function inputDateFormatHint(format: DateTimeFormatProps | undefined): string {
        switch (format?.date_format) {
            case 'mm/dd/yyyy':
                return 'MM/DD/YYYY';
            case 'yyyy-mm-dd':
                return 'YYYY-MM-DD';
            case 'd mmm yyyy':
                return 'D MMM YYYY';
            case 'dd/mm/yyyy':
            default:
                return 'DD/MM/YYYY';
        }
    }

    function inputTimeFormatHint(format: DateTimeFormatProps | undefined): string {
        return format?.time_format === '12h' ? 'hh:mm AM/PM' : 'HH:mm';
    }

    function resolveFlatpickrDateAltFormat(format: DateTimeFormatProps | undefined): string {
        switch (format?.date_format) {
            case 'mm/dd/yyyy':
                return 'm/d/Y';
            case 'yyyy-mm-dd':
                return 'Y-m-d';
            case 'd mmm yyyy':
                return 'j M Y';
            case 'dd/mm/yyyy':
            default:
                return 'd/m/Y';
        }
    }

    function resolveFlatpickrTimeAltFormat(format: DateTimeFormatProps | undefined): string {
        return format?.time_format === '12h' ? 'h:i K' : 'H:i';
    }

    function createFlatpickrForInput(
        input: HTMLInputElement,
        format: DateTimeFormatProps | undefined,
        isDateTime: boolean,
    ): void {
        if ((input as any)._flatpickr) {
            (input as any)._flatpickr.destroy();
        }

        const dateAlt = resolveFlatpickrDateAltFormat(format);
        const timeAlt = resolveFlatpickrTimeAltFormat(format);
        const dateHint = inputDateFormatHint(format);
        const dateTimeHint = `${dateHint} ${inputTimeFormatHint(format)}`;

        input.setAttribute('title', isDateTime ? dateTimeHint : dateHint);
        flatpickr(input, {
            allowInput: true,
            altInput: true,
            altFormat: isDateTime ? `${dateAlt} ${timeAlt}` : dateAlt,
            dateFormat: isDateTime ? 'Y-m-d\\TH:i' : 'Y-m-d',
            enableTime: isDateTime,
            noCalendar: false,
            time_24hr: format?.time_format !== '12h',
            disableMobile: true,
            minuteIncrement: 1,
            defaultHour: 0,
            defaultMinute: 0,
            onChange: () => {
                input.dispatchEvent(new Event('input', { bubbles: true }));
                input.dispatchEvent(new Event('change', { bubbles: true }));
            },
            onClose: () => {
                input.dispatchEvent(new Event('input', { bubbles: true }));
                input.dispatchEvent(new Event('change', { bubbles: true }));
            },
        });
    }

    function applyDateInputLocalization(format: DateTimeFormatProps | undefined): void {
        const resolved = buildDateTimeOptions(format);

        document.documentElement.setAttribute('lang', resolved.locale);
        document.querySelectorAll<HTMLInputElement>('input[type="date"]').forEach((input) => {
            input.setAttribute('lang', resolved.locale);
            createFlatpickrForInput(input, format, false);
        });
        document.querySelectorAll<HTMLInputElement>('input[type="datetime-local"]').forEach((input) => {
            input.setAttribute('lang', resolved.locale);
            createFlatpickrForInput(input, format, true);
        });
    }

    function scheduleDateInputLocalization(format: DateTimeFormatProps | undefined): void {
        if (dateInputRefreshTimer !== null) {
            window.clearTimeout(dateInputRefreshTimer);
        }
        dateInputRefreshTimer = window.setTimeout(() => {
            applyDateInputLocalization(format);
        }, 0);
    }

    function patchDateLocalization(format: DateTimeFormatProps | undefined): void {
        const resolved = buildDateTimeOptions(format);

        (['toLocaleDateString', 'toLocaleTimeString', 'toLocaleString'] as DateMethod[]).forEach((method) => {
            if (!originalDateMethods[method]) {
                originalDateMethods[method] = Date.prototype[method];
            }
            const original = originalDateMethods[method];
            if (!original) return;

            Date.prototype[method] = function (_locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions) {
                return original.call(this, resolved.locale, {
                    ...options,
                    timeZone: resolved.timezone,
                    hour12: method === 'toLocaleDateString' ? undefined : resolved.hour12,
                });
            };
        });
    }

    function refreshLocalizedDateInputs(format: DateTimeFormatProps | undefined): void {
        applyDateInputLocalization(format);
        requestAnimationFrame(() => applyDateInputLocalization(format));
        window.setTimeout(() => applyDateInputLocalization(format), 100);
    }

    createInertiaApp({
        title: (title) => (title ? `${title} - ${appName}` : appName),
        resolve: (name) =>
            resolvePageComponent(
                `./pages/${name}.vue`,
                import.meta.glob<DefineComponent>('./pages/**/*.vue'),
            ),
        setup({ el, App, props, plugin }) {
            const initialProps = (props?.initialPage?.props ?? {}) as { dateTimeFormat?: DateTimeFormatProps };
            activeDateTimeFormat = initialProps.dateTimeFormat;
            patchDateLocalization(initialProps.dateTimeFormat);
            refreshLocalizedDateInputs(initialProps.dateTimeFormat);

            document.addEventListener('focusin', (event) => {
                const target = event.target as HTMLElement | null;
                if (!(target instanceof HTMLInputElement)) return;
                if (target.type !== 'date' && target.type !== 'datetime-local') return;
                createFlatpickrForInput(target, activeDateTimeFormat, target.type === 'datetime-local');
            });

            if (dateInputMutationObserver) {
                dateInputMutationObserver.disconnect();
            }
            dateInputMutationObserver = new MutationObserver(() => {
                scheduleDateInputLocalization(activeDateTimeFormat);
            });
            // Observe the Inertia root instead of the whole document to keep the
            // localization pass scoped to app-driven DOM updates.
            dateInputMutationObserver.observe(el, {
                childList: true,
                subtree: true,
                attributes: true,
                attributeFilter: ['type', 'value'],
            });

            router.on('navigate', (event) => {
                const nextProps = (event.detail.page.props ?? {}) as { dateTimeFormat?: DateTimeFormatProps };
                activeDateTimeFormat = nextProps.dateTimeFormat;
                patchDateLocalization(nextProps.dateTimeFormat);
                refreshLocalizedDateInputs(nextProps.dateTimeFormat);
            });

            createApp({
                render: () =>
                    h(Fragment, [
                        h(App, props),
                        h(Toaster, {
                            closeButton: true,
                            position: 'top-right',
                            richColors: true,
                            theme: 'system',
                        }),
                        h(FlashToasts),
                    ]),
            })
                .use(plugin)
                .component('DragHandleIcon', DragHandleIcon)
                .mount(el);
        },
        progress: {
            color: '#4B5563',
        },
    });

    // This will set light / dark mode on page load...
    initializeTheme();

    // Global error handler for Inertia
    router.on('error', (event) => {
        console.error('Inertia error:', event.detail);

        const detail = event.detail as { errors?: unknown; message?: string };

        // Handle specific error types
        if (detail.message?.includes('Cannot read properties of null')) {
            console.warn('Null reference error detected, this may be due to missing data or route issues');
            // Don't show error to user for null reference errors as they're usually handled gracefully
            return;
        }

        // For other errors, you might want to show a user-friendly message
        // or redirect to an error page
    });