Skip to content

Commit

Permalink
Merge pull request #52 from rightsstatements/sort-collection-members
Browse files Browse the repository at this point in the history
Sort collection members
  • Loading branch information
anarchivist committed Apr 12, 2016
2 parents 45acbc8 + 3da2478 commit 31dde0d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
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;
}

0 comments on commit 31dde0d

Please sign in to comment.