Translated using Weblate.
[phpmyadmin.git] / js / tbl_structure.js
blob1e1d696f46d45ff1fbaeae52041337f7dccb5eee
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used on the table structure page
4  * @name            Table Structure
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @required    js/functions.js
9  */
11 /**
12  * AJAX scripts for tbl_structure.php
13  *
14  * Actions ajaxified here:
15  * Drop Column
16  * Add Primary Key
17  * Drop Primary Key/Index
18  *
19  */
20 $(document).ready(function() {
21     /**
22      * Attach Event Handler for 'Drop Column'
23      *
24      * @uses    $.PMA_confirm()
25      * @uses    PMA_ajaxShowMessage()
26      * (see $GLOBALS['cfg']['AjaxEnable'])
27      */
28     $(".drop_column_anchor").live('click', function(event) {
29         event.preventDefault();
31         /**
32          * @var curr_table_name String containing the name of the current table
33          */
34         var curr_table_name = window.parent.table;
35         /**
36          * @var curr_row    Object reference to the currently selected row (i.e. field in the table)
37          */
38         var $curr_row = $(this).parents('tr');
39         /**
40          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
41          */
42         var curr_column_name = $curr_row.children('th').children('label').text();
43         /**
44          * @var $after_field_item    Corresponding entry in the 'After' field.
45          */
46         var $after_field_item = $("select[name='after_field'] option[value='" + curr_column_name + "']");
47         /**
48          * @var question    String containing the question to be asked for confirmation
49          */
50         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` DROP `' + escapeHtml(curr_column_name) + '`';
52         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
54             PMA_ajaxShowMessage(PMA_messages['strDroppingColumn'], false);
56             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
57                 if(data.success == true) {
58                     PMA_ajaxShowMessage(data.message);
59                     toggleRowColors($curr_row.next());
60                     // Adjust the row numbers
61                     for (var $row = $curr_row.next(); $row.length > 0; $row = $row.next()) {
62                         var new_val = parseInt($row.find('td:nth-child(2)').text()) - 1;
63                         $row.find('td:nth-child(2)').text(new_val);
64                     }
65                     $after_field_item.remove();
66                     $curr_row.hide("medium").remove();
67                     // refresh the list of indexes (comes from sql.php)
68                     $('#indexes').html(data.indexes_list);
69                 }
70                 else {
71                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
72                 }
73             }) // end $.get()
74         }); // end $.PMA_confirm()
75     }) ; //end of Drop Column Anchor action
77     /**
78      * Ajax Event handler for 'Add Primary Key'
79      *
80      * @uses    $.PMA_confirm()
81      * @uses    PMA_ajaxShowMessage()
82      * (see $GLOBALS['cfg']['AjaxEnable'])
83      */
84     $(".action_primary a").live('click', function(event) {
85         event.preventDefault();
87         /**
88          * @var curr_table_name String containing the name of the current table
89          */
90         var curr_table_name = window.parent.table;
91         /**
92          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
93          */
94         var curr_column_name = $(this).parents('tr').children('th').children('label').text();
95         /**
96          * @var question    String containing the question to be asked for confirmation
97          */
98         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
100         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
102             PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
104             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
105                 if(data.success == true) {
106                     PMA_ajaxShowMessage(data.message);
107                     $(this).remove();
108                     if (typeof data.reload != 'undefined') {
109                         window.parent.frame_content.location.reload();
110                     }
111                 }
112                 else {
113                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
114                 }
115             }) // end $.get()
116         }) // end $.PMA_confirm()
117     })//end Add Primary Key
119     /**
120      * Ajax Event handler for 'Drop Primary Key/Index'
121      *
122      * @uses    $.PMA_confirm()
123      * @uses    PMA_ajaxShowMessage()
124      * (see $GLOBALS['cfg']['AjaxEnable'])
125      */
126     $('.drop_primary_key_index_anchor').live('click', function(event) {
127         event.preventDefault();
129         $anchor = $(this);
131         /**
132          * @var $curr_row    Object containing reference to the current field's row
133          */
134         var $curr_row = $anchor.parents('tr');
135         /** @var    Number of columns in the key */
136         var rows = $anchor.parents('td').attr('rowspan') || 1;
137         /** @var    Rows that should be hidden */
138         var $rows_to_hide = $curr_row;
139         for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
140             $rows_to_hide = $rows_to_hide.add($last_row);
141         }
143         var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
145         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
147             PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex'], false);
149             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
150                 if(data.success == true) {
151                     PMA_ajaxShowMessage(data.message);
152                     var $table_ref = $rows_to_hide.closest('table');
153                     if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
154                         // We are about to remove all rows from the table
155                         $table_ref.hide('medium', function() {
156                             $('.no_indexes_defined').show('medium');
157                             $rows_to_hide.remove();
158                         });
159                         $table_ref.siblings('div.notice').hide('medium');
160                     } else {
161                         // We are removing some of the rows only
162                         toggleRowColors($rows_to_hide.last().next());
163                         $rows_to_hide.hide("medium", function () {
164                             $(this).remove();
165                         });
166                     }
167                 }
168                 else {
169                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
170                 }
171             }) // end $.get()
172         }) // end $.PMA_confirm()
173     }) //end Drop Primary Key/Index
175     /**
176      *Ajax event handler for multi column change
177     **/
178     $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
179         event.preventDefault();
181         /*Check whether atleast one row is selected for change*/
182         if ($("#tablestructure tbody tr").hasClass("marked")) {
183             /*Define the action and $url variabls for the post method*/
184             var $form = $("#fieldsForm");
185             var action = $form.attr('action');
186             var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
187             /*Calling for the changeColumns fucntion*/
188             changeColumns(action,url);
189         } else {
190             PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
191         }
192     });
194     /**
195      *Ajax event handler for single column change
196     **/
197     $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
198         event.preventDefault();
199         /*Define the action and $url variabls for the post method*/
200         var action = "tbl_alter.php";
201         var url = $(this).attr('href');
202         if (url.substring(0, 13) == "tbl_alter.php") {
203             url = url.substring(14, url.length);
204         }
205         url = url + "&ajax_request=true";
206         /*Calling for the changeColumns fucntion*/
207         changeColumns(action,url);
208      });
210     /**
211      *Ajax event handler for index edit
212     **/
213     $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
214         event.preventDefault();
215         if ($(this).find("a").length == 0) {
216             // Add index
217             var valid = checkFormElementInRange(
218                 $(this).closest('form')[0],
219                 'added_fields',
220                 'Column count has to be larger than zero.'
221             );
222             if (! valid) {
223                 return;
224             }
225             var url = $(this).closest('form').serialize();
226             var title = PMA_messages['strAddIndex'];
227         } else {
228             // Edit index
229             var url = $(this).find("a").attr("href");
230             if (url.substring(0, 16) == "tbl_indexes.php?") {
231                 url = url.substring(16, url.length );
232             }
233             var title = PMA_messages['strEditIndex'];
234         }
235         url += "&ajax_request=true";
237         /*Remove the hidden dialogs if there are*/
238         if ($('#edit_index_dialog').length != 0) {
239             $('#edit_index_dialog').remove();
240         }
241         var $div = $('<div id="edit_index_dialog"></div>');
243         /**
244          * @var button_options Object that stores the options
245          *                     passed to jQueryUI dialog
246          */
247         var button_options = {};
248         button_options[PMA_messages['strGo']] = function() {
249             /**
250              *  @var    the_form    object referring to the export form
251              */
252             var $form = $("#index_frm");
253             PMA_prepareForAjaxRequest($form);
254             //User wants to submit the form
255             $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
256                 if ($("#sqlqueryresults").length != 0) {
257                     $("#sqlqueryresults").remove();
258                 }
259                 if (data.success == true) {
260                     PMA_ajaxShowMessage(data.message);
261                     $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
262                     $("#sqlqueryresults").html(data.sql_query);
263                     $("#result_query .notice").remove();
264                     $("#result_query").prepend(data.message);
266                     /*Reload the field form*/
267                     $("#table_index").remove();
268                     var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
269                     $temp_div.find("#table_index").insertAfter("#index_header");
270                     if ($("#edit_index_dialog").length > 0) {
271                         $("#edit_index_dialog").dialog("close");
272                     }
273                     $('.no_indexes_defined').hide();
274                 } else if (data.error != undefined) {
275                     var $temp_div = $("<div id='temp_div'><div>").append(data.error);
276                     if ($temp_div.find(".error code").length != 0) {
277                         var $error = $temp_div.find(".error code").addClass("error");
278                     } else {
279                         var $error = $temp_div;
280                     }
281                     PMA_ajaxShowMessage($error, false);
282                 }
283             }) // end $.post()
284         }
285         button_options[PMA_messages['strCancel']] = function() {
286             $(this).dialog('close');
287         }
288         var $msgbox = PMA_ajaxShowMessage();
289         $.get("tbl_indexes.php", url, function(data) {
290             if (data.error) {
291                 //in the case of an error, show the error message returned.
292                 PMA_ajaxShowMessage(data.error, false);
293             } else {
294                 PMA_ajaxRemoveMessage($msgbox);
295                 // Show dialog if the request was successful
296                 $div
297                 .append(data)
298                 .dialog({
299                     title: title,
300                     width: 450,
301                     open: PMA_verifyColumnsProperties,
302                     modal: true,
303                     buttons: button_options,
304                     close: function () {
305                         $(this).remove();
306                     }
307                 });
308                 checkIndexType();
309                 checkIndexName("index_frm");
310                 PMA_convertFootnotesToTooltips($div);
311                 // Add a slider for selecting how many columns to add to the index
312                 $div.find('.slider').slider({
313                     animate: true,
314                     value: 1,
315                     min: 1,
316                     max: 16,
317                     slide: function( event, ui ) {
318                         $(this).closest('fieldset').find('input[type=submit]').val(
319                             PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
320                         );
321                     }
322                 });
323                 // Focus the slider, otherwise it looks nearly transparent
324                 $('.ui-slider-handle').addClass('ui-state-focus');
325             }
326         }) // end $.get()
327     });
329     /**
330      * Handler for adding more columns to an index in the editor
331      */
332     $('#index_frm input[type=submit]').live('click', function(event) {
333         event.preventDefault();
334         var rows_to_add = $(this)
335             .closest('fieldset')
336             .find('.slider')
337             .slider('value');
338         while (rows_to_add--) {
339             var $newrow = $('#index_columns')
340                 .find('tbody > tr:first')
341                 .clone()
342                 .appendTo(
343                     $('#index_columns').find('tbody')
344                 );
345             $newrow.find(':input').each(function() {
346                 $(this).val('');
347             });
348         }
349     });
351     /**
352      *Ajax event handler for Add column(s)
353     **/
354     $("#addColumns.ajax input[type=submit]").live('click', function(event){
355         event.preventDefault();
357         /*Remove the hidden dialogs if there are*/
358         if ($('#add_columns').length != 0) {
359             $('#add_columns').remove();
360         }
361         var $div = $('<div id="add_columns"></div>');
363         var $form = $("#addColumns");
365         /**
366          *  @var    button_options  Object that stores the options passed to jQueryUI
367          *                          dialog
368          */
369         var button_options = {};
370         // in the following function we need to use $(this)
371         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
373         var button_options_error = {};
374         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
375         var $msgbox = PMA_ajaxShowMessage();
377         $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" ,  function(data) {
378             //in the case of an error, show the error message returned.
379             if (data.success != undefined && data.success == false) {
380                 $div
381                 .append(data.error)
382                 .dialog({
383                     title: PMA_messages['strAddColumns'],
384                     height: 230,
385                     width: 900,
386                     open: PMA_verifyColumnsProperties,
387                     modal: true,
388                     buttons : button_options_error
389                 })// end dialog options
390             } else {
391                 $div
392                 .append(data)
393                 .dialog({
394                     title: PMA_messages['strAddColumns'],
395                     height: 600,
396                     width: 900,
397                     open: PMA_verifyColumnsProperties,
398                     modal: true,
399                     buttons : button_options
400                 }); // end dialog options
402                 $div = $("#add_columns");
403                 /*changed the z-index of the enum editor to allow the edit*/
404                 $("#enum_editor").css("z-index", "1100");
405                 PMA_convertFootnotesToTooltips($div);
406             }
407             PMA_ajaxRemoveMessage($msgbox);
408         }) // end $.get()
409     });
413 }) // end $(document).ready()
416  * Loads the append_fields_form to the Change dialog allowing users
417  * to change the columns
418  * @param   string    action  Variable which parses the name of the
419  *                             destination file
420  * @param   string    $url    Variable which parses the data for the
421  *                             post action
422  */
423 function changeColumns(action,url)
425     /*Remove the hidden dialogs if there are*/
426     if ($('#change_column_dialog').length != 0) {
427         $('#change_column_dialog').remove();
428     }
429     var $div = $('<div id="change_column_dialog"></div>');
431     /**
432      *  @var    button_options  Object that stores the options passed to jQueryUI
433      *                          dialog
434      */
435     var button_options = {};
436     // in the following function we need to use $(this)
437     button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
439     var button_options_error = {};
440     button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
441     var $msgbox = PMA_ajaxShowMessage();
443     $.get( action , url ,  function(data) {
444         //in the case of an error, show the error message returned.
445         if (data.success != undefined && data.success == false) {
446             $div
447             .append(data.error)
448             .dialog({
449                 title: PMA_messages['strChangeTbl'],
450                 height: 230,
451                 width: 900,
452                 modal: true,
453                 open: PMA_verifyColumnsProperties,
454                 buttons : button_options_error
455             })// end dialog options
456         } else {
457             $div
458             .append(data)
459             .dialog({
460                 title: PMA_messages['strChangeTbl'],
461                 height: 600,
462                 width: 900,
463                 modal: true,
464                 open: PMA_verifyColumnsProperties,
465                 buttons : button_options
466             }); // end dialog options
467             $("#append_fields_form input[name=do_save_data]").addClass("ajax");
468             /*changed the z-index of the enum editor to allow the edit*/
469             $("#enum_editor").css("z-index", "1100");
470             $div = $("#change_column_dialog");
471             PMA_convertFootnotesToTooltips($div);
472         }
473         PMA_ajaxRemoveMessage($msgbox);
474     }) // end $.get()