This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const recoUrl = 'https://raw.githubusercontent.com/etalab/datasets_reco/master/reco_json/'; | ||
const maxRecos = 2; | ||
|
||
function getDatasetId() { | ||
const selector = '#json_ld'; | ||
const dataset = JSON.parse(document.querySelector(selector).text); | ||
return dataset && dataset['@id']; | ||
} | ||
|
||
function addRecos(recos) { | ||
const recoContainer = document.getElementById('dataset-recommendations-container'); | ||
let recoChildContainer, recoChildEmbed; | ||
recos.splice(0, maxRecos).forEach((reco) => { | ||
recoChildContainer = document.createElement('div'); | ||
recoChildContainer.classList.add('recommendation'); | ||
|
||
recoChildEmbed = document.createElement('div'); | ||
recoChildEmbed.setAttribute('data-udata-dataset-id', reco[0]); | ||
|
||
recoChildContainer.appendChild(recoChildEmbed); | ||
recoContainer.appendChild(recoChildContainer); | ||
}); | ||
} | ||
|
||
function addWidgetScript() { | ||
const scriptElm = document.createElement('script'); | ||
scriptElm.type = 'application/javascript'; | ||
scriptElm.src = '/static/widgets.js'; | ||
scriptElm.id = 'udata'; | ||
scriptElm.onload = loadDatasets; | ||
document.body.appendChild(scriptElm); | ||
} | ||
|
||
function loadDatasets() { | ||
udataScript.loadDatasets(); | ||
} | ||
|
||
function fetchRecos(datasetId) { | ||
fetch(recoUrl + datasetId + '.json').then((response) => { | ||
if(response.ok) { | ||
return response.json(); | ||
} | ||
}).then((recos) => { | ||
recos = recos && recos[datasetId]; | ||
if (recos) { | ||
addRecos(recos); | ||
addWidgetScript(); | ||
const recoParent = document.getElementById('dataset-recommendations'); | ||
recoParent.style.display = 'block'; | ||
} | ||
}).catch((err) => { | ||
console.log('Error while fetching recommendations:', err); | ||
}); | ||
} | ||
|
||
global.udataDatasetRecos = { | ||
load() { | ||
const datasetId = getDatasetId(); | ||
if (datasetId) { | ||
fetchRecos(datasetId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#dataset-recommendations { | ||
display: none; | ||
.dataset-card { | ||
margin: 0 0 1em 0 !important; | ||
} | ||
|
||
.recommendation { | ||
margin-bottom: 1em; | ||
} | ||
|
||
@media only screen and (min-device-width: 1200px){ | ||
|
||
#dataset-recommendations-container { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
} | ||
|
||
.recommendation { | ||
width: 405px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{% extends 'dataset/display.html' %} | ||
|
||
{% set community_subtitle = _('Explore with %(certifier)s', certifier=config.SITE_TITLE ) %} | ||
|
||
{% block left_column_bottom %} | ||
<!-- dataset recommendations --> | ||
<script src="{{ theme_static('dataset-recommendations.js') }}" type="text/javascript" onload="udataDatasetRecos.load()"></script> | ||
<div id="dataset-recommendations" class="hidden-xs"> | ||
<h3>{{ _('See also') }}</h3> | ||
<div id="dataset-recommendations-container"></div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters