Improve storing columns on designer and add a translation
[phpmyadmin.git] / js / tbl_operations.js
blobcb818a292c6329f3c9ed9d7753d9878b890d3ded
1 /**
2  * Unbind all event handlers before tearing down a page
3  */
4 AJAX.registerTeardown('tbl_operations.js', function () {
5     $(document).off('submit', '#copyTable.ajax');
6     $(document).off('submit', '#moveTableForm');
7     $(document).off('submit', '#tableOptionsForm');
8     $(document).off('submit', '#partitionsForm');
9     $(document).off('click', '#tbl_maintenance li a.maintain_action.ajax');
10     $(document).off('click', '#drop_tbl_anchor.ajax');
11     $(document).off('click', '#drop_view_anchor.ajax');
12     $(document).off('click', '#truncate_tbl_anchor.ajax');
13 });
15 /**
16  * jQuery coding for 'Table operations'.  Used on tbl_operations.php
17  * Attach Ajax Event handlers for Table operations
18  */
19 AJAX.registerOnload('tbl_operations.js', function () {
20     /**
21      *Ajax action for submitting the "Copy table"
22      **/
23     $(document).on('submit', '#copyTable.ajax', function (event) {
24         event.preventDefault();
25         var $form = $(this);
26         PMA_prepareForAjaxRequest($form);
27         var argsep = PMA_commonParams.get('arg_separator');
28         $.post($form.attr('action'), $form.serialize() + argsep + 'submit_copy=Go', function (data) {
29             if (typeof data !== 'undefined' && data.success === true) {
30                 if ($form.find('input[name=\'switch_to_new\']').prop('checked')) {
31                     PMA_commonParams.set(
32                         'db',
33                         $form.find('select[name=\'target_db\']').val()
34                     );
35                     PMA_commonParams.set(
36                         'table',
37                         $form.find('input[name=\'new_name\']').val()
38                     );
39                     PMA_commonActions.refreshMain(false, function () {
40                         PMA_ajaxShowMessage(data.message);
41                     });
42                 } else {
43                     PMA_ajaxShowMessage(data.message);
44                 }
45                 // Refresh navigation when the table is copied
46                 PMA_reloadNavigation();
47             } else {
48                 PMA_ajaxShowMessage(data.error, false);
49             }
50         }); // end $.post()
51     });// end of copyTable ajax submit
53     /**
54      *Ajax action for submitting the "Move table"
55      */
56     $(document).on('submit', '#moveTableForm', function (event) {
57         event.preventDefault();
58         var $form = $(this);
59         PMA_prepareForAjaxRequest($form);
60         var argsep = PMA_commonParams.get('arg_separator');
61         $.post($form.attr('action'), $form.serialize() + argsep + 'submit_move=1', function (data) {
62             if (typeof data !== 'undefined' && data.success === true) {
63                 PMA_commonParams.set('db', data._params.db);
64                 PMA_commonParams.set('table', data._params.table);
65                 PMA_commonActions.refreshMain('tbl_sql.php', function () {
66                     PMA_ajaxShowMessage(data.message);
67                 });
68                 // Refresh navigation when the table is copied
69                 PMA_reloadNavigation();
70             } else {
71                 PMA_ajaxShowMessage(data.error, false);
72             }
73         }); // end $.post()
74     });
76     /**
77      * Ajax action for submitting the "Table options"
78      */
79     $(document).on('submit', '#tableOptionsForm', function (event) {
80         event.preventDefault();
81         event.stopPropagation();
82         var $form = $(this);
83         var $tblNameField = $form.find('input[name=new_name]');
84         var $tblCollationField = $form.find('select[name=tbl_collation]');
85         var collationOrigValue = $('select[name="tbl_collation"] option[selected]').val();
86         var $changeAllColumnCollationsCheckBox = $('#checkbox_change_all_collations');
87         var question = PMA_messages.strChangeAllColumnCollationsWarning;
89         if ($tblNameField.val() !== $tblNameField[0].defaultValue) {
90             // reload page and navigation if the table has been renamed
91             PMA_prepareForAjaxRequest($form);
93             if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
94                 $form.PMA_confirm(question, $form.attr('action'), function (url) {
95                     submitOptionsForm();
96                 });
97             } else {
98                 submitOptionsForm();
99             }
100         } else {
101             if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
102                 $form.PMA_confirm(question, $form.attr('action'), function (url) {
103                     $form.removeClass('ajax').submit().addClass('ajax');
104                 });
105             } else {
106                 $form.removeClass('ajax').submit().addClass('ajax');
107             }
108         }
110         function submitOptionsForm () {
111             $.post($form.attr('action'), $form.serialize(), function (data) {
112                 if (typeof data !== 'undefined' && data.success === true) {
113                     PMA_commonParams.set('table', data._params.table);
114                     PMA_commonActions.refreshMain(false, function () {
115                         $('#page_content').html(data.message);
116                         PMA_highlightSQL($('#page_content'));
117                     });
118                     // Refresh navigation when the table is renamed
119                     PMA_reloadNavigation();
120                 } else {
121                     PMA_ajaxShowMessage(data.error, false);
122                 }
123             }); // end $.post()
124         }
125     });
127     /**
128      *Ajax events for actions in the "Table maintenance"
129     **/
130     $(document).on('click', '#tbl_maintenance li a.maintain_action.ajax', function (event) {
131         event.preventDefault();
132         var $link = $(this);
134         if ($('.sqlqueryresults').length !== 0) {
135             $('.sqlqueryresults').remove();
136         }
137         if ($('.result_query').length !== 0) {
138             $('.result_query').remove();
139         }
140         // variables which stores the common attributes
141         var params = $.param({
142             ajax_request: 1,
143             server: PMA_commonParams.get('server')
144         });
145         var postData = $link.getPostData();
146         if (postData) {
147             params += PMA_commonParams.get('arg_separator') + postData;
148         }
150         $.post($link.attr('href'), params, function (data) {
151             function scrollToTop () {
152                 $('html, body').animate({ scrollTop: 0 });
153             }
154             var $temp_div;
155             if (typeof data !== 'undefined' && data.success === true && data.sql_query !== undefined) {
156                 PMA_ajaxShowMessage(data.message);
157                 $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
158                 $('.sqlqueryresults').html(data.sql_query);
159                 PMA_highlightSQL($('#page_content'));
160                 scrollToTop();
161             } else if (typeof data !== 'undefined' && data.success === true) {
162                 $temp_div = $('<div id=\'temp_div\'></div>');
163                 $temp_div.html(data.message);
164                 var $success = $temp_div.find('.result_query .success');
165                 PMA_ajaxShowMessage($success);
166                 $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
167                 $('.sqlqueryresults').html(data.message);
168                 PMA_highlightSQL($('#page_content'));
169                 PMA_init_slider();
170                 $('.sqlqueryresults').children('fieldset,br').remove();
171                 scrollToTop();
172             } else {
173                 $temp_div = $('<div id=\'temp_div\'></div>');
174                 $temp_div.html(data.error);
176                 var $error;
177                 if ($temp_div.find('.error code').length !== 0) {
178                     $error = $temp_div.find('.error code').addClass('error');
179                 } else {
180                     $error = $temp_div;
181                 }
183                 PMA_ajaxShowMessage($error, false);
184             }
185         }); // end $.post()
186     });// end of table maintenance ajax click
188     /**
189      * Ajax action for submitting the "Partition Maintenance"
190      * Also, asks for confirmation when DROP partition is submitted
191      */
192     $(document).on('submit', '#partitionsForm', function (event) {
193         event.preventDefault();
194         var $form = $(this);
196         function submitPartitionMaintenance () {
197             var argsep = PMA_commonParams.get('arg_separator');
198             var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
199             PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
200             AJAX.source = $form;
201             $.post($form.attr('action'), submitData, AJAX.responseHandler);
202         }
204         if ($('#partition_operation_DROP').is(':checked')) {
205             var question = PMA_messages.strDropPartitionWarning;
206             $form.PMA_confirm(question, $form.attr('action'), function (url) {
207                 submitPartitionMaintenance();
208             });
209         } else if ($('#partition_operation_TRUNCATE').is(':checked')) {
210             var question = PMA_messages.strTruncatePartitionWarning;
211             $form.PMA_confirm(question, $form.attr('action'), function (url) {
212                 submitPartitionMaintenance();
213             });
214         } else {
215             submitPartitionMaintenance();
216         }
217     });
219     $(document).on('click', '#drop_tbl_anchor.ajax', function (event) {
220         event.preventDefault();
221         var $link = $(this);
222         /**
223          * @var question    String containing the question to be asked for confirmation
224          */
225         var question = PMA_messages.strDropTableStrongWarning + ' ';
226         question += PMA_sprintf(
227             PMA_messages.strDoYouReally,
228             'DROP TABLE `'  + escapeHtml(PMA_commonParams.get('db')) + '`.`' + escapeHtml(PMA_commonParams.get('table') + '`')
229         ) + getForeignKeyCheckboxLoader();
231         $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
232             var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
234             var params = getJSConfirmCommonParam(this, $link.getPostData());
236             $.post(url, params, function (data) {
237                 if (typeof data !== 'undefined' && data.success === true) {
238                     PMA_ajaxRemoveMessage($msgbox);
239                     // Table deleted successfully, refresh both the frames
240                     PMA_reloadNavigation();
241                     PMA_commonParams.set('table', '');
242                     PMA_commonActions.refreshMain(
243                         PMA_commonParams.get('opendb_url'),
244                         function () {
245                             PMA_ajaxShowMessage(data.message);
246                         }
247                     );
248                 } else {
249                     PMA_ajaxShowMessage(data.error, false);
250                 }
251             }); // end $.post()
252         }, loadForeignKeyCheckbox); // end $.PMA_confirm()
253     }); // end of Drop Table Ajax action
255     $(document).on('click', '#drop_view_anchor.ajax', function (event) {
256         event.preventDefault();
257         var $link = $(this);
258         /**
259          * @var question    String containing the question to be asked for confirmation
260          */
261         var question = PMA_messages.strDropTableStrongWarning + ' ';
262         question += PMA_sprintf(
263             PMA_messages.strDoYouReally,
264             'DROP VIEW `' + escapeHtml(PMA_commonParams.get('table') + '`')
265         );
267         $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
268             var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
269             var params = getJSConfirmCommonParam(this, $link.getPostData());
270             $.post(url, params, function (data) {
271                 if (typeof data !== 'undefined' && data.success === true) {
272                     PMA_ajaxRemoveMessage($msgbox);
273                     // Table deleted successfully, refresh both the frames
274                     PMA_reloadNavigation();
275                     PMA_commonParams.set('table', '');
276                     PMA_commonActions.refreshMain(
277                         PMA_commonParams.get('opendb_url'),
278                         function () {
279                             PMA_ajaxShowMessage(data.message);
280                         }
281                     );
282                 } else {
283                     PMA_ajaxShowMessage(data.error, false);
284                 }
285             }); // end $.post()
286         }); // end $.PMA_confirm()
287     }); // end of Drop View Ajax action
289     $(document).on('click', '#truncate_tbl_anchor.ajax', function (event) {
290         event.preventDefault();
291         var $link = $(this);
292         /**
293          * @var question    String containing the question to be asked for confirmation
294          */
295         var question = PMA_messages.strTruncateTableStrongWarning + ' ';
296         question += PMA_sprintf(
297             PMA_messages.strDoYouReally,
298             'TRUNCATE `' + escapeHtml(PMA_commonParams.get('db')) + '`.`' + escapeHtml(PMA_commonParams.get('table') + '`')
299         ) + getForeignKeyCheckboxLoader();
300         $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
301             PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
303             var params = getJSConfirmCommonParam(this, $link.getPostData());
305             $.post(url, params, function (data) {
306                 if ($('.sqlqueryresults').length !== 0) {
307                     $('.sqlqueryresults').remove();
308                 }
309                 if ($('.result_query').length !== 0) {
310                     $('.result_query').remove();
311                 }
312                 if (typeof data !== 'undefined' && data.success === true) {
313                     PMA_ajaxShowMessage(data.message);
314                     $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
315                     $('.sqlqueryresults').html(data.sql_query);
316                     PMA_highlightSQL($('#page_content'));
317                 } else {
318                     PMA_ajaxShowMessage(data.error, false);
319                 }
320             }); // end $.post()
321         }, loadForeignKeyCheckbox); // end $.PMA_confirm()
322     }); // end of Truncate Table Ajax action
323 }); // end $(document).ready for 'Table operations'