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

Add ROR Support for Institutions #2135

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8d0b429
add a new column `ror_id` to the `institutions` table.
whomingbird Dec 12, 2024
0d3ce74
be able to enter ROR ID in UI and save it
whomingbird Dec 12, 2024
c413060
display ROR id as a clickable link
whomingbird Dec 13, 2024
b12edcb
add ror related javascript
whomingbird Dec 13, 2024
29963a1
add ror related css
whomingbird Dec 13, 2024
0dc8740
Add ROR support: Enable users to type an institution name and see a s…
whomingbird Dec 13, 2024
6166a48
validate ROR ID
whomingbird Dec 13, 2024
8783b19
fetch institution metadata via ROR ID
whomingbird Dec 13, 2024
bb9d5b5
add test
whomingbird Dec 13, 2024
dba83bf
add test for institution model
whomingbird Dec 13, 2024
6438f03
Simplify the form view for entering institution data.
whomingbird Dec 13, 2024
e524b00
better UI: add more informative text and related links
whomingbird Dec 16, 2024
6940737
improve the error response message when ROR ID is invalid
whomingbird Dec 17, 2024
76170fe
convert country code to country name
whomingbird Dec 17, 2024
e59942b
mini twist
whomingbird Dec 17, 2024
d5cc2f3
add `ror_id` into institution_serializer
whomingbird Jan 13, 2025
ed349c3
add `ror_id` into institution typehead
whomingbird Jan 13, 2025
52fe079
Add ROR support for project creation: Implement local and remote inst…
whomingbird Jan 13, 2025
9f7b717
Add `ror_id` attribute to Institution Read/Write API
whomingbird Jan 13, 2025
17191c9
update API examples for the new attribute `ror_id` attribute
whomingbird Jan 13, 2025
4b07a17
fix the javascript to enable/disable "submit" button
whomingbird Jan 14, 2025
ef4fb71
add more help text
whomingbird Jan 14, 2025
411c9fe
remove unused javascript
whomingbird Jan 14, 2025
aff30dc
improve CSS
whomingbird Jan 14, 2025
81dba73
more javascript twist
whomingbird Jan 14, 2025
bf3dacb
enable to save ror_id when creating an institution along with a project
whomingbird Jan 14, 2025
ef485f0
disable the input fields when the institution metadata are loaded loc…
whomingbird Jan 17, 2025
ba5f0ed
fix test
whomingbird Jan 17, 2025
60bbdb5
disable the input fields when insitution is selected from locally sav…
whomingbird Jan 23, 2025
ac3807f
remove the typo
whomingbird Jan 27, 2025
9dea67d
add "clear-fields" button when creating institutions
whomingbird Jan 27, 2025
36f9f2b
Rewrite the toggleUserInput method to disable input fields when insti…
whomingbird Jan 27, 2025
5fc578c
ror_id should be unique.
whomingbird Jan 27, 2025
2bf6b5c
when editing institution, if ror ID exists, inputs fields should be d…
whomingbird Jan 27, 2025
a0e76d2
bug fix for the readonly fields
whomingbird Jan 27, 2025
38ae0a6
remove unnecessary field 'institution_name'
whomingbird Jan 27, 2025
50059ae
update checkSubmitButtonEnabled() function
whomingbird Jan 27, 2025
826e822
bug fix #2123
whomingbird Jan 27, 2025
92de4b9
minor javascript fix
whomingbird Jan 29, 2025
5ad6f0d
Merge branch 'main' into add-ror-support
whomingbird Jan 31, 2025
910c6c7
bug fix: the new institution country is not saved
whomingbird Feb 2, 2025
4e3f857
find the existing institutions by title and ror id
whomingbird Feb 4, 2025
d184613
remove "new_institution_reminder"
whomingbird Feb 4, 2025
2043078
bug fix for showing ROR ID link
whomingbird Feb 4, 2025
9c46f93
add tests for administer create request project with an existing inst…
whomingbird Feb 4, 2025
15c2afa
update "request create" project by not_admin and add tests
whomingbird Feb 4, 2025
669fbd9
mini bug
whomingbird Feb 10, 2025
93ffa5e
update according to the PR review
whomingbird Feb 10, 2025
2288a69
use select dropdown for country instead of free text
whomingbird Feb 12, 2025
22a912a
accept "query" as a parameter and dynamically fetch data based on use…
whomingbird Feb 12, 2025
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
Binary file added app/assets/images/logos/ror_16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@
//= require copasi/copasi_simulation
//= require plotly-2.27.0.min
//= require extended_metadata_type

