Skip to content

Commit

Permalink
Merge pull request #1383 from NatLibFi/issue1377-slow-hierarchy
Browse files Browse the repository at this point in the history
Avoid slow HTML parsing in hierarchy sort function
  • Loading branch information
osma authored Oct 26, 2022
2 parents 1d48d64 + 61c6d07 commit 0a51dd5
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions resource/js/hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function createObjectsFromChildren(conceptData, conceptUri) {
var childArray = [];
for (var i = 0; i < conceptData.narrower.length; i++) {
var childObject = {
text: getLabel(conceptData.narrower[i]),
text: getLabel(conceptData.narrower[i]),
label: pickLabel(conceptData.narrower[i]),
a_attr: getHrefForUri(conceptData.narrower[i].uri),
uri: conceptData.narrower[i].uri,
notation: conceptData.narrower[i].notation,
Expand All @@ -86,7 +87,8 @@ function createObjectsFromChildren(conceptData, conceptUri) {
*/
function createConceptObject(conceptUri, conceptData) {
var newNode = {
text: getLabel(conceptData),
text: getLabel(conceptData),
label: pickLabel(conceptData),
a_attr: getHrefForUri(conceptData.uri),
uri: conceptUri,
notation: conceptData.notation,
Expand Down Expand Up @@ -188,7 +190,8 @@ function vocabRoot(topConcepts) {
for (var i = 0; i < topConcepts.length; i++) {
var conceptData = topConcepts[i];
var childObject = {
text: getLabel(conceptData),
text: getLabel(conceptData),
label: pickLabel(conceptData),
a_attr : getHrefForUri(conceptData.uri),
uri: conceptData.uri,
notation: conceptData.notation,
Expand Down Expand Up @@ -234,7 +237,8 @@ function createObjectsFromNarrowers(narrowerResponse) {
for (var i = 0; i < narrowerResponse.narrower.length; i++) {
var conceptObject = narrowerResponse.narrower[i];
var childObject = {
text : getLabel(conceptObject),
text : getLabel(conceptObject),
label: pickLabel(conceptObject),
a_attr : getHrefForUri(conceptObject.uri),
uri: conceptObject.uri,
notation: conceptObject.notation,
Expand Down Expand Up @@ -301,7 +305,8 @@ function schemeRoot(schemes) {
if(theDomainLabel != '') {
// Step 2.1 : create domain node without children
var domainObject = {
text: theDomainLabel,
text: theDomainLabel,
label: theDomainLabel,
// note that the class 'domain' will make sure the node will be sorted _before_ others
// (see the 'sort' function at the end)
a_attr : { "href" : vocab + '/' + lang + '/page/?uri=' + theDomain.uri, 'class': 'domain'},
Expand All @@ -321,6 +326,7 @@ function schemeRoot(schemes) {
domainObject.children.push(
{
text: theSchemeLabel,
label: theSchemeLabel,
a_attr:{ "href" : vocab + '/' + lang + '/page/?uri=' + theScheme.uri, 'class': 'scheme'},
uri: theScheme.uri,
children: true,
Expand All @@ -346,6 +352,7 @@ function schemeRoot(schemes) {
topArray.push(
{
text:theSchemeLabel,
label:theSchemeLabel,
a_attr:{ "href" : vocab + '/' + lang + '/page/?uri=' + theScheme.uri, 'class': 'scheme'},
uri: theScheme.uri,
children: true,
Expand Down Expand Up @@ -379,7 +386,8 @@ function topConceptsToSchemes(topConcepts, schemes) {
var topConcept = topConcepts[i];
var hasChildren = topConcept.hasChildren;
var childObject = {
text : getLabel(topConcept),
text: getLabel(topConcept),
label: pickLabel(topConcept),
a_attr: getHrefForUri(topConcept.uri),
uri: topConcept.uri,
notation: topConcept.notation,
Expand Down Expand Up @@ -407,10 +415,7 @@ function nodeLabelSortKey(node) {
// make sure the tree nodes with class 'domain' are sorted before the others
// domain will be "0" if the node has a domain class, else "1"
var domain = (node.original.a_attr['class'] == 'domain') ? "0" : "1";

// parse the HTML code in node.text and return just the label as a lower case value for sorting
// should look like '<span class="tree-notation">12.3</span> <span class="tree-label">Hello</span>'
var label = $($.parseHTML(node.text.toLowerCase())).filter('.tree-label').text();
var label = node.original.label.toLowerCase();

return domain + " " + label;
}
Expand Down

0 comments on commit 0a51dd5

Please sign in to comment.