|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Directive, |
| 4 | + ElementRef, |
| 5 | + afterRenderEffect, |
| 6 | + computed, |
| 7 | + contentChildren, |
| 8 | + inject, |
| 9 | + input, |
| 10 | + output, |
| 11 | + signal, |
| 12 | + viewChild, |
| 13 | +} from '@angular/core'; |
| 14 | +import {TranslocoPipe} from '@jsverse/transloco'; |
| 15 | +import {LucideEllipsis, LucideX} from '@lucide/angular'; |
| 16 | + |
| 17 | +import {AppButtonComponent} from '../../ui/button/app-button.component'; |
| 18 | +import {AppMenuComponent} from '../../ui/menu/app-menu.component'; |
| 19 | +import {AppMenuTriggerDirective} from '../../ui/menu/app-menu-trigger.directive'; |
| 20 | +import {LayoutService} from '../../layout/layout.service'; |
| 21 | + |
| 22 | +const PILL_CHROME_WIDTH = 46; |
| 23 | +const ITEM_GAP = 4; |
| 24 | +const MORE_BUTTON_WIDTH = 40 + ITEM_GAP; |
| 25 | + |
| 26 | +@Component({ |
| 27 | + selector: 'app-browse-bulk-actions-divider', |
| 28 | + template: `@if (!mobileShell()) { |
| 29 | + <span class="mx-1.5 block h-6 w-px bg-border" aria-hidden="true"></span> |
| 30 | + }`, |
| 31 | + host: {class: 'contents'}, |
| 32 | +}) |
| 33 | +export class BrowseBulkActionsDividerComponent { |
| 34 | + private readonly layout = inject(LayoutService); |
| 35 | + protected readonly mobileShell = computed(() => !this.layout.isDesktop()); |
| 36 | +} |
| 37 | + |
| 38 | +@Directive({ |
| 39 | + selector: '[appBrowseBulkActionsItem]', |
| 40 | + host: { |
| 41 | + '[class.invisible]': 'overflowed()', |
| 42 | + '[class.absolute]': 'overflowed()', |
| 43 | + '[class.left-0]': 'overflowed()', |
| 44 | + '[class.top-0]': 'overflowed()', |
| 45 | + '[attr.inert]': "overflowed() ? '' : null", |
| 46 | + }, |
| 47 | +}) |
| 48 | +export class BrowseBulkActionsItemDirective { |
| 49 | + readonly id = input.required<string>({alias: 'appBrowseBulkActionsItem'}); |
| 50 | + readonly element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement; |
| 51 | + |
| 52 | + private readonly bar = inject(BrowseBulkActionsBarComponent); |
| 53 | + protected readonly overflowed = computed(() => this.bar.overflowed().has(this.id())); |
| 54 | +} |
| 55 | + |
| 56 | +@Component({ |
| 57 | + selector: 'app-browse-bulk-actions-bar', |
| 58 | + imports: [TranslocoPipe, AppButtonComponent, AppMenuTriggerDirective, BrowseBulkActionsDividerComponent, LucideEllipsis, LucideX], |
| 59 | + host: {class: 'contents'}, |
| 60 | + template: ` |
| 61 | + <div |
| 62 | + #strip |
| 63 | + class="pointer-events-none fixed inset-x-0 bottom-[max(1.25rem,env(safe-area-inset-bottom))] z-30 flex justify-center pl-[calc(var(--sidebar-width,0px)*(1-var(--mobile-shell-active,0)))]" |
| 64 | + > |
| 65 | + <div |
| 66 | + class="pointer-events-auto relative flex h-12 max-w-[calc(100%-2rem)] items-center gap-1 overflow-hidden whitespace-nowrap rounded-xl border border-border bg-card px-1.5 text-sm shadow-float animate-in fade-in-0 slide-in-from-bottom-1 motion-reduce:animate-none" |
| 67 | + > |
| 68 | + @if (!mobileShell()) { |
| 69 | + <span #leading class="flex items-center gap-1"> |
| 70 | + <app-button |
| 71 | + variant="ghost" |
| 72 | + size="md" |
| 73 | + iconOnly |
| 74 | + [ariaLabel]="'shared.ui.select.clearSelection' | transloco" |
| 75 | + (clicked)="clearSelection.emit()" |
| 76 | + > |
| 77 | + <svg lucideX aria-hidden="true"></svg> |
| 78 | + </app-button> |
| 79 | + <span role="status" class="px-1 font-semibold tabular-nums text-text"> |
| 80 | + {{ 'shared.ui.select.selectedCount' | transloco: {count: countLabel()} }} |
| 81 | + </span> |
| 82 | + @if (showSelectAll()) { |
| 83 | + <app-button |
| 84 | + variant="ghost" |
| 85 | + tone="primary" |
| 86 | + size="md" |
| 87 | + [label]="'shared.ui.bulkActions.selectAll' | transloco" |
| 88 | + (clicked)="selectAll.emit()" |
| 89 | + /> |
| 90 | + } |
| 91 | + <app-browse-bulk-actions-divider /> |
| 92 | + </span> |
| 93 | + } |
| 94 | + <ng-content /> |
| 95 | + @if (moreMenu(); as menu) { |
| 96 | + @if (moreShown()) { |
| 97 | + <app-button |
| 98 | + variant="ghost" |
| 99 | + size="md" |
| 100 | + iconOnly |
| 101 | + [disabled]="moreDisabled()" |
| 102 | + [ariaLabel]="'browse.moreActions' | transloco" |
| 103 | + [appMenuTriggerFor]="menu" |
| 104 | + > |
| 105 | + <svg lucideEllipsis aria-hidden="true"></svg> |
| 106 | + </app-button> |
| 107 | + } |
| 108 | + } |
| 109 | + <ng-content select="[appBrowseBulkActionsTrailing]" /> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + `, |
| 113 | +}) |
| 114 | +export class BrowseBulkActionsBarComponent { |
| 115 | + readonly count = input.required<number>(); |
| 116 | + readonly total = input<number | null>(null); |
| 117 | + readonly moreMenu = input<AppMenuComponent | null>(null); |
| 118 | + readonly moreAlways = input(false); |
| 119 | + readonly moreDisabled = input(false); |
| 120 | + |
| 121 | + readonly clearSelection = output<void>(); |
| 122 | + readonly selectAll = output<void>(); |
| 123 | + |
| 124 | + private readonly overflowedIds = signal<ReadonlySet<string>>(new Set(), {equal: sameIds}); |
| 125 | + readonly overflowed = this.overflowedIds.asReadonly(); |
| 126 | + |
| 127 | + private readonly layout = inject(LayoutService); |
| 128 | + private readonly strip = viewChild.required<ElementRef<HTMLElement>>('strip'); |
| 129 | + private readonly leading = viewChild<ElementRef<HTMLElement>>('leading'); |
| 130 | + private readonly items = contentChildren(BrowseBulkActionsItemDirective); |
| 131 | + protected readonly mobileShell = computed(() => !this.layout.isDesktop()); |
| 132 | + protected readonly countLabel = computed(() => this.count().toLocaleString()); |
| 133 | + protected readonly showSelectAll = computed(() => { |
| 134 | + const total = this.total(); |
| 135 | + return total !== null && this.count() < total; |
| 136 | + }); |
| 137 | + protected readonly moreShown = computed(() => this.moreAlways() || this.overflowed().size > 0); |
| 138 | + |
| 139 | + constructor() { |
| 140 | + afterRenderEffect(onCleanup => { |
| 141 | + const strip = this.strip().nativeElement; |
| 142 | + const leading = this.leading()?.nativeElement; |
| 143 | + const items = this.items().map(item => ({id: item.id(), element: item.element})); |
| 144 | + const moreAlways = this.moreAlways(); |
| 145 | + let availableWidth = 0; |
| 146 | + const observer = new ResizeObserver(entries => { |
| 147 | + const stripEntry = entries.find(entry => entry.target === strip); |
| 148 | + if (stripEntry) availableWidth = stripEntry.contentRect.width; |
| 149 | + const capacity = availableWidth - PILL_CHROME_WIDTH - (leading ? leading.offsetWidth + ITEM_GAP : 0); |
| 150 | + const widths = items.map(item => ({id: item.id, width: item.element.offsetWidth + ITEM_GAP})); |
| 151 | + const withoutMoreButton = overflowingIds(widths, capacity); |
| 152 | + this.overflowedIds.set(moreAlways || withoutMoreButton.size > 0 |
| 153 | + ? overflowingIds(widths, capacity - MORE_BUTTON_WIDTH) |
| 154 | + : withoutMoreButton); |
| 155 | + }); |
| 156 | + observer.observe(strip); |
| 157 | + if (leading) observer.observe(leading); |
| 158 | + for (const item of items) observer.observe(item.element); |
| 159 | + onCleanup(() => observer.disconnect()); |
| 160 | + }); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function overflowingIds(items: readonly {id: string; width: number}[], capacity: number): ReadonlySet<string> { |
| 165 | + const overflowed = new Set<string>(); |
| 166 | + let used = 0; |
| 167 | + for (const item of items) { |
| 168 | + if (overflowed.size === 0 && used + item.width <= capacity) { |
| 169 | + used += item.width; |
| 170 | + } else { |
| 171 | + overflowed.add(item.id); |
| 172 | + } |
| 173 | + } |
| 174 | + return overflowed; |
| 175 | +} |
| 176 | + |
| 177 | +function sameIds(a: ReadonlySet<string>, b: ReadonlySet<string>): boolean { |
| 178 | + return a.size === b.size && [...a].every(id => b.has(id)); |
| 179 | +} |
0 commit comments