Skip to content
Snippets Groups Projects
csentry.js 1.76 KiB
Newer Older
function flash_alert(message, category) {
  var htmlString = '<div class="alert alert-' + category + ' alert-dismissible" role="alert">'
  htmlString += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">'
  htmlString += '<span aria-hidden="true">&times;</span></button>' + message + '</div>'
  $(htmlString).prependTo("#mainContent").hide().slideDown();
}

function remove_alerts() {
  $(".alert").slideUp("normal", function() {
    $(this).remove();
  });
}

// Function to dynamically update a select field
function update_selectfield(field_id, data, selected_value) {
  var $field = $(field_id);
  $field.empty();
  $.map(data, function(option, index) {
    if( option == "None" ) {
      var text = "";
    } else {
      var text = option;
    }
    $field.append($("<option></option>").attr("value", option).text(text));
  });
  $field.val(selected_value);
}

// Function to populate dynamically the stack_member field
// in the create and edit item forms
function update_stack_member() {
  // Retrieve free stack members
  var host_id = $("#host_id").val();
  $.getJSON(
    $SCRIPT_ROOT + "/inventory/_retrieve_free_stack_members/" + host_id,
    {
      ics_id: $("#ics_id").val()
    },
    function(json) {
      update_selectfield("#stack_member", json.data.stack_members, json.data.selected_member);
      $("#stack_member").prop("disabled", json.data.disabled);
    }
  );
}

$(document).ready(function() {

  // When an invalid input was submitted, the server
  // adds the "is-invalid" class to form fields to display
  // them in red with an error message
  // When starting to type again, we want to remove the error
  // message to not confuse the user
  $('input[type="text"]').keyup(function(event) {
    $(this).removeClass("is-invalid");
  });

});