From d8735f1e28d48a172098faca7504af8b98c4dc0a Mon Sep 17 00:00:00 2001 From: Anita Caron Date: Mon, 11 Mar 2024 10:39:06 +0000 Subject: [PATCH] autofill ontology id when using autocomplete in label --- src/ontology/src/assets/ols-autocomplete.js | 4 +- src/ontology/src/resources/ols_form.html | 120 +++++--------------- 2 files changed, 30 insertions(+), 94 deletions(-) diff --git a/src/ontology/src/assets/ols-autocomplete.js b/src/ontology/src/assets/ols-autocomplete.js index 66d3c77..52d9eeb 100644 --- a/src/ontology/src/assets/ols-autocomplete.js +++ b/src/ontology/src/assets/ols-autocomplete.js @@ -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; } diff --git a/src/ontology/src/resources/ols_form.html b/src/ontology/src/resources/ols_form.html index e470419..82b008f 100644 --- a/src/ontology/src/resources/ols_form.html +++ b/src/ontology/src/resources/ols_form.html @@ -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", @@ -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) @@ -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 '

' + data.cell_label + ' [' + data.cell_set_accession + ']

'; - } - } - }); - - $("*[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 = ""; - } - }); {% endblock %} \ No newline at end of file