Newer
Older
dmpopidor / app / assets / javascripts / views / plans / share.js
$(document).ready(function(){
  /*----------------
    Listener for changes in access-level for a plan shared with a user
    TODO partial update instead of forcing a page reload
  ------------------*/
  $(".toggle-existing-user-access").change(function(){
    var params = {role: {access_level: $(this).find("option:checked").val()}};
    asyncRequest({
      url: "/roles/" + $(this).closest("form").find("#role_id").val(), 
      type: 'PUT', 
      data: JSON.stringify(params)
    });
  });
  
  // Run the input validations when the focus changes
  $("#role_user_email").on('blur', function(){
    toggleFormElementError(this, validateEmail($(this).val().trim()));
  });
  
  // See if we should enable the add collaborator button
  $("#role_user_email").on('change keyup', function(){
    toggleAddCollaboratorSubmit();
  });
  $("input[name='role[access_level]']").on('click', function(){
    toggleAddCollaboratorSubmit();
  });
  
  $("input[name='plan[visibility]']").on('click, change', function(e){
    var params = {plan: {visibility: $("input[name='plan[visibility]']:checked").val()}};
    asyncRequest({
      url: "/plans/" + $("#plan_id").val() + "/visibility", 
      type: 'POST', 
      data: JSON.stringify(params)
    });
  });
  
  // Display the submit button only if there is a valid email and password
  function toggleAddCollaboratorSubmit(){
    var disabled = (validateEmail($("#role_user_email").val()) != '' || 
                    $("input[name='role[access_level]']:checked").val() == undefined);
    $("#add-collaborator-button").attr('aria-disabled', disabled);
  }
});