Merge pull request #15404 from jfcherng/fix-php74-curly-braces
[phpmyadmin.git] / js / import.js
blob7bd91a2ecc45b0db4a1169b9c3455cdf31262ac0
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * Functions used in the import tab
4  *
5  */
8 /**
9  * Toggles the hiding and showing of each plugin's options
10  * according to the currently selected plugin from the dropdown list
11  */
12 function changePluginOpts () {
13     $('#format_specific_opts').find('div.format_specific_options').each(function () {
14         $(this).hide();
15     });
16     var selected_plugin_name = $('#plugins').find('option:selected').val();
17     $('#' + selected_plugin_name + '_options').fadeIn('slow');
18     if (selected_plugin_name === 'csv') {
19         $('#import_notification').text(PMA_messages.strImportCSV);
20     } else {
21         $('#import_notification').text('');
22     }
25 /**
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
28  */
29 function matchFile (fname) {
30     var fname_array = fname.toLowerCase().split('.');
31     var len = fname_array.length;
32     if (len !== 0) {
33         var extension = fname_array[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(fname_array[len - 1]).length === 1) {
39             $('select[name=\'format\'] option').filterByValue(fname_array[len - 1]).prop('selected', true);
40             changePluginOpts();
41         }
42     }
45 /**
46  * Unbind all event handlers before tearing down a page
47  */
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');
55 });
57 AJAX.registerOnload('import.js', function () {
58     // import_file_form validation.
59     $(document).on('submit', '#import_file_form', function (event) {
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" /> ' + PMA_messages.strImportDialogMessage + '</div>';
64         if (radioLocalImport.length !== 0) {
65             // remote upload.
67             if (radioImport.is(':checked') && $('#input_import_file').val() === '') {
68                 $('#input_import_file').focus();
69                 PMA_ajaxShowMessage(fileMsg, false);
70                 return false;
71             }
73             if (radioLocalImport.is(':checked')) {
74                 if ($('#select_local_import_file').length === 0) {
75                     PMA_ajaxShowMessage('<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error" /> ' + PMA_messages.strNoImportFile + ' </div>', false);
76                     return false;
77                 }
79                 if ($('#select_local_import_file').val() === '') {
80                     $('#select_local_import_file').focus();
81                     PMA_ajaxShowMessage(fileMsg, false);
82                     return false;
83                 }
84             }
85         } else {
86             // local upload.
87             if ($('#input_import_file').val() === '') {
88                 $('#input_import_file').focus();
89                 PMA_ajaxShowMessage(fileMsg, false);
90                 return false;
91             }
92         }
94         // show progress bar.
95         $('#upload_form_status').css('display', 'inline');
96         $('#upload_form_status_info').css('display', 'inline');
97     });
99     // Initially display the options for the selected plugin
100     changePluginOpts();
102     // Whenever the selected plugin changes, change the options displayed
103     $('#plugins').change(function () {
104         changePluginOpts();
105     });
107     $('#input_import_file').change(function () {
108         matchFile($(this).val());
109     });
111     $('#select_local_import_file').change(function () {
112         matchFile($(this).val());
113     });
115     /*
116      * When the "Browse the server" form is clicked or the "Select from the web server upload directory"
117      * form is clicked, the radio button beside it becomes selected and the other form becomes disabled.
118      */
119     $('#input_import_file').on('focus change', function () {
120         $('#radio_import_file').prop('checked', true);
121         $('#radio_local_import_file').prop('checked', false);
122     });
123     $('#select_local_import_file').focus(function () {
124         $('#radio_local_import_file').prop('checked', true);
125         $('#radio_import_file').prop('checked', false);
126     });
128     /**
129      * Set up the interface for Javascript-enabled browsers since the default is for
130      *  Javascript-disabled browsers
131      */
132     $('#scroll_to_options_msg').hide();
133     $('#format_specific_opts').find('div.format_specific_options')
134         .css({
135             'border': 0,
136             'margin': 0,
137             'padding': 0
138         })
139         .find('h3')
140         .remove();
141     // $("form[name=import] *").unwrap();
143     /**
144      * for input element text_csv_enclosed and text_csv_escaped allow just one character to enter.
145      * as mysql allows just one character for these fields,
146      * if first character is escape then allow two including escape character.
147      */
148     $('#text_csv_enclosed').add('#text_csv_escaped').on('keyup', function () {
149         if ($(this).val().length === 2 && $(this).val().charAt(0) !== '\\') {
150             $(this).val($(this).val().substring(0, 1));
151             return false;
152         }
153         return true;
154     });