Bundle hiddne tags, and a single remove with warning

This commit is contained in:
shamoon committed 2026-11-25 16:20:09 -08:00
1 parent 5ea1bd92c0
commit f412d16d8c
4 files changed
+139 -5

No files matched your search

@@ -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<string, string> = {
exclamationTriangle,
exclamationTriangleFill,
eye,
eyeSlash,
fileEarmark,
fileEarmarkArrowUp,
fileEarmarkCheck,
@@ -7,7 +7,7 @@
}
<div class="position-relative" [class.col-md-9]="horizontal">
<div class="input-group flex-nowrap">
<ng-select #tagSelect name="tags" [items]="tags" bindLabel="name" bindValue="id" [(ngModel)]="value" [ngModelOptions]="{standalone: true}"
<ng-select #tagSelect name="tags" [items]="tags" bindLabel="name" bindValue="id" [ngModel]="value" (ngModelChange)="onModelChange($event)" [ngModelOptions]="{standalone: true}"
[disabled]="disabled"
[multiple]="multiple"
[closeOnSelect]="false"
@@ -24,11 +24,23 @@
<ng-template ng-label-tmp let-item="item">
@if (isImplied(item.id)) {
<span class="implied-tag"></span>
} @else if (isHidden(item.id)) {
@if (item.id === hiddenTagIds[0]) {
<button class="tag-wrap btn p-0 d-flex align-items-center mw-100" (click)="removeHiddenTags()" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tags you don't have access to" i18n-title>
<pngx-bs-icon name="x" style="margin-inline-end: 1px;"></pngx-bs-icon>
<span class="badge text-bg-secondary d-inline-flex align-items-center gap-1">
<pngx-bs-icon name="eye-slash" width=".8em" height=".8em"></pngx-bs-icon>
<ng-container i18n>{{hiddenTagIds.length}} hidden</ng-container>
</span>
</button>
} @else {
<span class="implied-tag"></span>
}
} @else {
<button class="tag-wrap btn p-0 d-flex align-items-center mw-100" (click)="removeBadge(item.id)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tag" i18n-title>
<pngx-bs-icon name="x" style="margin-inline-end: 1px;"></pngx-bs-icon>
@if (item.id && tags) {
<pngx-tag class="mw-100 overflow-hidden" [tag]="getTag(item.id)" [split]="preserveHierarchy" (click)="removeTag(item.id); $event.stopPropagation()" (parentClick)="removeTag($event)"></pngx-tag>
<pngx-tag class="mw-100 overflow-hidden d-flex" [tag]="getTag(item.id)" [split]="preserveHierarchy" (click)="removeTag(item.id); $event.stopPropagation()" (parentClick)="removeTag($event)"></pngx-tag>
}
</button>
}
@@ -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()
})
})
})
@@ -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)