import.js 6.0 KB

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