ncbo/bioportal_web_ui

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

Summary

Maintainability
Test Coverage
"use strict";

// The count returned may not match the actual number of mappings
// To get around this, we re-calculate based on the mapping table size

function updateMappingCount() {
  var rows = jQuery("#concept_mappings_table tbody tr"), mappings_count = null;
  rows.first().children("td").each(function() {
    if (this.innerHTML.indexOf("no mappings") > -1) {
      mappings_count = 0;
    }
  });
  if (mappings_count === null) {
    mappings_count = rows.length;
  }
  jQuery("#mapping_count").html(mappings_count);
}

// Also in bp_create_mappings.js
function updateMappingDeletePermissions() {
  var mapping_permission_checkbox = jQuery("#delete_mappings_permission");
  if (mapping_permission_checkbox.length === 0){
    //console.error("Failed to select #delete_mappings_permission");
    jQuery("#delete_mappings_button").hide();
    jQuery(".delete_mappings_column").hide();
    jQuery("input[name='delete_mapping_checkbox']").prop('disabled', true);
  } else {
    // Ensure the permission checkbox is hidden.
    mapping_permission_checkbox.hide();
    if (mapping_permission_checkbox.is(':checked')) {
      jQuery("#delete_mappings_button").show();
      jQuery(".delete_mappings_column").show();
      jQuery("input[name='delete_mapping_checkbox']").prop('disabled', false);
    } else {
      jQuery("#delete_mappings_button").hide();
      jQuery(".delete_mappings_column").hide();
      jQuery("input[name='delete_mapping_checkbox']").prop('disabled', true);
    }
  }
  jQuery("input[name='delete_mapping_checkbox']").prop('checked', false);
}

function loadMappings(value) {
    jQuery('#mappingCount').html("");
    jQuery('#mapping_load').show();

    jQuery('#mappingCount').load('/mappings/' + value + '/count', function () {
        jQuery('#mapping_load').hide();
        let mappings_table_elem = jQuery("#mapping_count_table");
        mappings_table_elem.effect("highlight", {color: "#F5F5F5"}, 500);
        mappings_table_elem.dataTable({
            "paging": false,
            "info": false,
            "searching": false,
            "columns": [
                {
                    "className": "text-left"
                },
                {
                    "type": "num-fmt",
                    "orderSequence": ["desc", "asc"],
                    "className": "text-right"
                }
            ]
        });
        jQuery(".mappings.index a.facebox").facebox();
    });
}

jQuery(document).ready(function(){
  updateMappingCount();
  updateMappingDeletePermissions();

  jQuery("#search_ontologies").chosen({search_contains: true, width:"100%"});

  // Handle visualization changes
  jQuery("#display_options input").on("click", function(e, button){
    var selectedOnt = jQuery("#search_ontologies").val();
    if (selectedOnt !== "") {
      loadMappings(selectedOnt);
    }
  });

  jQuery("#mappings-help").on("click", bpPopWindow);
});

jQuery(document).bind("tree_changed", updateMappingCount);

// deleteMappings() is a callback that is called by "#delete_mappings_button" created in
// /app/views/mappings/_concept_mappings.html.haml
// The appearance of that button is controlled by updateMappingDeletePermissions(), which
// relies on @delete_mapping_permission in /app/views/mappings/_mapping_table.html.haml; which,
// in turn, is set by /app/controllers/application_controller.check_delete_mapping_permission()
function deleteMappings() {
  var mappingsToDelete = [], params;
  var ontologyId = jQuery(document).data().bp.ont_viewer.ontology_id;
  var conceptId = jQuery(document).data().bp.ont_viewer.concept_id;

  jQuery("#delete_mappings_error").html("");
  jQuery("#delete_mappings_spinner").show();

  jQuery("input[name='delete_mapping_checkbox']:checked").each(function(){
    mappingsToDelete.push(jQuery(this).val());
  });

  params = {
    mappingids: mappingsToDelete.join(","),
    _method: "delete",
    ontologyid: ontologyId,
    conceptid: conceptId
  };

  jQuery("#delete_mappings_error").html("");

  jQuery.ajax({
    url: "/mappings/mappingids", // routed to mappings_controller::destroy
    type: "POST",
    data: params,

    success: function(data){
      jQuery("#delete_mappings_spinner").hide();

      var i, rowId, len = data.success.length;
      for (i=0; i<len; ++i) {
        rowId = data.success[i].replace(/.*\//, "");
        jQuery("#" + rowId).hide();
      }

      len = data.error.length;
      for (i=0; i<len; ++i) {
        jQuery("#delete_mappings_error").html("There was a problem deleting, please try again");
        rowId = data.error[i].replace(/.*\//, "");
        jQuery("#" + rowId).css("border", "red solid");
      }

      jQuery("#mapping_count").html(jQuery("#mapping_details tbody tr:visible").size());
      jQuery.bioportal.ont_pages["mappings"].retrieve_and_publish();
      updateMappingDeletePermissions();
    },

    error: function(){
      jQuery("#delete_mappings_spinner").hide();
      jQuery("#delete_mappings_error").html("There was a problem deleting, please try again");
    }
  });
}