ncbo/bioportal_web_ui

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

Summary

Maintainability
Test Coverage
// Show/hide location inputs
function location_toggle(input_div) {
  jQuery('.hidden_field').hide();
  jQuery('.hidden_field input').attr("disabled", true);
  jQuery('#' + input_div + " input").removeAttr("disabled");
  jQuery('#' + input_div).show();
}

jQuery(document).ready(function(){
  jQuery("#datepicker").datepicker({
    dateFormat: "M d, yy",
  });
  jQuery("#ui-datepicker-div").css("display", "none");

  jQuery('.fa-calendar-alt').on('click', function() {
    jQuery('#datepicker').datepicker('show');
  });

  // Properly display the information for 'File Location' based on radio button
  if (jQuery("#submission_isRemote_0").is(":checked")) {
    location_toggle("upload");
  }
  
  if (jQuery("#submission_isRemote_1").is(":checked")) {
    location_toggle("remote");
  }
  
  // Select default 'File Location' radio button
  if (!jQuery("#submission_isRemote_0").is(":checked") && !jQuery("#submission_isRemote_1").is(":checked") && !jQuery("#submission_isRemote_2").is(":checked")) {
    jQuery("#submission_isRemote_0").attr("checked", "checked")
  }

  jQuery("#submission_hasOntologyLanguage").change(function() {
    toggleOntologyFormatOptions(jQuery(this).val());
  });
  jQuery("#submission_hasOntologyLanguage").trigger("change");

  jQuery("#collapseOWLOptions").click(function() {
    var button = jQuery(this);
    button.text(button.text() == "Show advanced OWL options" ? "Hide advanced OWL options" : "Show advanced OWL options");
  })

  jQuery("#contacts").on("click", ".add-contact", addContact);
  jQuery("#contacts").on("click", ".remove-contact", removeContact);

  jQuery("#submission_naturalLanguage").select2({
    dropdownParent: jQuery(".submissions form"),
    include_hidden: false,
  });

  jQuery("#ontology_submission_form").validate({
    highlight: function(element) {
      $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function(element) {
      $(element).closest('.form-group').removeClass('has-error');
    },
    errorElement: 'span',
    errorClass: 'help-block error-help-block',
    errorPlacement: function(error, element) {
      if(element.parent('.input-group').length) {
        error.insertAfter(element.parent());
      } else {
        error.insertAfter(element);
      }
    }  
  });
});

function addContact(event) {
  event.preventDefault();

  var contacts = document.querySelectorAll("div.contact");
  var newContact = contacts[0].cloneNode(true);
  newContact.classList.add("offset-sm-2");

  var removeButton = newContact.querySelector("button").cloneNode(true);
  removeButton.classList.replace("btn-success", "btn-danger");
  removeButton.classList.replace("add-contact", "remove-contact");
  removeButton.classList.add("ms-1")
  removeButton.querySelector("i").classList.replace("fa-plus", "fa-minus");
  newContact.appendChild(removeButton);
  
  var index = contacts.length;
  var inputs = newContact.getElementsByTagName("input");
  for (var i = 0; i < inputs.length; i++) {
    var input = inputs[i];

    var id = input.getAttribute("id").replace(/0/g, index);
    input.setAttribute("id", id);

    var name = input.getAttribute("name").replace(/0/g, index);
    input.setAttribute("name", name);
    
    input.setAttribute("value", "");

    input.removeAttribute("required");
  }

  contacts[index - 1].insertAdjacentElement('afterend', newContact);
}

function removeContact(event) {
  event.preventDefault();

  var target = event.target;
  var contact;
  if (target.matches("button.remove-contact")) {
    contact = target.parentNode;
  } else if (target.matches("i.fa-minus")) {
    contact = target.parentNode.parentNode;
  }

  document.querySelector("#contacts").removeChild(contact);
}

function toggleOntologyFormatOptions(selectedFormat) {
  var advancedOWLOptionsButton = jQuery("#collapseOWLOptions");
  var skosHelpText = jQuery("#helpSkosFormat");
  
  switch(selectedFormat){
    case 'OWL':
      advancedOWLOptionsButton.show();
      skosHelpText.hide();
      break;
    case 'SKOS':
      advancedOWLOptionsButton.hide();
      skosHelpText.show();
      break;
    default:
      advancedOWLOptionsButton.hide();
      skosHelpText.hide();
  }
}