Skip to content

Commit

Permalink
Merge pull request #229 from edamontology/cors
Browse files Browse the repository at this point in the history
feat: Add error handling/form validation to custom version
  • Loading branch information
HagerDakroury authored Aug 19, 2021
2 parents a198d4a + 21c9a40 commit cf6a119
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,20 @@ <h4 class="modal-title" id="myModalLabel">Load custom ontology</h4>
</button>
<h4 class="modal-title" id="myModalLabel">Load custom EDAM version</h4>
</div>
<form action="/" method="get" onsubmit="" id="custom_version_from">
<form action="/" method="get" onsubmit="browser.cmd.loadCustomVersion();browser.current_branch('edam','custom'); return false;" id="custom_version_from">
<div class="modal-body">
<div class="form-group">
<label for="version_url">Custom Version URL</label>
<input type="text" class="form-control" id="version_url" required name="url">
<input type="url" class="form-control" id="version_url" required name="url" pattern=".+\.owl$">
<small class="form-text text-muted">You can try with https://raw.githubusercontent.com/edamontology/edamontology/main/EDAM_dev.owl</small>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="browser.cmd.loadCustomVersion();browser.current_branch('edam','custom')">Load
<button type="submit" class="btn btn-primary" >Load
EDAM
</button>
<button type="submit" style="display:none;"></button>
</div>
</form>
</div>
Expand Down
58 changes: 27 additions & 31 deletions js/tree-edam-stand-alone.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,57 +71,45 @@ function interactive_edam_browser(){
branch='edam';
}
$("#edam-branches .branch."+branch).addClass("active");
updateVersion(version);
updateBranch(branch);
//setCookie("edam_browser_branch",branch);
current_branch=branch;

//get tree from cache (either same version or a subset request)
if(!version || version==getCookie("edam_version","stable" )){

if(!version){
tree=JSON.parse(localStorage.getItem("current_edam"));
//cache is empty, load default version
if(!tree){
version='stable';
tree_url=getTreeURL(version);
__my_interactive_tree.data_url(tree_url);
setCookie("edam_version",version);
}
setCookie("isLoaded",true)
__my_interactive_tree.data(tree);
}

//load version (pre-determined or custom)
else {
//show loading spinner
document.getElementById("tree").style.display = "none";
$(".loader-wrapper").show();
//in case we're passed the raw url link directly

//in case we're passed the raw url link directly, no need to map it
if(customRe.test(version)){
tree_url=version;
setCookie("edam_version",version);

}
else{
tree_url=getTreeURL(version);
setCookie("edam_version",version);

setCookie("edam_version",tree_url);
}

if(version=='custom'){
version=tree_url;
//listed branch
else {
//maping branch to url
tree_url=getTreeURL(version);
setCookie("edam_version",version);
}

let uri = __my_interactive_tree.cmd.getElementByIdentifier(getInitURI(current_branch));
if (uri){
uri = __my_interactive_tree.identifierAccessor()(uri)
let params = ""
if(version!="stable"){
params+="&version="+version
}
if(current_branch!="edam"){
params+="&branch="+current_branch
if(version=='custom'){
//custom branch, identify it by the url
version=tree_url;
setCookie("edam_version",version);
}
window.location.hash = uri.replace("http://edamontology.org/","") + params;

}
}
__my_interactive_tree.data_url(tree_url);

}
Expand All @@ -131,15 +119,15 @@ function interactive_edam_browser(){
function loadCustomVersion(){
$("#versionModal").modal('hide');
let versionURL=document.getElementById('version_url').value;
setCookie("edam_version",versionURL);
setCookie("custom_url",versionURL);
}

function getTreeURL(version){
switch(version){
case 'latest':
return "https://raw.githubusercontent.com/edamontology/edamontology/main/EDAM_dev.owl";
case 'custom':
return getCookie("edam_version","");
return getCookie("custom_url","");
case 'stable':
return "https://raw.githubusercontent.com/edamontology/edamontology/main/releases/EDAM_1.25.owl";
default:
Expand Down Expand Up @@ -286,13 +274,21 @@ function interactive_edam_browser(){
.toLowerCase()
.replace(/[-]+/g,'-');
let params = ""
//update url only if the tree is loaded successfully
if(getCookie("isLoaded",true)=="true") {

if(version!="stable"){
params+="&version="+version
}
if(current_branch!="edam"){
params+="&branch="+current_branch
}
window.location.hash = uri.replace("http://edamontology.org/","") + params;

//update version and branch as well
updateVersion(version);
updateBranch(current_branch);
}
if(current_branch!="custom_url" && window.location.search){
setUrlParameters("");
}
Expand Down
14 changes: 13 additions & 1 deletion js/tree-reusable-d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,11 +872,23 @@ var interactive_tree = function() {

identifierToElement={};
data_url = value;
jsonTreeFromURL(value,(tree) => {
jsonTreeFromURL(value,
//tree loaded successfully
(tree) => {
setCookie("isLoaded",true);
localStorage.setItem("current_edam",JSON.stringify(tree))
setCookie("edam_url",value)
tree.meta.data_url=data_url;
chart.data(tree);
},
//tree can't be loaded, restore the previous state
(err)=>{
//alert the error
alert('We encountered the following error: '+ err.message+". Taking you back to the previous version.");
//load previous version from cache
setCookie("isLoaded",false);
var tree=JSON.parse(localStorage.getItem("current_edam"));
chart.data(tree);
});
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/preset-env": "^7.14.8",
"@babel/preset-env": "^7.14.9",
"@fortawesome/fontawesome-free": "^5.15.3",
"cssnano": "^5.0.6",
"jshint": "^2.9.5",
Expand All @@ -20,7 +20,7 @@
"dependencies": {
"bootstrap": "^3.4.1",
"d3": "^4.13.0",
"edam2json-js": "^0.2.2",
"edam2json-js": "^0.2.3",
"ga-gtag": "^1.1.0",
"gh-pages": "^3.2.3",
"jquery": "^3.2.1",
Expand Down

0 comments on commit cf6a119

Please sign in to comment.