# All files util.js

10.71% Statements 3/28
7.69% Branches 2/26
16.67% Functions 1/6
10.71% Lines 3/28

    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

    4x
    4x
    4x

     export function isScrolledIntoView(element, useDOM) {
        if (!useDOM) {
            return false;
        }
        const elementTop = element.getBoundingClientRect().top;
        const elementBottom = element.getBoundingClientRect().bottom;
        return (
            (elementTop <= 0 && elementBottom >= 0) ||
            (elementTop >= 0 && elementBottom <= window.innerHeight) ||
            (elementTop <= window.innerHeight && elementBottom >= window.innerHeight)
        );
    }

    export function getWindowHeight(useDOM) {
        if (!useDOM) {
            return 0;
        }

        const w = window;
        const d = document;
        const e = d.documentElement;
        const g = d.getElementsByTagName('body')[0];

        return w.innerHeight || e.clientHeight || g.clientHeight;
    }

    export function getNodeHeight(useDOM, parent) {
        if (!useDOM) {
            return 0;
        }

        if (!parent) {
            return getWindowHeight(useDOM);
        }

        return parent.clientHeight;
    }

    export function canUseDOM() {
        return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
    }

    export function getPercentage(startpos, endpos, currentpos) {
        const distance = endpos - startpos;
        const displacement = currentpos - startpos;
        return displacement / distance || 0;
    }

    export function getRelativePosition(node, useDOM, parent) {
        if (!useDOM) {
            return 0;
        }
        const element = node;
        let y = Math.round(element.getBoundingClientRect().top);
        const parentHeight = getNodeHeight(useDOM);
        y = y > parentHeight ? parentHeight : y;

        return getPercentage(0, parentHeight, y);
    }