All files monthMore.js

94.59% Statements 35/37
92.85% Branches 13/14
81.81% Functions 9/11
96.96% Lines 32/33

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                            1x                               3x     4x           2x             1x         1x     1x 2x 1x       1x     1x                                           1x 10x 1x   9x 9x 9x 9x     9x                   1x 4x 4x                       1x 6x 6x 2x         4x 4x 4x             4x       4x                     1x      
/**
 * Month view's "more" link, below the day's events.
 *
 * When a day has more events than fit, Toast UI shows "N more" in the cell's
 * header, beside the date, where people do not look for it (wordpress.org
 * support: "I would expect it to be below the 3 items listed"). Toast UI's
 * month cells can also have a footer, which gets the same hidden-event count;
 * giving the footer a height makes Toast leave room for it when deciding how
 * many events fit, so the link sits below the last one without covering it.
 * Shared by the public calendar and the admin calendar.
 */
import { _n, sprintf } from '@wordpress/i18n';
 
/** Footer height in px: one line of the link. */
const FOOTER_HEIGHT = 18;
 
/**
 * Month view's height, given the height the calendar has in other views.
 *
 * The footer's room comes out of the events: Toast fits them (26px each)
 * into the row less a 30px header and the footer. At the public calendar's
 * 600px, a six-week month's rows are ~94px, and an 18px footer left room
 * for one event where there had been two (1.5.2's first gate caught a day
 * with two events showing one and "+1 more"). Month view grows by one
 * footer per week row, so every cell keeps the room it had without one.
 *
 * @param {number} base Height in px in the other views.
 * @param {string} view The current view.
 * @return {number} Height in px.
 */
export const calendarHeight = (base, view) => (view === 'month' ? base + 6 * FOOTER_HEIGHT : base);
 
/** "+2 more", translated, singular and plural. */
export const moreLabel = (count) => sprintf(
    /* translators: %d: number of events on the day that are not shown. */
    _n('+%d more', '+%d more', count, 'agenticdaisy-calendar'),
    count
);
 
const escapeHtml = (text) => String(text)
    .replace(/&/g, '&')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;');
 
/** Toast UI theme: a footer on every month cell. */
export const MONTH_MORE_THEME = {
    month: { gridCell: { footerHeight: FOOTER_HEIGHT } },
};
 
/** Toast UI templates: the link in the footer, nothing in the header. */
export const MONTH_MORE_TEMPLATES = {
    // Toast still renders an (empty) button in the header; frontend.css and
    // admin-calendar.css hide it.
    monthGridHeaderExceed: () => '',
    monthGridFooterExceed: (hiddenEvents) => `<span class="webcal-month-more">${escapeHtml(moreLabel(hiddenEvents))}</span>`,
    monthGridFooter: () => '',
};
 
/** Space in px kept between the "more" popup and the window's edges. */
export const SEE_MORE_MARGIN = 8;
 
/** Space in px below the last event, as above the first. */
const SEE_MORE_LIST_END = 5;
 
/**
 * The "more" popup's box, tall enough for every event of the day.
 *
 * Toast UI sizes the popup for 26px per event but lays them out 29px apart,
 * cuts its list a further 7px short and stops counting at 10 events, so the
 * popup always came up short and hid some of the very events it was opened
 * for (wordpress.org support: "you still only see 3 events unless you use the
 * tiny scroll bars"). It grows by what its list is missing, up to the
 * window's height, and moves up rather than run off the bottom; only a day
 * with more events than the window holds still scrolls.
 *
 * @param {Object} box
 * @param {number} box.top            Popup top, relative to the window, in px.
 * @param {number} box.height         Popup height Toast UI gave it, in px.
 * @param {number} box.overflow       How much of its list is hidden, in px.
 * @param {number} box.viewportHeight Window height in px.
 * @param {number} [box.topInset]     Height in px covered at the top of the
 *                                    window (the WordPress toolbar).
 * @return {{top: number, height: number}} The box to give it.
 */
export const fitSeeMoreBox = ({ top, height, overflow, viewportHeight, topInset = 0 }) => {
    if (overflow <= 0) {
        return { top, height };
    }
    const topLimit = topInset + SEE_MORE_MARGIN;
    const bottomLimit = viewportHeight - SEE_MORE_MARGIN;
    const fitted = Math.max(height, Math.min(height + overflow + SEE_MORE_LIST_END, bottomLimit - topLimit));
    const newTop = top + fitted > bottomLimit
        ? Math.max(topLimit, bottomLimit - fitted)
        : top;
    return { top: newTop, height: fitted };
};
 
/**
 * Where the WordPress toolbar ends, for a signed-in user: it stays fixed at
 * the top of the window and would cover the popup's date and close button.
 *
 * @param {Document} doc
 * @return {number} Its bottom in px from the top of the window, or 0.
 */
const toolbarBottom = (doc) => {
    const bar = doc.getElementById('wpadminbar');
    return bar ? Math.max(0, bar.getBoundingClientRect().bottom) : 0;
};
 
/**
 * Resize an open "more" popup to show every event (see fitSeeMoreBox).
 *
 * Called from Toast UI's clickMoreEventsBtn, whose target is the popup's
 * container, positioned by Toast with an inline top and height.
 *
 * @param {?HTMLElement} container      .toastui-calendar-see-more-container
 * @param {number}       viewportHeight Window height in px.
 */
export const fitSeeMorePopup = (container, viewportHeight = window.innerHeight) => {
    const list = container?.querySelector('.toastui-calendar-month-more-list');
    if (!list) {
        return;
    }
    // Measured where it really is: themes' positioned wrappers, the admin
    // toolbar and the public calendar's popup corrector all move it from
    // the top Toast UI wrote.
    container.style.marginTop = '';
    const rect = container.getBoundingClientRect();
    const box = fitSeeMoreBox({
        top: rect.top,
        height: rect.height,
        overflow: list.scrollHeight - list.clientHeight,
        viewportHeight,
        topInset: toolbarBottom(container.ownerDocument),
    });
    container.style.height = `${box.height}px`;
    // Moved with a margin, not its top: the public calendar's corrector
    // rewrites any top it did not set (frontend.js), and a margin moves it
    // by the same amount whatever it is positioned against.
    container.style.marginTop = box.top === rect.top ? '' : `${box.top - rect.top}px`;
};
 
/**
 * Toast UI's clickMoreEventsBtn handler: fit the popup once it is in place.
 *
 * A frame later, once the popup has been drawn and the public calendar has
 * moved it into place.
 *
 * @param {{target: ?HTMLElement}} event Toast UI's event.
 */
export const onSeeMoreOpened = ({ target }) => {
    window.requestAnimationFrame(() => fitSeeMorePopup(target));
};