[ui] sorting logic into util
This commit is contained in:
parent
212b904864
commit
9639bec677
3 changed files with 89 additions and 117 deletions
|
@ -2,7 +2,8 @@
|
||||||
import { readable, Readable } from "svelte/store";
|
import { readable, Readable } from "svelte/store";
|
||||||
|
|
||||||
import type { UpEntry, UpListing } from "upend";
|
import type { UpEntry, UpListing } from "upend";
|
||||||
import { query } from "../../lib/entity";
|
import { query } from "../../lib/entity";
|
||||||
|
import { defaultEntitySort } from "../../util/sort";
|
||||||
import Thumbnail from "./gallery/Thumbnail.svelte";
|
import Thumbnail from "./gallery/Thumbnail.svelte";
|
||||||
|
|
||||||
export let entries: UpEntry[];
|
export let entries: UpEntry[];
|
||||||
|
@ -47,64 +48,7 @@ import { query } from "../../lib/entity";
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortAttributes() {
|
function sortAttributes() {
|
||||||
sortedAttributes = entries
|
sortedAttributes = defaultEntitySort(entries, sortKeys);
|
||||||
.concat()
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
if (
|
|
||||||
!sortKeys[aEntry.value.c]?.length ||
|
|
||||||
!sortKeys[bEntry.value.c]?.length
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
Boolean(sortKeys[aEntry.value.c]?.length) &&
|
|
||||||
!sortKeys[bEntry.value.c]?.length
|
|
||||||
) {
|
|
||||||
return -1;
|
|
||||||
} else if (
|
|
||||||
!sortKeys[aEntry.value.c]?.length &&
|
|
||||||
Boolean(sortKeys[bEntry.value.c]?.length)
|
|
||||||
) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return String(aEntry.value.c).localeCompare(String(bEntry.value.c));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return sortKeys[aEntry.value.c][0].localeCompare(
|
|
||||||
sortKeys[bEntry.value.c][0],
|
|
||||||
undefined,
|
|
||||||
{ numeric: true, sensitivity: "base" }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
return String(aEntry.value.c).length - String(bEntry.value.c).length;
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
return aEntry.attribute.localeCompare(bEntry.attribute);
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
if (
|
|
||||||
!sortKeys[aEntry.entity]?.length ||
|
|
||||||
!sortKeys[bEntry.entity]?.length
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
Boolean(sortKeys[aEntry.entity]?.length) &&
|
|
||||||
!sortKeys[bEntry.entity]?.length
|
|
||||||
) {
|
|
||||||
return -1;
|
|
||||||
} else if (
|
|
||||||
!sortKeys[aEntry.entity]?.length &&
|
|
||||||
Boolean(sortKeys[bEntry.entity]?.length)
|
|
||||||
) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return aEntry.entity.localeCompare(bEntry.entity);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return sortKeys[aEntry.entity][0].localeCompare(
|
|
||||||
sortKeys[bEntry.entity][0]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
import Editable from "../utils/Editable.svelte";
|
import Editable from "../utils/Editable.svelte";
|
||||||
import { query } from "../../lib/entity";
|
import { query } from "../../lib/entity";
|
||||||
import { Readable, readable } from "svelte/store";
|
import { Readable, readable } from "svelte/store";
|
||||||
|
import { defaultEntitySort } from "../../util/sort";
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
export let columns: string;
|
export let columns: string;
|
||||||
|
@ -99,64 +100,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortAttributes() {
|
function sortAttributes() {
|
||||||
sortedAttributes = entries
|
sortedAttributes = defaultEntitySort(entries, sortKeys);
|
||||||
.concat()
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
if (
|
|
||||||
!sortKeys[aEntry.value.c]?.length ||
|
|
||||||
!sortKeys[bEntry.value.c]?.length
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
Boolean(sortKeys[aEntry.value.c]?.length) &&
|
|
||||||
!sortKeys[bEntry.value.c]?.length
|
|
||||||
) {
|
|
||||||
return -1;
|
|
||||||
} else if (
|
|
||||||
!sortKeys[aEntry.value.c]?.length &&
|
|
||||||
Boolean(sortKeys[bEntry.value.c]?.length)
|
|
||||||
) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return String(aEntry.value.c).localeCompare(String(bEntry.value.c));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return sortKeys[aEntry.value.c][0].localeCompare(
|
|
||||||
sortKeys[bEntry.value.c][0],
|
|
||||||
undefined,
|
|
||||||
{ numeric: true, sensitivity: "base" }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
return String(aEntry.value.c).length - String(bEntry.value.c).length;
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
return aEntry.attribute.localeCompare(bEntry.attribute);
|
|
||||||
})
|
|
||||||
.sort((aEntry, bEntry) => {
|
|
||||||
if (
|
|
||||||
!sortKeys[aEntry.entity]?.length ||
|
|
||||||
!sortKeys[bEntry.entity]?.length
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
Boolean(sortKeys[aEntry.entity]?.length) &&
|
|
||||||
!sortKeys[bEntry.entity]?.length
|
|
||||||
) {
|
|
||||||
return -1;
|
|
||||||
} else if (
|
|
||||||
!sortKeys[aEntry.entity]?.length &&
|
|
||||||
Boolean(sortKeys[bEntry.entity]?.length)
|
|
||||||
) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return aEntry.entity.localeCompare(bEntry.entity);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return sortKeys[aEntry.entity][0].localeCompare(
|
|
||||||
sortKeys[bEntry.entity][0]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
|
|
84
webui/src/util/sort.ts
Normal file
84
webui/src/util/sort.ts
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import type { UpEntry } from "upend";
|
||||||
|
|
||||||
|
export type SortKeys = { [key: string]: string[] };
|
||||||
|
|
||||||
|
export function sortByValue(entries: UpEntry[], sortKeys: SortKeys): void {
|
||||||
|
entries
|
||||||
|
.sort((aEntry, bEntry) => {
|
||||||
|
if (
|
||||||
|
!sortKeys[aEntry.value.c]?.length ||
|
||||||
|
!sortKeys[bEntry.value.c]?.length
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
Boolean(sortKeys[aEntry.value.c]?.length) &&
|
||||||
|
!sortKeys[bEntry.value.c]?.length
|
||||||
|
) {
|
||||||
|
return -1;
|
||||||
|
} else if (
|
||||||
|
!sortKeys[aEntry.value.c]?.length &&
|
||||||
|
Boolean(sortKeys[bEntry.value.c]?.length)
|
||||||
|
) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return String(aEntry.value.c).localeCompare(String(bEntry.value.c));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sortKeys[aEntry.value.c][0].localeCompare(
|
||||||
|
sortKeys[bEntry.value.c][0],
|
||||||
|
undefined,
|
||||||
|
{ numeric: true, sensitivity: "base" }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.sort((aEntry, bEntry) => {
|
||||||
|
return String(aEntry.value.c).length - String(bEntry.value.c).length;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sortByAttribute(entries: UpEntry[]): void {
|
||||||
|
entries.sort((aEntry, bEntry) => {
|
||||||
|
return aEntry.attribute.localeCompare(bEntry.attribute);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sortByEntity(entries: UpEntry[], sortKeys: SortKeys): void {
|
||||||
|
entries
|
||||||
|
.sort((aEntry, bEntry) => {
|
||||||
|
return aEntry.attribute.localeCompare(bEntry.attribute);
|
||||||
|
})
|
||||||
|
.sort((aEntry, bEntry) => {
|
||||||
|
if (
|
||||||
|
!sortKeys[aEntry.entity]?.length ||
|
||||||
|
!sortKeys[bEntry.entity]?.length
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
Boolean(sortKeys[aEntry.entity]?.length) &&
|
||||||
|
!sortKeys[bEntry.entity]?.length
|
||||||
|
) {
|
||||||
|
return -1;
|
||||||
|
} else if (
|
||||||
|
!sortKeys[aEntry.entity]?.length &&
|
||||||
|
Boolean(sortKeys[bEntry.entity]?.length)
|
||||||
|
) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return aEntry.entity.localeCompare(bEntry.entity);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sortKeys[aEntry.entity][0].localeCompare(
|
||||||
|
sortKeys[bEntry.entity][0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defaultEntitySort(
|
||||||
|
entries: UpEntry[],
|
||||||
|
sortKeys: SortKeys
|
||||||
|
): UpEntry[] {
|
||||||
|
const result = entries.concat();
|
||||||
|
sortByValue(entries, sortKeys);
|
||||||
|
sortByAttribute(entries);
|
||||||
|
sortByEntity(entries, sortKeys);
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in a new issue