]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Tweak: place 0 result items at bottom of filterable list (#8924)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Mon, 27 Jan 2025 08:02:56 +0000 (00:02 -0800)
committerGitHub <noreply@github.com>
Mon, 27 Jan 2025 08:02:56 +0000 (00:02 -0800)
src-ui/src/app/components/common/filterable-dropdown/filterable-dropdown.component.spec.ts
src-ui/src/app/components/common/filterable-dropdown/filterable-dropdown.component.ts

index f402cbdcec4a321f8be908b91a4d1ba6a688c90f..6ba15eacd30679ad220463aece08b5ce1171e56a 100644 (file)
@@ -496,6 +496,38 @@ describe('FilterableDropdownComponent & FilterableDropdownSelectionModel', () =>
     ])
   })
 
+  it('selection model should sort items by state and document counts = 0, if set', () => {
+    const tagA = { id: 4, name: 'Tag A' }
+    component.items = items.concat([tagA])
+    component.selectionModel = selectionModel
+    component.documentCounts = [
+      { id: 1, document_count: 0 }, // Tag1
+      { id: 2, document_count: 1 }, // Tag2
+      { id: 4, document_count: 2 }, // Tag A
+    ]
+    component.selectionModel.apply()
+    expect(selectionModel.items).toEqual([
+      nullItem,
+      tagA,
+      items[1], // Tag2
+      items[0], // Tag1
+    ])
+
+    selectionModel.toggle(items[1].id)
+    component.documentCounts = [
+      { id: 1, document_count: 0 },
+      { id: 2, document_count: 1 },
+      { id: 4, document_count: 0 },
+    ]
+    selectionModel.apply()
+    expect(selectionModel.items).toEqual([
+      nullItem,
+      items[1], // Tag2
+      tagA,
+      items[0], // Tag1
+    ])
+  })
+
   it('should set support create, keep open model and call createRef method', fakeAsync(() => {
     component.items = items
     component.icon = 'tag-fill'
index 73b7fd27b6cd44cc2e5ad388518830c79965aa4a..4e3d280dc5d173e8633abca9484d0d49af02646e 100644 (file)
@@ -81,6 +81,18 @@ export class FilterableDropdownSelectionModel {
         this.getNonTemporary(b.id) == ToggleableItemState.NotSelected
       ) {
         return -1
+      } else if (
+        this._documentCounts.length &&
+        this.getDocumentCount(b.id) === 0 &&
+        this.getDocumentCount(a.id) > this.getDocumentCount(b.id)
+      ) {
+        return -1
+      } else if (
+        this._documentCounts.length &&
+        this.getDocumentCount(a.id) === 0 &&
+        this.getDocumentCount(a.id) < this.getDocumentCount(b.id)
+      ) {
+        return 1
       } else {
         return a.name.localeCompare(b.name)
       }