Skip to content

Commit

Permalink
autofill ontology id when using autocomplete in label
Browse files Browse the repository at this point in the history
  • Loading branch information
anitacaron committed Mar 11, 2024
1 parent 7291c39 commit d8735f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/ontology/src/assets/ols-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ require = function t(e, o, i) {
// now automatically fill ID field based on the selection
autocomplete_input_name = e.currentTarget.name;
// get the matching 'id' input field's name
// classifying_ontology_term_name -> classifying_ontology_term_id
target_input_name = autocomplete_input_name.replace("_name", "_id");
// classifying_ontology_term_label -> classifying_ontology_term
target_input_name = autocomplete_input_name.replace("_label", "");
if ($('input[name="' + target_input_name + '"]').length > 0) {
$('input[name="' + target_input_name + '"]')[0].value = i.data.shortForm;
}
Expand Down
120 changes: 28 additions & 92 deletions src/ontology/src/resources/ols_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// ONTOLOGY AUTOCOMPLETE
$("*[name='stressor_label']").first().attr({
"data-selectpath": "https://www.ebi.ac.uk/ols4/",
"data-olsontology": "pato",
"data-olsontology": "pato,chebi,ncit,envo,foodon,maxo,ncbitaxon",
"data-olswidget": "select",
"olstype": "",
"placeholder": "Search for ontology entity",
Expand All @@ -37,6 +37,33 @@
"placeholder": "Search for ontology entity",
});

$("*[name='attribute']").first().attr("readonly", true);
$("*[name='attribute_label']").first().attr({
"data-selectpath": "https://www.ebi.ac.uk/ols4/",
"data-olsontology": "chebi,envo",
"data-olswidget": "select",
"olstype": "",
"placeholder": "Search for ontology entity",
});

$("*[name='degree']").first().attr("readonly", true);
$("*[name='degree_label']").first().attr({
"data-selectpath": "https://www.ebi.ac.uk/ols4/",
"data-olsontology": "pato",
"data-olswidget": "select",
"olstype": "",
"placeholder": "Search for ontology entity",
});

$("*[name='medium']").first().attr("readonly", true);
$("*[name='medium_label']").first().attr({
"data-selectpath": "https://www.ebi.ac.uk/ols4/",
"data-olsontology": "envo,foodon",
"data-olswidget": "select",
"olstype": "",
"placeholder": "Search for ontology entity",
});

var app = require("ols-autocomplete");
var instance = new app();
console.log(instance)
Expand All @@ -55,96 +82,5 @@
}
instance.start(options);

// custom tokenizer that tokenizes with underscore char as well
function getObjTokenizer() {
return function setKey(keys) {
keys = Array.isArray(keys) ? keys : [].slice.call(arguments, 0);

return function tokenize(o) {
var tokens = [];

keys.forEach(function(k) {
tokens = tokens.concat(custom_nonword_tokenizer(String(o[k])));
});

return tokens;
};
};
}

function custom_nonword_tokenizer(str) {
str = String(str);
return str ? str.split(/[^a-zA-Z0-9]+/) : [];
}

$("*[name='stressor_label']").first().on('change', function(evt, item) {
if (!this.value) {
// clear id when term cleaned
$("*[name='stressor']").first()[0].value = "";
}
});

// PARENT AUTOCOMPLETE
$("*[name='parent_cell_set_accession']").first().attr("readonly", true);

function configure_autocomplete(divName, records) {
my_obj_tokenizer = getObjTokenizer();
// constructs the suggestion engine
var typeaheadBH = new Bloodhound({
// datumTokenizer: Bloodhound.tokenizers.obj.nonword(['cell_label', 'cell_set_accession']),
datumTokenizer: my_obj_tokenizer(['cell_label', 'cell_set_accession']),
queryTokenizer: Bloodhound.tokenizers.nonword,
local: records
});
typeaheadBH.initialize();

$("*[name='" + divName + "']").first().typeahead({
hint: false,
highlight: true,
minLength: 1
},
{
name: 'autocomplete',
source: typeaheadBH,
limit: 10,
display: function(selection){
return selection['cell_label'];
},
templates: {
suggestion: function(data) {
return '<p>' + data.cell_label + ' [' + data.cell_set_accession + '] </p>';
}
}
});

$("*[name='" + divName + "']").first().on('typeahead:selected', function(evt, item) {
$("*[name='parent_cell_set_accession']").first()[0].value = item['cell_set_accession'];
});
}// end configure_autocomplete

var table_json_url = String(window.location.href).split("/row")[0] + ".json?limit=100000&shape=value_rows";
var row_number = String(window.location.href).split("?")[0].split("/row/")[1]

$.getJSON(table_json_url, function (table_data) {
var form_data = table_data[parseInt(row_number) - 1]

console.log("Form data ", form_data);
console.log("All data ", table_data);
// sort highest rank first but ordered by accession_id if same rank. Then filter higher ranks only
// table_data.sort((a, b) => (a.rank > b.rank) ? -1 : (a.rank === b.rank) ? ((a.cell_set_accession > b.cell_set_accession) ? 1 : -1) : 1 );
// var parents = table_data.filter(function (el) {
// return parseInt(el.rank) > parseInt(form_data.rank);
// });
// console.log("Table data ", parents);

configure_autocomplete("parent_cell_set_name", table_data);
});

$("*[name='parent_cell_set_name']").first().on('change', function(evt, item) {
if (!this.value) {
// clear id when term cleaned
$("*[name='parent_cell_set_accession']").first()[0].value = "";
}
});
</script>
{% endblock %}

0 comments on commit d8735f1

Please sign in to comment.