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

Sort collection members #52

Merged
merged 3 commits into from
Apr 12, 2016
Merged
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
14 changes: 7 additions & 7 deletions public/handlebars/statement.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
<div class="statement-textcolumn">
<h1>{{ data.prefLabel.[@value] }}</h1>
<p>{{ data.definition.[@value] }}</p>
<h2>{{i18n "notices" locale=language}}</h2>
<ul>
{{#data.note}}
<li><span>{{ [@value] }}</span></li>
{{/data.note}}
</ul>
<p class="disclaimer">{{{i18n "Disclaimer" locale=language}}}</p>
{{#parameters.date}}
<p>{{i18n data.identifier . locale=language}}</p>
{{/parameters.date}}

{{#parameters.relatedURL}}
<p>{{{i18n data.identifier . locale=language}}}</p>
{{/parameters.relatedURL}}
<h2>{{i18n "notices" locale=language}}</h2>
<ul>
{{#sort data.note direction="asc"}}
<li><span>{{ [@value] }}</span></li>
{{/sort}}
</ul>
<p class="disclaimer">{{{i18n "Disclaimer" locale=language}}}</p>
</div>
</div>
</div>
Expand Down
70 changes: 68 additions & 2 deletions public/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,78 @@ Handlebars.registerHelper('a', function (href, options) {
return new Handlebars.SafeString("<a href=\"" + href + "\">" + href + "</a>");
});

Handlebars.registerHelper('id', function(id, graph, options) {
Handlebars.registerHelper('resource', function(id, graph, options) {

for (var i = 0; i < graph.length; i++) {
if (graph[i]['@id'].trim() == id.trim()) {
if (graph[i]['@id'] == id) {
return options.fn(graph[i]);
}
}

});

Handlebars.registerHelper('property', function(property, graph, options) {

var sort = options.hash['sort'].split(" ");
var on_property = sort[0];
var order = sort[1];

var graphs = [];

for (var i = 0; i < property.length; i++) {
for (var j = 0; j < graph.length; j++) {
if (graph[j]['@id'] == property[i]) {
graphs.push(graph[j]);
}
}
}

var ret = "";

graphs.sort(order == "asc" ? sort_property_asc(on_property) : sort_property_desc(on_property));

for (var k = 0; k < graphs.length; k++) {
ret = ret + options.fn(graphs[k]);
}

return ret;

});

Handlebars.registerHelper('sort', function(values, options) {
// weird Rhino behavior, explicitly create array
var vals = [];
for (var i = 0; i < values.length; i++) {
vals.push(values[i]);
}
vals.sort(options.hash['direction'] == "asc" ? sort_asc : sort_desc);
var ret = "";
for (var i = 0; i < vals.length; i++) {
ret = ret + options.fn(vals[i]);
}
return ret;
});

function sort_property_asc(on_property) {
return function(a, b) {
return a[on_property]['@value'] == b[on_property]['@value']
? 0 : +(a[on_property]['@value'] > b[on_property]['@value']) || -1;
}
}

function sort_property_desc(on_property) {
return function(a, b) {
return a[on_property]['@value'] == b[on_property]['@value']
? 0 : +(a[on_property]['@value'] < b[on_property]['@value']) || -1;
}
}

function sort_asc(a, b) {
return a['@value'] == b['@value']
? 0 : +(a['@value'] > b['@value']) || -1;
}

function sort_desc(a, b) {
return a['@value'] == b['@value']
? 0 : +(a['@value'] < b['@value']) || -1;
}