ncbo/bioportal_web_ui

View on GitHub
public/widgets/minified/jquery.ncbo.tree-2.0.0.min.js

Summary

Maintainability
A
0 mins
Test Coverage
(function(b){var a=function(e,d){var o=this;var q;var u="roots";var j=false;var t;var r;var c;var l;q={autoclose:false,beforeExpand:false,afterExpand:false,afterExpandError:false,afterSelect:false,afterJumpToClass:false,timeout:999999,treeClass:"ncboTree",autocompleteClass:"ncboAutocomplete",width:350,ncboAPIURL:"http://data.bioontology.org",ncboUIURL:"http://bioportal.bioontology.org",apikey:null,ontology:null,startingClass:null,startingRoot:u,defaultRoot:u};q=b.extend(q,d);if(q.apikey==null){throw new Error("You must provide an API Key for NCBO Tree Widget to operate")}if(q.ontology==null){throw new Error("You must provide an ontology id for NCBO Tree Widget to operate")}this.options=function(){return q};this.jumpToClass=function(v,w){c.html(b("<span>").html("Loading...").css("font-size","smaller"));b.ajax({url:m(q.ncboAPIURL)+"/ontologies/"+q.ontology+"/classes/"+encodeURIComponent(v)+"/tree",data:{apikey:q.apikey,include:"prefLabel,childrenCount",no_context:true},contentType:"json",crossDomain:true,success:function(x){x=p(x);c.html(h(x));g(c,false);if(typeof w=="function"){w()}o.selectClass(v);if(typeof q.afterJumpToClass=="function"){q.afterJumpToClass(v)}t.trigger("afterJumpToClass",v)}})};this.selectClass=function(v){var w=b(r.find("a[data-id='"+encodeURIComponent(v)+"']"));b(r.find("a.active")[0]).removeClass("active");w.addClass("active")};this.selectedClass=function(){var v=b(r.find("a.active")[0]);if(v.length==0){return null}else{return{id:decodeURIComponent(v.data("id")),prefLabel:v.html(),URL:v.attr("href")}}};this.changeOntology=function(v){var w=b("<ul>").append(b("<li>").addClass("root"));t.html("");r=w;q.ontology=v;o.init()};var h=function(v){var x=b("<span>");var w=b("<ul>");v.sort(function(z,y){var B=z.prefLabel.toLowerCase();var A=y.prefLabel.toLowerCase();return((B<A)?-1:((B>A)?1:0))});b.each(v,function(C,z){var F=b("<li>");var E=b("<a>").attr("href",m(z.links.self)).html(z.prefLabel);E.attr("data-id",encodeURIComponent(z["@id"]));w.append(F.append(E));var A=typeof z.children!=="undefined"&&z.childrenCount>0&&z.children.length==0;if(z.childrenCount>0&&typeof z.children==="undefined"||A){var D=b("<ul>").addClass("ajax");var G=b("<li>");var y=b("<a>").attr("href",z.links.children);F.append(D.append(G.append(y)))}else{if(typeof z.children!=="undefined"&&z.children.length>0){var B=h(z.children);F.attr("class","folder-open");F.append(B)}}});x.append(w);return x.html()};var p=function(x){var z=(q.startingRoot==q.defaultRoot)?null:q.startingRoot;if(z==null){return x}var w=false;var v=x;while(v.length>0||w==false){var y=v.shift();if(y["@id"]==z){w=[y]}else{if(typeof y.children!=="undefined"&&y.children.length>0){v=v.concat(y.children)}}}return w};var n=function(v){b(v).siblings().filter(".folder-open, .folder-open-last").each(function(){var w=b(">ul",this);var x=this.className;this.className=x.replace("open","close");w.hide()})};var k=function(w){var v=b(">ul",w);if(v.is(":visible")){w.className=w.className.replace("open","close");v.hide()}else{w.className=w.className.replace("close","open");v.show();if(q.autoclose){n(w)}if(v.is(".ajax")){i(v,w.id)}}};var i=function(y,z,v,w){if(typeof q.beforeExpand=="function"){q.beforeExpand(y)}t.trigger("beforeExpand",y);var x=b.trim(b("a",y).attr("href"));if(x){b.ajax({type:"GET",url:x,data:{apikey:q.apikey,include:"prefLabel,childrenCount",no_context:true},crossDomain:true,contentType:"json",timeout:q.timeout,success:function(B){var A=h(B.collection);y.removeAttr("class");y.html(A);b.extend(y,{url:x});g(y,true);if(typeof q.afterExpand=="function"){q.afterExpand(y)}t.trigger("afterExpand",y);if(typeof v=="function"){v(y)}},error:function(A){if(typeof q.afterExpandError=="function"){q.afterExpandError(y)}if(typeof w=="function"){w(y)}t.trigger("afterExpandError",y)}})}};var g=function(w,v){w=v?w.parent():w;b("li>a",w).addClass("text").bind("selectstart",function(){return false}).click(function(){var x=b(this).parent();var y=b(this);b(".active",r).attr("class","text");if(this.className=="text"){this.className="active"}if(typeof q.afterSelect=="function"){q.afterSelect(decodeURIComponent(y.data("id")),y.text(),y)}t.trigger("afterSelect",[decodeURIComponent(y.data("id")),y.text(),y]);return false}).bind("contextmenu",function(){b(".active",r).attr("class","text");if(this.className=="text"){this.className="active"}if(typeof q.afterContextMenu=="function"){q.afterContextMenu(parent)}return false}).mousedown(function(y){j=true;cloneNode=b(this).parent().clone();var x=b(this).parent();return false});b("li",w).each(function(B){var C=this.className;var z=false;var y=false;var x=this;var A=b(">ul",this);if(A.size()>0){var D="folder-";if(C&&C.indexOf("open")>=0){D=D+"open";z=true}else{D=D+"close"}this.className=D+(b(this).is(":last-child")?"-last":"");if(!z||C.indexOf("ajax")>=0){A.hide()}f(this)}else{var D="doc";this.className=D+(b(this).is(":last-child")?"-last":"")}}).before('<li class="line">&nbsp;</li>').filter(":last-child").after('<li class="line-last"></li>')};var f=function(w){b(">a",w).before('<img class="trigger" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" border=0>');var v=b(">.trigger",w);v.click(function(x){k(w)})};var m=function(v){return v.replace("http:",("https:"==document.location.protocol?"https:":"http:"))};var s=function(){var w=b("<div>").addClass(q.autocompleteClass).addClass("ncboTree");var v=b("<input>").addClass(q.autocompleteClass).css("width",q.width).attr("placeholder","Search for class...");w.append(v);v.NCBOAutocomplete({url:q.ncboAPIURL+"/search",searchParameter:"q",resultAttribute:"collection",property:"prefLabel",searchTextSuffix:"*",searchFromRoot:l,onSelect:function(y,x){o.jumpToClass(y["@id"]);x.val("")},minCharacters:3,additionalParameters:{apikey:q.apikey,no_context:true,ontologies:q.ontology}});t.append(w)};this.init=function(){t=e;r=b("<ul>").append(b("<li>").addClass("root"));c=b(".root",r);r.css("width",q.width).addClass(q.treeClass);t.html("");l=(q.startingRoot==q.defaultRoot)?null:q.startingRoot;s();t.append(r);if(q.startingClass!==null){o.jumpToClass(q.startingClass);q.startingClass=null}else{c.html(b("<span>").html("Loading...").css("font-size","smaller"));b.ajax({url:m(q.ncboAPIURL)+"/ontologies/"+q.ontology+"/classes/"+encodeURIComponent(q.startingRoot),data:{apikey:q.apikey,include:"prefLabel,childrenCount",no_context:true},contentType:"json",crossDomain:true,success:function(v){v=b.map([v],function(w){return w});c.html(h(v));g(c,false)}})}}};b.fn.NCBOTree=function(c){return this.each(function(){var e=b(this);if(e.data("NCBOTree")){return}var d=new a(e,c);this.NCBOTree=d;b.ajax({url:d.options().ncboUIURL.replace("http:",("https:"==document.location.protocol?"https:":"http:"))+"/widgets/jquery.ncbo.autocomplete.js",type:"GET",crossDomain:true,dataType:"script",success:function(){d.init();e.data("NCBOTree",d)}})})}}(jQuery));