diff --git a/src-ui/src/app/components/common/bootstrap-icon/icons.ts b/src-ui/src/app/components/common/bootstrap-icon/icons.ts index 454588274..aff3b9534 100644 --- a/src-ui/src/app/components/common/bootstrap-icon/icons.ts +++ b/src-ui/src/app/components/common/bootstrap-icon/icons.ts @@ -67,6 +67,7 @@ import exclamationCircleFill from 'bootstrap-icons/icons/exclamation-circle-fill import exclamationTriangleFill from 'bootstrap-icons/icons/exclamation-triangle-fill.svg' import exclamationTriangle from 'bootstrap-icons/icons/exclamation-triangle.svg' import eye from 'bootstrap-icons/icons/eye.svg' +import eyeSlash from 'bootstrap-icons/icons/eye-slash.svg' import fileEarmarkArrowUp from 'bootstrap-icons/icons/file-earmark-arrow-up.svg' import fileEarmarkCheck from 'bootstrap-icons/icons/file-earmark-check.svg' import fileEarmarkDiff from 'bootstrap-icons/icons/file-earmark-diff.svg' @@ -230,6 +231,7 @@ export const icons: Record = { exclamationTriangle, exclamationTriangleFill, eye, + eyeSlash, fileEarmark, fileEarmarkArrowUp, fileEarmarkCheck, diff --git a/src-ui/src/app/components/common/input/tags/tags.component.html b/src-ui/src/app/components/common/input/tags/tags.component.html index 1227bb35a..a1c4a524a 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.html +++ b/src-ui/src/app/components/common/input/tags/tags.component.html @@ -7,7 +7,7 @@ }
- @if (isImplied(item.id)) { + } @else if (isHidden(item.id)) { + @if (item.id === hiddenTagIds[0]) { + + } @else { + + } } @else { } diff --git a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts index 738a2413b..8bc1876e6 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts @@ -333,4 +333,78 @@ describe('TagsComponent', () => { ).toBe(true) }) }) + + describe('hidden tags', () => { + // 98, 99 are not in the tags list i.e. the user cannot view them + beforeEach(() => { + component.tags = tags + component.value = [1, 98, 2, 99] + fixture.detectChanges() + }) + + it('should collapse hidden tags into a single badge', async () => { + await fixture.whenStable() + fixture.detectChanges() + expect(component.hiddenTagIds).toEqual([98, 99]) + expect(component.isHidden(1)).toBe(false) + const badges = fixture.nativeElement.querySelectorAll('.ng-value') + const visible = Array.from(badges).filter( + (b: HTMLElement) => !b.querySelector('.implied-tag') + ) as HTMLElement[] + expect(visible).toHaveLength(3) + expect(visible[1].textContent).toContain('2 hidden') + }) + + it('should not treat tags as hidden before tags load', () => { + ;(component as any).tagsLoaded = false + expect(component.hiddenTagIds).toEqual([]) + }) + + it('should not treat a hidden ancestor of a visible tag as hidden', () => { + component.tags = [{ id: 3, name: 'child', parent: 98 }] + component.value = [3, 98] + expect(component.hiddenTagIds).toEqual([]) + }) + + it('should keep hidden tags when ng-select changes the selection', () => { + component.onModelChange([]) + expect(component.value).toEqual([98, 99]) + component.onModelChange([1, 10]) + expect(component.value).toEqual([1, 10, 98, 99]) + }) + + it('should skip hidden tags on backspace', () => { + const backspace = new KeyboardEvent('keydown', { key: 'Backspace' }) + expect(component.onKeyDown(backspace)).toBe(false) + expect(component.value).toEqual([1, 98, 99]) + expect(component.onKeyDown(backspace)).toBe(false) + expect(component.onKeyDown(backspace)).toBe(false) + expect(component.value).toEqual([98, 99]) + }) + + it('should remove all hidden tags after confirmation', () => { + let propagated: number[] + component.registerOnChange((value: number[]) => (propagated = value)) + let activeInstances: NgbModalRef[] + modalService.activeInstances.subscribe((v) => (activeInstances = v)) + + component.removeHiddenTags() + expect(modalService.hasOpenModals()).toBeTruthy() + expect(component.value).toEqual([1, 98, 2, 99]) + + activeInstances[0].componentInstance.confirmClicked.emit() + expect(component.value).toEqual([1, 2]) + expect(propagated).toEqual([1, 2]) + }) + + it('should not remove hidden tags when disabled or none exist', () => { + component.disabled = true + component.removeHiddenTags() + expect(modalService.hasOpenModals()).toBeFalsy() + component.disabled = false + component.value = [1] + component.removeHiddenTags() + expect(modalService.hasOpenModals()).toBeFalsy() + }) + }) }) diff --git a/src-ui/src/app/components/common/input/tags/tags.component.ts b/src-ui/src/app/components/common/input/tags/tags.component.ts index 710334fd7..d43601e24 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.ts @@ -29,6 +29,7 @@ import { getTagAncestors, } from 'src/app/utils/collapse-tags' import { matchesSearchText } from 'src/app/utils/text-search' +import { ConfirmDialogComponent } from '../../confirm-dialog/confirm-dialog.component' import { EditDialogMode } from '../../edit-dialog/edit-dialog.component' import { TagEditDialogComponent } from '../../edit-dialog/tag-edit-dialog/tag-edit-dialog.component' import { TagComponent } from '../../tag/tag.component' @@ -85,6 +86,7 @@ export class TagsComponent implements OnInit, ControlValueAccessor { ngOnInit(): void { this.tagService.listAll().subscribe((result) => { this.tags = result.results + this.tagsLoaded = true this.changeDetector.markForCheck() }) } @@ -128,6 +130,8 @@ export class TagsComponent implements OnInit, ControlValueAccessor { tags: Tag[] = [] + private tagsLoaded = false + public createTagRef: (name) => void public searchFn = (term: string, tag: Tag): boolean => @@ -154,17 +158,59 @@ export class TagsComponent implements OnInit, ControlValueAccessor { ) } + isHidden(id: number): boolean { + return this.tagsLoaded && !this.tagsById.has(id) && !this.isImplied(id) + } + /** - * Remove the last visible tag (in case its actually a hidden ancestor) + * Tags that are hidden from the current user. + */ + get hiddenTagIds(): number[] { + return this.value?.filter((id) => this.isHidden(id)) ?? [] + } + + /** + * Remove the last visible tag (in case its actually a hidden ancestor or a + * tag the user cannot view) */ readonly onKeyDown = (event: KeyboardEvent): boolean => { if (event.key !== 'Backspace' || this.select?.searchTerm) return true - const visible = this.value?.filter((id) => !this.isImplied(id)) ?? [] + const visible = + this.value?.filter((id) => !this.isImplied(id) && !this.isHidden(id)) ?? + [] if (visible.length === (this.value?.length ?? 0)) return true - this.removeTag(visible.at(-1)) + if (visible.length) this.removeTag(visible.at(-1)) return false } + /** + * Changes from ng-select itself (e.g. clear all) never drop hidden tags, + * those are only removed via their own badge. + */ + onModelChange(newValue: number[]) { + const kept = this.hiddenTagIds.filter((id) => !newValue?.includes(id)) + this.value = [...(newValue ?? []), ...kept] + } + + removeHiddenTags() { + if (this.disabled) return + const hidden = new Set(this.hiddenTagIds) + if (!hidden.size) return + const modal = this.modalService.open(ConfirmDialogComponent, { + backdrop: 'static', + }) + modal.componentInstance.title = $localize`Remove hidden tags` + modal.componentInstance.messageBold = $localize`Remove ${hidden.size} tag(s) you don't have access to?` + modal.componentInstance.message = $localize`You will not be able to add them back.` + modal.componentInstance.btnClass = 'btn-danger' + modal.componentInstance.btnCaption = $localize`Remove` + modal.componentInstance.confirmClicked.pipe(first()).subscribe(() => { + modal.close() + this.value = this.value.filter((id) => !hidden.has(id)) + this.onChange(this.value) + }) + } + getTag(id: number) { if (this.tags) { return this.tags.find((tag) => tag.id == id)