All files / owid-grapher/grapher/mapCharts MapConfig.ts

95% Statements 76/80
88.89% Branches 8/9
100% Functions 4/4
95% Lines 76/80

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 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 224x 224x 224x 224x 224x 224x 224x 224x 224x 224x 224x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 59x 59x 59x 59x 59x 59x 59x 59x 59x 58x 58x 59x 59x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x         5x 5x 5x 1x 1x 219x 219x 219x 1x 1x
import { observable } from "mobx"
import { MapProjectionName } from "./MapProjections"
import { ColorScaleConfig } from "../color/ColorScaleConfig"
import { ColumnSlug, OwidVariableId } from "../../clientUtils/owidTypes"
import {
    Persistable,
    updatePersistables,
    objectWithPersistablesToObject,
    deleteRuntimeAndUnchangedProps,
} from "../../clientUtils/persistable/Persistable"
import {
    maxTimeBoundFromJSONOrPositiveInfinity,
    maxTimeToJSON,
} from "../../clientUtils/TimeBounds"
import { trimObject, NoUndefinedValues } from "../../clientUtils/Util"
 
// MapConfig holds the data and underlying logic needed by MapTab.
// It wraps the map property on ChartConfig.
// TODO: migrate database config & only pass legend props
class MapConfigDefaults {
    @observable columnSlug?: ColumnSlug
    @observable time?: number
    @observable timeTolerance?: number
    @observable hideTimeline?: boolean
    @observable projection = MapProjectionName.World
 
    @observable colorScale = new ColorScaleConfig()
    // Show the label from colorSchemeLabels in the tooltip instead of the numeric value
    @observable tooltipUseCustomLabels?: boolean = undefined
}
 
export type MapConfigInterface = MapConfigDefaults
 
interface MapConfigWithLegacyInterface extends MapConfigInterface {
    variableId?: OwidVariableId
    targetYear?: number
}
 
export class MapConfig extends MapConfigDefaults implements Persistable {
    updateFromObject(obj: Partial<MapConfigWithLegacyInterface>): void {
        // Migrate variableIds to columnSlugs
        if (obj.variableId && !obj.columnSlug)
            obj.columnSlug = obj.variableId.toString()
 
        updatePersistables(this, obj)
 
        // Migrate "targetYear" to "time"
        // TODO migrate the database property instead
        if (obj.targetYear)
            this.time = maxTimeBoundFromJSONOrPositiveInfinity(obj.targetYear)
        else if (obj.time)
            this.time = maxTimeBoundFromJSONOrPositiveInfinity(obj.time)
    }
 
    toObject(): NoUndefinedValues<MapConfigWithLegacyInterface> {
        const obj = objectWithPersistablesToObject(
            this
        ) as MapConfigWithLegacyInterface
        deleteRuntimeAndUnchangedProps(obj, new MapConfigDefaults())
 
        if (obj.time) obj.time = maxTimeToJSON(this.time) as any
 
        if (obj.columnSlug) {
            // Restore variableId for legacy for now
            obj.variableId = parseInt(obj.columnSlug)
            delete obj.columnSlug
        }
 
        return trimObject(obj)
    }
 
    constructor(obj?: Partial<MapConfigWithLegacyInterface>) {
        super()
        if (obj) this.updateFromObject(obj)
    }
}