-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexportTriggerAndModal.js
More file actions
139 lines (125 loc) · 5.09 KB
/
Copy pathexportTriggerAndModal.js
File metadata and controls
139 lines (125 loc) · 5.09 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
/**
* @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 { DATA_EXPORT_TYPES } from '../../../domain/enums/DataExportTypes.js';
import errorAlert from '../errorAlert.js';
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 { selectedFields, selectedExportType, dataExportConfiguration } = exportModel;
const fields = Object.keys(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', DATA_EXPORT_TYPES.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 exportBtn = h('button.shadow-level1.btn.btn-success.mt2#send', {
disabled: !fieldsSelected,
onclick: async () => {
await exportModel.createExport();
modalHandler.dismiss();
},
}, 'Export');
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,
];
};
/**
* 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();
return h('div#export-data-modal', [
h('h2.w-50', 'Export data'),
exportModel.items.match({
NotAsked: () => errorAlert(),
Failure: (errors) => errorAlert(errors),
Loading: () => spinner({ size: 2, absolute: false }),
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');