All files / src/custom-element/helpers/managers popupManager.ts

16.13% Statements 5/31
0% Branches 0/12
0% Functions 0/4
16.13% Lines 5/31

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 1041x     1x           1x                                                                                                                                                                                 1x         1x
import getAllChildren from "./getAllChildren";
 
// The elements being shown
let _shownElements: HTMLElement[] = [];
 
// After the setShown method gets called, the global handler gets called with the same element as a target
// So to avoid extra processing we set this variable to test if set in the global handler
let _justShown: HTMLElement;
 
const popupManager = {
 
    /**
     * Called when a popup source is going to show its content so other popups need to be hidden
     * @param element The popup source that is going to be shown
     */
    setShown(element: HTMLElement) {
 
        let count = _shownElements.length;
 
        while (count > 0) {
 
            const shownElement = _shownElements[count - 1]; // Peek the last element
 
            if (shownElement !== element &&
                !getAllChildren(shownElement as any).includes(element as any)) { // Do not close nested popups
 
                (shownElement as any).hideContent(); // Hide the shown element (and this causes the element to call setHidden which pops the element as well)
            }
 
            --count;
        }
 
        _shownElements.push(element);
 
        _justShown = element;
    },
 
    /**
     * Called when a popup source is going to hide its content so it can be removed from the shown items in the manager
     * @param element  The popup source that is going to be hidden
     */
    setHidden(element: HTMLElement) {
 
        while (_shownElements.length > 0) {
 
            const shownElement = _shownElements[_shownElements.length - 1]; // Peek the last element
 
            if (shownElement !== element) { // Hide any nested elements of the element
 
                (shownElement as any).hideContent(); // Hide the shown element
 
                _shownElements.pop(); // Remove the now hidden element
 
            }
            else { // Remove the element itself
 
                _shownElements.pop(); // Remove the now hidden element
 
                break; // Done
            }
        }
 
    },
 
    /**
     * Any target clicked and captured using the global click handler
     * @param target 
     */
    handleGlobal(target: HTMLElement) {
 
        if (_justShown !== undefined) { // If the target was just requested to shown, do nothing
 
            _justShown = undefined;
 
            return;
        }
 
        let count = _shownElements.length;
 
        while (count > 0) {
 
            const shownElement = _shownElements[count - 1]; // Peek the last element
 
            if (!shownElement.contains(target)
                && (target as any).dropdown !== shownElement) { // handle combo boxes
 
                (shownElement as any).hideContent(); // Hide the shown element (and this causes the element to call setHidden which pops the element as well)
            }
            else {
 
                break; // Done when the above condition is not longer true
            }
 
            --count;
        }
    }
};
 
window.onclick = function (event: any) {
 
    popupManager.handleGlobal(event.target);
}
 
export default popupManager;