-
Notifications
You must be signed in to change notification settings - Fork 322
/
exportMenu.ts
186 lines (152 loc) Β· 5.79 KB
/
exportMenu.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
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
import Component from './component';
import { Categories, ChartState, Options, Series } from '@t/store/store';
import { ExportMenuModels } from '@t/components/exportMenu';
import { isExportMenuVisible, padding } from '@src/store/layout';
import { TitleOption } from '@t/options';
import { execDownload, downloadSpreadSheet } from '@src/helpers/downloader';
import { isString } from '@src/helpers/utils';
import { RectResponderModel } from '@t/components/series';
import { ExportMenuTheme, ExportMenuButtonTheme, FontTheme, ExportMenuPanelTheme } from '@t/theme';
import { getFontStyleString, getTranslateString } from '@src/helpers/style';
import { getScrollPosition } from '@src/helpers/tooltip';
const EXPORT_MENU_WIDTH = 140;
export const BUTTON_RECT_SIZE = 24;
export interface DataToExport {
series: Series;
categories?: Categories;
}
export default class ExportMenu extends Component {
responders!: RectResponderModel[];
models: ExportMenuModels = { exportMenuButton: [] };
opened = false;
fileName!: string;
data!: DataToExport;
chartEl!: HTMLDivElement;
exportMenuEl!: HTMLDivElement;
theme!: Required<ExportMenuTheme>;
chartBackgroundColor!: string;
toggleExportMenu = () => {
this.opened = !this.opened;
this.models.exportMenuButton[0].opened = this.opened;
this.eventBus.emit('needDraw');
if (this.opened) {
this.applyPanelWrapperStyle();
this.chartEl.appendChild(this.exportMenuEl);
} else {
this.chartEl.removeChild(this.exportMenuEl);
}
};
getCanvasExportBtnRemoved = () => {
const canvas = this.chartEl.getElementsByTagName('canvas')[0];
const ctx = canvas.getContext('2d')!;
const { x, y, height: h, width: w } = this.rect;
['#fff', this.chartBackgroundColor].forEach((color) => {
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
});
return canvas;
};
onClickExportButton = (ev) => {
const { id } = ev.target;
if (id === 'png' || id === 'jpeg') {
const canvas = this.getCanvasExportBtnRemoved();
execDownload(this.fileName, id, canvas.toDataURL(`image/${id}`, 1));
} else {
downloadSpreadSheet(this.fileName, id, this.data);
}
this.toggleExportMenu();
};
applyExportButtonPanelStyle() {
const exportMenuTitle = this.exportMenuEl.querySelector('.toastui-chart-export-menu-title')!;
const menuBtnWrapper = this.exportMenuEl.querySelector(
'.toastui-chart-export-menu-btn-wrapper'
)!;
exportMenuTitle.setAttribute('style', this.makePanelStyle('header'));
menuBtnWrapper.setAttribute('style', this.makePanelStyle('body'));
}
makeExportMenuButton() {
const el = document.createElement('div');
el.onclick = this.onClickExportButton;
el.innerHTML = `
<div class="toastui-chart-export-menu">
<p class="toastui-chart-export-menu-title">Export to</p>
<div class="toastui-chart-export-menu-btn-wrapper">
<button class="toastui-chart-export-menu-btn" id="xls">xls</button>
<button class="toastui-chart-export-menu-btn" id="csv">csv</button>
<button class="toastui-chart-export-menu-btn" id="png">png</button>
<button class="toastui-chart-export-menu-btn" id="jpeg">jpeg</button>
</div>
</div>
`;
return el;
}
initialize({ chartEl }) {
this.chartEl = chartEl;
this.type = 'exportMenu';
this.name = 'exportMenu';
this.exportMenuEl = this.makeExportMenuButton();
}
onClick({ responders }: { responders: RectResponderModel[] }) {
if (responders.length) {
this.toggleExportMenu();
}
}
getFileName(title?: string | TitleOption) {
return isString(title) ? title : title?.text ?? 'toast-ui-chart';
}
render({ options, layout, chart, series, rawCategories, theme }: ChartState<Options>) {
this.isShow = isExportMenuVisible(options);
if (!this.isShow) {
return;
}
this.chartBackgroundColor = theme.chart.backgroundColor!;
this.theme = theme.exportMenu as Required<ExportMenuTheme>;
this.data = { series, categories: rawCategories };
this.fileName = this.getFileName(options?.exportMenu?.filename || chart.title);
this.applyExportButtonPanelStyle();
this.rect = layout.exportMenu;
this.models.exportMenuButton = [
{
type: 'exportMenuButton',
x: 0,
y: 0,
opened: this.opened,
theme: this.theme.button as Required<ExportMenuButtonTheme>,
},
];
this.responders = [
{
type: 'rect',
width: BUTTON_RECT_SIZE,
height: BUTTON_RECT_SIZE,
x: 0,
y: 0,
},
];
}
applyPanelWrapperStyle() {
const exportMenu = this.exportMenuEl.querySelector('.toastui-chart-export-menu')!;
const { top, left, width } = this.chartEl.getBoundingClientRect();
const { scrollX, scrollY } = getScrollPosition();
const x = left + width - EXPORT_MENU_WIDTH - padding.X + scrollX;
const y = top + padding.Y + BUTTON_RECT_SIZE + 5 + scrollY;
const { borderRadius, borderWidth, borderColor } = this.theme.panel;
const style = `
transform: ${getTranslateString(x, y)};
border: ${borderWidth}px solid ${borderColor};
border-radius: ${borderRadius}px;`;
exportMenu.setAttribute('style', style);
}
makePanelStyle(type: 'header' | 'body') {
const sectionTheme = this.theme.panel![type];
const direction = type === 'header' ? 'top' : 'bottom';
const { borderRadius, borderWidth } = this.theme.panel as Required<ExportMenuPanelTheme>;
const borderRadiusPx = `${borderRadius - borderWidth}px`;
return [
`${getFontStyleString(sectionTheme as FontTheme)}`,
`border-${direction}-left-radius: ${borderRadiusPx};`,
`border-${direction}-right-radius: ${borderRadiusPx};`,
`background-color: ${sectionTheme!.backgroundColor};`,
].join('');
}
}