3">
+ @for (tagID of getTagIDs(visibleTagIDs); track tagID) {
+
}
- @if (moreTags) {
+ @if (getMoreTags(visibleTagIDs); as moreTags) {
+ {{moreTags}}
diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.scss b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.scss
index d098b126bc..0aa525fa31 100644
--- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.scss
+++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.scss
@@ -103,7 +103,8 @@ a {
line-height: 1;
&.tags-no-wrap {
- ::ng-deep .badge {
+ // split-pill parents clip to a stub instead (see TagComponent)
+ ::ng-deep .badge:not(.split-parent) {
display: inline-block;
max-width: 100%;
white-space: nowrap;
diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts
index 2b5b3245d4..c28037ab7f 100644
--- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts
+++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts
@@ -4,6 +4,9 @@ import { provideHttpClientTesting } from '@angular/common/http/testing'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { By } from '@angular/platform-browser'
import { RouterTestingModule } from '@angular/router/testing'
+import { of } from 'rxjs'
+import { PermissionsService } from 'src/app/services/permissions.service'
+import { TagService } from 'src/app/services/rest/tag.service'
import { TagComponent } from '../../common/tag/tag.component'
import { DocumentCardSmallComponent } from './document-card-small.component'
@@ -97,8 +100,36 @@ describe('DocumentCardSmallComponent', () => {
).toHaveLength(6)
})
+ it('should hide parent tags implied by a child and count pills for the hidden tag counter', () => {
+ jest
+ .spyOn(TestBed.inject(PermissionsService), 'currentUserCan')
+ .mockReturnValue(true)
+ jest.spyOn(TestBed.inject(TagService), 'listAll').mockReturnValue(
+ of({
+ count: 8,
+ all: [1, 2, 3, 4, 5, 6, 7, 8],
+ results: [
+ { id: 1, name: 'Parent' },
+ { id: 2, name: 'Child', parent: 1 },
+ ...[3, 4, 5, 6, 7, 8].map((id) => ({ id, name: `Tag ${id}` })),
+ ],
+ })
+ )
+ fixture.componentRef.setInput('document', {
+ ...component.document(),
+ tags: [1, 2, 3, 4, 5, 6, 7, 8],
+ })
+ fixture.detectChanges()
+
+ const tags = fixture.debugElement.queryAll(By.directive(TagComponent))
+ expect(tags.map((t) => t.componentInstance.tagID())).toEqual([
+ 2, 3, 4, 5, 6,
+ ])
+ expect(fixture.nativeElement.textContent).toContain('+ 2')
+ })
+
it('should clear hidden tag counter when tag count falls below the limit', () => {
- expect(component.moreTags).toEqual(3)
+ expect(component.getMoreTags(component.document().tags)).toEqual(3)
fixture.componentRef.setInput('document', {
...component.document(),
@@ -106,7 +137,7 @@ describe('DocumentCardSmallComponent', () => {
})
fixture.detectChanges()
- expect(component.moreTags).toBeNull()
+ expect(component.getMoreTags(component.document().tags)).toBeNull()
expect(fixture.nativeElement.textContent).not.toContain('+ 3')
})
diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts
index 784b47ce08..6b9e5f0972 100644
--- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts
+++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts
@@ -20,6 +20,7 @@ import {
Document,
} from 'src/app/data/document'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
+import { CollapseTagsPipe } from 'src/app/pipes/collapse-tags.pipe'
import { CorrespondentNamePipe } from 'src/app/pipes/correspondent-name.pipe'
import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe'
import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe'
@@ -45,6 +46,7 @@ import { LoadingComponentWithPermissions } from '../../loading-component/loading
TagComponent,
CustomFieldDisplayComponent,
AsyncPipe,
+ CollapseTagsPipe,
NgOptimizedImage,
UsernamePipe,
CorrespondentNamePipe,
@@ -90,12 +92,13 @@ export class DocumentCardSmallComponent
@Output()
clickStoragePath = new EventEmitter
()
- get moreTags(): number {
- const document = this.document()
- const limit = document?.notes.length > 0 ? 6 : 7
- return document?.tags.length > limit
- ? document.tags.length - (limit - 1)
- : null
+ private get tagLimit(): number {
+ return this.document()?.notes.length > 0 ? 6 : 7
+ }
+
+ getMoreTags(tagIDs: number[]): number {
+ const limit = this.tagLimit
+ return tagIDs?.length > limit ? tagIDs.length - (limit - 1) : null
}
@ViewChild('popupPreview') popupPreview: PreviewPopupComponent
@@ -116,14 +119,9 @@ export class DocumentCardSmallComponent
return this.documentService.getDownloadUrl(this.document().id)
}
- get tagIDs() {
- const document = this.document()
- const limit = document.notes.length > 0 ? 6 : 7
- if (document.tags.length > limit) {
- return document.tags.slice(0, limit - 1)
- } else {
- return document.tags
- }
+ getTagIDs(tagIDs: number[]): number[] {
+ const limit = this.tagLimit
+ return tagIDs?.length > limit ? tagIDs.slice(0, limit - 1) : tagIDs
}
mouseLeaveCard() {
diff --git a/src-ui/src/app/components/document-list/document-list.component.html b/src-ui/src/app/components/document-list/document-list.component.html
index 71c384300c..3f6e2e56bd 100644
--- a/src-ui/src/app/components/document-list/document-list.component.html
+++ b/src-ui/src/app/components/document-list/document-list.component.html
@@ -335,8 +335,8 @@
{{d.title | documentTitle}}
}
@if (activeDisplayFields.includes(DisplayField.TAGS)) {
- @for (tagID of d.tags; track tagID) {
-
+ @for (tagID of d.tags | collapseTags | async; track tagID) {
+
}
}
diff --git a/src-ui/src/app/components/document-list/document-list.component.ts b/src-ui/src/app/components/document-list/document-list.component.ts
index b182b40af2..146e3dda88 100644
--- a/src-ui/src/app/components/document-list/document-list.component.ts
+++ b/src-ui/src/app/components/document-list/document-list.component.ts
@@ -39,6 +39,7 @@ import {
SortableDirective,
SortEvent,
} from 'src/app/directives/sortable.directive'
+import { CollapseTagsPipe } from 'src/app/pipes/collapse-tags.pipe'
import { CorrespondentNamePipe } from 'src/app/pipes/correspondent-name.pipe'
import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe'
import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe'
@@ -96,6 +97,7 @@ import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-vi
StoragePathNamePipe,
BootstrapIconComponent,
AsyncPipe,
+ CollapseTagsPipe,
FormsModule,
ReactiveFormsModule,
NgTemplateOutlet,
diff --git a/src-ui/src/app/data/ui-settings.ts b/src-ui/src/app/data/ui-settings.ts
index 7542f10918..58049f3717 100644
--- a/src-ui/src/app/data/ui-settings.ts
+++ b/src-ui/src/app/data/ui-settings.ts
@@ -95,6 +95,7 @@ export const SETTINGS_KEYS = {
DEFAULT_PERMS_VIEW_GROUPS: 'general-settings:permissions:default-view-groups',
DEFAULT_PERMS_EDIT_USERS: 'general-settings:permissions:default-edit-users',
DEFAULT_PERMS_EDIT_GROUPS: 'general-settings:permissions:default-edit-groups',
+ TAGS_SHOW_HIERARCHY: 'general-settings:tags:show-hierarchy',
DOCUMENT_EDITING_REMOVE_INBOX_TAGS:
'general-settings:document-editing:remove-inbox-tags',
DOCUMENT_EDITING_OVERLAY_THUMBNAIL:
@@ -311,6 +312,11 @@ export const SETTINGS: UiSetting[] = [
type: 'string',
default: '',
},
+ {
+ key: SETTINGS_KEYS.TAGS_SHOW_HIERARCHY,
+ type: 'boolean',
+ default: true,
+ },
{
key: SETTINGS_KEYS.DOCUMENT_EDITING_REMOVE_INBOX_TAGS,
type: 'boolean',
diff --git a/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts b/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts
new file mode 100644
index 0000000000..d9f3b23220
--- /dev/null
+++ b/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts
@@ -0,0 +1,67 @@
+import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
+import { provideHttpClientTesting } from '@angular/common/http/testing'
+import { TestBed } from '@angular/core/testing'
+import { firstValueFrom, of, throwError } from 'rxjs'
+import { Tag } from '../data/tag'
+import { PermissionsService } from '../services/permissions.service'
+import { TagService } from '../services/rest/tag.service'
+import { SettingsService } from '../services/settings.service'
+import { CollapseTagsPipe } from './collapse-tags.pipe'
+
+const tags: Tag[] = [
+ { id: 1, name: 'Parent' },
+ { id: 2, name: 'Child', parent: 1 },
+ { id: 3, name: 'Other' },
+]
+
+describe('CollapseTagsPipe', () => {
+ let pipe: CollapseTagsPipe
+ let tagService: TagService
+ let settingsService: SettingsService
+ let permissionsService: PermissionsService
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({
+ providers: [
+ CollapseTagsPipe,
+ provideHttpClient(withInterceptorsFromDi()),
+ provideHttpClientTesting(),
+ ],
+ })
+ pipe = TestBed.inject(CollapseTagsPipe)
+ tagService = TestBed.inject(TagService)
+ settingsService = TestBed.inject(SettingsService)
+ permissionsService = TestBed.inject(PermissionsService)
+ jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true)
+ jest.spyOn(settingsService, 'get').mockReturnValue(true)
+ jest
+ .spyOn(tagService, 'listAll')
+ .mockReturnValue(of({ count: 3, all: [1, 2, 3], results: tags }))
+ })
+
+ it('should drop parents implied by a selected child', async () => {
+ expect(await firstValueFrom(pipe.transform([1, 2, 3]))).toEqual([2, 3])
+ })
+
+ it('should pass ids through when hierarchy display is off or not permitted', async () => {
+ jest.spyOn(settingsService, 'get').mockReturnValue(false)
+ expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
+
+ jest.spyOn(settingsService, 'get').mockReturnValue(true)
+ jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(false)
+ expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
+ expect(tagService.listAll).not.toHaveBeenCalled()
+ })
+
+ it('should handle empty input', async () => {
+ expect(await firstValueFrom(pipe.transform(null))).toEqual([])
+ expect(await firstValueFrom(pipe.transform([]))).toEqual([])
+ })
+
+ it('should fall back to the original ids if tags fail to load', async () => {
+ jest
+ .spyOn(tagService, 'listAll')
+ .mockReturnValue(throwError(() => new Error('failed')))
+ expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
+ })
+})
diff --git a/src-ui/src/app/pipes/collapse-tags.pipe.ts b/src-ui/src/app/pipes/collapse-tags.pipe.ts
new file mode 100644
index 0000000000..5ba98766d3
--- /dev/null
+++ b/src-ui/src/app/pipes/collapse-tags.pipe.ts
@@ -0,0 +1,47 @@
+import { inject, Pipe, PipeTransform } from '@angular/core'
+import { catchError, map, Observable, of } from 'rxjs'
+import { Tag } from '../data/tag'
+import { SETTINGS_KEYS } from '../data/ui-settings'
+import {
+ PermissionAction,
+ PermissionsService,
+ PermissionType,
+} from '../services/permissions.service'
+import { TagService } from '../services/rest/tag.service'
+import { SettingsService } from '../services/settings.service'
+import { collapseImpliedAncestors } from '../utils/collapse-tags'
+
+/**
+ * Drops tag IDs whose descendant is also in the list for when the child's
+ * Tag badge already shows its parent.
+ */
+@Pipe({
+ name: 'collapseTags',
+})
+export class CollapseTagsPipe implements PipeTransform {
+ private readonly permissionsService = inject(PermissionsService)
+ private readonly tagService = inject(TagService)
+ private readonly settingsService = inject(SettingsService)
+
+ transform(ids: number[]): Observable {
+ if (
+ !ids?.length ||
+ !this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY) ||
+ !this.permissionsService.currentUserCan(
+ PermissionAction.View,
+ PermissionType.Tag
+ )
+ ) {
+ return of(ids ?? [])
+ }
+ return this.tagService.listAll().pipe(
+ map((tags) =>
+ collapseImpliedAncestors(
+ ids,
+ new Map(tags.results.map((t) => [t.id, t]))
+ )
+ ),
+ catchError(() => of(ids))
+ )
+ }
+}
diff --git a/src-ui/src/app/utils/collapse-tags.spec.ts b/src-ui/src/app/utils/collapse-tags.spec.ts
new file mode 100644
index 0000000000..f63ed36dfd
--- /dev/null
+++ b/src-ui/src/app/utils/collapse-tags.spec.ts
@@ -0,0 +1,47 @@
+import { Tag } from '../data/tag'
+import { collapseImpliedAncestors, getTagAncestors } from './collapse-tags'
+
+const tags: Tag[] = [
+ { id: 1, name: 'Finance' },
+ { id: 2, name: 'Account', parent: 1 },
+ { id: 3, name: 'Mortgage', parent: 2 },
+ { id: 4, name: 'Taxes', parent: 1 },
+ { id: 5, name: 'Inbox' },
+]
+const tagsById = new Map(tags.map((t) => [t.id, t]))
+
+describe('collapseImpliedAncestors', () => {
+ it('drops ancestors that are implied by a descendant in the list', () => {
+ expect(collapseImpliedAncestors([1, 2, 3, 4, 5], tagsById)).toEqual([
+ 3, 4, 5,
+ ])
+ })
+
+ it('keeps a parent with none of its children present', () => {
+ expect(collapseImpliedAncestors([1, 5], tagsById)).toEqual([1, 5])
+ })
+
+ it('keeps unknown (private) ids and handles empty input', () => {
+ expect(collapseImpliedAncestors([99, 2], tagsById)).toEqual([99, 2])
+ expect(collapseImpliedAncestors([], tagsById)).toEqual([])
+ expect(collapseImpliedAncestors(null, tagsById)).toEqual([])
+ })
+})
+
+describe('getTagAncestors', () => {
+ it('returns ancestors root first', () => {
+ expect(getTagAncestors(3, tagsById).map((t) => t.id)).toEqual([1, 2])
+ expect(getTagAncestors(5, tagsById)).toEqual([])
+ })
+
+ it('stops at a parent that is not visible or forms a cycle', () => {
+ const hidden = new Map([[6, { id: 6, name: 'x', parent: 99 }]])
+ expect(getTagAncestors(6, hidden)).toEqual([])
+
+ const cyclic = new Map([
+ [7, { id: 7, name: 'a', parent: 8 }],
+ [8, { id: 8, name: 'b', parent: 7 }],
+ ])
+ expect(getTagAncestors(7, cyclic).map((t) => t.id)).toEqual([7, 8])
+ })
+})
diff --git a/src-ui/src/app/utils/collapse-tags.ts b/src-ui/src/app/utils/collapse-tags.ts
new file mode 100644
index 0000000000..72f31646fb
--- /dev/null
+++ b/src-ui/src/app/utils/collapse-tags.ts
@@ -0,0 +1,43 @@
+import { Tag } from '../data/tag'
+
+/**
+ * Collapses implied ancestor tags from a list of tag IDs such that if a tag's
+ * ancestor is also present in the list, the ancestor is removed. For example
+ * [Finance, Account, Mortgage] -> [Mortgage]
+ */
+export function collapseImpliedAncestors(
+ ids: number[],
+ tagsById: Map
+): number[] {
+ if (!ids?.length) return ids ?? []
+ const present = new Set(ids)
+ const implied = new Set()
+ for (const id of ids) {
+ const seen = new Set()
+ let parentId = tagsById.get(id)?.parent
+ while (parentId && !seen.has(parentId)) {
+ seen.add(parentId)
+ if (present.has(parentId)) implied.add(parentId)
+ parentId = tagsById.get(parentId)?.parent
+ }
+ }
+ return ids.filter((id) => !implied.has(id))
+}
+
+/**
+ * Returns an array of ancestor tags for the given tag ID, starting from the
+ * root and ending with the immediate parent.
+ */
+export function getTagAncestors(id: number, tagsById: Map): Tag[] {
+ const chain: Tag[] = []
+ const seen = new Set()
+ let parentId = tagsById.get(id)?.parent
+ while (parentId && !seen.has(parentId)) {
+ seen.add(parentId)
+ const parent = tagsById.get(parentId)
+ if (!parent) break
+ chain.unshift(parent)
+ parentId = parent.parent
+ }
+ return chain
+}