import.js 5.5 KB

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