1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * Functions used in the import tab
9 * Toggles the hiding and showing of each plugin's options
10 * according to the currently selected plugin from the dropdown list
12 function changePluginOpts () {
13 $('#format_specific_opts').find('div.format_specific_options').each(function () {
16 var selectedPluginName = $('#plugins').find('option:selected').val();
17 $('#' + selectedPluginName + '_options').fadeIn('slow');
18 if (selectedPluginName === 'csv') {
19 $('#import_notification').text(Messages.strImportCSV);
21 $('#import_notification').text('');
26 * Toggles the hiding and showing of each plugin's options and sets the selected value
27 * in the plugin dropdown list according to the format of the selected file
29 function matchFile (fname) {
30 var fnameArray = fname.toLowerCase().split('.');
31 var len = fnameArray.length;
33 var extension = fnameArray[len - 1];
34 if (extension === 'gz' || extension === 'bz2' || extension === 'zip') {
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);
46 * Unbind all event handlers before tearing down a page
48 AJAX.registerTeardown('import.js', function () {
49 $('#plugins').off('change');
50 $('#input_import_file').off('change');
51 $('#select_local_import_file').off('change');
52 $('#input_import_file').off('change').off('focus');
53 $('#select_local_import_file').off('focus');
54 $('#text_csv_enclosed').add('#text_csv_escaped').off('keyup');
57 AJAX.registerOnload('import.js', function () {
58 // import_file_form validation.
59 $(document).on('submit', '#import_file_form', function () {
60 var radioLocalImport = $('#radio_local_import_file');
61 var radioImport = $('#radio_import_file');
62 var fileMsg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> ' + Messages.strImportDialogMessage + '</div>';
63 var wrongTblNameMsg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error">' + Messages.strTableNameDialogMessage + '</div>';
64 var wrongDBNameMsg = '<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error">' + Messages.strDBNameDialogMessage + '</div>';
66 if (radioLocalImport.length !== 0) {
69 if (radioImport.is(':checked') && $('#input_import_file').val() === '') {
70 $('#input_import_file').trigger('focus');
71 Functions.ajaxShowMessage(fileMsg, false);
75 if (radioLocalImport.is(':checked')) {
76 if ($('#select_local_import_file').length === 0) {
77 Functions.ajaxShowMessage('<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> ' + Messages.strNoImportFile + ' </div>', false);
81 if ($('#select_local_import_file').val() === '') {
82 $('#select_local_import_file').trigger('focus');
83 Functions.ajaxShowMessage(fileMsg, false);
89 if ($('#input_import_file').val() === '') {
90 $('#input_import_file').trigger('focus');
91 Functions.ajaxShowMessage(fileMsg, false);
94 if ($('#text_csv_new_tbl_name').length > 0) {
95 var newTblName = $('#text_csv_new_tbl_name').val();
96 if (newTblName.length > 0 && $.trim(newTblName).length === 0) {
97 Functions.ajaxShowMessage(wrongTblNameMsg, false);
101 if ($('#text_csv_new_db_name').length > 0) {
102 var newDBName = $('#text_csv_new_db_name').val();
103 if (newDBName.length > 0 && $.trim(newDBName).length === 0) {
104 Functions.ajaxShowMessage(wrongDBNameMsg, false);
110 // show progress bar.
111 $('#upload_form_status').css('display', 'inline');
112 $('#upload_form_status_info').css('display', 'inline');
115 // Initially display the options for the selected plugin
118 // Whenever the selected plugin changes, change the options displayed
119 $('#plugins').on('change', function () {
123 $('#input_import_file').on('change', function () {
124 matchFile($(this).val());
127 $('#select_local_import_file').on('change', function () {
128 matchFile($(this).val());
132 * When the "Browse the server" form is clicked or the "Select from the web server upload directory"
133 * form is clicked, the radio button beside it becomes selected and the other form becomes disabled.
135 $('#input_import_file').on('focus change', function () {
136 $('#radio_import_file').prop('checked', true);
137 $('#radio_local_import_file').prop('checked', false);
139 $('#select_local_import_file').on('focus', function () {
140 $('#radio_local_import_file').prop('checked', true);
141 $('#radio_import_file').prop('checked', false);
145 * Set up the interface for Javascript-enabled browsers since the default is for
146 * Javascript-disabled browsers
148 $('#scroll_to_options_msg').hide();
149 $('#format_specific_opts').find('div.format_specific_options')
157 // $("form[name=import] *").unwrap();
160 * for input element text_csv_enclosed and text_csv_escaped allow just one character to enter.
161 * as mysql allows just one character for these fields,
162 * if first character is escape then allow two including escape character.
164 $('#text_csv_enclosed').add('#text_csv_escaped').on('keyup', function () {
165 if ($(this).val().length === 2 && $(this).val().charAt(0) !== '\\') {
166 $(this).val($(this).val().substring(0, 1));