-
Notifications
You must be signed in to change notification settings - Fork 1
/
_s3_resources.html.erb
200 lines (190 loc) · 6.96 KB
/
_s3_resources.html.erb
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<section class="files-section">
<div class="lux">
<div class="card">
<div class="files card-body">
<h3>
<div id="total-file-count-spinner" class="spinner-border spinner-border-sm" role="status"></div>
<span id="total-file-count-in-table"></span> Files or Directories in <%= @work.approved? ? "post-curation" : "pre-curation" %> storage
</h3>
<table id="files-table" class="table">
<thead>
<tr>
<th scope="col" nowrap="nowrap"><span>Filename</span></th>
<th scope="col"><span>Last Modified</span></th>
<th scope="col"><span>Size</span></th>
<th scope="col"><span></span></th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot></tfoot>
</table>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$(function() {
// work.id is nil when the form is rendered during work creation and the work has not been saved
var work = '<%= @work %>';
let fileListUrl;
if (work) {
fileListUrl = '<%= @work_decorator.file_list_path %>';
} else {
fileListUrl = '<%= root_path %>';
}
var isEditMode = <%= edit_mode %>;
// Notice that Ruby's `defined?` does not return a boolean, hence the `!= nil` comparison.
var isWizardMode = <%= defined?(wizard_mode) != nil && wizard_mode == true %>;
// Wire DataTable for the file list.
// Related documentation
// AJAX loading: https://datatables.net/manual/ajax
// Column display configuration: https://datatables.net/examples/advanced_init/column_render.html
// Row Id: https://datatables.net/reference/option/rowId
let $fileTable = $('#files-table');
let fileTable;
const $filesTableWrapper = $('#files-table_wrapper');
const filesTableLength = $('#files-table_length');
if (filesTableLength.length > 0) {
if (DataTable.isDataTable($fileTable)) {
fileTable = $fileTable.dataTable();
const fileTableApi = fileTable.api();
fileTableApi.destroy();
} else {
$fileTable = $fileTable.detach();
const $cardBody = $('.files.card-body');
$cardBody.append($fileTable);
}
$filesTableWrapper.remove();
}
filesTable = $fileTable.DataTable({
select: true,
ajax: {
url: fileListUrl,
dataSrc: function(json) {
// The JSON payload includes a few extra properties (fetched via AJAX because
// they be slow for large datasets). Here we pick up those properties and update
// the display with them.
$("#total-file-size").text(json.total_size_display);
$("#total-file-size-spinner").hide();
$("#total-file-count-in-table").text(json.total_file_count.toLocaleString("en-US"));
$("#total-file-count-spinner").hide();
// The JSON payload include the file list in the `data` property
// and that's what we return to DataTables as the "source" data.
return json.data;
}
},
rowId: 'safe_id',
columns: [
{ data: 'filename' },
{ data: 'last_modified_display' },
{ data: 'display_size' },
{ data: 'filename' }
],
columnDefs: [
{
render: function (data, type, row) {
// filename
if (type == "display") {
var html;
if (row.filename_display.startsWith("*")) {
// On edit mode, file is marked to be deleted display strikethrough
html = `<span><s>${row.filename_display.substring(1)}</s></span>`;
} else {
if (row.size == 0) {
// Display the filename as text (i.e. not a hyperlink)
html = `<span>
<i class="bi bi-dash-square"></i>
${row.filename_display}
</span>`;
} else {
// Display as a hyperlink with the download icon
// (Note: Interpolation in the client is interpreted after the server-side Ruby builds the download path)
let downloadUrl;
if (work) {
downloadUrl = `<%= @work_decorator.download_path %>?filename=${data}`;
} else {
downloadUrl = '<%= root_path %>';
}
var encodedUrl = window.encodeURI(downloadUrl);
html = `<span>
<i class="bi bi-file-arrow-down"></i>
<a href="${encodedUrl}" target="_blank">${row.filename_display}</a>
</span>`;
}
}
return html;
}
// Force any readme file to sort to the top
var sortValue;
if (data.toLowerCase().includes("readme")) {
sortValue = "A" + data;
} else {
sortValue = "Z" + data;
}
return sortValue;
},
targets: 0,
},
{
render: function (data, type, row) {
// last_modified_display
if (type == "display") {
return data;
}
return row.last_modified; // sortable, e.g. yyyy-mm-dd hh:mm
},
targets: 1,
},
{
render: function (data, type, row) {
// size
if (type == "display") {
if (row.is_folder === true) {
// display no size for folders
return "";
} else {
// human readable (e.g. 34 KB)
return row.display_size;
}
}
// raw bytes
return row.size;
},
targets: 2,
className: 'dt-right'
},
{
render: function (data, type, row) {
// Don't render the delete icon for the readme while in the Wizard
if (isWizardMode && row.filename.includes("README")) {
return "";
}
// delete icon
var html = null;
if (type == "display") {
if (row.filename_display.startsWith("*")) {
html = `<span>
<a class="undo-delete-file" data-safe_id=${row.safe_id} data-filename=${row.filename} data-filename_display=${row.filename_display} href="#">
Undo delete
</a>
</span>`;
} else {
html = `<span>
<a class="delete-file" data-safe_id=${row.safe_id} data-filename=${row.filename} data-filename_display=${row.filename_display} href="#" id="delete-file-${row.safe_id}">
Delete file
</a>
</span>`;
}
}
return html;
},
targets: 3,
className: 'dt-right',
sortable: false,
visible: isEditMode
}
]
});
});
</script>