import.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Functions used in the import tab
  3. *
  4. */
  5. /**
  6. * Toggles the hiding and showing of each plugin's options
  7. * according to the currently selected plugin from the dropdown list
  8. */
  9. function changePluginOpts() {
  10. $('#format_specific_opts').find('div.format_specific_options').each(function () {
  11. $(this).hide();
  12. });
  13. var selectedPluginName = $('#plugins').find('option:selected').val();
  14. $('#' + selectedPluginName + '_options').fadeIn('slow');
  15. const importNotification = document.getElementById('import_notification');
  16. if (importNotification) {
  17. importNotification.innerText = '';
  18. if (selectedPluginName === 'csv') {
  19. importNotification.innerHTML = '<div class="alert alert-info mb-0 mt-3" role="alert">' + Messages.strImportCSV + '</div>';
  20. }
  21. }
  22. }
  23. /**
  24. * Toggles the hiding and showing of each plugin's options and sets the selected value
  25. * in the plugin dropdown list according to the format of the selected file
  26. *
  27. * @param {string} fname
  28. */
  29. function matchFile(fname) {
  30. var fnameArray = fname.toLowerCase().split('.');
  31. var len = fnameArray.length;
  32. if (len !== 0) {
  33. var extension = fnameArray[len - 1];
  34. if (extension === 'gz' || extension === 'bz2' || extension === 'zip') {
  35. len--;
  36. }
  37. // Only toggle if the format of the file can be imported
  38. if ($('select[name=\'format\'] option').filterByValue(fnameArray[len - 1]).length === 1) {
  39. $('select[name=\'format\'] option').filterByValue(fnameArray[len - 1]).prop('selected', true);
  40. changePluginOpts();
  41. }
  42. }
  43. }
  44. /**
  45. * Unbind all event handlers before tearing down a page
  46. */
  47. AJAX.registerTeardown('import.js', function () {
  48. $('#plugins').off('change');
  49. $('#input_import_file').off('change');
  50. $('#select_local_import_file').off('change');
  51. $('#input_import_file').off('change').off('focus');
  52. $('#select_local_import_file').off('focus');
  53. $('#text_csv_enclosed').add('#text_csv_escaped').off('keyup');
  54. });
  55. AJAX.registerOnload('import.js', function () {
  56. // import_file_form validation.
  57. $(document).on('submit', '#import_file_form', function () {
  58. var radioLocalImport = $('#localFileTab');
  59. var radioImport = $('#uploadFileTab');
  60. var fileMsg = '<div class="alert alert-danger" role="alert"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> ' + Messages.strImportDialogMessage + '</div>';
  61. var wrongTblNameMsg = '<div class="alert alert-danger" role="alert"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error">' + Messages.strTableNameDialogMessage + '</div>';
  62. var wrongDBNameMsg = '<div class="alert alert-danger" role="alert"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error">' + Messages.strDBNameDialogMessage + '</div>';
  63. if (radioLocalImport.length !== 0) {
  64. // remote upload.
  65. if (radioImport.hasClass('active') && $('#input_import_file').val() === '') {
  66. $('#input_import_file').trigger('focus');
  67. Functions.ajaxShowMessage(fileMsg, false);
  68. return false;
  69. }
  70. if (radioLocalImport.hasClass('active')) {
  71. if ($('#select_local_import_file').length === 0) {
  72. Functions.ajaxShowMessage('<div class="alert alert-danger" role="alert"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> ' + Messages.strNoImportFile + ' </div>', false);
  73. return false;
  74. }
  75. if ($('#select_local_import_file').val() === '') {
  76. $('#select_local_import_file').trigger('focus');
  77. Functions.ajaxShowMessage(fileMsg, false);
  78. return false;
  79. }
  80. }
  81. } else {
  82. // local upload.
  83. if ($('#input_import_file').val() === '') {
  84. $('#input_import_file').trigger('focus');
  85. Functions.ajaxShowMessage(fileMsg, false);
  86. return false;
  87. }
  88. if ($('#text_csv_new_tbl_name').length > 0) {
  89. var newTblName = $('#text_csv_new_tbl_name').val();
  90. if (newTblName.length > 0 && newTblName.trim().length === 0) {
  91. Functions.ajaxShowMessage(wrongTblNameMsg, false);
  92. return false;
  93. }
  94. }
  95. if ($('#text_csv_new_db_name').length > 0) {
  96. var newDBName = $('#text_csv_new_db_name').val();
  97. if (newDBName.length > 0 && newDBName.trim().length === 0) {
  98. Functions.ajaxShowMessage(wrongDBNameMsg, false);
  99. return false;
  100. }
  101. }
  102. }
  103. // show progress bar.
  104. $('#upload_form_status').css('display', 'inline');
  105. $('#upload_form_status_info').css('display', 'inline');
  106. });
  107. // Initially display the options for the selected plugin
  108. changePluginOpts();
  109. // Whenever the selected plugin changes, change the options displayed
  110. $('#plugins').on('change', function () {
  111. changePluginOpts();
  112. });
  113. $('#input_import_file').on('change', function () {
  114. matchFile($(this).val());
  115. });
  116. $('#select_local_import_file').on('change', function () {
  117. matchFile($(this).val());
  118. });
  119. /**
  120. * Set up the interface for Javascript-enabled browsers since the default is for
  121. * Javascript-disabled browsers
  122. */
  123. $('#format_specific_opts').find('div.format_specific_options').find('h3').remove();
  124. // $("form[name=import] *").unwrap();
  125. /**
  126. * for input element text_csv_enclosed and text_csv_escaped allow just one character to enter.
  127. * as mysql allows just one character for these fields,
  128. * if first character is escape then allow two including escape character.
  129. */
  130. $('#text_csv_enclosed').add('#text_csv_escaped').on('keyup', function () {
  131. if ($(this).val().length === 2 && $(this).val().charAt(0) !== '\\') {
  132. $(this).val($(this).val().substring(0, 1));
  133. return false;
  134. }
  135. return true;
  136. });
  137. });