Skip to content

Commit

Permalink
Moved sorting by title then IRI to a reusable function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiefeiss committed Sep 4, 2023
1 parent 10ba156 commit 74987e5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 67 deletions.
16 changes: 2 additions & 14 deletions src/components/search/CatPrezSearchMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useApiRequest, useSparqlRequest } from "@/composables/api";
import { useRdfStore } from "@/composables/rdfStore";
import { catalogSpatialSearch, getThemesQuery } from "@/sparqlQueries/catalogSearch";
import { shapeQueryPart } from "@/util/mapSearchHelper"
import { copyToClipboard } from "@/util/helpers";
import { copyToClipboard, sortByTitle } from "@/util/helpers";
import MapClient from "@/components/MapClient.vue";
import LoadingMessage from "@/components/LoadingMessage.vue";
import ErrorMessage from "@/components/ErrorMessage.vue";
Expand Down Expand Up @@ -137,19 +137,7 @@ async function getCatalogs() {
}
}, namedNode(qnameToIri("a")), namedNode(qnameToIri("dcat:Catalog")), null);
// sort by title first, then by IRI if no title
catalogOptions.sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
});
catalogs.value = catalogOptions;
catalogs.value = catalogOptions.sort(sortByTitle);
}
}
Expand Down
28 changes: 4 additions & 24 deletions src/components/search/SpacePrezSearchMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { apiBaseUrlConfigKey, mapConfigKey, type MapConfig, type ProfileHeader }
import { useUiStore } from "@/stores/ui";
import { useApiRequest, useConcurrentApiRequests, useSparqlRequest } from "@/composables/api";
import { useRdfStore } from "@/composables/rdfStore";
import { copyToClipboard } from "@/util/helpers";
import { copyToClipboard, sortByTitle } from "@/util/helpers";
import { AreaTypes, ShapeTypes, type Coords } from "@/components/MapClient.d";
import { enumToOptions } from "@/util/mapSearchHelper";
import MapClient from "@/components/MapClient.vue";
Expand Down Expand Up @@ -215,31 +215,11 @@ async function getDatasets() {
}, subject, namedNode(qnameToIri("rdfs:member")), null);
// sort by title first, then by IRI if no title
datasetOptions[subject.value].featureCollections.sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
});
datasetOptions[subject.value].featureCollections.sort(sortByTitle);
}, namedNode(qnameToIri("a")), namedNode(qnameToIri("dcat:Dataset")), null);
// sort by title first, then by IRI if no title
datasets.value = Object.values(datasetOptions).sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
});
datasets.value = Object.values(datasetOptions).sort(sortByTitle);
}
}
Expand Down Expand Up @@ -343,7 +323,7 @@ onMounted(async () => {
<template v-else>Collapse all <i class="fa-regular fa-chevron-up"></i></template>
</button>
</div>
<LoadingMessage v-if="datasetLoading" />
<LoadingMessage v-if="datasetLoading || fcLoading" />
<ErrorMessage v-else-if="datasetError" :message="`Unable to load datasets: ${datasetError}`" />
<ul v-else class="dataset-options">
<li v-for="(dataset, dIndex) in datasets" class="dataset-option">
Expand Down
19 changes: 19 additions & 0 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ export function ensureProfiles() {
export function copyToClipboard(text: string) {
navigator.clipboard.writeText(text.trim());
}

/**
* Sorts an array of objects alphabetically first, and then by IRI if some elements lack a title
*
* @param a
* @param b
* @returns
*/
export const sortByTitle = <T extends {title?: string; iri: string;}>(a: T, b: T): number => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
};
15 changes: 2 additions & 13 deletions src/views/ItemListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import PaginationComponent from "@/components/PaginationComponent.vue";
import { getPrezSystemLabel } from "@/util/prezSystemLabelMapping";
import SortableTabularList from "@/components/SortableTabularList.vue";
import LoadingMessage from "@/components/LoadingMessage.vue";
import { ensureProfiles } from "@/util/helpers";
import { ensureProfiles, sortByTitle } from "@/util/helpers";
const { namedNode } = DataFactory;
Expand Down Expand Up @@ -227,18 +227,7 @@ function getProperties() {
items.value.push(c);
});
// sort by title first, then by IRI if no title
items.value.sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
});
items.value.sort(sortByTitle);
}
function getIRILocalName(iri: string) {
Expand Down
22 changes: 6 additions & 16 deletions src/views/PropTableView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getPrezSystemLabel } from "@/util/prezSystemLabelMapping";
import MapClient from "@/components/MapClient.vue";
import SortableTabularList from "@/components/SortableTabularList.vue";
import LoadingMessage from "@/components/LoadingMessage.vue";
import { ensureProfiles } from "@/util/helpers";
import { ensureProfiles, sortByTitle } from "@/util/helpers";
const { namedNode } = DataFactory;
Expand Down Expand Up @@ -320,17 +320,7 @@ function getChildren() {
}, namedNode(item.value.iri), namedNode(childrenPredicate.value), null);
// sort by title, then by IRI
children.value.sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
} else if (a.title) {
return -1;
} else if (b.title) {
return 1;
} else {
return a.iri.localeCompare(b.iri);
}
});
children.value.sort(sortByTitle);
}
}
Expand Down Expand Up @@ -388,11 +378,11 @@ function getAllConcepts() {
if (!!c.broader && c.broader !== "") {
const parent = conceptArray[indexMap[c.broader]];
parent.children = [...(parent.children || []), c].sort((a, b) => a.title.localeCompare(b.title));
parent.children = [...(parent.children || []), c].sort(sortByTitle);
parent.childrenCount = parent.children.length;
}
});
conceptsList.sort((a, b) => a.title.localeCompare(b.title));
conceptsList.sort(sortByTitle);
concepts.value = conceptsList;
}
Expand Down Expand Up @@ -422,7 +412,7 @@ async function getTopConcepts(page: number = 1) {
concepts.value.push(c);
}, namedNode(item.value.iri), namedNode(conceptQnameToIri("skos:hasTopConcept")), null);
concepts.value.sort((a, b) => a.title.localeCompare(b.title));
concepts.value.sort(sortByTitle);
}
}
Expand Down Expand Up @@ -466,7 +456,7 @@ async function getNarrowers({ iriPath, link, page = 1 }: { iriPath: string, link
parent!.children.push(c);
}, namedNode(parent!.iri), namedNode(conceptQnameToIri("skos:narrower")), null);
parent!.children.sort((a, b) => a.title.localeCompare(b.title));
parent!.children.sort(sortByTitle);
}
}
Expand Down

0 comments on commit 74987e5

Please sign in to comment.