From aa08d1f41304660368c1e0a73a6a0afb9cb35f57 Mon Sep 17 00:00:00 2001 From: kouralex <1723419+kouralex@users.noreply.github.com> Date: Fri, 25 Nov 2022 00:26:06 +0200 Subject: [PATCH] refactor sorting code for jstree; fixes #1390 --- resource/js/docready.js | 16 ++++++---- resource/js/groups.js | 32 +++++++++++++------ resource/js/hierarchy.js | 66 +------------------------------------- resource/js/scripts.js | 68 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 81 deletions(-) diff --git a/resource/js/docready.js b/resource/js/docready.js index c94842d67..70460851a 100644 --- a/resource/js/docready.js +++ b/resource/js/docready.js @@ -428,25 +428,29 @@ $(function() { // DOCUMENT READY $(document).on('click','div.group-hierarchy a', function(event) { $.ajaxQ.abortContentQueries(); + var targetUrl = event.target.href || event.target.parentElement.href; + $('#hier-trigger').attr('href', targetUrl); var $content = $('.content').empty().append($delayedSpinner.hide()); var loading = delaySpinner(); // ajaxing the sidebar content $.ajax({ - url : event.target.href, + url : targetUrl, req_kind: $.ajaxQ.requestKind.CONTENT, + complete: function() { clearTimeout(loading); }, success : function(data) { + $content.empty(); + var response = $('.content', data).html(); + if (window.history.pushState) { window.history.pushState({url: targetUrl}, '', targetUrl); } initHierarchyTooltip(); - $('#hier-trigger').attr('href', event.target.href); + $content.append(response); + updateJsonLD(data); updateTitle(data); updateTopbarLang(data); - $content.empty().append($('.content', data).html()); - $('.nav').scrollTop(0); - if (window.history.pushState) { window.history.pushState({}, null, event.target.href); } - updateTitle(data); ajaxConceptMapping(data); // take the content language buttons from the response $('.header-float .dropdown-menu').empty().append($('.header-float .dropdown-menu', data).html()); + $('.nav').scrollTop(0); } }); return false; diff --git a/resource/js/groups.js b/resource/js/groups.js index 26b86fc98..94155750c 100644 --- a/resource/js/groups.js +++ b/resource/js/groups.js @@ -52,7 +52,7 @@ function invokeGroupTree() { $treeObject.jstree({ 'plugins' : ['sort'], - 'sort' : function (a,b) { return naturalCompare(this.get_text(a).toLowerCase(), this.get_text(b).toLowerCase()); }, + 'sort' : hierarchySort, 'core' : { 'data' : function(node, cb) { @@ -69,13 +69,20 @@ function invokeGroupTree() { var children = []; for (var i in response.members) { var member = response.members[i]; - var child = {'text' : member.prefLabel,'parent' : node.a_attr['data-uri'], children : false, a_attr : { 'data-uri' : member.uri, "href" : getHrefForUri(member.uri, true) }}; + var child = { + text: getLabel(member), + label: pickLabel(member), + parent: node.a_attr['data-uri'], + notation: member.notation, + children: false, + a_attr: { + 'data-uri': member.uri, + "href": getHrefForUri(member.uri, true) + } + }; if (member.hasMembers || member.isSuper) { child.children = true; } - if (showNotation && member.notation) { - child.text = '' + member.notation + ' ' + child.text; - } children.push(JSON.parse(JSON.stringify(child))); } return cb(JSON.parse(JSON.stringify(children))); @@ -91,12 +98,19 @@ function invokeGroupTree() { } function createGroupNode(uri, groupObject) { - var node = {children : [], a_attr : {'data-uri' : uri, "href" : getHrefForUri(uri, true), "class" : "group" }}; - node.text = groupObject.prefLabel; + var node = { + text: getLabel(groupObject), + label: pickLabel(groupObject), + notation: groupObject.notation, + children: [], + a_attr: { + 'data-uri': uri, + "href": getHrefForUri(uri, true), + "class": "group" + } + }; if (groupObject.hasMembers || groupObject.isSuper) node.children = true; - if (showNotation && groupObject.notation) - node.text = '' + groupObject.notation + ' ' + node.text; return node; } diff --git a/resource/js/hierarchy.js b/resource/js/hierarchy.js index 0ae2dde22..8653c9798 100644 --- a/resource/js/hierarchy.js +++ b/resource/js/hierarchy.js @@ -45,17 +45,6 @@ function invokeParentTree(tree) { }); } -function getLabel(object) { - var labelProp = 'prefLabel'; - if (!object.prefLabel) { - labelProp = 'label'; - } - if (window.showNotation && object.notation) { - return '' + object.notation + ' ' + escapeHtml(object[labelProp]) + ''; - } - return '' + escapeHtml(object[labelProp]) + ''; -} - function createObjectsFromChildren(conceptData, conceptUri) { var childArray = []; for (var i = 0; i < conceptData.narrower.length; i++) { @@ -258,17 +247,6 @@ function getParams(node) { return $.param({'uri' : nodeId, 'lang' : clang}); } -function pickLabel(entity) { - var label = ''; - if (entity.prefLabel) - label = entity.prefLabel; - else if (entity.label) - label = entity.label; - else if (entity.title) - label = entity.title; - return label; -} - function schemeRoot(schemes) { var topArray = []; @@ -407,19 +385,6 @@ function topConceptsToSchemes(topConcepts, schemes) { return childArray; } -/* - * Return a sort key suitable for sorting hierarchy nodes mainly by label. - * Nodes with domain class will be sorted first, followed by non-domain nodes. - */ -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"; - var label = node.original.label.toLowerCase(); - - return domain + " " + label; -} - /* * Gives you the Skosmos default jsTree configuration. */ @@ -500,36 +465,7 @@ function getTreeConfiguration() { } }, 'plugins' : ['sort'], - 'sort' : function (a,b) { - var aNode = this.get_node(a); - var bNode = this.get_node(b); - - // sort on notation if requested, and notations exist - if (window.sortByNotation) { - var aNotation = aNode.original.notation; - var bNotation = bNode.original.notation; - - if (aNotation) { - if (bNotation) { - if (window.sortByNotation == "lexical") { - if (aNotation < bNotation) { - return -1; - } - else if (aNotation > bNotation) { - return 1; - } - } else { // natural - return naturalCompare(aNotation, bNotation); - } - } - else return -1; - } - else if (bNotation) return 1; - // NOTE: if no notations found, fall back on label comparison below - } - // no sorting on notation requested, or notations don't exist - return naturalCompare(nodeLabelSortKey(aNode), nodeLabelSortKey(bNode)); - } + 'sort' : hierarchySort }); } diff --git a/resource/js/scripts.js b/resource/js/scripts.js index 8d629f579..49fb051fe 100644 --- a/resource/js/scripts.js +++ b/resource/js/scripts.js @@ -1,4 +1,4 @@ -/* exported getUrlParams, getHrefForUri, readCookie, createCookie, debounce, updateContent, updateJsonLD, updateTopbarLang, updateTitle, updateSidebar, setLangCookie, clearResultsAndAddSpinner, loadLimitations, loadPage, hideCrumbs, hidePropertyValues, shortenProperties, countAndSetOffset, combineStatistics, loadLimitedResults, naturalCompare, makeCallbacks, escapeHtml, makeSelection, copyToClickboard, renderPropertyMappingValues, renderPropertyMappings, loadMappingProperties */ +/* exported getUrlParams, getHrefForUri, readCookie, createCookie, debounce, updateContent, updateJsonLD, updateTopbarLang, updateTitle, updateSidebar, setLangCookie, clearResultsAndAddSpinner, loadLimitations, loadPage, hideCrumbs, hidePropertyValues, shortenProperties, countAndSetOffset, combineStatistics, loadLimitedResults, naturalCompare, makeCallbacks, escapeHtml, makeSelection, copyToClickboard, renderPropertyMappingValues, renderPropertyMappings, loadMappingProperties, getLabel, pickLabel, nodeLabelSortKey, hierarchySort */ /* * Creates a cookie value and stores it for the user. Takes the given @@ -353,6 +353,72 @@ function naturalCompare(a, b) { return negVsPos(ax.length - bx.length); } +function getLabel(object) { + var labelProp = 'prefLabel'; + if (!object.prefLabel) { + labelProp = 'label'; + } + if (window.showNotation && object.notation) { + return '' + object.notation + ' ' + escapeHtml(object[labelProp]) + ''; + } + return '' + escapeHtml(object[labelProp]) + ''; +} + +function pickLabel(entity) { + var label = ''; + if (entity.prefLabel) + label = entity.prefLabel; + else if (entity.label) + label = entity.label; + else if (entity.title) + label = entity.title; + return label; +} + +/* + * Return a sort key suitable for sorting hierarchy nodes mainly by label. + * Nodes with domain class will be sorted first, followed by non-domain nodes. + */ +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"; + var label = node.original.label.toLowerCase(); + + return domain + " " + label; +} + +function hierarchySort(a,b) { + var aNode = this.get_node(a); + var bNode = this.get_node(b); + + // sort on notation if requested, and notations exist + if (window.sortByNotation) { + var aNotation = aNode.original.notation; + var bNotation = bNode.original.notation; + + if (aNotation) { + if (bNotation) { + if (window.sortByNotation == "lexical") { + if (aNotation < bNotation) { + return -1; + } + else if (aNotation > bNotation) { + return 1; + } + } else { // natural + return naturalCompare(aNotation, bNotation); + } + } + else return -1; + } + else if (bNotation) return 1; + // NOTE: if no notations found, fall back on label comparison below + } + // no sorting on notation requested, or notations don't exist + return naturalCompare(nodeLabelSortKey(aNode), nodeLabelSortKey(bNode)); +} + function makeCallbacks(data, pageType) { if (!pageType) { pageType = 'page';