All files / owid-grapher/grapher/footer Footer.tsx

94.81% Statements 256/270
91.67% Branches 44/48
95.65% Functions 22/23
94.81% Lines 256/270

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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 2821x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 10x 10x 10x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 10x 10x 36x 36x 10x 10x 10x 10x 10x 10x 10x 10x 36x 36x 36x 10x 10x 36x 36x 36x 36x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 18x 18x 36x 36x 18x 18x 18x 18x 18x 18x 18x 18x 18x 36x 36x 1x 1x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x                     36x 36x 18x 18x 36x 36x 1x 1x 36x 36x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 5x 18x 18x 18x 18x 13x 5x 5x 5x 5x 5x 5x 5x 5x 5x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x                        
import * as React from "react"
import { observable, computed, action } from "mobx"
import { observer } from "mobx-react"
import parseUrl from "url-parse"
import { TextWrap } from "../text/TextWrap"
import { Bounds, DEFAULT_BOUNDS } from "../../clientUtils/Bounds"
import { getRelativeMouse } from "../../clientUtils/Util"
import { Tooltip } from "../tooltip/Tooltip"
import { BASE_FONT_SIZE, GrapherTabOption } from "../core/GrapherConstants"
import { FooterManager } from "./FooterManager"
 
@observer
export class Footer extends React.Component<{
    manager: FooterManager
    maxWidth?: number
}> {
    @computed private get maxWidth(): number {
        return this.props.maxWidth ?? DEFAULT_BOUNDS.width
    }
 
    @computed private get manager(): FooterManager {
        return this.props.manager
    }
 
    @computed private get sourcesText(): string {
        const sourcesLine = this.manager.sourcesLine
        return sourcesLine ? `Source: ${sourcesLine}` : ""
    }
 
    @computed private get noteText(): string {
        return this.manager.note ? `Note: ${this.manager.note}` : ""
    }
 
    @computed private get ccSvg(): string {
        if (this.manager.hasOWIDLogo)
            return `<a style="fill: #777;" class="cclogo" href="http://creativecommons.org/licenses/by/4.0/deed.en_US" target="_blank">CC BY</a>`
 
        return `<a href="https://ourworldindata.org" target="_blank">Powered by ourworldindata.org</a>`
    }
 
    @computed private get originUrlWithProtocol(): string {
        return this.manager.originUrlWithProtocol ?? "http://localhost"
    }
 
    @computed private get finalUrl(): string {
        const originUrl = this.originUrlWithProtocol
        const url = parseUrl(originUrl)
        return `https://${url.hostname}${url.pathname}`
    }
 
    @computed private get finalUrlText(): string | undefined {
        const originUrl = this.originUrlWithProtocol
 
        // Make sure the link back to OWID is consistent
        // And don't show the full url if there isn't enough room
        if (
            !originUrl ||
            !originUrl.toLowerCase().match(/^https?:\/\/./) ||
            this.manager.shouldLinkToOwid
        )
            return undefined
 
        const url = parseUrl(originUrl)
        const finalUrlText = `${url.hostname}${url.pathname}`.replace(
            "ourworldindata.org",
            "OurWorldInData.org"
        )
        if (
            Bounds.forText(finalUrlText, { fontSize: this.fontSize }).width >
            0.7 * this.maxWidth
        )
            return undefined
        return finalUrlText
    }
 
    @computed private get licenseSvg(): string {
        const { finalUrl, finalUrlText, ccSvg } = this
        if (!finalUrlText) return ccSvg
 
        return `*data-entry* • ${ccSvg}`.replace(
            /\*data-entry\*/,
            "<a target='_blank' style='fill: #777;' href='" +
                finalUrl +
                "'>" +
                finalUrlText +
                "</a>"
        )
    }
 
    @computed private get fontSize(): number {
        return 0.7 * (this.manager.fontSize ?? BASE_FONT_SIZE)
    }
 
    @computed private get sources(): TextWrap {
        const { maxWidth, fontSize, sourcesText } = this
        return new TextWrap({
            maxWidth,
            fontSize,
            text: sourcesText,
            linkifyText: true,
        })
    }
 
    @computed private get note(): TextWrap {
        const { maxWidth, fontSize, noteText } = this
        return new TextWrap({
            maxWidth,
            fontSize,
            text: noteText,
            linkifyText: true,
        })
    }
 
    @computed private get license(): TextWrap {
        const { maxWidth, fontSize, licenseSvg } = this
        return new TextWrap({
            maxWidth: maxWidth * 3,
            fontSize,
            text: licenseSvg,
            rawHtml: true,
        })
    }
 
    // Put the license stuff to the side if there's room
    @computed private get isCompact(): boolean {
        return this.maxWidth - this.sources.width - 5 > this.license.width
    }
 
    @computed private get paraMargin(): number {
        return 2
    }
 
    @computed get height(): number {
        if (this.manager.isMediaCard) return 0
 
        const { sources, note, license, isCompact, paraMargin } = this
        return (
            sources.height +
            (note.height ? paraMargin + note.height : 0) +
            (isCompact ? 0 : paraMargin + license.height)
        )
    }
 
    @action.bound private onSourcesClick(): void {
        this.manager.currentTab = GrapherTabOption.sources
    }
 
    renderStatic(targetX: number, targetY: number): JSX.Element | null {
        if (this.manager.isMediaCard) return null
 
        const { sources, note, license, maxWidth, isCompact, paraMargin } = this
 
        return (
            <g className="SourcesFooter" style={{ fill: "#777" }}>
                <g style={{ fill: "#777" }}>
                    {sources.render(targetX, targetY)}
                </g>
                {note.render(targetX, targetY + sources.height + paraMargin)}
                {isCompact
                    ? license.render(
                          targetX + maxWidth - license.width,
                          targetY
                      )
                    : license.render(
                          targetX,
                          targetY +
                              sources.height +
                              paraMargin +
                              (note.height ? note.height + paraMargin : 0)
                      )}
            </g>
        )
    }
 
    base: React.RefObject<HTMLDivElement> = React.createRef()
    @observable.ref tooltipTarget?: { x: number; y: number }
 
    @action.bound private onMouseMove(e: MouseEvent): void {
        const cc = this.base.current!.querySelector(".cclogo")
        if (cc && cc.matches(":hover")) {
            const div = this.base.current as HTMLDivElement
            const grapher = div.closest(".GrapherComponent")
            if (grapher) {
                const mouse = getRelativeMouse(grapher, e)
                this.tooltipTarget = { x: mouse.x, y: mouse.y }
            } else console.error("Grapher was falsy")
        } else this.tooltipTarget = undefined
    }
 
    componentDidMount(): void {
        window.addEventListener("mousemove", this.onMouseMove)
    }
 
    componentWillUnmount(): void {
        window.removeEventListener("mousemove", this.onMouseMove)
    }
 
    render(): JSX.Element {
        const { tooltipTarget } = this
 
        const license = (
            <div
                className="license"
                style={{
                    fontSize: this.license.fontSize,
                    lineHeight: this.sources.lineHeight,
                }}
            >
                {this.finalUrlText && (
                    <a href={this.finalUrl} target="_blank" rel="noopener">
                        {this.finalUrlText} •{" "}
                    </a>
                )}
                {this.manager.hasOWIDLogo ? (
                    <a
                        className="cclogo"
                        href="http://creativecommons.org/licenses/by/4.0/deed.en_US"
                        target="_blank"
                        rel="noopener"
                    >
                        CC BY
                    </a>
                ) : (
                    <a
                        href="https://ourworldindata.org"
                        target="_blank"
                        rel="noopener"
                    >
                        Powered by ourworldindata.org
                    </a>
                )}
            </div>
        )
 
        return (
            <footer
                className={
                    "SourcesFooterHTML" + (this.isCompact ? " compact" : "")
                }
                ref={this.base}
                style={{ color: "#777" }}
            >
                {this.isCompact && license}
                <p
                    style={this.sources.htmlStyle}
                    className="clickable"
                    onClick={this.onSourcesClick}
                >
                    {this.sources.renderHTML()}
                </p>
                {this.note && (
                    <p style={this.note.htmlStyle}>{this.note.renderHTML()}</p>
                )}
                {!this.isCompact && license}
                {tooltipTarget && (
                    <Tooltip
                        tooltipManager={this.manager}
                        x={tooltipTarget.x}
                        y={tooltipTarget.y}
                        style={{
                            textAlign: "center",
                            maxWidth: "300px",
                            whiteSpace: "inherit",
                            padding: "10px",
                            fontSize: "0.8em",
                        }}
                    >
                        <p>
                            Our World in Data charts are licensed under Creative
                            Commons; you are free to use, share, and adapt this
                            material. Click through to the CC BY page for more
                            information. Please bear in mind that the underlying
                            source data for all our charts might be subject to
                            different license terms from third-party authors.
                        </p>
                    </Tooltip>
                )}
            </footer>
        )
    }
}