//= require ror/ror-flat-min
//= require ror/typeahead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed. typeahead.js.js in vendor is referenced above

//= require ror/institution-typeahead
272 changes: 272 additions & 0 deletions app/assets/javascripts/ror/institution-typeahead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
var ROR_API_URL = "https://api.ror.org/organizations";


function toggleUserInput(disabled) {
const action = disabled ? 'addClass' : 'removeClass';
const elements = [
'#institution_title',
'#institution_city',
'#institution_country',
'#institution_ror_id',
'#institution_web_page',
'.tt-input'
];

elements.forEach(selector => {
$j(selector)[action]('institution-input-disable');
$j(selector).prop("readonly", disabled);
});
}

function extractRorId(rorUrl) {
const regex = /https:\/\/ror\.org\/([^\/]+)/;
const match = rorUrl.match(regex);
if (match) {
return match[1];
} else {
return null;
}
}

function fetchRorData(rorId) {
var url = ROR_API_URL + '/' + rorId;
console.log(url);
fetch(url)
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.errors ? err.errors[0] : "Unknown error occurred");
});
}
return response.json();
})
.then(data => {
$j('#ror-response').html(JSON.stringify(data, undefined, 4));
$j('#institution_title').val(data.name);
$j('#institution_city').val(data.addresses[0]['city']);
$j('#institution_country').val(data.country.country_name);
$j('#institution_ror_id').val(extractRorId(data.id));
$j('#institution_web_page').val(data.links?.[0] || 'N/A');
$j('#ror-error-message').text('').hide();
$j('#institution_ror_id').removeClass("field_with_errors");
$j("#ror-error-message").closest(".form-group").removeClass("field_with_errors");
toggleUserInput(true);
})
.catch(error => {
$j('#ror-error-message').text(error.message).show();
$j('#institution_ror_id').addClass("field_with_errors");
$j("#ror-error-message").closest(".form-group").addClass("field_with_errors");
});
}

// ROR API source logic
function rorQuerySource(query, processSync, processAsync) {
if (query.length < 3) {
return processAsync([]); // Trigger only for 3+ characters
}
var url = ROR_API_URL + '?query=' + encodeURIComponent(query);
return $j.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function (json) {
const orgs = json.items;
return processAsync(orgs);
}
});
}

// Template for Local Institution suggestions
function localSuggestionTemplate(data) {
return `
<div>
<strong>${data.text}</strong>
<small>${data.hint || ''}</small>
</div>`;
}

// Template for ROR suggestions
function rorSuggestionTemplate(data) {
var altNames = "";
if (data.aliases.length > 0) {
altNames += data.aliases.join(", ") + ", ";
}
if (data.acronyms.length > 0) {
altNames += data.acronyms.join(", ") + ", ";
}
if (data.labels.length > 0) {
data.labels.forEach(label => {
altNames += label.label + ", ";
});
}
altNames = altNames.replace(/,\s*$/, ""); // Trim trailing comma
return `
<div>
<p>
${data.name}<br>
<small>${data.types[0]}, ${data.country.country_name}<br>
<i>${altNames}</i></small>
</p>
</div>`;
}


function initializeLocalInstitutions(endpoint = '/institutions/typeahead.json', cache = false) {

const baseUrl = `${window.location.protocol}//${window.location.host}`;
const fullUrl = `${baseUrl}${endpoint}`;
console.log("Institutions URL: " + fullUrl);

// Initialize and return the Bloodhound instance
return new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: fullUrl,
cache: cache, // Use the provided cache option
transform: function (response) {
// Map the results array to the structure Bloodhound expects
return response.results;
}
}
});
}

