-
Notifications
You must be signed in to change notification settings - Fork 5
/
TaskPicker.vue
394 lines (370 loc) · 12 KB
/
TaskPicker.vue
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
<template>
<PvPanel header="Task Picker">
<template #icons>
<div class="flex flex-row">
<span>Show only named variants</span>
<PvToggleSwitch v-model="namedOnly" class="ml-2" />
<!-- <button @click="tasksPaneOpen = !tasksPaneOpen">toggle pane</button> -->
</div>
</template>
<div class="w-full flex flex-column lg:flex-row gap-2">
<div v-if="tasksPaneOpen" class="w-full lg:w-6">
<div class="flex flex-row mb-2">
<PvIconField class="w-full">
<PvInputIcon class="pi pi-search" />
<PvInputText
v-model="searchTerm"
class="w-full"
placeholder="Variant name, ID, or Task ID"
data-cy="input-variant-name"
/>
</PvIconField>
<PvButton
v-if="searchTerm"
class="bg-primary text-white border-none border-round pl-3 pr-3 hover:bg-red-900"
style="margin-right: 0"
@click="clearSearch"
>
<i class="pi pi-times" />
</PvButton>
</div>
<div v-if="searchTerm.length >= 3">
<div v-if="isSearching">
<span>Searching...</span>
</div>
<div v-else-if="_isEmpty(searchResults)">
<span>No search results for {{ searchTerm }}</span>
</div>
<PvScrollPanel style="height: 31rem; width: 100%; overflow-y: auto">
<!-- Draggable Zone 3 -->
<VueDraggableNext
v-model="searchResults"
:reorderable-columns="true"
:group="{ name: 'variants', pull: 'clone', put: false }"
:sort="false"
:move="handleCardMove"
>
<transition-group>
<div
v-for="element in searchResults"
:id="element.id"
:key="element.id"
:data-task-id="element.task.id"
style="cursor: grab"
>
<VariantCard :variant="element" @select="selectCard" />
</div>
</transition-group>
</VueDraggableNext>
</PvScrollPanel>
</div>
<div v-if="searchTerm.length < 3">
<PvSelect
v-model="currentTask"
:options="taskOptions"
option-label="label"
option-value="value"
class="w-full mb-2"
placeholder="Select TaskID"
/>
<PvScrollPanel style="height: 27.75rem; width: 100%; overflow-y: auto">
<div v-if="!currentTask">Select a TaskID to display a list of variants.</div>
<div v-else-if="!currentVariants.length">
No variants to show. Make sure 'Show only named variants' is unchecked to view all.
<span class="text-link" @click="namedOnly = false">View all</span>
</div>
<!-- Draggable Zone 1 -->
<VueDraggableNext
v-model="currentVariants"
:reorderable-columns="true"
:group="{ name: 'variants', pull: 'clone', put: false }"
:sort="false"
:move="handleCardMove"
>
<transition-group>
<div
v-for="element in currentVariants"
:id="element.id"
:key="element.id"
:data-task-id="element.task.id"
style="cursor: grab"
>
<VariantCard :variant="element" :update-variant="updateVariant" @select="selectCard" />
</div>
</transition-group>
</VueDraggableNext>
</PvScrollPanel>
</div>
</div>
<div v-else class="w-1 bg-gray-400">
<i class="pi pi-angle-double-right" />
</div>
<div class="divider"></div>
<div class="w-full lg:w-6" data-cy="panel-droppable-zone">
<div class="panel-title mb-2">Selected Variants</div>
<PvScrollPanel style="height: 32rem; width: 100%; overflow-y: auto">
<!-- Draggable Zone 2 -->
<VueDraggableNext
v-model="selectedVariants"
:move="handleCardMove"
:group="{
name: 'variants',
pull: true,
put: true,
animation: 100,
}"
:sort="true"
class="w-full h-full overflow-auto"
@add="handleCardAdd"
>
<transition-group>
<div
v-for="element in selectedVariants"
:id="element.id"
:key="element.id"
:data-task-id="element.task.id"
style="cursor: grab"
>
<VariantCard
:variant="element"
has-controls
:update-variant="updateVariant"
:pre-existing-assessment-info="preExistingAssessmentInfo"
@remove="removeCard"
@move-up="moveCardUp"
@move-down="moveCardDown"
/>
</div>
</transition-group>
</VueDraggableNext>
</PvScrollPanel>
</div>
</div>
</PvPanel>
</template>
<script setup>
import { computed, ref, watch } from 'vue';
import _filter from 'lodash/filter';
import _findIndex from 'lodash/findIndex';
import _debounce from 'lodash/debounce';
import _toLower from 'lodash/toLower';
import _isEmpty from 'lodash/isEmpty';
import _union from 'lodash/union';
import { VueDraggableNext } from 'vue-draggable-next';
import VariantCard from './VariantCard.vue';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const props = defineProps({
allVariants: {
type: Object,
required: true,
},
inputVariants: {
type: Array,
default: () => [],
},
preExistingAssessmentInfo: {
type: Array,
default: () => [],
},
});
const emit = defineEmits(['variants-changed']);
const taskOptions = computed(() => {
return Object.entries(props.allVariants).map((entry) => {
const key = entry[0];
const value = entry[1];
return {
label: value[0].task.name ?? key,
value: key,
};
});
});
watch(
() => props.inputVariants,
(newVariants) => {
selectedVariants.value = _union(selectedVariants.value, newVariants);
// Update the conditions for the variants that were pre-existing
selectedVariants.value = selectedVariants.value.map((variant) => {
const preExistingInfo = props.preExistingAssessmentInfo.find((info) => info?.variantId === variant?.id);
if (preExistingInfo) {
return { ...variant, variant: { ...variant?.variant, conditions: preExistingInfo.conditions } };
}
return variant;
});
},
);
const updateVariant = (variantId, conditions) => {
const updatedVariants = selectedVariants.value.map((variant) => {
if (variant.id === variantId) {
return { ...variant, variant: { ...variant.variant, conditions: conditions } };
} else {
return variant;
}
});
selectedVariants.value = updatedVariants;
return;
// props.selectedVariant[]
};
const selectedVariants = ref([]);
const namedOnly = ref(true);
const currentTask = ref(Object.keys(props.allVariants)[0]);
const currentVariants = computed(() => {
if (namedOnly.value) {
return _filter(props.allVariants[currentTask.value], (variant) => variant.variant.name);
}
return props.allVariants[currentTask.value];
});
// Pane handlers
const tasksPaneOpen = ref(true);
// Search handlers
const searchTerm = ref('');
const searchResults = ref([]);
const isSearching = ref(false);
const searchCards = (term) => {
isSearching.value = true;
searchResults.value = [];
Object.values(props.allVariants).forEach((variants) => {
const matchingVariants = _filter(variants, (variant) => {
if (
_toLower(variant.variant.name).includes(_toLower(term)) ||
_toLower(variant.id).includes(_toLower(term)) ||
_toLower(variant.task.id).includes(_toLower(term))
)
return true;
else return false;
});
searchResults.value.push(...matchingVariants);
});
isSearching.value = false;
};
function clearSearch() {
searchTerm.value = '';
searchResults.value = [];
}
const debounceSearch = _debounce(searchCards, 250);
watch(searchTerm, (term) => {
if (term.length >= 3) {
debounceSearch(term);
} else {
searchResults.value = [];
}
});
// Handle card move events
const debounceToast = _debounce(
() => {
toast.add({ severity: 'error', summary: 'Duplicate', detail: 'That variant is already selected.', life: 3000 });
},
3000,
{ leading: true },
);
const handleCardAdd = (card) => {
// Check if the current task is already selected.
const taskIds = [];
// Add fires after the move is complete, so check if there is a duplicate task in the list.
for (const variant of selectedVariants.value) {
// If the duplicate task is also the current task, send a warn toast.
if (taskIds.includes(variant.task.id) && variant.task.id === card.item.dataset.taskId) {
toast.add({
severity: 'warn',
summary: 'Task Selected',
detail: 'There is a task with that Task ID already selected.',
life: 3000,
});
} else {
taskIds.push(variant.task.id);
}
}
};
const handleCardMove = (card) => {
// Check if this variant card is already in the list
const cardVariantId = card.dragged.id;
const index = _findIndex(selectedVariants.value, (element) => element.id === cardVariantId);
if (index !== -1 && card.from !== card.to) {
debounceToast();
return false;
} else return true;
};
watch(
selectedVariants,
(variants) => {
emit('variants-changed', variants);
},
{ deep: true },
);
// Card event handlers
const removeCard = (variant) => {
selectedVariants.value = selectedVariants.value.filter((selectedVariant) => selectedVariant.id !== variant.id);
};
const selectCard = (variant) => {
// Check if this variant is already in the list
const cardVariantId = variant.id;
const index = _findIndex(selectedVariants.value, (element) => element.id === cardVariantId);
if (index === -1) {
// If this variant is not already selected, check if the taskId is already selected.
// If so, warn but add regardless.
const selectedTasks = selectedVariants.value.map((selectedVariant) => selectedVariant.task.id);
if (selectedTasks.includes(variant.task.id)) {
toast.add({
severity: 'warn',
summary: 'Task Selected',
detail: 'There is a task with that Task ID already selected.',
life: 3000,
});
}
selectedVariants.value.push(variant);
} else {
debounceToast();
}
};
const moveCardUp = (variant) => {
const index = _findIndex(selectedVariants.value, (currentVariant) => currentVariant.id === variant.id);
if (index === 0) return;
const item = selectedVariants.value[index];
selectedVariants.value.splice(index, 1);
selectedVariants.value.splice(index - 1, 0, item);
};
const moveCardDown = (variant) => {
const index = _findIndex(selectedVariants.value, (currentVariant) => currentVariant.id === variant.id);
if (index === selectedVariants.value.length) return;
const item = selectedVariants.value[index];
selectedVariants.value.splice(index, 1);
selectedVariants.value.splice(index + 1, 0, item);
};
</script>
<style lang="scss">
.task-tab {
height: 100%;
overflow: auto;
}
.variant-selector {
width: 50%;
}
.selected-container {
width: 100%;
border: 1px solid var(--surface-d);
}
.text-link {
cursor: pointer;
color: var(--text-color-secondary);
font-weight: bold;
text-decoration: underline;
}
.divider {
// TODO: Figure out how to reference SCSS variable $lg, rather than 992px
@media screen and (min-width: 992px) {
min-height: 100%;
max-width: 0;
border-left: 1px solid var(--surface-d);
}
// TODO: Figure out how to reference SCSS variable $lg, rather than 992px
@media screen and (max-width: 992px) {
min-width: 100%;
max-height: 0;
border-bottom: 1px solid var(--surface-d);
}
}
.panel-title {
font-size: x-large;
font-weight: bold;
}
</style>