Adding / removal of parents in UI

This commit is contained in:
shamoon
2025-02-10 22:37:35 -08:00
parent ef25dbc1bb
commit 07abad3315
2 changed files with 28 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
[addTag]="allowCreate ? createTagRef : false" [addTag]="allowCreate ? createTagRef : false"
addTagText="Add tag" addTagText="Add tag"
i18n-addTagText i18n-addTagText
(add)="onAdd($event)"
(change)="onChange(value)"> (change)="onChange(value)">
<ng-template ng-label-tmp let-item="item"> <ng-template ng-label-tmp let-item="item">

View File

@@ -129,13 +129,40 @@ export class TagsComponent implements OnInit, ControlValueAccessor {
let index = this.value.indexOf(id) let index = this.value.indexOf(id)
if (index > -1) { if (index > -1) {
const tag = this.getTag(id)
// remove tag
let oldValue = this.value let oldValue = this.value
oldValue.splice(index, 1) oldValue.splice(index, 1)
// remove children
oldValue = this.removeChildren(oldValue, tag)
this.value = [...oldValue] this.value = [...oldValue]
this.onChange(this.value) this.onChange(this.value)
} }
} }
private removeChildren(tagIDs: number[], tag: Tag) {
if (tag.children.length > 0) {
const childIDs = tag.children.map((child) => child.id)
tagIDs = tagIDs.filter((id) => !childIDs.includes(id))
}
for (const child of tag.children) {
tagIDs = this.removeChildren(tagIDs, child)
}
return tagIDs
}
public onAdd(tag: Tag) {
if (tag.parent) {
// add all parents recursively
const parent = this.getTag(tag.parent)
this.value = [...this.value, parent.id]
this.onAdd(parent)
}
}
createTag(name: string = null) { createTag(name: string = null) {
var modal = this.modalService.open(TagEditDialogComponent, { var modal = this.modalService.open(TagEditDialogComponent, {
backdrop: 'static', backdrop: 'static',