ncbo/bioportal_web_ui

View on GitHub
app/assets/javascripts/bp_visualize.js.erb

Summary

Maintainability
Test Coverage
(function(window,undefined) {
  // Abort it not right page
  var path = currentPathArray();
  if (path[0] !== "ontologies" || (path[0] === "ontologies" && path.length !== 2)) {
    return;
  }

  jQuery(document).data().bp.classesTab = {};

  function jumpToValue(li){
    jQuery.blockUI({ message: '<h1><img src="<%= asset_path("jquery.simple.tree/spinner.gif") %>" /> Loading Class...</h1>', showOverlay: false });

    if( !!li.extra ){
      var sValue = jQuery("#jump_to_concept_id").val();

      // TODO_REV: Handle flat ontologies (replace `if (false)` with `if @ontology.flat?`)
      if (false) {
        History.pushState({p:"classes", conceptid:sValue, suid:"jump_to", flat:true, label:li.extra[4]}, jQuery.bioportal.ont_pages["classes"].page_name + " | " + org_site, "?p=classes&conceptid=" + sValue);
      } else {
        document.location="/ontologies/"+jQuery(document).data().bp.ontology.acronym+"/?p=classes&conceptid="+encodeURIComponent(sValue)+"&jump_to_nav=true";
        jQuery.blockUI({ message: '<h1><img src="<%= asset_path("jquery.simple.tree/spinner.gif") %>" /> Loading Class...</h1>', showOverlay: false });
        return;
      }
    }
  }

  // Sets a hidden form value that records the concept id when a concept is chosen in the jump to
  // This is a workaround because the default autocomplete search method cannot distinguish between two
  // concepts that have the same preferred name but different ids.
  function jumpToSelect(li){
    jQuery("#jump_to_concept_id").val(li.extra[0]);
    jumpToValue(li);
  }

  // Formats the Jump To search results
  function formatItem(row) {
    var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
    var keywords = jQuery("#search_box").val().trim().replace(specials, "\\$&").split(' ').join('|');
    var regex = new RegExp( '(' + keywords + ')', 'gi' );
    var matchType = "";
    if (typeof row[2] !== "undefined" && row[2] !== "") {
      matchType = " <span style='font-size:9px;color:blue;'>(" + row[2] + ")</span>";
    }

    if (row[0].match(regex) == null) {
      var contents = row[6].split("\t");
      var synonym = contents[0] || "";
      synonym = synonym.split(";");
      if (synonym !== "") {
        var matchSynonym = jQuery.grep(synonym, function(e){
          return e.match(regex) != null;
        });
        row[0] = row[0] + " (synonym: " + matchSynonym.join(" ") + ")";
      }
    }
    // Cleanup obsolete class tag before markup for search keywords.
    if(row[0].indexOf("[obsolete]") != -1) {
      row[0] = row[0].replace("[obsolete]", "");
      obsolete_prefix = "<span class='obsolete_class' title='obsolete class'>";
      obsolete_suffix = "</span>";
    } else {
      obsolete_prefix = "";
      obsolete_suffix = "";
    }
    // Markup the search keywords.
    var row0_markup = row[0].replace(regex, "<b><span style='color:#006600;'>$1</span></b>");
    return obsolete_prefix + row0_markup + matchType + obsolete_suffix;
  }

  // The tab system
  jQuery(".tab").live("click", function(){
    var tabId = jQuery(this).children("a:first").attr("href").replace("#", "");
    showClassesTab(tabId);
  });

  function showClassesTab(tabId) {
    // Get the target content area
    var target = document.getElementById(tabId + "_content");

    if (target != null) {
      jQuery(".tab_container").addClass("not_visible");
      jQuery(target).removeClass("not_visible");
      jQuery(".tab").removeClass("selected");
      jQuery("#" + tabId + "_top").addClass("selected");
      jQuery(document).trigger("classes_tab_visible");
    }

    jQuery(document).trigger("visualize_tab_change", [{tabType: tabId}]);
  }

  // Only show BioMixer when tab is clicked
  jQuery(document).live("visualize_tab_change", function(event, data){
    if (data.tabType == "visualization") {
      jQuery("#biomixer_iframe").attr("src", jQuery("#biomixer_iframe").data("src"));
    }
  });

  jQuery(document).data().bp.classesTab.search_box_init = function(){
    if (jQuery("#search_box").bioportal_autocomplete) {
      jQuery("#search_box").bioportal_autocomplete("/search/json_search/"+jQuery(document).data().bp.ontology.acronym, {
        extraParams: { objecttypes: "class" },
        selectFirst: true,
        lineSeparator: "~!~",
        matchSubset: 0,
        minChars: 1,
        maxItemsToShow: 25,
        onFindValue: jumpToValue,
        onItemSelect: jumpToSelect,
        formatItem: formatItem
      });
    }
  }

  jQuery(document).ready(function(){
    let searchParams = new URLSearchParams(window.location.search);
    if (searchParams.has('t')) {
      showClassesTab(searchParams.get('t'));
    }

    jQuery("#classPermalinkModal").on("shown.bs.modal", function (e) {
      var currentPurl = jQuery("a.class-permalink").data("current-purl");
      jQuery("#purl_input").val(currentPurl);
    })

    jQuery("#purl_input").on("click", function () {
      jQuery(this).select();
    });
  });
})(window);