Translated using Weblate (Catalan)
[phpmyadmin.git] / js / export.js
blob7f0c6ee72346796a32d2dc9f66af7bbc21dc649f
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * Functions used in the export tab
4  *
5  */
7 /**
8  * Disables the "Dump some row(s)" sub-options
9  */
10 function disable_dump_some_rows_sub_options () {
11     $('label[for=\'limit_to\']').fadeTo('fast', 0.4);
12     $('label[for=\'limit_from\']').fadeTo('fast', 0.4);
13     $('input[type=\'text\'][name=\'limit_to\']').prop('disabled', 'disabled');
14     $('input[type=\'text\'][name=\'limit_from\']').prop('disabled', 'disabled');
17 /**
18  * Enables the "Dump some row(s)" sub-options
19  */
20 function enable_dump_some_rows_sub_options () {
21     $('label[for=\'limit_to\']').fadeTo('fast', 1);
22     $('label[for=\'limit_from\']').fadeTo('fast', 1);
23     $('input[type=\'text\'][name=\'limit_to\']').prop('disabled', '');
24     $('input[type=\'text\'][name=\'limit_from\']').prop('disabled', '');
27 /**
28  * Return template data as a json object
29  *
30  * @returns template data
31  */
32 function getTemplateData () {
33     var $form = $('form[name="dump"]');
34     var blacklist = ['token', 'server', 'db', 'table', 'single_table',
35         'export_type', 'export_method', 'sql_query', 'template_id'];
36     var obj = {};
37     var arr = $form.serializeArray();
38     $.each(arr, function () {
39         if ($.inArray(this.name, blacklist) < 0) {
40             if (obj[this.name] !== undefined) {
41                 if (! obj[this.name].push) {
42                     obj[this.name] = [obj[this.name]];
43                 }
44                 obj[this.name].push(this.value || '');
45             } else {
46                 obj[this.name] = this.value || '';
47             }
48         }
49     });
50     // include unchecked checboxes (which are ignored by serializeArray()) with null
51     // to uncheck them when loading the template
52     $form.find('input[type="checkbox"]:not(:checked)').each(function () {
53         if (obj[this.name] === undefined) {
54             obj[this.name] = null;
55         }
56     });
57     // include empty multiselects
58     $form.find('select').each(function () {
59         if ($(this).find('option:selected').length === 0) {
60             obj[this.name] = [];
61         }
62     });
63     return obj;
66 /**
67  * Create a template with selected options
68  *
69  * @param name name of the template
70  */
71 function createTemplate (name) {
72     var templateData = getTemplateData();
74     var params = {
75         ajax_request : true,
76         server : PMA_commonParams.get('server'),
77         db : PMA_commonParams.get('db'),
78         table : PMA_commonParams.get('table'),
79         exportType : $('input[name="export_type"]').val(),
80         templateAction : 'create',
81         templateName : name,
82         templateData : JSON.stringify(templateData)
83     };
85     PMA_ajaxShowMessage();
86     $.post('tbl_export.php', params, function (response) {
87         if (response.success === true) {
88             $('#templateName').val('');
89             $('#template').html(response.data);
90             $('#template').find('option').each(function () {
91                 if ($(this).text() === name) {
92                     $(this).prop('selected', true);
93                 }
94             });
95             PMA_ajaxShowMessage(PMA_messages.strTemplateCreated);
96         } else {
97             PMA_ajaxShowMessage(response.error, false);
98         }
99     });
103  * Loads a template
105  * @param id ID of the template to load
106  */
107 function loadTemplate (id) {
108     var params = {
109         ajax_request : true,
110         server : PMA_commonParams.get('server'),
111         db : PMA_commonParams.get('db'),
112         table : PMA_commonParams.get('table'),
113         exportType : $('input[name="export_type"]').val(),
114         templateAction : 'load',
115         templateId : id,
116     };
118     PMA_ajaxShowMessage();
119     $.post('tbl_export.php', params, function (response) {
120         if (response.success === true) {
121             var $form = $('form[name="dump"]');
122             var options = JSON.parse(response.data);
123             $.each(options, function (key, value) {
124                 var $element = $form.find('[name="' + key + '"]');
125                 if ($element.length) {
126                     if (($element.is('input') && $element.attr('type') === 'checkbox') && value === null) {
127                         $element.prop('checked', false);
128                     } else {
129                         if (($element.is('input') && $element.attr('type') === 'checkbox') ||
130                             ($element.is('input') && $element.attr('type') === 'radio') ||
131                             ($element.is('select') && $element.attr('multiple') === 'multiple')) {
132                             if (! value.push) {
133                                 value = [value];
134                             }
135                         }
136                         $element.val(value);
137                     }
138                     $element.trigger('change');
139                 }
140             });
141             $('input[name="template_id"]').val(id);
142             PMA_ajaxShowMessage(PMA_messages.strTemplateLoaded);
143         } else {
144             PMA_ajaxShowMessage(response.error, false);
145         }
146     });
150  * Updates an existing template with current options
152  * @param id ID of the template to update
153  */
154 function updateTemplate (id) {
155     var templateData = getTemplateData();
157     var params = {
158         ajax_request : true,
159         server : PMA_commonParams.get('server'),
160         db : PMA_commonParams.get('db'),
161         table : PMA_commonParams.get('table'),
162         exportType : $('input[name="export_type"]').val(),
163         templateAction : 'update',
164         templateId : id,
165         templateData : JSON.stringify(templateData)
166     };
168     PMA_ajaxShowMessage();
169     $.post('tbl_export.php', params, function (response) {
170         if (response.success === true) {
171             PMA_ajaxShowMessage(PMA_messages.strTemplateUpdated);
172         } else {
173             PMA_ajaxShowMessage(response.error, false);
174         }
175     });
179  * Delete a template
181  * @param id ID of the template to delete
182  */
183 function deleteTemplate (id) {
184     var params = {
185         ajax_request : true,
186         server : PMA_commonParams.get('server'),
187         db : PMA_commonParams.get('db'),
188         table : PMA_commonParams.get('table'),
189         exportType : $('input[name="export_type"]').val(),
190         templateAction : 'delete',
191         templateId : id,
192     };
194     PMA_ajaxShowMessage();
195     $.post('tbl_export.php', params, function (response) {
196         if (response.success === true) {
197             $('#template').find('option[value="' + id + '"]').remove();
198             PMA_ajaxShowMessage(PMA_messages.strTemplateDeleted);
199         } else {
200             PMA_ajaxShowMessage(response.error, false);
201         }
202     });
206  * Unbind all event handlers before tearing down a page
207  */
208 AJAX.registerTeardown('export.js', function () {
209     $('#plugins').off('change');
210     $('input[type=\'radio\'][name=\'sql_structure_or_data\']').off('change');
211     $('input[type=\'radio\'][name$=\'_structure_or_data\']').off('change');
212     $('input[type=\'radio\'][name=\'output_format\']').off('change');
213     $('#checkbox_sql_include_comments').off('change');
214     $('input[type=\'radio\'][name=\'quick_or_custom\']').off('change');
215     $('input[type=\'radio\'][name=\'allrows\']').off('change');
216     $('#btn_alias_config').off('click');
217     $('.alias_remove').off('click');
218     $('#db_alias_button').off('click');
219     $('#table_alias_button').off('click');
220     $('#column_alias_button').off('click');
221     $('input[name="table_select[]"]').off('change');
222     $('input[name="table_structure[]"]').off('change');
223     $('input[name="table_data[]"]').off('change');
224     $('#table_structure_all').off('change');
225     $('#table_data_all').off('change');
226     $('input[name="createTemplate"]').off('click');
227     $('select[name="template"]').off('change');
228     $('input[name="updateTemplate"]').off('click');
229     $('input[name="deleteTemplate"]').off('click');
232 AJAX.registerOnload('export.js', function () {
233     /**
234      * Export template handling code
235      */
236     // create a new template
237     $('input[name="createTemplate"]').on('click', function (e) {
238         e.preventDefault();
239         var name = $('input[name="templateName"]').val();
240         if (name.length) {
241             createTemplate(name);
242         }
243     });
245     // load an existing template
246     $('select[name="template"]').on('change', function (e) {
247         e.preventDefault();
248         var id = $(this).val();
249         if (id.length) {
250             loadTemplate(id);
251         }
252     });
254     // udpate an existing template with new criteria
255     $('input[name="updateTemplate"]').on('click', function (e) {
256         e.preventDefault();
257         var id = $('select[name="template"]').val();
258         if (id.length) {
259             updateTemplate(id);
260         }
261     });
263     // delete an existing template
264     $('input[name="deleteTemplate"]').on('click', function (e) {
265         e.preventDefault();
266         var id = $('select[name="template"]').val();
267         if (id.length) {
268             deleteTemplate(id);
269         }
270     });
272     /**
273      * Toggles the hiding and showing of each plugin's options
274      * according to the currently selected plugin from the dropdown list
275      */
276     $('#plugins').change(function () {
277         $('#format_specific_opts').find('div.format_specific_options').hide();
278         var selected_plugin_name = $('#plugins').find('option:selected').val();
279         $('#' + selected_plugin_name + '_options').show();
280     });
282     /**
283      * Toggles the enabling and disabling of the SQL plugin's comment options that apply only when exporting structure
284      */
285     $('input[type=\'radio\'][name=\'sql_structure_or_data\']').change(function () {
286         var comments_are_present = $('#checkbox_sql_include_comments').prop('checked');
287         var show = $('input[type=\'radio\'][name=\'sql_structure_or_data\']:checked').val();
288         if (show === 'data') {
289             // disable the SQL comment options
290             if (comments_are_present) {
291                 $('#checkbox_sql_dates').prop('disabled', true).parent().fadeTo('fast', 0.4);
292             }
293             $('#checkbox_sql_relation').prop('disabled', true).parent().fadeTo('fast', 0.4);
294             $('#checkbox_sql_mime').prop('disabled', true).parent().fadeTo('fast', 0.4);
295         } else {
296             // enable the SQL comment options
297             if (comments_are_present) {
298                 $('#checkbox_sql_dates').prop('disabled', false).parent().fadeTo('fast', 1);
299             }
300             $('#checkbox_sql_relation').prop('disabled', false).parent().fadeTo('fast', 1);
301             $('#checkbox_sql_mime').prop('disabled', false).parent().fadeTo('fast', 1);
302         }
304         if (show === 'structure') {
305             $('#checkbox_sql_auto_increment').prop('disabled', true).parent().fadeTo('fast', 0.4);
306         } else {
307             $('#checkbox_sql_auto_increment').prop('disabled', false).parent().fadeTo('fast', 1);
308         }
309     });
311     // For separate-file exports only ZIP compression is allowed
312     $('input[type="checkbox"][name="as_separate_files"]').change(function () {
313         if ($(this).is(':checked')) {
314             $('#compression').val('zip');
315         }
316     });
318     $('#compression').change(function () {
319         if ($('option:selected').val() !== 'zip') {
320             $('input[type="checkbox"][name="as_separate_files"]').prop('checked', false);
321         }
322     });
325 function setup_table_structure_or_data () {
326     if ($('input[name=\'export_type\']').val() !== 'database') {
327         return;
328     }
329     var pluginName = $('#plugins').find('option:selected').val();
330     var formElemName = pluginName + '_structure_or_data';
331     var force_structure_or_data = !($('input[name=\'' + formElemName + '_default\']').length);
333     if (force_structure_or_data === true) {
334         $('input[name="structure_or_data_forced"]').val(1);
335         $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]')
336             .prop('disabled', true);
337         $('.export_structure, .export_data').fadeTo('fast', 0.4);
338     } else {
339         $('input[name="structure_or_data_forced"]').val(0);
340         $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]')
341             .prop('disabled', false);
342         $('.export_structure, .export_data').fadeTo('fast', 1);
344         var structure_or_data = $('input[name="' + formElemName + '_default"]').val();
346         if (structure_or_data === 'structure') {
347             $('.export_data input[type="checkbox"]')
348                 .prop('checked', false);
349         } else if (structure_or_data === 'data') {
350             $('.export_structure input[type="checkbox"]')
351                 .prop('checked', false);
352         }
353         if (structure_or_data === 'structure' || structure_or_data === 'structure_and_data') {
354             if (!$('.export_structure input[type="checkbox"]:checked').length) {
355                 $('input[name="table_select[]"]:checked')
356                     .closest('tr')
357                     .find('.export_structure input[type="checkbox"]')
358                     .prop('checked', true);
359             }
360         }
361         if (structure_or_data === 'data' || structure_or_data === 'structure_and_data') {
362             if (!$('.export_data input[type="checkbox"]:checked').length) {
363                 $('input[name="table_select[]"]:checked')
364                     .closest('tr')
365                     .find('.export_data input[type="checkbox"]')
366                     .prop('checked', true);
367             }
368         }
370         check_selected_tables();
371         check_table_select_all();
372     }
376  * Toggles the hiding and showing of plugin structure-specific and data-specific
377  * options
378  */
379 function toggle_structure_data_opts () {
380     var pluginName = $('select#plugins').val();
381     var radioFormName = pluginName + '_structure_or_data';
382     var dataDiv = '#' + pluginName + '_data';
383     var structureDiv = '#' + pluginName + '_structure';
384     var show = $('input[type=\'radio\'][name=\'' + radioFormName + '\']:checked').val();
385     if (show === 'data') {
386         $(dataDiv).slideDown('slow');
387         $(structureDiv).slideUp('slow');
388     } else {
389         $(structureDiv).slideDown('slow');
390         if (show === 'structure') {
391             $(dataDiv).slideUp('slow');
392         } else {
393             $(dataDiv).slideDown('slow');
394         }
395     }
399  * Toggles the disabling of the "save to file" options
400  */
401 function toggle_save_to_file () {
402     var $ulSaveAsfile = $('#ul_save_asfile');
403     if (!$('#radio_dump_asfile').prop('checked')) {
404         $ulSaveAsfile.find('> li').fadeTo('fast', 0.4);
405         $ulSaveAsfile.find('> li > input').prop('disabled', true);
406         $ulSaveAsfile.find('> li > select').prop('disabled', true);
407     } else {
408         $ulSaveAsfile.find('> li').fadeTo('fast', 1);
409         $ulSaveAsfile.find('> li > input').prop('disabled', false);
410         $ulSaveAsfile.find('> li > select').prop('disabled', false);
411     }
414 AJAX.registerOnload('export.js', function () {
415     toggle_save_to_file();
416     $('input[type=\'radio\'][name=\'output_format\']').change(toggle_save_to_file);
420  * For SQL plugin, toggles the disabling of the "display comments" options
421  */
422 function toggle_sql_include_comments () {
423     $('#checkbox_sql_include_comments').change(function () {
424         var $ulIncludeComments = $('#ul_include_comments');
425         if (!$('#checkbox_sql_include_comments').prop('checked')) {
426             $ulIncludeComments.find('> li').fadeTo('fast', 0.4);
427             $ulIncludeComments.find('> li > input').prop('disabled', true);
428         } else {
429             // If structure is not being exported, the comment options for structure should not be enabled
430             if ($('#radio_sql_structure_or_data_data').prop('checked')) {
431                 $('#text_sql_header_comment').prop('disabled', false).parent('li').fadeTo('fast', 1);
432             } else {
433                 $ulIncludeComments.find('> li').fadeTo('fast', 1);
434                 $ulIncludeComments.find('> li > input').prop('disabled', false);
435             }
436         }
437     });
440 function check_table_select_all () {
441     var total = $('input[name="table_select[]"]').length;
442     var str_checked = $('input[name="table_structure[]"]:checked').length;
443     var data_checked = $('input[name="table_data[]"]:checked').length;
444     var str_all = $('#table_structure_all');
445     var data_all = $('#table_data_all');
447     if (str_checked === total) {
448         str_all
449             .prop('indeterminate', false)
450             .prop('checked', true);
451     } else if (str_checked === 0) {
452         str_all
453             .prop('indeterminate', false)
454             .prop('checked', false);
455     } else {
456         str_all
457             .prop('indeterminate', true)
458             .prop('checked', false);
459     }
461     if (data_checked === total) {
462         data_all
463             .prop('indeterminate', false)
464             .prop('checked', true);
465     } else if (data_checked === 0) {
466         data_all
467             .prop('indeterminate', false)
468             .prop('checked', false);
469     } else {
470         data_all
471             .prop('indeterminate', true)
472             .prop('checked', false);
473     }
476 function toggle_table_select_all_str () {
477     var str_all = $('#table_structure_all').is(':checked');
478     if (str_all) {
479         $('input[name="table_structure[]"]').prop('checked', true);
480     } else {
481         $('input[name="table_structure[]"]').prop('checked', false);
482     }
485 function toggle_table_select_all_data () {
486     var data_all = $('#table_data_all').is(':checked');
487     if (data_all) {
488         $('input[name="table_data[]"]').prop('checked', true);
489     } else {
490         $('input[name="table_data[]"]').prop('checked', false);
491     }
494 function check_selected_tables (argument) {
495     $('.export_table_select tbody tr').each(function () {
496         check_table_selected(this);
497     });
500 function check_table_selected (row) {
501     var $row = $(row);
502     var table_select = $row.find('input[name="table_select[]"]');
503     var str_check = $row.find('input[name="table_structure[]"]');
504     var data_check = $row.find('input[name="table_data[]"]');
506     var data = data_check.is(':checked:not(:disabled)');
507     var structure = str_check.is(':checked:not(:disabled)');
509     if (data && structure) {
510         table_select.prop({ checked: true, indeterminate: false });
511         $row.addClass('marked');
512     } else if (data || structure) {
513         table_select.prop({ checked: true, indeterminate: true });
514         $row.removeClass('marked');
515     } else {
516         table_select.prop({ checked: false, indeterminate: false });
517         $row.removeClass('marked');
518     }
521 function toggle_table_select (row) {
522     var $row = $(row);
523     var table_selected = $row.find('input[name="table_select[]"]').is(':checked');
525     if (table_selected) {
526         $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', true);
527         $row.addClass('marked');
528     } else {
529         $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', false);
530         $row.removeClass('marked');
531     }
534 function handleAddProcCheckbox () {
535     if ($('#table_structure_all').is(':checked') === true
536         && $('#table_data_all').is(':checked') === true
537     ) {
538         $('#checkbox_sql_procedure_function').prop('checked', true);
539     } else {
540         $('#checkbox_sql_procedure_function').prop('checked', false);
541     }
544 AJAX.registerOnload('export.js', function () {
545     /**
546      * For SQL plugin, if "CREATE TABLE options" is checked/unchecked, check/uncheck each of its sub-options
547      */
548     var $create = $('#checkbox_sql_create_table_statements');
549     var $create_options = $('#ul_create_table_statements').find('input');
550     $create.change(function () {
551         $create_options.prop('checked', $(this).prop('checked'));
552     });
553     $create_options.change(function () {
554         if ($create_options.is(':checked')) {
555             $create.prop('checked', true);
556         }
557     });
559     /**
560      * Disables the view output as text option if the output must be saved as a file
561      */
562     $('#plugins').change(function () {
563         var active_plugin = $('#plugins').find('option:selected').val();
564         var force_file = $('#force_file_' + active_plugin).val();
565         if (force_file === 'true') {
566             if ($('#radio_dump_asfile').prop('checked') !== true) {
567                 $('#radio_dump_asfile').prop('checked', true);
568                 toggle_save_to_file();
569             }
570             $('#radio_view_as_text').prop('disabled', true).parent().fadeTo('fast', 0.4);
571         } else {
572             $('#radio_view_as_text').prop('disabled', false).parent().fadeTo('fast', 1);
573         }
574     });
576     $('input[type=\'radio\'][name$=\'_structure_or_data\']').on('change', function () {
577         toggle_structure_data_opts();
578     });
580     $('input[name="table_select[]"]').on('change', function () {
581         toggle_table_select($(this).closest('tr'));
582         check_table_select_all();
583         handleAddProcCheckbox();
584     });
586     $('input[name="table_structure[]"]').on('change', function () {
587         check_table_selected($(this).closest('tr'));
588         check_table_select_all();
589         handleAddProcCheckbox();
590     });
592     $('input[name="table_data[]"]').on('change', function () {
593         check_table_selected($(this).closest('tr'));
594         check_table_select_all();
595         handleAddProcCheckbox();
596     });
598     $('#table_structure_all').on('change', function () {
599         toggle_table_select_all_str();
600         check_selected_tables();
601         handleAddProcCheckbox();
602     });
604     $('#table_data_all').on('change', function () {
605         toggle_table_select_all_data();
606         check_selected_tables();
607         handleAddProcCheckbox();
608     });
610     if ($('input[name=\'export_type\']').val() === 'database') {
611         // Hide structure or data radio buttons
612         $('input[type=\'radio\'][name$=\'_structure_or_data\']').each(function () {
613             var $this = $(this);
614             var name = $this.prop('name');
615             var val = $('input[name="' + name + '"]:checked').val();
616             var name_default = name + '_default';
617             if (!$('input[name="' + name_default + '"]').length) {
618                 $this
619                     .after(
620                         $('<input type="hidden" name="' + name_default + '" value="' + val + '" disabled>')
621                     )
622                     .after(
623                         $('<input type="hidden" name="' + name + '" value="structure_and_data">')
624                     );
625                 $this.parent().find('label').remove();
626             } else {
627                 $this.parent().remove();
628             }
629         });
630         $('input[type=\'radio\'][name$=\'_structure_or_data\']').remove();
632         // Disable CREATE table checkbox for sql
633         var createTableCheckbox = $('#checkbox_sql_create_table');
634         createTableCheckbox.prop('checked', true);
635         var dummyCreateTable = $('#checkbox_sql_create_table')
636             .clone()
637             .removeAttr('id')
638             .attr('type', 'hidden');
639         createTableCheckbox
640             .prop('disabled', true)
641             .after(dummyCreateTable)
642             .parent()
643             .fadeTo('fast', 0.4);
645         setup_table_structure_or_data();
646     }
648     /**
649      * Handle force structure_or_data
650      */
651     $('#plugins').change(setup_table_structure_or_data);
655  * Toggles display of options when quick and custom export are selected
656  */
657 function toggle_quick_or_custom () {
658     if ($('input[name=\'quick_or_custom\']').length === 0 // custom_no_form option
659         || $('#radio_custom_export').prop('checked') // custom
660     ) {
661         $('#databases_and_tables').show();
662         $('#rows').show();
663         $('#output').show();
664         $('#format_specific_opts').show();
665         $('#output_quick_export').hide();
666         var selected_plugin_name = $('#plugins').find('option:selected').val();
667         $('#' + selected_plugin_name + '_options').show();
668     } else { // quick
669         $('#databases_and_tables').hide();
670         $('#rows').hide();
671         $('#output').hide();
672         $('#format_specific_opts').hide();
673         $('#output_quick_export').show();
674     }
676 var time_out;
677 function check_time_out (time_limit) {
678     if (typeof time_limit === 'undefined' || time_limit === 0) {
679         return true;
680     }
681     // margin of one second to avoid race condition to set/access session variable
682     time_limit = time_limit + 1;
683     var href = 'export.php';
684     var params = {
685         'ajax_request' : true,
686         'check_time_out' : true
687     };
688     clearTimeout(time_out);
689     time_out = setTimeout(function () {
690         $.get(href, params, function (data) {
691             if (data.message === 'timeout') {
692                 PMA_ajaxShowMessage(
693                     '<div class="error">' +
694                     PMA_messages.strTimeOutError +
695                     '</div>',
696                     false
697                 );
698             }
699         });
700     }, time_limit * 1000);
704  * Handler for Database/table alias select
706  * @param event object the event object
708  * @return void
709  */
710 function aliasSelectHandler (event) {
711     var sel = event.data.sel;
712     var type = event.data.type;
713     var inputId = $(this).val();
714     var $label = $(this).next('label');
715     $('input#' + $label.attr('for')).addClass('hide');
716     $('input#' + inputId).removeClass('hide');
717     $label.attr('for', inputId);
718     $('#alias_modal ' + sel + '[id$=' + type + ']:visible').addClass('hide');
719     var $inputWrapper = $('#alias_modal ' + sel + '#' + inputId + type);
720     $inputWrapper.removeClass('hide');
721     if (type === '_cols' && $inputWrapper.length > 0) {
722         var outer = $inputWrapper[0].outerHTML;
723         // Replace opening tags
724         var regex = /<dummy_inp/gi;
725         if (outer.match(regex)) {
726             var newTag = outer.replace(regex, '<input');
727             // Replace closing tags
728             regex = /<\/dummy_inp/gi;
729             newTag = newTag.replace(regex, '</input');
730             // Assign replacement
731             $inputWrapper.replaceWith(newTag);
732         }
733     } else if (type === '_tables') {
734         $('.table_alias_select:visible').change();
735     }
736     $('#alias_modal').dialog('option', 'position', 'center');
740  * Handler for Alias dialog box
742  * @param event object the event object
744  * @return void
745  */
746 function createAliasModal (event) {
747     event.preventDefault();
748     var dlgButtons = {};
749     dlgButtons[PMA_messages.strSaveAndClose] = function () {
750         $(this).dialog('close');
751         $('#alias_modal').parent().appendTo($('form[name="dump"]'));
752     };
753     $('#alias_modal').dialog({
754         width: Math.min($(window).width() - 100, 700),
755         maxHeight: $(window).height(),
756         modal: true,
757         dialogClass: 'alias-dialog',
758         buttons: dlgButtons,
759         create: function () {
760             $(this).css('maxHeight', $(window).height() - 150);
761             var db = PMA_commonParams.get('db');
762             if (db) {
763                 var option = $('<option></option>');
764                 option.text(db);
765                 option.attr('value', db);
766                 $('#db_alias_select').append(option).val(db).change();
767             } else {
768                 var params = {
769                     ajax_request : true,
770                     server : PMA_commonParams.get('server'),
771                     type: 'list-databases'
772                 };
773                 $.post('ajax.php', params, function (response) {
774                     if (response.success === true) {
775                         $.each(response.databases, function (idx, value) {
776                             var option = $('<option></option>');
777                             option.text(value);
778                             option.attr('value', value);
779                             $('#db_alias_select').append(option);
780                         });
781                     } else {
782                         PMA_ajaxShowMessage(response.error, false);
783                     }
784                 });
785             }
786         },
787         close: function () {
788             var isEmpty = true;
789             $(this).find('input[type="text"]').each(function () {
790                 // trim empty input fields on close
791                 if ($(this).val()) {
792                     isEmpty = false;
793                 } else {
794                     $(this).parents('tr').remove();
795                 }
796             });
797             // Toggle checkbox based on aliases
798             $('input#btn_alias_config').prop('checked', !isEmpty);
799         },
800         position: { my: 'center top', at: 'center top', of: window }
801     });
804 function aliasToggleRow (elm) {
805     var inputs = elm.parents('tr').find('input,button');
806     if (elm.val()) {
807         inputs.attr('disabled', false);
808     } else {
809         inputs.attr('disabled', true);
810     }
813 function addAlias (type, name, field, value) {
814     if (value === '') {
815         return;
816     }
818     var row = $('#alias_data tfoot tr').clone();
819     row.find('th').text(type);
820     row.find('td:first').text(name);
821     row.find('input').attr('name', field);
822     row.find('input').val(value);
823     row.find('.alias_remove').on('click', function () {
824         $(this).parents('tr').remove();
825     });
827     var matching = $('#alias_data [name="' + $.escapeSelector(field) + '"]');
828     if (matching.length > 0) {
829         matching.parents('tr').remove();
830     }
832     $('#alias_data tbody').append(row);
835 AJAX.registerOnload('export.js', function () {
836     $('input[type=\'radio\'][name=\'quick_or_custom\']').change(toggle_quick_or_custom);
838     $('#scroll_to_options_msg').hide();
839     $('#format_specific_opts').find('div.format_specific_options')
840         .hide()
841         .css({
842             'border': 0,
843             'margin': 0,
844             'padding': 0
845         })
846         .find('h3')
847         .remove();
848     toggle_quick_or_custom();
849     toggle_structure_data_opts();
850     toggle_sql_include_comments();
851     check_table_select_all();
852     handleAddProcCheckbox();
854     /**
855      * Initially disables the "Dump some row(s)" sub-options
856      */
857     disable_dump_some_rows_sub_options();
859     /**
860      * Disables the "Dump some row(s)" sub-options when it is not selected
861      */
862     $('input[type=\'radio\'][name=\'allrows\']').change(function () {
863         if ($('input[type=\'radio\'][name=\'allrows\']').prop('checked')) {
864             enable_dump_some_rows_sub_options();
865         } else {
866             disable_dump_some_rows_sub_options();
867         }
868     });
870     // Open Alias Modal Dialog on click
871     $('#btn_alias_config').on('click', createAliasModal);
872     $('.alias_remove').on('click', function () {
873         $(this).parents('tr').remove();
874     });
875     $('#db_alias_select').on('change', function () {
876         aliasToggleRow($(this));
877         var db = $(this).val();
878         var table = PMA_commonParams.get('table');
879         if (table) {
880             var option = $('<option></option>');
881             option.text(table);
882             option.attr('value', table);
883             $('#table_alias_select').append(option).val(table).change();
884         } else {
885             var params = {
886                 ajax_request : true,
887                 server : PMA_commonParams.get('server'),
888                 db : $(this).val(),
889                 type: 'list-tables'
890             };
891             $.post('ajax.php', params, function (response) {
892                 if (response.success === true) {
893                     $.each(response.tables, function (idx, value) {
894                         var option = $('<option></option>');
895                         option.text(value);
896                         option.attr('value', value);
897                         $('#table_alias_select').append(option);
898                     });
899                 } else {
900                     PMA_ajaxShowMessage(response.error, false);
901                 }
902             });
903         }
904     });
905     $('#table_alias_select').on('change', function () {
906         aliasToggleRow($(this));
907         var params = {
908             ajax_request : true,
909             server : PMA_commonParams.get('server'),
910             db : $('#db_alias_select').val(),
911             table: $(this).val(),
912             type: 'list-columns'
913         };
914         $.post('ajax.php', params, function (response) {
915             if (response.success === true) {
916                 $.each(response.columns, function (idx, value) {
917                     var option = $('<option></option>');
918                     option.text(value);
919                     option.attr('value', value);
920                     $('#column_alias_select').append(option);
921                 });
922             } else {
923                 PMA_ajaxShowMessage(response.error, false);
924             }
925         });
926     });
927     $('#column_alias_select').on('change', function () {
928         aliasToggleRow($(this));
929     });
930     $('#db_alias_button').on('click', function (e) {
931         e.preventDefault();
932         var db = $('#db_alias_select').val();
933         addAlias(
934             PMA_messages.strAliasDatabase,
935             db,
936             'aliases[' + db + '][alias]',
937             $('#db_alias_name').val()
938         );
939         $('#db_alias_name').val('');
940     });
941     $('#table_alias_button').on('click', function (e) {
942         e.preventDefault();
943         var db = $('#db_alias_select').val();
944         var table = $('#table_alias_select').val();
945         addAlias(
946             PMA_messages.strAliasTable,
947             db + '.' + table,
948             'aliases[' + db + '][tables][' + table + '][alias]',
949             $('#table_alias_name').val()
950         );
951         $('#table_alias_name').val('');
952     });
953     $('#column_alias_button').on('click', function (e) {
954         e.preventDefault();
955         var db = $('#db_alias_select').val();
956         var table = $('#table_alias_select').val();
957         var column = $('#column_alias_select').val();
958         addAlias(
959             PMA_messages.strAliasColumn,
960             db + '.' + table + '.' + column,
961             'aliases[' + db + '][tables][' + table + '][colums][' + column + ']',
962             $('#column_alias_name').val()
963         );
964         $('#column_alias_name').val('');
965     });