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