b4lduin

Admin JS

Jun 7th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getSaveFields()
  2. {
  3.     let $process_elements   = jQuery('#admin_content input, #admin_content select');
  4.     let response            = {};
  5.  
  6.     $process_elements.each(function(index, element)
  7.     {
  8.         if(element.name == '')
  9.         {
  10.             return true;
  11.         }
  12.  
  13.         let $element        = jQuery(element);
  14.         let element_val     = jQuery.trim($element.val());
  15.  
  16.         if(element_val != '')
  17.         {
  18.             if(((element.type == 'checkbox' || element.type == 'radio') && $element.is(':checked')) || (element.type != 'checkbox' && element.type != 'radio') || element.nodeName == 'SELECT')
  19.             {
  20.                 if(element.name.indexOf('[') != -1)
  21.                 {
  22.                     let names = element.name.split('[');
  23.  
  24.                     //a main position exists alywas so we can directly assign it
  25.                     let main_position = names.shift();
  26.  
  27.                     //a sub position exists too but we need to check if it has a name or not
  28.                     let sub_position =  names.shift().replace(']', '').toString();
  29.  
  30.                     //depending on the sub_position string we need to create an object or an array if the main is not set already
  31.                     if(typeof response[main_position] == 'undefined')
  32.                     {
  33.                         if(sub_position != '')
  34.                         {
  35.                             response[main_position] = {};
  36.                         }
  37.                         else
  38.                         {
  39.                             response[main_position] = [];
  40.                         }
  41.                     }
  42.  
  43.                     if(sub_position == '')
  44.                     {
  45.                         response[main_position].push(element_val);
  46.                     }
  47.                     else
  48.                     {
  49.                         let sub_sub_position = undefined;
  50.  
  51.                         if(names.length > 0)
  52.                         {
  53.                             sub_sub_position = names.shift().replace(']', '').toString();
  54.                         }
  55.  
  56.                         if(typeof sub_sub_position == 'undefined')
  57.                         {
  58.                             response[main_position][sub_position] = element_val;
  59.                         }
  60.                         else
  61.                         {
  62.                             //only create the object/array if it is not defined
  63.                             if(typeof response[main_position][sub_position] == 'undefined')
  64.                             {
  65.                                 if(sub_sub_position != '')
  66.                                 {
  67.                                     response[main_position][sub_position] = {};
  68.                                 }
  69.                                 else
  70.                                 {
  71.                                     response[main_position][sub_position] = [];
  72.                                 }
  73.                             }
  74.  
  75.                             if(sub_sub_position == '')
  76.                             {
  77.                                 response[main_position][sub_position].push(element_val);
  78.                             }
  79.                             else
  80.                             {
  81.                                 response[main_position][sub_position][sub_sub_position] = element_val;
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     response[element.name] = element_val;
  89.                 }
  90.             }
  91.         }
  92.     });
  93.  
  94.     return response;
  95. }
Add Comment
Please, Sign In to add comment