-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGCard.vue
More file actions
784 lines (699 loc) · 32.7 KB
/
GCard.vue
File metadata and controls
784 lines (699 loc) · 32.7 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<script setup lang="ts">
import { faStar as farStar } from "@fortawesome/free-regular-svg-icons";
import { faCaretDown, faEdit, faPen, faSpinner, faStar, type IconDefinition } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { BBadge, BButton, BButtonGroup, BDropdown, BDropdownItem, BFormCheckbox, BLink } from "bootstrap-vue";
import { computed, ref } from "vue";
import { useMarkdown } from "@/composables/markdown";
import { useUid } from "@/composables/utils/uid";
import localize from "@/utils/localization";
import type { CardAction, CardBadge, CardIndicator, Title, TitleIcon, TitleSize } from "./GCard.types";
import Heading from "@/components/Common/Heading.vue";
import TextSummary from "@/components/Common/TextSummary.vue";
import StatelessTags from "@/components/TagsMultiselect/StatelessTags.vue";
import UtcDate from "@/components/UtcDate.vue";
interface Props {
/** Unique identifier for the card
* @default useUid("g-card-").value
*/
id?: string;
/** Badges displayed in the top-right corner
* @default []
*/
badges?: CardBadge[];
/** Whether the card is bookmarked
* @default undefined
*/
bookmarked?: boolean;
/** Whether the card is clickable (emits click events)
* @default undefined
*/
clickable?: boolean;
/** Additional CSS classes for the card container
* @default ""
*/
containerClass?: string | string[];
/** Additional CSS classes for the card content
* @default ""
*/
contentClass?: string | string[];
/** Whether the card is marked as current/active
* @default false
*/
current?: boolean;
/** Description text (supports Markdown)
* @default ""
*/
description?: string;
/** Extra actions shown in dropdown menu
* @default []
*/
extraActions?: CardAction[];
/** Whether to show full description (no truncation)
* @default undefined
*/
fullDescription?: boolean;
/** Whether displayed in grid view mode
* @default false
*/
gridView?: boolean;
/** Indicators shown as small buttons/icons
* @default []
*/
indicators?: CardIndicator[];
/** Max visible tags before "show more"
* @default 3
*/
maxVisibleTags?: number;
/** Primary actions in card footer
* @default []
*/
primaryActions?: CardAction[];
/** Whether the card represents published content
* @default false
*/
published?: boolean;
/** Tooltip text for rename button
* @default "Rename"
*/
renameTitle?: string;
/** Whether the card title is editable
* @default false
*/
canRenameTitle?: boolean;
/** Secondary actions in card footer
* @default []
*/
secondaryActions?: CardAction[];
/** Whether the card is selectable via checkbox
* @default false
*/
selectable?: boolean;
/** Whether the card is currently selected
* @default false
*/
selected?: boolean;
/** Tooltip text for select checkbox
* @default ""
*/
selectTitle?: string;
/** Whether to show bookmark button
* @default undefined
*/
showBookmark?: boolean;
/** Tags displayed in card footer
* @default []
*/
tags?: string[];
/** Whether tags are editable/clickable
* @default false
*/
tagsEditable?: boolean;
/** Card title (string or interactive object)
* @default ""
*/
title?: Title;
/** Badges displayed next to title
* @default []
*/
titleBadges?: CardBadge[];
/** Icon displayed before title
* @default undefined
*/
titleIcon?: TitleIcon;
/** Whether the title should be truncated to a certain number of lines
* @default undefined
*/
titleNLines?: number;
/** Size of the card title
* @default "sm"
*/
titleSize?: TitleSize;
/** Icon for update time badge
* @default faEdit
*/
updateTimeIcon?: IconDefinition;
/** Last update timestamp
* @default ""
*/
updateTime?: string;
/** Tooltip for update time badge
* @default "Last updated"
*/
updateTimeTitle?: string;
/** Whether this card is highlighted (for example, as a range selection anchor)
* @default false
*/
highlighted?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
id: () => useUid("g-card-").value,
badges: () => [],
containerClass: "",
contentClass: "",
current: false,
description: "",
extraActions: () => [],
gridView: false,
indicators: () => [],
maxVisibleTags: 3,
primaryActions: () => [],
published: false,
renameTitle: "Rename",
canRenameTitle: false,
secondaryActions: () => [],
selectable: false,
selected: false,
selectTitle: "",
tags: () => [],
tagsEditable: false,
title: "",
titleBadges: () => [],
titleIcon: undefined,
titleNLines: undefined,
titleSize: "sm",
updateTime: "",
updateTimeIcon: () => faEdit,
updateTimeTitle: "Last updated",
highlighted: false,
});
/**
* Events emitted by the GCard component
*/
const emit = defineEmits<{
/** Emitted when card is clicked
* @event click
*/
(e: "click", event: MouseEvent | KeyboardEvent): void;
/** Emitted when bookmark button is clicked
* @event bookmark
*/
(e: "bookmark"): Promise<void>;
/** Emitted when dropdown opens/closes
* @event dropdown
*/
(e: "dropdown", open: boolean): void;
/** Emitted when rename button is clicked
* @event rename
*/
(e: "rename"): void;
/** Emitted when selection checkbox is toggled
* @event select
*/
(e: "select"): void;
/** Emitted when title is clicked
* @event titleClick
*/
(e: "titleClick"): void;
/** Emitted when tag is clicked
* @event tagClick
*/
(e: "tagClick", tag: string): void;
/** Emitted when tags are updated
* @event tagsUpdate
*/
(e: "tagsUpdate", tags: string[]): void;
(e: "keydown", event: KeyboardEvent): void;
}>();
const bookmarkLoading = ref(false);
/**
* Toggles bookmark status with loading state
*/
async function toggleBookmark() {
bookmarkLoading.value = true;
await emit("bookmark");
bookmarkLoading.value = false;
}
const { renderMarkdown } = useMarkdown({ noMargin: true, openLinksInNewPage: true });
/**
* Helper functions for generating consistent element IDs
*/
const getElementId = (cardId: string, element: string) => `g-card-${element}-${cardId}`;
const getIndicatorId = (cardId: string, indicatorId: string) => `g-card-indicator-${indicatorId}-${cardId}`;
const getBadgeId = (cardId: string, badgeId: string) => `g-card-badge-${badgeId}-${cardId}`;
const getActionId = (cardId: string, actionId: string) => `g-card-action-${actionId}-${cardId}`;
/**
* Number of lines before title truncation (undefined = no truncation)
*/
const allowedTitleLines = computed(() => props.titleNLines);
function onKeyDown(event: KeyboardEvent) {
if ((props.clickable && event.key === "Enter") || event.key === " ") {
event.stopPropagation();
emit("click", event);
} else if (props.clickable) {
event.stopPropagation();
emit("keydown", event);
}
}
</script>
<template>
<component
:is="'div'"
:id="`g-card-${props.id}`"
:role="props.clickable ? 'button' : undefined"
class="g-card pt-0 px-1 mb-2"
:class="[
{ 'g-card-grid-view': gridView },
{ 'g-card-selected': selected },
{ 'g-card-current': current },
{ 'g-card-published': published },
{ 'g-card-clickable': props.clickable },
containerClass,
]"
:tabindex="props.clickable ? 0 : undefined"
@click="props.clickable ? emit('click', $event) : undefined"
@keydown="onKeyDown">
<div
:id="`g-card-content-${props.id}`"
class="g-card-content d-flex flex-column justify-content-between h-100 p-2"
:class="[{ 'g-card-highlighted': props.highlighted }, contentClass]">
<slot>
<div class="d-flex flex-column flex-gapy-1">
<div
:id="`g-card-${props.id}-header`"
class="d-flex flex-wrap flex-gapy-1 flex-gapx-1 justify-content-between">
<div class="d-flex flex-column flex-grow-1 g-card-title-section">
<div class="d-flex">
<div v-if="selectable">
<slot name="select">
<BFormCheckbox
:id="getElementId(props.id, 'select')"
v-b-tooltip.hover.noninteractive
:checked="selected"
:title="props.selectTitle || localize('Select for bulk actions')"
@change="emit('select')" />
</slot>
</div>
<div :id="`g-card-${props.id}-header-title`">
<slot name="title">
<Heading
:id="getElementId(props.id, 'title')"
bold
inline
class="d-block"
:size="props.titleSize">
<FontAwesomeIcon
v-if="props.titleIcon?.icon"
class="mr-1"
:class="props.titleIcon.class"
:icon="props.titleIcon.icon"
:title="props.titleIcon.title"
:size="props.titleIcon.size"
fixed-width />
<BLink
v-if="typeof title === 'object'"
:id="getElementId(props.id, 'title-link')"
v-b-tooltip.hover.noninteractive
:title="localize(title.title)"
:class="{ 'g-card-title-truncate': props.titleNLines }"
@click.stop.prevent="title.handler">
{{ title.label }}
</BLink>
<template v-else>
<span
:id="getElementId(props.id, 'title-text')"
v-b-tooltip.hover.noninteractive
:title="localize(title)"
:class="{ 'g-card-title-truncate': props.titleNLines }">
{{ title }}
</span>
</template>
<slot name="titleActions">
<BButton
v-if="props.canRenameTitle"
:id="getElementId(props.id, 'rename')"
v-b-tooltip.hover.noninteractive
class="inline-icon-button g-card-rename"
variant="link"
:title="localize(props.renameTitle)"
@click="emit('rename')">
<FontAwesomeIcon :icon="faPen" fixed-width />
</BButton>
</slot>
</Heading>
</slot>
</div>
</div>
<div class="align-items-center d-flex flex-gapx-1">
<slot name="titleBadges">
<template v-for="badge in props.titleBadges">
<BBadge
v-if="badge.visible ?? true"
:id="getBadgeId(props.id, badge.id)"
:key="badge.id"
v-b-tooltip.hover.noninteractive
:pill="badge.type !== 'badge'"
class="mt-1"
:class="{
'outline-badge': badge.variant?.includes('outline'),
'cursor-pointer': badge.handler,
[String(badge.class)]: badge.class,
}"
:title="localize(badge.title)"
:variant="badge.variant || 'secondary'"
:to="badge.to"
@click.stop="badge.handler">
<FontAwesomeIcon v-if="badge.icon" :icon="badge.icon" fixed-width />
{{ localize(badge.label) }}
</BBadge>
</template>
</slot>
</div>
</div>
<div class="align-items-start d-flex flex-row-reverse flex-wrap gap-1 flex-shrink-0">
<div>
<slot v-if="props.showBookmark" name="bookmark">
<BButton
v-if="!bookmarkLoading"
:id="
getElementId(
props.id,
props.bookmarked ? 'bookmark-remove' : 'bookmark-add',
)
"
v-b-tooltip.hover.noninteractive
class="inline-icon-button"
variant="link"
:title="props.bookmarked ? 'Remove bookmark' : 'Add to bookmarks'"
@click="toggleBookmark">
<FontAwesomeIcon :icon="props.bookmarked ? faStar : farStar" fixed-width />
</BButton>
<BButton
v-else
:id="getElementId(props.id, 'bookmark-loading')"
v-b-tooltip.hover.noninteractive
class="inline-icon-button"
variant="link"
:title="localize('Bookmarking...')"
disabled>
<FontAwesomeIcon :icon="faSpinner" spin fixed-width />
</BButton>
</slot>
<slot name="extra-actions">
<BDropdown
v-if="
props.extraActions?.length &&
props.extraActions.some((ea) => ea.visible ?? true)
"
:id="getElementId(props.id, 'extra-actions')"
v-b-tooltip.hover.noninteractive
right
no-caret
title="More options"
toggle-class="inline-icon-button"
variant="link"
@show="() => emit('dropdown', true)"
@hide="() => emit('dropdown', false)">
<template v-slot:button-content>
<FontAwesomeIcon :icon="faCaretDown" fixed-width />
</template>
<template v-for="ea in props.extraActions">
<BDropdownItem
v-if="ea.visible ?? true"
:id="getActionId(props.id, ea.id)"
:key="ea.id"
:disabled="ea.disabled"
:variant="ea.variant || 'link'"
:to="ea.to"
:href="ea.href"
:title="ea.title"
:size="ea.size || 'sm'"
:target="ea.externalLink ? '_blank' : undefined"
@click="ea.handler && ea.handler()">
<FontAwesomeIcon v-if="ea.icon" :icon="ea.icon" fixed-width />
{{ localize(ea.label) }}
</BDropdownItem>
</template>
</BDropdown>
</slot>
</div>
<div class="d-flex flex-gapx-1" style="margin-top: 1px">
<div
:id="getElementId(props.id, 'badges')"
class="align-items-center align-self-baseline d-flex flex-gapx-1">
<slot name="badges">
<template v-for="badge in props.badges">
<BBadge
v-if="badge.visible ?? true"
:id="getBadgeId(props.id, badge.id)"
:key="badge.id"
v-b-tooltip.hover.top.noninteractive
:pill="badge.type !== 'badge'"
:class="{
'outline-badge': badge.variant?.includes('outline'),
'cursor-pointer': badge.handler,
[String(badge.class)]: badge.class,
}"
:title="localize(badge.title)"
:variant="badge.variant || 'secondary'"
:to="badge.to"
:href="badge.href"
@click.stop="badge.handler">
<FontAwesomeIcon
v-if="badge.icon"
:icon="badge.icon"
fixed-width
:spin="badge.spin" />
{{ localize(badge.label) }}
</BBadge>
</template>
</slot>
</div>
<div :id="getElementId(props.id, 'indicators')" class="align-self-baseline">
<slot name="indicators">
<template v-for="indicator in props.indicators">
<BButton
v-if="(indicator.visible ?? true) && !indicator.disabled"
:id="getIndicatorId(props.id, indicator.id)"
:key="`${indicator.id}-button`"
v-b-tooltip.hover.noninteractive
class="inline-icon-button"
:title="localize(indicator.title)"
:variant="indicator.variant || 'outline-secondary'"
:size="indicator.size || 'sm'"
:to="indicator.to"
:href="indicator.href"
:disabled="indicator.disabled"
:target="indicator.externalLink ? '_blank' : undefined"
@click.stop="indicator.handler">
<FontAwesomeIcon
v-if="indicator.icon"
:icon="indicator.icon"
fixed-width />
{{ localize(indicator.label) }}
</BButton>
<FontAwesomeIcon
v-else-if="(indicator.visible ?? true) && indicator.disabled"
:id="getIndicatorId(props.id, indicator.id)"
:key="`${indicator.id}-icon`"
v-b-tooltip.hover.noninteractive
:title="localize(indicator.title)"
:icon="indicator.icon"
:size="indicator.size || 'sm'"
fixed-width />
</template>
</slot>
</div>
</div>
</div>
</div>
<div :id="getElementId(props.id, 'description')" class="g-card-description">
<slot name="description">
<template v-if="props.description">
<TextSummary
v-if="!props.fullDescription"
:id="getElementId(props.id, 'text-summary')"
:description="props.description" />
<div v-else v-html="renderMarkdown(props.description)" />
</template>
</slot>
</div>
</div>
<div
:id="getElementId(props.id, 'footer')"
class="align-items-end align-items-sm-stretch d-flex flex-sm-column justify-content-between">
<slot name="tags">
<StatelessTags
:id="getElementId(props.id, 'tags')"
inline
:clickable="props.tagsEditable"
:value="props.tags"
:disabled="!props.tagsEditable"
:max-visible-tags="props.maxVisibleTags"
@input="emit('tagsUpdate', $event)"
@tag-click="emit('tagClick', $event)" />
</slot>
<div
:id="getElementId(props.id, 'footer-update-time-actions')"
class="align-items-center d-flex flex-gapy-1 flex-sm-wrap justify-content-between">
<slot name="update-time">
<div
v-if="props.updateTime"
:id="`g-card-${props.id}-update-time`"
class="align-self-end mt-1">
<BBadge
v-b-tooltip.hover.noninteractive
pill
variant="secondary"
:title="localize(props.updateTimeTitle)">
<FontAwesomeIcon :icon="props.updateTimeIcon" fixed-width />
<UtcDate :date="props.updateTime" mode="elapsed" />
</BBadge>
</div>
</slot>
<div class="align-items-center d-flex flex-gapx-1 justify-content-end ml-auto">
<slot name="secondary-actions">
<BButtonGroup
v-if="props.secondaryActions?.length"
:id="getElementId(props.id, 'secondary-actions')"
size="sm"
class="mt-1">
<template v-for="sa in props.secondaryActions">
<BButton
v-if="sa.visible ?? true"
:id="getActionId(props.id, sa.id)"
:key="sa.id"
v-b-tooltip.hover.noninteractive
:disabled="sa.disabled"
:title="localize(sa.title)"
:variant="sa.variant || 'outline-primary'"
:size="sa.size || 'sm'"
:to="sa.to"
:href="sa.href"
:target="sa.externalLink ? '_blank' : undefined"
@click.stop="sa.handler">
<FontAwesomeIcon
v-if="sa.icon"
:icon="sa.icon"
fixed-width
:size="sa.size || undefined" />
<span class="g-card-secondary-action-label">
{{ localize(sa.label) }}
</span>
</BButton>
</template>
</BButtonGroup>
</slot>
<div :id="getElementId(props.id, 'primary-actions')" class="d-flex flex-gapx-1">
<slot name="primary-actions">
<template v-if="props.primaryActions?.length">
<template v-for="pa in props.primaryActions">
<BButton
v-if="pa.visible ?? true"
:id="getActionId(props.id, pa.id)"
:key="pa.id"
v-b-tooltip.hover.noninteractive
class="mt-1"
:disabled="pa.disabled"
:title="localize(pa.title)"
:variant="pa.variant || 'primary'"
:size="pa.size || 'sm'"
:to="pa.to"
:href="pa.href"
:class="{
'inline-icon-button': pa.inline,
[String(pa.class)]: pa.class,
}"
@click.stop="pa.handler">
<FontAwesomeIcon
v-if="pa.icon"
:icon="pa.icon"
:size="pa.size || undefined"
fixed-width />
{{ localize(pa.label) }}
</BButton>
</template>
</template>
</slot>
</div>
</div>
</div>
</div>
</slot>
</div>
</component>
</template>
<style scoped lang="scss">
@import "@/style/scss/theme/blue.scss";
@import "@/style/scss/_breakpoints.scss";
.g-card {
container: g-card / inline-size;
width: 100%;
&.g-card-grid-view {
width: calc(100% / 3);
@container cards-list (max-width: #{$breakpoint-xl}) {
width: calc(100% / 2);
}
@container cards-list (max-width: #{$breakpoint-sm}) {
width: 100%;
}
}
&.g-card-selected .g-card-content {
background-color: $brand-light;
border-color: $brand-primary;
}
&.g-card-current .g-card-content {
background-color: $brand-light;
border-width: 2px;
border-color: $brand-primary;
border-left: 0.25rem solid $brand-primary;
}
&.g-card-published .g-card-content {
border-left: 0.25rem solid $brand-primary;
}
&.g-card-highlighted .g-card-content {
box-shadow: 0 0 0 0.2rem transparentize($brand-primary, 0.75);
}
&.g-card-clickable {
cursor: pointer;
&:hover,
&:focus-within {
.g-card-content {
border-color: $brand-secondary;
box-shadow: 0 0 0 0.5px;
background-color: lighten($brand-light, 0.5);
}
}
}
.g-card-title-section {
min-width: 50%;
}
.g-card-rename {
visibility: hidden;
}
&:hover,
&:focus-within {
.g-card-rename {
visibility: visible;
}
}
.g-card-content {
background-color: $body-bg;
border: 1px solid $brand-secondary;
border-radius: 0.5rem;
.g-card-title-truncate {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: v-bind(allowedTitleLines);
line-clamp: v-bind(allowedTitleLines);
overflow: hidden;
line-height: 1.2;
white-space: normal;
text-overflow: unset;
}
.g-card-secondary-action-label {
@container g-card (max-width: #{$breakpoint-sm}) {
display: none;
}
}
}
&:focus {
outline: none;
.g-card-content {
border-color: $brand-primary;
box-shadow: 0 0 0 0.5px;
background-color: lighten($brand-light, 0.5);
}
}
}
</style>