-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPasteLinksUpload.vue
More file actions
466 lines (404 loc) · 16.3 KB
/
PasteLinksUpload.vue
File metadata and controls
466 lines (404 loc) · 16.3 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<script setup lang="ts">
import { faLink, faPlus, faTimes } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { BFormInput } from "bootstrap-vue";
import { computed, nextTick, ref, watch } from "vue";
import type { TableField } from "@/components/Common/GTable.types";
import { useBulkUploadOperations } from "@/composables/upload/bulkUploadOperations";
import { useCollectionCreation } from "@/composables/upload/collectionCreation";
import { useUploadAdvancedMode } from "@/composables/upload/uploadAdvancedMode";
import { useUploadDefaults } from "@/composables/upload/uploadDefaults";
import { useUploadItemValidation } from "@/composables/upload/uploadItemValidation";
import { useUploadReadyState } from "@/composables/upload/uploadReadyState";
import { useUploadStaging } from "@/composables/upload/useUploadStaging";
import { buildPreparedUpload } from "@/utils/upload";
import { mapToPasteUrlUpload } from "@/utils/upload/itemMappers";
import { extractNameFromUrl, isValidUrl, validateUrl } from "@/utils/url";
import type { PreparedUpload, UploadMethodComponent, UploadMethodConfig } from "../types";
import type { PasteUrlItem } from "../types/uploadItem";
import CollectionCreationConfig from "../CollectionCreationConfig.vue";
import UploadTableBulkDbKeyHeader from "../shared/UploadTableBulkDbKeyHeader.vue";
import UploadTableBulkExtensionHeader from "../shared/UploadTableBulkExtensionHeader.vue";
import UploadTableDbKeyCell from "../shared/UploadTableDbKeyCell.vue";
import UploadTableExtensionCell from "../shared/UploadTableExtensionCell.vue";
import UploadTableNameCell from "../shared/UploadTableNameCell.vue";
import UploadTableOptionsCell from "../shared/UploadTableOptionsCell.vue";
import UploadTableOptionsHeader from "../shared/UploadTableOptionsHeader.vue";
import GButton from "@/components/BaseComponents/GButton.vue";
import GTable from "@/components/Common/GTable.vue";
interface Props {
method: UploadMethodConfig;
/** History ID where uploaded datasets will be added. */
targetHistoryId: string;
/** Allow creating dataset collections from pasted URLs. */
allowCollections?: boolean;
/** Optional list of allowed formats to constrain selectable extensions. */
formats?: string[];
/** When false, restrict to a single URL entry. */
multiple?: boolean;
/** When true, do not persist staging to the shared store (modal use). */
transient?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
allowCollections: true,
formats: undefined,
multiple: true,
transient: false,
});
const emit = defineEmits<{
(e: "ready", ready: boolean): void;
}>();
const { advancedMode } = useUploadAdvancedMode();
const { effectiveExtensions, listDbKeys, configurationsReady, createItemDefaults } = useUploadDefaults(props.formats);
const tableContainerRef = ref<HTMLElement | null>(null);
const collectionConfigComponent = ref<InstanceType<typeof CollectionCreationConfig> | null>(null);
const { buildCollectionConfig, collectionState, handleCollectionStateChange, resetCollection } =
useCollectionCreation(collectionConfigComponent);
let nextId = 1;
function createPasteUrlItem(id: number, url: string, name: string): PasteUrlItem {
return {
id,
url,
name,
...createItemDefaults(),
deferred: false,
};
}
const urlItems = ref<PasteUrlItem[]>([]);
const urlText = ref("");
const showInputArea = ref(true);
const { clear: clearStaging } = useUploadStaging<PasteUrlItem>(props.method.id, urlItems, {
disableStore: props.transient,
});
const placeholder = "https://example.org/data1.txt\nhttps://example.org/data2.txt";
const hasItems = computed(() => urlItems.value.length > 0);
const isSingleMode = computed(() => props.multiple === false);
const addMoreUrlsTitle = computed(() =>
isSingleMode.value ? "Change selected URL" : "Add more URLs to the upload list",
);
const addMoreUrlsLabel = computed(() => (isSingleMode.value ? "Change selected URL" : "Add More URLs"));
const { isNameValid, restoreOriginalName } = useUploadItemValidation();
const bulk = useBulkUploadOperations(urlItems, effectiveExtensions);
// Additional validation for URL items
const hasInvalidUrls = computed(() => urlItems.value.some((item) => !isValidUrl(item.url)));
const { isReadyToUpload } = useUploadReadyState(
hasItems,
collectionState,
computed(() => !hasInvalidUrls.value),
);
watch(isReadyToUpload, (ready) => emit("ready", ready), { immediate: true });
function getUrlValidationMessage(url: string): string {
return validateUrl(url).message || url;
}
function addUrlsFromText() {
if (!urlText.value.trim()) {
return;
}
const urls = urlText.value
.split(/\r?\n/)
.map((u) => u.trim())
.filter((u) => u.length > 0);
// Enforce single URL when multiple is false
const urlsToAdd = isSingleMode.value ? urls.slice(0, 1) : urls;
if (isSingleMode.value) {
urlItems.value = [];
}
for (const url of urlsToAdd) {
urlItems.value.push(createPasteUrlItem(nextId++, url, extractNameFromUrl(url)));
}
urlText.value = "";
showInputArea.value = false;
scrollToBottom();
}
function showUrlInput() {
showInputArea.value = true;
}
function showUrlList() {
showInputArea.value = false;
}
function scrollToBottom() {
nextTick(() => {
if (tableContainerRef.value) {
const container = tableContainerRef.value;
container.scrollTop = container.scrollHeight;
}
});
}
function removeItem(id: number) {
urlItems.value = urlItems.value.filter((item) => item.id !== id);
if (urlItems.value.length === 0) {
showInputArea.value = true;
resetCollection();
}
}
const tableFields: TableField[] = [
{
key: "name",
label: "Name",
sortable: true,
width: "200px",
class: "url-name-cell",
},
{
key: "url",
label: "URL",
sortable: false,
class: "url-column",
},
{
key: "extension",
label: "Type",
sortable: false,
width: "180px",
align: "center",
},
{
key: "dbKey",
label: "Reference",
sortable: false,
width: "200px",
align: "center",
},
{
key: "options",
label: "Upload Settings",
sortable: false,
align: "center",
},
{
key: "actions",
label: "",
sortable: false,
width: "50px",
align: "center",
},
];
function reset() {
urlItems.value = [];
urlText.value = "";
clearStaging();
showInputArea.value = true;
resetCollection();
}
function prepareUpload(): PreparedUpload | null {
const validItems = urlItems.value.filter((item) => item.url.trim().length > 0);
if (validItems.length === 0) {
return null;
}
const uploads = validItems.map((item) => mapToPasteUrlUpload(item, props.targetHistoryId));
return buildPreparedUpload(uploads, buildCollectionConfig(props.targetHistoryId));
}
defineExpose<UploadMethodComponent>({ prepareUpload, reset });
</script>
<template>
<div class="paste-links-upload">
<!-- URL Input Area -->
<div v-if="showInputArea" class="url-input-area">
<label for="paste-links-textarea" class="font-weight-bold mb-0">
Paste URLs
<small class="text-muted ml-2">One URL per line</small>
<GButton v-if="hasItems" size="small" inline class="ui-link p-0 ml-2" @click="showUrlList">
{{ urlItems.length }} URL(s) added
</GButton>
</label>
<textarea
id="paste-links-textarea"
v-model="urlText"
class="form-control mb-2 url-textarea"
rows="8"
:placeholder="placeholder"></textarea>
<div class="d-flex justify-content-end align-items-center">
<GButton v-if="hasItems" class="mr-2" @click="showUrlList">View Added URLs</GButton>
<GButton color="blue" :disabled="!urlText.trim()" data-test-id="add-urls" @click="addUrlsFromText">
<FontAwesomeIcon :icon="faLink" class="mr-1" />
Add URLs
</GButton>
</div>
</div>
<!-- URL Table with Metadata Editing -->
<div v-else class="url-list">
<div class="url-list-header mb-2">
<div class="d-flex justify-content-between align-items-center">
<span class="font-weight-bold">{{ urlItems.length }} URL(s) added</span>
</div>
</div>
<div ref="tableContainerRef" class="url-table-container">
<GTable hover striped fixed compact :items="urlItems" :fields="tableFields" class="url-table">
<!-- Name column -->
<template v-slot:cell(name)="{ item, index }">
<UploadTableNameCell
:data-test-id="`upload-row-${index + 1}-name`"
:value="item.name"
:state="isNameValid(item.name)"
@input="item.name = $event"
@blur="restoreOriginalName(item, extractNameFromUrl(item.url))" />
</template>
<!-- URL column -->
<template v-slot:cell(url)="{ item }">
<div class="d-flex align-items-center">
<BFormInput
v-model="item.url"
v-g-tooltip.hover
size="sm"
:state="isValidUrl(item.url)"
:title="getUrlValidationMessage(item.url)"
class="url-input" />
</div>
</template>
<!-- Extension column with bulk operations -->
<template v-slot:head(extension)>
<UploadTableBulkExtensionHeader
:value="bulk.bulkExtension.value"
:extensions="effectiveExtensions"
:warning="bulk.bulkExtensionWarning.value"
:disabled="!configurationsReady"
tooltip="Set file format for all URLs"
@input="bulk.setAllExtensions" />
</template>
<template v-slot:cell(extension)="{ item, index }">
<UploadTableExtensionCell
:data-test-id="`upload-row-${index + 1}-extension`"
:value="item.extension"
:extensions="effectiveExtensions"
:warning="bulk.getExtensionWarning(item.extension)"
:disabled="!configurationsReady"
@input="item.extension = $event" />
</template>
<!-- DbKey column with bulk operations -->
<template v-slot:head(dbKey)>
<UploadTableBulkDbKeyHeader
:value="bulk.bulkDbKey.value"
:db-keys="listDbKeys"
:disabled="!configurationsReady"
tooltip="Set database key for all URLs"
@input="bulk.setAllDbKeys" />
</template>
<template v-slot:cell(dbKey)="{ item, index }">
<UploadTableDbKeyCell
:data-test-id="`upload-row-${index + 1}-dbkey`"
:value="item.dbkey"
:db-keys="listDbKeys"
:disabled="!configurationsReady"
@input="item.dbkey = $event" />
</template>
<!-- Options column with bulk checkboxes -->
<template v-slot:head(options)>
<UploadTableOptionsHeader
:all-space-to-tab="bulk.allSpaceToTab.value"
:space-to-tab-indeterminate="bulk.spaceToTabIndeterminate.value"
:show-posix="advancedMode"
:all-to-posix-lines="bulk.allToPosixLines.value"
:to-posix-lines-indeterminate="bulk.toPosixLinesIndeterminate.value"
:show-deferred="true"
:all-deferred="bulk.allDeferred.value"
:deferred-indeterminate="bulk.deferredIndeterminate.value"
@toggle-space-to-tab="bulk.toggleAllSpaceToTab"
@toggle-to-posix-lines="bulk.toggleAllToPosixLines"
@toggle-deferred="bulk.toggleAllDeferred" />
</template>
<template v-slot:cell(options)="{ item }">
<UploadTableOptionsCell
:space-to-tab="item.spaceToTab"
:show-posix="advancedMode"
:to-posix-lines="item.toPosixLines"
:show-deferred="true"
:deferred="item.deferred"
@updateSpaceToTab="item.spaceToTab = $event"
@updateToPosixLines="item.toPosixLines = $event"
@updateDeferred="item.deferred = $event" />
</template>
<!-- Actions column -->
<template v-slot:cell(actions)="{ item, index }">
<GButton
v-g-tooltip.hover
class="remove-btn"
:data-test-id="`upload-row-${index + 1}-remove`"
color="red"
outline
transparent
title="Remove URL from list"
@click="removeItem(item.id)">
<FontAwesomeIcon :icon="faTimes" />
</GButton>
</template>
</GTable>
</div>
<!-- Collection Creation Section -->
<CollectionCreationConfig
v-if="props.allowCollections !== false"
ref="collectionConfigComponent"
:files="urlItems"
@update:state="handleCollectionStateChange" />
<div class="url-list-actions mt-2">
<GButton color="grey" tooltip tooltip-placement="top" :title="addMoreUrlsTitle" @click="showUrlInput">
<FontAwesomeIcon :icon="faPlus" class="mr-1" />
{{ addMoreUrlsLabel }}
</GButton>
<GButton
outline
color="grey"
tooltip
tooltip-placement="top"
title="Remove all URLs from the upload list"
@click="reset">
Clear All
</GButton>
</div>
</div>
</div>
</template>
<style scoped lang="scss">
@import "@/style/scss/theme/blue.scss";
@import "../shared/upload-table-shared.scss";
.paste-links-upload {
height: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
}
.url-input-area {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
min-height: 0;
.url-textarea {
font-family: monospace;
font-size: 0.9rem;
flex: 1;
resize: none;
}
}
.url-list {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
min-height: 0;
}
.url-list-header {
@include upload-list-header;
}
.url-table-container {
@include upload-table-container;
:deep(.url-table thead) {
@include upload-table-header;
}
:deep(.url-name-cell) {
min-width: 200px;
}
:deep(.url-column) {
width: 100%;
max-width: 400px;
overflow: hidden;
.url-input {
font-family: monospace;
font-size: 0.85rem;
}
}
}
.url-list-actions {
@include upload-list-actions;
}
</style>