-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexportTriggerAndModal.js
More file actions
147 lines (130 loc) · 5.38 KB
/
Copy pathexportTriggerAndModal.js
File metadata and controls
147 lines (130 loc) · 5.38 KB
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
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import spinner from '../spinner.js';
import { h } from '/js/src/index.js';
/**
* Export form component, containing the fields to export, the export type and the export button
*
* @param {DataExportModel} exportModel export model
* @param {ModalHandler} modalHandler The modal handler, used to dismiss modal after export
*
* @return {Component} the form component
*/
const exportForm = (exportModel, modalHandler) => {
const exportTypes = ['JSON', 'CSV'];
const { selectedFields } = exportModel;
const { selectedExportType } = exportModel;
const fields = Object.keys(exportModel.dataExportConfiguration);
const fieldsSelected = selectedFields.length > 0;
const fieldsSelectionLabels = [
h('label.form-check-label.f4.mt1', { for: 'fields' }, 'Fields'),
h(
'label.form-check-label.f6',
{ for: 'fields' },
'Select which fields to be exported. (CTRL + click for multiple selection)',
),
];
const fieldsSelection = h('select#fields.form-control', {
style: 'min-height: 20rem;',
multiple: true,
onchange: ({ target }) => exportModel.setSelectedFields(target.selectedOptions),
}, [
...fields
.filter((name) => !['id', 'actions'].includes(name))
.map((name) => h('option', {
value: name,
selected: selectedFields.length ? selectedFields.includes(name) : false,
}, name)),
]);
const exportTypeSelectionLabels = [
h('label.form-check-label.f4.mt1', 'Export type'),
h('label.form-check-label.f6', 'Select output format'),
];
const exportTypeSelect = h('.flex-row.g3', exportTypes.map((exportType) => {
const id = `data-export-type-${exportType}`;
return h('.form-check', [
h('input.form-check-input', {
id,
type: 'radio',
value: exportType,
checked: selectedExportType.length ? selectedExportType.includes(exportType) : false,
name: 'export-type',
onclick: () => exportModel.setSelectedExportType(exportType),
}),
h('label.form-check-label', {
for: id,
}, exportType),
]);
}));
const dataAvailable = exportModel.items.match({ Success: () => true, Other: () => false });
const exportBtn = h('button.shadow-level1.btn.btn-success.mt2#send', {
disabled: !(fieldsSelected && dataAvailable),
onclick: async () => {
await exportModel.createExport();
modalHandler.dismiss();
},
}, dataAvailable ? 'Export' : 'Loading data...');
const dataLength = exportModel.items.match({ Success: ({ length } = {}) => length, Other: () => null });
const { totalExistingItemsCount } = exportModel;
const truncatedDataInfo = dataLength && dataLength < totalExistingItemsCount
? h(
'#truncated-export-warning.warning',
`The data export is limited to ${dataLength} entries, only the most recent data will be exported`,
)
: null;
return [
truncatedDataInfo,
fieldsSelectionLabels,
fieldsSelection,
exportTypeSelectionLabels,
exportTypeSelect,
exportBtn,
];
};
const errorDisplay = () => h('.danger', 'Data fetching failed');
/**
* A function to construct the exports data screen
*
* @param {DataExportModel} exportModel export model
* @param {ModalHandler} modalHandler The modal handler, used to dismiss modal after export
* @return {Component} Return the view of the inputs
*/
const exportModal = (exportModel, modalHandler) => {
exportModel.callForData();
const dataLoading = exportModel.items.match({ Loading: () => true, Other: () => false });
return h('div#export-data-modal', [
h('.flex-row', [
h('h2.w-50', 'Export data'),
dataLoading ? h('.w-50', spinner({ size: 2, absolute: false })) : null,
]),
exportModel.items.match({
NotAsked: () => errorDisplay(),
Failure: () => errorDisplay(),
Other: () => exportForm(exportModel, modalHandler),
}),
]);
};
/**
* Builds a button which will open popover for data export
*
* @param {DataExportModel} exportModel export model
* @param {ModalModel} modalModel modal model
* @param {object} [displayConfiguration] additional display options
* @param {boolean} [displayConfiguration.autoMarginLeft = true] if true margin left is set to auto, otherwise not
* @returns {Component} button
*/
export const exportTriggerAndModal = (exportModel, modalModel, { autoMarginLeft = true } = {}) =>
h(`button.btn.btn-primary${autoMarginLeft ? '.mlauto' : ''}#export-data-trigger`, {
disabled: exportModel.disabled,
onclick: () => modalModel.display({ content: (modalModel) => exportModal(exportModel, modalModel), size: 'medium' }),
}, 'Export data');