-
Notifications
You must be signed in to change notification settings - Fork 346
/
export_controls.ts
144 lines (128 loc) · 4.64 KB
/
export_controls.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
/**
* @fileoverview A resusable component for downloading data in LIT
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable:no-new-decorators
import './popup_container';
import {html} from 'lit';
import {classMap} from 'lit/directives/class-map.js';
import {customElement, property} from 'lit/decorators.js';
import * as papa from 'papaparse';
import {ReactiveElement} from '../lib/elements';
import {styles as sharedStyles} from '../lib/shared_styles.css';
import {styles} from './export_controls.css';
import {PopupContainer} from './popup_container';
import {SortableTableEntry} from './table';
/**
* An element that handles logic for downloading data.
*/
@customElement('export-controls')
export class ExportControls extends ReactiveElement {
static override get styles() {
return [sharedStyles, styles];
}
/** The default file download name. */
@property({type: String}) downloadFilename = 'data.csv';
/** A list of rows of data to download. */
@property({type: Object}) data: SortableTableEntry[][] = [];
/** Column names. */
@property({type: Object}) columnNames: string[] = [];
/** Download popup position defaults to below the download icon. */
@property({type: String}) popupPosition = 'below';
@property({type: String}) tooltipPosition = 'left';
/** If true, disable controls. */
@property({type: Boolean}) disabled = false;
getCSVContent(): string {
return papa.unparse(
{fields: this.columnNames, data: this.data},
{newline: '\r\n'});
}
getPopupClasses() {
return classMap({
'hidden': this.disabled,
'download-popup': true,
'above': this.popupPosition === 'above'
});
}
/**
* Renders the copy and download buttons to download data.
*/
override render() {
const copyCSV = () => {
const csvContent = this.getCSVContent();
navigator.clipboard.writeText(csvContent);
};
const downloadCSV = () => {
const csvContent = this.getCSVContent();
const blob = new Blob([csvContent], {type: 'text/csv'});
const a = window.document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = this.downloadFilename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
const controls: PopupContainer =
this.shadowRoot!.querySelector('popup-container.download-popup')!;
controls.expanded = false;
};
const updateFilename = (e: Event) => {
this.downloadFilename = (e.target as HTMLInputElement).value;
};
function onEnter(e: KeyboardEvent) {
if (e.key === 'Enter') downloadCSV();
}
const iconClass = classMap({
'icon-button': true,
'mdi-outlined': true,
'disabled': this.disabled,
});
// TODO(b/265978596): Consider a better place for download tooltips.
// clang-format off
return html`
<div id='export-controls'>
<lit-tooltip content="Copy ${this.data.length} rows as CSV"
tooltipPosition=${this.tooltipPosition}>
<mwc-icon class=${iconClass} slot="tooltip-anchor" @click=${copyCSV}>
file_copy
</mwc-icon>
</lit-tooltip>
<popup-container class='${this.getPopupClasses()}'>
<lit-tooltip content="Download ${this.data.length} rows as CSV"
tooltipPosition="${this.tooltipPosition}" slot="toggle-anchor">
<mwc-icon class=${iconClass} slot="tooltip-anchor">
file_download
</mwc-icon>
</lit-tooltip>
<div class='download-popup-controls'>
<label for="filename">Filename</label>
<input type="text" name="filename" value=${this.downloadFilename}
@input=${updateFilename} @keydown=${onEnter}>
<button class='download-button filled-button nowrap'
@click=${downloadCSV}
?disabled=${!this.downloadFilename}>
Download ${this.data.length} rows
</button>
</div>
</popup-container>
</div>`;
// clang-format on
}
}
declare global {
interface HTMLElementTagNameMap {
'export-controls': ExportControls;
}
}