Skip to content

Commit

Permalink
Add project sidebar filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lydiascarf committed Jun 14, 2024
1 parent 4817561 commit 7b03152
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
3 changes: 3 additions & 0 deletions datasets.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ <h3>Geographical Scale</h3>
<h3>Licenses</h3>
<div class="list-group" data-component="rdl-license-filter" data-show="5"></div>

<h3>Projects</h3>
<div class="list-group" data-component="rdl-project-filter" data-show="5"></div>

</div>
<div class="col-sm-8" data-component="rdl-datasets-list">
<h3 class="rdl-datasets-count" data-hook="rdl-datasets-count"></h3>
Expand Down
6 changes: 5 additions & 1 deletion rdl-datasets.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
{{ "None" | jsonify }}
{% endif %},
"url": "{{ site.baseurl }}{{ dataset.url }}",
{% if dataset.version != "" %}"version": {{ dataset.version | jsonify }},{% endif %}
"project": {% if dataset.project %}
{{ dataset.project | jsonify }}
{% else %}
{{ "None" | jsonify }}
{% endif %}, {% if dataset.version != "" %}"version": {{ dataset.version | jsonify }},{% endif %}
"resources": {{ dataset.resources | jsonify }}
}{% unless forloop.last %},{% endunless %}{% endfor %}
]
2 changes: 1 addition & 1 deletion scripts/dist/bundle.js

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions scripts/src/components/rdl-project-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import $ from 'jquery'
import {chain, omit, defaults} from 'lodash'

import TmplListGroupItem from '../templates/list-group-item'
import {setContent, slugify, collapseListGroup} from '../util'


function prep_project(project, params, datasetsForProject) {
const projectSlug = slugify(project)
const selected = params.project && params.project === projectSlug
const itemParams = selected ? omit(params, 'project') : defaults({project: projectSlug}, params)
return {
title: project,
url: '?' + $.param(itemParams),
count: datasetsForProject.length,
unfilteredCount: params.length,
selected: selected
}
}

export default class {
constructor (opts) {
const projects = this._projectsWithCount(opts.datasets, opts.params)
var consolidated = []
projects.forEach(function(l) {
const idx = consolidated.findIndex(x => x.title === l.title)
if (idx == -1) {
consolidated.push(l)
} else {
consolidated[idx].count += l.count
consolidated[idx].unfilteredCount += l.unfilteredCount
}
})

const projectsMarkup = consolidated.sort((a,b) => {
// ignore upper and lowercase
const titleA = a.title.toUpperCase();
const titleB = b.title.toUpperCase();
if (titleA < titleB) return -1;
if (titleA > titleB) return 1;
return 0;
}).map(TmplListGroupItem)
setContent(opts.el, projectsMarkup)
collapseListGroup(opts.el)
}

_projectsWithCount (datasets, params) {
return chain(datasets)
.groupBy('project')
.flatMap(function (datasetsForProject, project) {
var projects = project.split(",")
var collated = projects.map(c => prep_project(c, params, datasetsForProject))

return collated
})
.orderBy('unfilteredCount', 'desc')
.value()
}
}
2 changes: 2 additions & 0 deletions scripts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import RDLDatasetDisplay from './components/rdl-datasets-display'
import ViewSwitcher from './components/view-switcher'
import ThemeGallery from './components/theme-gallery'
import {queryByComponent, setParams} from './util'
import RDLProjectFilter from './components/rdl-project-filter'

const params = $.deparam(window.location.search.substr(1))

Expand Down Expand Up @@ -46,6 +47,7 @@ const components = [
{tag: 'categories-filter', class: CategoriesFilter, usesDatasets: true},
{tag: 'rdl-country-filter', class: RDLCountryFilter, usesDatasets: true},
{tag: 'rdl-license-filter', class: RDLLicenseFilter, usesDatasets: true},
{tag: 'rdl-project-filter', class: RDLProjectFilter, usesDatasets: true},
{tag: 'rdl-geographical-scale-filter', class: RDLGeographicalScaleFilter, usesDatasets: true},
]
for (let component of components) {
Expand Down

0 comments on commit 7b03152

Please sign in to comment.