mrnavaone

Javascript

May 22nd, 2020
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. var inputs = [
  3.   'id',
  4.   'name',
  5.   'nota1',
  6.   'nota2',
  7.   'nota3'  
  8. ];
  9.  
  10.    
  11. $(function(){
  12.   var pageApp = new formApp();
  13.   $('#submitButton').on('click', pageApp.submitForm);
  14.   $('#clearFormButton').on('click', pageApp.clearForm);
  15. });
  16.  
  17. var formApp = function(){
  18.   var self = this;
  19.  
  20.   //Clears form input fields, removes message, enables submit
  21.   self.clearForm = function(){
  22.     for(var i = 0; i < inputs.length; i++){
  23.        $('#'+inputs[i]).val('');
  24.    }
  25.    toggleSubmitButton(false);
  26.    setErrorMessage(false);
  27.    setSuccessMessage(false);
  28.  }
  29.  
  30.  //Submits the form to apps script
  31.  self.submitForm = function(){
  32.    toggleSubmitButton(true);
  33.    setSuccessMessage(false);
  34.    setErrorMessage(false);
  35.    
  36.    google.script.run
  37.        .withSuccessHandler(self.sucessfullySubmitted)
  38.        .withFailureHandler(self.failedToSubmit)
  39.        .formSubmit(self.getFormData());    
  40.  };
  41.  
  42.  //Retrieves the form data absed on the input fields
  43.  self.getFormData = function(){
  44.    var output = {};
  45.    for(var i = 0; i < inputs.length; i++){
  46.        output[inputs[i]] = $('#'+inputs[i]).val();
  47.    }
  48.    console.log(output)
  49.    return output;  
  50.  }
  51.  
  52.  //When the apps script sucessfully returns
  53.  self.sucessfullySubmitted = function(value){
  54.    if(value.success){
  55.      setSuccessMessage(true, value.message);
  56.    } else {
  57.      setErrorMessage(true, value.message);
  58.      toggleSubmitButton(false);
  59.    }
  60.  }
  61.  
  62.  //When the apps script threw an error
  63.  self.failedToSubmit = function(value){
  64.    toggleSubmitButton(false);
  65.    setErrorMessage(true, value.message);
  66.  }
  67. }
  68.  
  69. //Disables/enables the submit button
  70. function toggleSubmitButton(disabled){
  71.  $('#submitButton').prop('disabled', disabled);
  72. }
  73.  
  74. //Sets the general message box's message and enables or disabled the error box
  75. function setSuccessMessage(show, message){
  76.  if(show){
  77.    $('.success.message').removeClass('hidden');
  78.    $('.success.message .message').text(message);
  79.  } else {
  80.    $('.success.message').addClass('hidden');
  81.    $('.success.message .message').text('');
  82.  }
  83. }
  84. //Sets the error message box's message and enables or disabled the error box
  85. function setErrorMessage(show, message){
  86.  if(show){
  87.    $('.error.message').removeClass('hidden');
  88.    $('.error.message .message').text(message);
  89.  } else {
  90.    $('.error.message').addClass('hidden');
  91.    $('.error.message .message').text('');
  92.  }
  93. }
  94.  
  95. function getFormData(){
  96.  var output = {};
  97.  for(var i = 0; i < inputs.length; i++){
  98.      output[inputs[i]] = $('#'+inputs[i]).val();
  99.  }
  100.  return output;
  101. }
  102. </script>
RAW Paste Data