-
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathproject_bom_export_controller.js
More file actions
176 lines (139 loc) · 4.99 KB
/
Copy pathproject_bom_export_controller.js
File metadata and controls
176 lines (139 loc) · 4.99 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
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
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
static values = {
url: String,
};
async export(event) {
event.preventDefault();
const tableElement = this.element
.closest('#bom-tab-pane')
?.querySelector('table');
if (!tableElement) {
throw new Error('Could not find the project BOM table.');
}
if (
typeof window.jQuery === 'undefined'
|| !window.jQuery.fn.DataTable.isDataTable(tableElement)
) {
throw new Error('The project BOM DataTable is not initialized.');
}
const dataTable = window.jQuery(tableElement).DataTable();
const ajaxParameters = dataTable.ajax.params();
const parameters = this.toSearchParameters(ajaxParameters);
/*
* Export the currently visible columns in their current display order.
* Exclude the picture column because it has no useful CSV value.
*/
dataTable
.columns()
.every(function () {
/*
* column.visible() returns DataTables' configured visibility.
*
* Responsive may hide a column visually and move it into a child
* row, but it does not change this configured visibility state.
*/
if (!this.visible()) {
return;
}
const index = this.index();
const settings = dataTable.settings()[0];
const columnSettings = settings.aoColumns[index];
const name = columnSettings.data;
/*
* The picture column is not useful in CSV.
*/
if (!name || name === 'picture') {
return;
}
const heading = this.header().textContent.trim();
parameters.append('exportColumns[]', name);
parameters.append(
'exportLabels[]',
heading || name
);
});
await this.downloadExport(parameters);
}
async downloadExport(parameters) {
const response = await fetch(this.urlValue, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
},
body: parameters.toString(),
credentials: 'same-origin',
});
if (!response.ok) {
const errorBody = await response.text();
console.error('BOM CSV export failed:', {
status: response.status,
statusText: response.statusText,
body: errorBody,
});
throw new Error(
`BOM CSV export failed with HTTP ${response.status}`
);
}
const blob = await response.blob();
const filename = this.getFilename(response)
?? 'project_bom.csv';
const downloadUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = filename;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(downloadUrl);
}
getFilename(response) {
const disposition = response.headers.get('Content-Disposition');
if (!disposition) {
return null;
}
/*
* Prefer RFC 5987 filename*=UTF-8''... when present.
*/
const encodedMatch = disposition.match(
/filename\*=UTF-8''([^;]+)/i
);
if (encodedMatch) {
return decodeURIComponent(encodedMatch[1]);
}
const filenameMatch = disposition.match(
/filename="?([^";]+)"?/i
);
return filenameMatch ? filenameMatch[1] : null;
}
/**
* Convert DataTables' nested AJAX object into query-string parameters.
*/
toSearchParameters(object) {
const parameters = new URLSearchParams();
const append = (key, value) => {
if (Array.isArray(value)) {
value.forEach((item, index) => {
append(`${key}[${index}]`, item);
});
return;
}
if (value !== null && typeof value === 'object') {
Object.entries(value).forEach(([childKey, childValue]) => {
const fullKey = key
? `${key}[${childKey}]`
: childKey;
append(fullKey, childValue);
});
return;
}
parameters.append(key, String(value ?? ''));
};
Object.entries(object).forEach(([key, value]) => {
append(key, value);
});
return parameters;
}
}