Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sorting code for hierarchical and groups tabs #1391

Open
wants to merge 1 commit into
base: skosmos-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions resource/js/docready.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 23 additions & 9 deletions resource/js/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = '<span class="tree-notation">' + member.notation + '</span> ' + child.text;
}
children.push(JSON.parse(JSON.stringify(child)));
}
return cb(JSON.parse(JSON.stringify(children)));
Expand All @@ -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 = '<span class="tree-notation">' + groupObject.notation + '</span> ' + node.text;
return node;
}

66 changes: 1 addition & 65 deletions resource/js/hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ function invokeParentTree(tree) {
});
}

function getLabel(object) {
var labelProp = 'prefLabel';
if (!object.prefLabel) {
labelProp = 'label';
}
if (window.showNotation && object.notation) {
return '<span class="tree-notation">' + object.notation + '</span> <span class="tree-label">' + escapeHtml(object[labelProp]) + '</span>';
}
return '<span class="tree-label">' + escapeHtml(object[labelProp]) + '</span>';
}

function createObjectsFromChildren(conceptData, conceptUri) {
var childArray = [];
for (var i = 0; i < conceptData.narrower.length; i++) {
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
});
}

68 changes: 67 additions & 1 deletion resource/js/scripts.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 '<span class="tree-notation">' + object.notation + '</span> <span class="tree-label">' + escapeHtml(object[labelProp]) + '</span>';
}
return '<span class="tree-label">' + escapeHtml(object[labelProp]) + '</span>';
}

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';
Expand Down