-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ChartViewEditor.ts
114 lines (97 loc) · 3.29 KB
/
ChartViewEditor.ts
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
import { computed, runInAction } from "mobx"
import {
AbstractChartEditor,
AbstractChartEditorManager,
References,
type EditorTab,
} from "./AbstractChartEditor.js"
import { ENV } from "../settings/clientSettings.js"
import {
CHART_VIEW_PROPS_TO_OMIT,
CHART_VIEW_PROPS_TO_PERSIST,
GrapherInterface,
} from "@ourworldindata/types"
import { diffGrapherConfigs, omit, pick } from "@ourworldindata/utils"
// Don't yet show chart views in the admin interface
// This is low-stakes - if it shows up anyhow (e.g. on staging servers), it's not a big deal.
// TODO: Remove this flag once we're launching this feature
export const chartViewsFeatureEnabled = ENV === "development"
export interface Chart {
id: number
title?: string
variantName?: string
isChild: boolean
}
export interface ChartViewEditorManager extends AbstractChartEditorManager {
chartViewId: number
parentChartId: number
}
export class ChartViewEditor extends AbstractChartEditor<ChartViewEditorManager> {
constructor(props: { manager: ChartViewEditorManager }) {
super(props)
}
@computed
get availableTabs(): EditorTab[] {
const tabs: EditorTab[] = ["basic", "data", "text", "customize"]
if (this.grapher.hasMapTab) tabs.push("map")
if (this.grapher.isScatter) tabs.push("scatter")
if (this.grapher.isMarimekko) tabs.push("marimekko")
tabs.push("refs")
tabs.push("export")
tabs.push("debug")
return tabs
}
@computed get references(): References | undefined {
// Not yet implemented for chart views
return undefined
}
@computed override get patchConfig(): GrapherInterface {
const config = omit(
this.liveConfigWithDefaults,
CHART_VIEW_PROPS_TO_OMIT
)
const patchToParentChart = diffGrapherConfigs(
config,
this.activeParentConfigWithDefaults || {}
)
return {
...patchToParentChart,
// We want to make sure we're explicitly persisting some props like entity selection
// always, so they never change when the parent chart changes.
// For this, we need to ensure we include the default layer, so that we even
// persist these props when they are the same as the default.
...pick(this.liveConfigWithDefaults, CHART_VIEW_PROPS_TO_PERSIST),
}
}
@computed get chartViewId(): number {
return this.manager.chartViewId
}
@computed get isNewGrapher(): boolean {
return false
}
@computed get parentChartId(): number {
return this.manager.parentChartId
}
async saveGrapher({
onError,
}: { onError?: () => void } = {}): Promise<void> {
const { patchConfig, chartViewId } = this
const json = await this.manager.admin.requestJSON(
`/api/chartViews/${chartViewId}`,
{ config: patchConfig },
"PUT"
)
if (json.success) {
runInAction(() => {
this.savedPatchConfig = json.savedPatch
})
} else {
onError?.()
}
}
}
export function isChartViewEditorInstance(
editor: AbstractChartEditor
): editor is ChartViewEditor {
return editor instanceof ChartViewEditor
}