All files / owid-grapher/site bakeGlobalEntitySelector.tsx

48.89% Statements 22/45
100% Branches 0/0
0% Functions 0/1
48.89% Lines 22/45

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 351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                               1x
import * as React from "react"
import * as ReactDOMServer from "react-dom/server"
import { ENV } from "../settings/serverSettings"
import { GlobalEntitySelector } from "../grapher/controls/globalEntitySelector/GlobalEntitySelector"
import {
    GLOBAL_ENTITY_SELECTOR_DATA_ATTR,
    GLOBAL_ENTITY_SELECTOR_ELEMENT,
} from "../grapher/controls/globalEntitySelector/GlobalEntitySelectorConstants"
import { SelectionArray } from "../grapher/selection/SelectionArray"
 
export const bakeGlobalEntitySelector = (cheerioEl: CheerioStatic) => {
    // The data attr used to be `data-entity-select`, but later changed for consistency in the code.
    // But we should still support the old attribute.
    cheerioEl(`*[data-entity-select], ${GLOBAL_ENTITY_SELECTOR_ELEMENT}`).each(
        (_, el) => {
            const $el = cheerioEl(el)
            const $section = $el.closest("section")

            const rendered = ReactDOMServer.renderToString(
                <GlobalEntitySelector
                    environment={ENV}
                    selection={new SelectionArray()}
                />
            )

            // Move the element to top-level where <section>s are,
            // in order to make position:sticky work.
            $el.remove()
            $el.attr(GLOBAL_ENTITY_SELECTOR_DATA_ATTR, "")
            $el.addClass("global-entity-control-container")
            $el.html(rendered).insertAfter($section)
        }
    )
}