JAVASCRIPT 22
Import.js Guest on 4th May 2021 10:49:36 AM
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3.  * Functions used in the import tab
  4.  *
  5.  */
  6.  
  7.  
  8. /**
  9.  * Toggles the hiding and showing of each plugin's options
  10.  * according to the currently selected plugin from the dropdown list
  11.  */
  12. function changePluginOpts()
  13. {
  14.     $("#format_specific_opts").find("div.format_specific_options").each(function () {
  15.         $(this).hide();
  16.     });
  17.     var selected_plugin_name = $("#plugins").find("option:selected").val();
  18.     $("#" + selected_plugin_name + "_options").fadeIn('slow');
  19.     if (selected_plugin_name == "csv") {
  20.         $("#import_notification").text(PMA_messages.strImportCSV);
  21.     } else {
  22.         $("#import_notification").text("");
  23.     }
  24. }
  25.  
  26. /**
  27.  * Toggles the hiding and showing of each plugin's options and sets the selected value
  28.  * in the plugin dropdown list according to the format of the selected file
  29.  */
  30. function matchFile(fname)
  31. {
  32.     var fname_array = fname.toLowerCase().split(".");
  33.     var len = fname_array.length;
  34.     if (len !== 0) {
  35.         var extension = fname_array[len - 1];
  36.         if (extension == "gz" || extension == "bz2" || extension == "zip") {
  37.             len--;
  38.         }
  39.         // Only toggle if the format of the file can be imported
  40.         if ($("select[name='format'] option").filterByValue(fname_array[len - 1]).length == 1) {
  41.             $("select[name='format'] option").filterByValue(fname_array[len - 1]).prop('selected', true);
  42.             changePluginOpts();
  43.         }
  44.     }
  45. }
  46.  
  47. /**
  48.  * Unbind all event handlers before tearing down a page
  49.  */
  50. AJAX.registerTeardown('import.js', function () {
  51.     $("#plugins").unbind('change');
  52.     $("#input_import_file").unbind('change');
  53.     $("#select_local_import_file").unbind('change');
  54.     $("#input_import_file").unbind('change').unbind('focus');
  55.     $("#select_local_import_file").unbind('focus');
  56.     $("#text_csv_enclosed").add("#text_csv_escaped").unbind('keyup');
  57. });
  58.  
  59. AJAX.registerOnload('import.js', function () {
  60.     // import_file_form validation.
  61.     $(document).on('submit', '#import_file_form', function (event) {
  62.         var radioLocalImport = $("#radio_local_import_file");
  63.         var radioImport = $("#radio_import_file");
  64.         var fileMsg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ' + PMA_messages.strImportDialogMessage + '</div>';
  65.  
  66.         if (radioLocalImport.length !== 0) {
  67.             // remote upload.
  68.  
  69.             if (radioImport.is(":checked") && $("#input_import_file").val() === '') {
  70.                 $("#input_import_file").focus();
  71.                 PMA_ajaxShowMessage(fileMsg, false);
  72.                 return false;
  73.             }
  74.  
  75.             if (radioLocalImport.is(":checked")) {
  76.                 if ($("#select_local_import_file").length === 0) {
  77.                     PMA_ajaxShowMessage('<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ' + PMA_messages.strNoImportFile + ' </div>', false);
  78.                     return false;
  79.                 }
  80.  
  81.                 if ($("#select_local_import_file").val() === '') {
  82.                     $("#select_local_import_file").focus();
  83.                     PMA_ajaxShowMessage(fileMsg, false);
  84.                     return false;
  85.                 }
  86.             }
  87.         } else {
  88.             // local upload.
  89.             if ($("#input_import_file").val() === '') {
  90.                 $("#input_import_file").focus();
  91.                 PMA_ajaxShowMessage(fileMsg, false);
  92.                 return false;
  93.             }
  94.         }
  95.  
  96.         // show progress bar.
  97.         $("#upload_form_status").css("display", "inline");
  98.         $("#upload_form_status_info").css("display", "inline");
  99.     });
  100.  
  101.     // Initially display the options for the selected plugin
  102.     changePluginOpts();
  103.  
  104.    // Whenever the selected plugin changes, change the options displayed
  105.     $("#plugins").change(function () {
  106.         changePluginOpts();
  107.     });
  108.  
  109.     $("#input_import_file").change(function () {
  110.         matchFile($(this).val());
  111.     });
  112.  
  113.     $("#select_local_import_file").change(function () {
  114.         matchFile($(this).val());
  115.     });
  116.  
  117.     /*
  118.      * When the "Browse the server" form is clicked or the "Select from the web server upload directory"
  119.      * form is clicked, the radio button beside it becomes selected and the other form becomes disabled.
  120.      */
  121.     $("#input_import_file").bind("focus change", function () {
  122.         $("#radio_import_file").prop('checked', true);
  123.         $("#radio_local_import_file").prop('checked', false);
  124.     });
  125.     $("#select_local_import_file").focus(function () {
  126.         $("#radio_local_import_file").prop('checked', true);
  127.         $("#radio_import_file").prop('checked', false);
  128.     });
  129.  
  130.     /**
  131.      * Set up the interface for Javascript-enabled browsers since the default is for
  132.      *  Javascript-disabled browsers
  133.      */
  134.     $("#scroll_to_options_msg").hide();
  135.     $("#format_specific_opts").find("div.format_specific_options")
  136.     .css({
  137.         "border": 0,
  138.         "margin": 0,
  139.         "padding": 0
  140.     })
  141.     .find("h3")
  142.     .remove();
  143.     //$("form[name=import] *").unwrap();
  144.  
  145.     /**
  146.      * for input element text_csv_enclosed and text_csv_escaped allow just one character to enter.
  147.      * as mysql allows just one character for these fields,
  148.      * if first character is escape then allow two including escape character.
  149.      */
  150.     $("#text_csv_enclosed").add("#text_csv_escaped").bind('keyup', function() {
  151.         if($(this).val().length === 2 && $(this).val().charAt(0) !== "\\") {
  152.             $(this).val($(this).val().substring(0, 1));
  153.             return false;
  154.         }
  155.         return true;
  156.     });
  157.  
  158. });

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.