function clearInstitutionFields() {
$j('#institution_title').val('');
$j('#institution_id').val('');
$j('#institution_ror_id').val('');
$j('#institution_city').val('');
$j('#institution_country').val('');
$j('#institution_web_page').val('');
$j('#institution_address').val('');
}


$j(document).ready(function () {
var $j = jQuery.noConflict();

$j('#fetch-ror-data-with-id').on('click', function () {
console.log("Fetching ROR data by ID...");
fetchRorData($j('#institution_ror_id').val());
});

// if the institution title is not selected from the local list or ROR, but entered manually by user
$j('#institution_title').on('change', function () {
const inputValue = $j(this).val();
$j('#institution_title').val(inputValue);
checkSubmitButtonEnabled();
});


$j('#combined_typeahead .typeahead').typeahead(
{
hint: true,
highlight: true,
minLength: 1 // Start suggestions after typing at least 1 character
},
// First Dataset: Local Institutions
{
name: 'institutions',
display: 'text', // Display the 'text' field in the dropdown
source: initializeLocalInstitutions(), // Local data source
templates: {
header: '<div class="league-name">Institutions saved locally</div>',
suggestion: localSuggestionTemplate
}
},
// Second Dataset: Remote ROR Query
{
name: 'ror-query',
limit: 50,
async: true,
source: rorQuerySource,
templates: {
header: '<div class="league-name">Institutions fetched from ROR</div>',
pending: '<div class="empty-message">Fetching from ROR API ...</div>',
suggestion: rorSuggestionTemplate
},
display: function (data) {
return data.name;
},
value: function (data) {
return data.identifier;
}
}
);


$j('#ror_query_name .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
limit: 50,
async: true,
source: rorQuerySource,
templates: {
pending: [
'<div class="empty-message">',
'Fetching list ...',
'</div>'
].join('\n'),
suggestion: rorSuggestionTemplate
},
display: function (data) {
console.log("Fetching ROR data by name remotely...");
return data.name;
},
value: function (data) {
return data.identifier;
}
});

$j('#combined_typeahead .typeahead').bind('typeahead:select', function (ev, data) {
$j('#combined_typeahead .typeahead').typeahead('close');

if (data.hasOwnProperty("text")) {
$j('#institution_title').val(data.text);
$j('#institution_id').val(data.id);
$j('#institution_ror_id').val(data.ror_id);
$j('#institution_city').val(data.city);
$j('#institution_country').val(data.country_name);
$j('#institution_web_page').val(data.web_page);
}
else
{
$j('#institution_title').val(data.name);
$j('#institution_ror_id').val(data.id);
$j('#institution_city').val(data.addresses[0]['city']);
$j('#institution_country').val(data.country.country_name);
$j('#institution_ror_id').val(extractRorId(data.id));
$j('#institution_web_page').val(data.links[0]);
}
toggleUserInput(true);
checkSubmitButtonEnabled();
});

$j('#ror_query_name .typeahead').bind('typeahead:select', function (ev, suggestion) {
$j('#ror-response').html(JSON.stringify(suggestion, undefined, 4));
$j('#institution_city').val(suggestion.addresses[0]['city']);
$j('#institution_country').val(suggestion.country.country_name);
$j('#institution_ror_id').val(extractRorId(suggestion.id));
$j('#institution_web_page').val(suggestion.links[0]);
toggleUserInput(true);
});

$j('#clear-fields').on('click', function(event) {
event.preventDefault();
clearInstitutionFields();
toggleUserInput(false);
checkSubmitButtonEnabled();
});


if ($j('#institution_ror_id').val()!== '') {
toggleUserInput(true);
}
$j('#basic #name-01').bind('change', function() {
$j('#ror-response').html('');
});
});
1 change: 1 addition & 0 deletions app/assets/javascripts/ror/ror-flat-min.js

Large diffs are not rendered by default.

Loading