import.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. {
  12. $("#format_specific_opts div.format_specific_options").each(function () {
  13. $(this).hide();
  14. });
  15. var selected_plugin_name = $("#plugins option:selected").val();
  16. $("#" + selected_plugin_name + "_options").fadeIn('slow');
  17. if (selected_plugin_name == "csv") {
  18. $("#import_notification").text(PMA_messages.strImportCSV);
  19. } else {
  20. $("#import_notification").text("");
  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. function matchFile(fname)
  28. {
  29. var fname_array = fname.toLowerCase().split(".");
  30. var len = fname_array.length;
  31. if (len !== 0) {
  32. var extension = fname_array[len - 1];
  33. if (extension == "gz" || extension == "bz2" || extension == "zip") {
  34. len--;
  35. }
  36. // Only toggle if the format of the file can be imported
  37. if ($("select[name='format'] option").filterByValue(fname_array[len - 1]).length == 1) {
  38. $("select[name='format'] option").filterByValue(fname_array[len - 1]).prop('selected', true);
  39. changePluginOpts();
  40. }
  41. }
  42. }
  43. /**
  44. * Unbind all event handlers before tearing down a page
  45. */
  46. AJAX.registerTeardown('import.js', function () {
  47. $("#plugins").unbind('change');
  48. $("#input_import_file").unbind('change');
  49. $("#select_local_import_file").unbind('change');
  50. $("#input_import_file").unbind('change').unbind('focus');
  51. $("#select_local_import_file").unbind('focus');
  52. $("#text_csv_enclosed").add("#text_csv_escaped").unbind('keyup');
  53. });
  54. AJAX.registerOnload('import.js', function () {
  55. // import_file_form validation.
  56. $('#import_file_form').live('submit', function (event) {
  57. var radioLocalImport = $("#radio_local_import_file");
  58. var radioImport = $("#radio_import_file");
  59. var fileMsg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ' + PMA_messages.strImportDialogMessage + '</div>';
  60. if (radioLocalImport.length !== 0) {
  61. // remote upload.
  62. // TODO Remove this section when all browsers support HTML5 "required" property
  63. if (! radioLocalImport.is(":checked") && ! radioImport.is(":checked")) {
  64. radioImport.focus();
  65. var msg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ';
  66. msg += PMA_messages.strRadioUnchecked;
  67. msg += '</div>';
  68. PMA_ajaxShowMessage(msg, false);
  69. return false;
  70. }
  71. if (radioImport.is(":checked") && $("#input_import_file").val() === '') {
  72. $("#input_import_file").focus();
  73. PMA_ajaxShowMessage(fileMsg, false);
  74. return false;
  75. }
  76. if (radioLocalImport.is(":checked")) {
  77. if ($("#select_local_import_file").length === 0) {
  78. PMA_ajaxShowMessage('<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ' + PMA_messages.strNoImportFile + ' </div>', false);
  79. return false;
  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. // show progress bar.
  96. $("#upload_form_status").css("display", "inline");
  97. $("#upload_form_status_info").css("display", "inline");
  98. return;
  99. });
  100. // Initially display the options for the selected plugin
  101. changePluginOpts();
  102. // Whenever the selected plugin changes, change the options displayed
  103. $("#plugins").change(function () {
  104. changePluginOpts();
  105. });
  106. $("#input_import_file").change(function () {
  107. matchFile($(this).val());
  108. });
  109. $("#select_local_import_file").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").bind("focus change", function () {
  117. $("#radio_import_file").prop('checked', true);
  118. $("#radio_local_import_file").prop('checked', false);
  119. });
  120. $("#select_local_import_file").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 div.format_specific_options")
  130. .css({
  131. "border": 0,
  132. "margin": 0,
  133. "padding": 0
  134. })
  135. .find("h3")
  136. .remove();
  137. //$("form[name=import] *").unwrap();
  138. /**
  139. * for input element text_csv_enclosed and text_csv_escaped allow just one character to enter.
  140. * as mysql allows just one character for these fields,
  141. * if first character is escape then allow two including escape character.
  142. */
  143. $("#text_csv_enclosed").add("#text_csv_escaped").bind('keyup', function() {
  144. if($(this).val().length === 2 && $(this).val().charAt(0) !== "\\") {
  145. $(this).val($(this).val().substring(0, 1));
  146. return false;
  147. }
  148. return true;
  149. });
  150. });