Update po files
[phpmyadmin/madhuracj.git] / js / tbl_structure.js
blobc9fd93bf13798996f00259142f32c4275170f55e
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                     $after_field_item.remove();
60                     $(curr_row).hide("medium").remove();
61                 }
62                 else {
63                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
64                 }
65             }) // end $.get()
66         }); // end $.PMA_confirm()
67     }) ; //end of Drop Column Anchor action
69     /**
70      * Ajax Event handler for 'Add Primary Key'
71      *
72      * @uses    $.PMA_confirm()
73      * @uses    PMA_ajaxShowMessage()
74      * (see $GLOBALS['cfg']['AjaxEnable'])
75      */
76     $(".action_primary a").live('click', function(event) {
77         event.preventDefault();
79         /**
80          * @var curr_table_name String containing the name of the current table
81          */
82         var curr_table_name = window.parent.table;
83         /**
84          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
85          */
86         var curr_column_name = $(this).parents('tr').children('th').children('label').text();
87         /**
88          * @var question    String containing the question to be asked for confirmation
89          */
90         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
92         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
94             PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
96             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
97                 if(data.success == true) {
98                     PMA_ajaxShowMessage(data.message);
99                     $(this).remove();
100                     if (typeof data.reload != 'undefined') {
101                         window.parent.frame_content.location.reload();
102                     }
103                 }
104                 else {
105                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
106                 }
107             }) // end $.get()
108         }) // end $.PMA_confirm()
109     })//end Add Primary Key
111     /**
112      * Ajax Event handler for 'Drop Primary Key/Index'
113      *
114      * @uses    $.PMA_confirm()
115      * @uses    PMA_ajaxShowMessage()
116      * (see $GLOBALS['cfg']['AjaxEnable'])
117      */
118     $('.drop_primary_key_index_anchor').live('click', function(event) {
119         event.preventDefault();
121         $anchor = $(this);
123         /**
124          * @var $curr_row    Object containing reference to the current field's row
125          */
126         var $curr_row = $anchor.parents('tr');
127         /** @var    Number of columns in the key */
128         var rows = $anchor.parents('td').attr('rowspan') || 1;
129         /** @var    Rows that should be hidden */
130         var $rows_to_hide = $curr_row;
131         for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
132             $rows_to_hide = $rows_to_hide.add($last_row);
133         }
135         var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
137         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
139             PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex'], false);
141             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
142                 if(data.success == true) {
143                     PMA_ajaxShowMessage(data.message);
144                     var $table_ref = $rows_to_hide.closest('table');
145                     if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
146                         // We are about to remove all rows from the table
147                         $table_ref.hide('medium', function() {
148                             $('.no_indexes_defined').show('medium');
149                             $rows_to_hide.remove();
150                         });
151                         $table_ref.siblings('div.notice').hide('medium');
152                     } else {
153                         // We are removing some of the rows only
154                         $rows_to_hide.hide("medium", function () {
155                             $(this).remove();
156                         });
157                     }
158                 }
159                 else {
160                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
161                 }
162             }) // end $.get()
163         }) // end $.PMA_confirm()
164     }) //end Drop Primary Key/Index
166     /**
167      *Ajax event handler for multi column change
168     **/
169     $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
170         event.preventDefault();
172         /*Check whether atleast one row is selected for change*/
173         if ($("#tablestructure tbody tr").hasClass("marked")) {
174             /*Define the action and $url variabls for the post method*/
175             var $form = $("#fieldsForm");
176             var action = $form.attr('action');
177             var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
178             /*Calling for the changeColumns fucntion*/
179             changeColumns(action,url);
180         } else {
181             PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
182         }
183     });
185     /**
186      *Ajax event handler for single column change
187     **/
188     $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
189         event.preventDefault();
190         /*Define the action and $url variabls for the post method*/
191         var action = "tbl_alter.php";
192         var url = $(this).attr('href');
193         if (url.substring(0, 13) == "tbl_alter.php") {
194             url = url.substring(14, url.length);
195         }
196         url = url + "&ajax_request=true";
197         /*Calling for the changeColumns fucntion*/
198         changeColumns(action,url);
199      });
201     /**
202      *Ajax event handler for index edit
203     **/
204     $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
205         event.preventDefault();
206         if ($(this).find("a").length == 0) {
207             // Add index
208             var valid = checkFormElementInRange(
209                 $(this).closest('form')[0],
210                 'added_fields',
211                 'Column count has to be larger than zero.'
212             );
213             if (! valid) {
214                 return;
215             }
216             var url = $(this).closest('form').serialize();
217             var title = PMA_messages['strAddIndex'];
218         } else {
219             // Edit index
220             var url = $(this).find("a").attr("href");
221             if (url.substring(0, 16) == "tbl_indexes.php?") {
222                 url = url.substring(16, url.length );
223             }
224             var title = PMA_messages['strEditIndex'];
225         }
226         url += "&ajax_request=true";
228         /*Remove the hidden dialogs if there are*/
229         if ($('#edit_index_dialog').length != 0) {
230             $('#edit_index_dialog').remove();
231         }
232         var $div = $('<div id="edit_index_dialog"></div>');
234         /**
235          * @var button_options Object that stores the options
236          *                     passed to jQueryUI dialog
237          */
238         var button_options = {};
239         button_options[PMA_messages['strGo']] = function() {
240             /**
241              *  @var    the_form    object referring to the export form
242              */
243             var $form = $("#index_frm");
244             PMA_prepareForAjaxRequest($form);
245             //User wants to submit the form
246             $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
247                 if ($("#sqlqueryresults").length != 0) {
248                     $("#sqlqueryresults").remove();
249                 }
250                 if (data.success == true) {
251                     PMA_ajaxShowMessage(data.message);
252                     $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
253                     $("#sqlqueryresults").html(data.sql_query);
254                     $("#result_query .notice").remove();
255                     $("#result_query").prepend(data.message);
257                     /*Reload the field form*/
258                     $("#table_index").remove();
259                     var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
260                     $temp_div.find("#table_index").insertAfter("#index_header");
261                     if ($("#edit_index_dialog").length > 0) {
262                         $("#edit_index_dialog").dialog("close");
263                     }
264                     $('.no_indexes_defined').hide();
265                 } else if (data.error != undefined) {
266                     var $temp_div = $("<div id='temp_div'><div>").append(data.error);
267                     if ($temp_div.find(".error code").length != 0) {
268                         var $error = $temp_div.find(".error code").addClass("error");
269                     } else {
270                         var $error = $temp_div;
271                     }
272                     PMA_ajaxShowMessage($error, false);
273                 }
274             }) // end $.post()
275         }
276         button_options[PMA_messages['strCancel']] = function() {
277             $(this).dialog('close');
278         }
279         var $msgbox = PMA_ajaxShowMessage();
280         $.get("tbl_indexes.php", url, function(data) {
281             if (data.error) {
282                 //in the case of an error, show the error message returned.
283                 PMA_ajaxShowMessage(data.error, false);
284             } else {
285                 PMA_ajaxRemoveMessage($msgbox);
286                 // Show dialog if the request was successful
287                 $div
288                 .append(data)
289                 .dialog({
290                     title: title,
291                     width: 450,
292                     open: PMA_verifyColumnsProperties,
293                     modal: true,
294                     buttons: button_options,
295                     close: function () {
296                         $(this).remove();
297                     }
298                 });
299                 checkIndexType();
300                 checkIndexName("index_frm");
301                 PMA_convertFootnotesToTooltips($div);
302                 // Add a slider for selecting how many columns to add to the index
303                 $div.find('.slider').slider({
304                    animate: true,
305                    value: 1,
306                    min: 1,
307                    max: 16,
308                    slide: function( event, ui ) {
309                         $(this).closest('fieldset').find('input[type=submit]').val(
310                             PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
311                         );
312                    }
313                             });
314                 // Focus the slider, otherwise it looks nearly transparent
315                 $('.ui-slider-handle').addClass('ui-state-focus');
316             }
317         }) // end $.get()
318     });
320     /**
321      * Handler for adding more columns to an index in the editor
322      */
323     $('#index_frm input[type=submit]').live('click', function(event) {
324         event.preventDefault();
325         var rows_to_add = $(this)
326             .closest('fieldset')
327             .find('.slider')
328             .slider('value');
329         while (rows_to_add--) {
330             var $newrow = $('#index_columns')
331                 .find('tbody > tr:first')
332                 .clone()
333                 .appendTo(
334                     $('#index_columns').find('tbody')
335                 );
336             $newrow.find(':input').each(function() {
337                 $(this).val('');
338             });
339         }
340     });
342     /**
343      *Ajax event handler for Add column(s)
344     **/
345     $("#addColumns.ajax input[type=submit]").live('click', function(event){
346         event.preventDefault();
348         /*Remove the hidden dialogs if there are*/
349         if ($('#add_columns').length != 0) {
350             $('#add_columns').remove();
351         }
352         var $div = $('<div id="add_columns"></div>');
354         var $form = $("#addColumns");
356         /**
357          *  @var    button_options  Object that stores the options passed to jQueryUI
358          *                          dialog
359          */
360         var button_options = {};
361         // in the following function we need to use $(this)
362         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
364         var button_options_error = {};
365         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
366         var $msgbox = PMA_ajaxShowMessage();
368         $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" ,  function(data) {
369             //in the case of an error, show the error message returned.
370             if (data.success != undefined && data.success == false) {
371                 $div
372                 .append(data.error)
373                 .dialog({
374                     title: PMA_messages['strAddColumns'],
375                     height: 230,
376                     width: 900,
377                     open: PMA_verifyColumnsProperties,
378                     modal: true,
379                     buttons : button_options_error
380                 })// end dialog options
381             } else {
382                 $div
383                 .append(data)
384                 .dialog({
385                     title: PMA_messages['strAddColumns'],
386                     height: 600,
387                     width: 900,
388                     open: PMA_verifyColumnsProperties,
389                     modal: true,
390                     buttons : button_options
391                 }); // end dialog options
393                 $div = $("#add_columns");
394                 /*changed the z-index of the enum editor to allow the edit*/
395                 $("#enum_editor").css("z-index", "1100");
396                 PMA_convertFootnotesToTooltips($div);
397             }
398             PMA_ajaxRemoveMessage($msgbox);
399         }) // end $.get()
400     });
404 }) // end $(document).ready()
407  * Loads the append_fields_form to the Change dialog allowing users
408  * to change the columns
409  * @param   string    action  Variable which parses the name of the
410  *                             destination file
411  * @param   string    $url    Variable which parses the data for the
412  *                             post action
413  */
414 function changeColumns(action,url)
416     /*Remove the hidden dialogs if there are*/
417     if ($('#change_column_dialog').length != 0) {
418         $('#change_column_dialog').remove();
419     }
420     var $div = $('<div id="change_column_dialog"></div>');
422     /**
423      *  @var    button_options  Object that stores the options passed to jQueryUI
424      *                          dialog
425      */
426     var button_options = {};
427     // in the following function we need to use $(this)
428     button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
430     var button_options_error = {};
431     button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
432     var $msgbox = PMA_ajaxShowMessage();
434     $.get( action , url ,  function(data) {
435         //in the case of an error, show the error message returned.
436         if (data.success != undefined && data.success == false) {
437             $div
438             .append(data.error)
439             .dialog({
440                 title: PMA_messages['strChangeTbl'],
441                 height: 230,
442                 width: 900,
443                 modal: true,
444                 open: PMA_verifyColumnsProperties,
445                 buttons : button_options_error
446             })// end dialog options
447         } else {
448             $div
449             .append(data)
450             .dialog({
451                 title: PMA_messages['strChangeTbl'],
452                 height: 600,
453                 width: 900,
454                 modal: true,
455                 open: PMA_verifyColumnsProperties,
456                 buttons : button_options
457             }); // end dialog options
458             $("#append_fields_form input[name=do_save_data]").addClass("ajax");
459             /*changed the z-index of the enum editor to allow the edit*/
460             $("#enum_editor").css("z-index", "1100");
461             $div = $("#change_column_dialog");
462             PMA_convertFootnotesToTooltips($div);
463         }
464         PMA_ajaxRemoveMessage($msgbox);
465     }) // end $.get()