-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathIndicatorChartEditor.ts
91 lines (77 loc) · 2.42 KB
/
IndicatorChartEditor.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
import { computed, observable, runInAction, when } from "mobx"
import {
AbstractChartEditor,
AbstractChartEditorManager,
References,
type EditorTab,
} from "./AbstractChartEditor.js"
export interface IndicatorChartInfo {
id: number
title?: string
variantName?: string
isChild: boolean
isInheritanceEnabled: boolean
isPublished: boolean
}
export interface IndicatorChartEditorManager
extends AbstractChartEditorManager {
variableId: number
references: References | undefined
isNewGrapher?: boolean
charts: IndicatorChartInfo[]
}
export class IndicatorChartEditor extends AbstractChartEditor<IndicatorChartEditorManager> {
@observable.ref _isNewGrapher: boolean | undefined = undefined
constructor(props: { manager: IndicatorChartEditorManager }) {
super(props)
when(
() => this.manager.isNewGrapher !== undefined,
() => (this._isNewGrapher = this.manager.isNewGrapher)
)
}
@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("debug")
return tabs
}
@computed get references(): References | undefined {
return this.manager.references
}
@computed get variableId(): number {
return this.manager.variableId
}
@computed get isNewGrapher(): boolean {
return this._isNewGrapher ?? false
}
@computed get charts(): IndicatorChartInfo[] {
return this.manager.charts
}
async saveGrapher({
onError,
}: { onError?: () => void } = {}): Promise<void> {
const { patchConfig, variableId } = this
const json = await this.manager.admin.requestJSON(
`/api/variables/${variableId}/grapherConfigAdmin`,
patchConfig,
"PUT"
)
if (json.success) {
runInAction(() => {
this.savedPatchConfig = json.savedPatch
this._isNewGrapher = false
})
} else {
onError?.()
}
}
}
export function isIndicatorChartEditorInstance(
editor: AbstractChartEditor
): editor is IndicatorChartEditor {
return editor instanceof IndicatorChartEditor
}