Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / js / tbl_structure.js
blob63fa60f90f7c19e6723926a62e7f928a67a9c99d
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() {
22     /**
23      * Attach Event Handler for 'Drop Column'
24      *
25      * @uses    $.PMA_confirm()
26      * @uses    PMA_ajaxShowMessage()
27      * (see $GLOBALS['cfg']['AjaxEnable'])
28      */
29     $(".drop_column_anchor").live('click', function(event) {
30         event.preventDefault();
32         /**
33          * @var curr_table_name String containing the name of the current table
34          */
35         var curr_table_name = window.parent.table;
36         /**
37          * @var curr_row    Object reference to the currently selected row (i.e. field in the table)
38          */
39         var curr_row = $(this).parents('tr');
40         /**
41          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
42          */
43         var curr_column_name = $(curr_row).children('th').children('label').text();
44         /**
45          * @var question    String containing the question to be asked for confirmation
46          */
47         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + curr_table_name + '` DROP `' + curr_column_name + '`';
49         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
51             PMA_ajaxShowMessage(PMA_messages['strDroppingColumn']);
53             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
54                 if(data.success == true) {
55                     PMA_ajaxShowMessage(data.message);
56                     $(curr_row).hide("medium").remove();
57                 }
58                 else {
59                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
60                 }
61             }) // end $.get()
62         }); // end $.PMA_confirm()
63     }) ; //end of Drop Column Anchor action
65     /**
66      * Ajax Event handler for 'Add Primary Key'
67      *
68      * @uses    $.PMA_confirm()
69      * @uses    PMA_ajaxShowMessage()
70      * (see $GLOBALS['cfg']['AjaxEnable'])
71      */
72     $(".action_primary a").live('click', function(event) {
73         event.preventDefault();
75         /**
76          * @var curr_table_name String containing the name of the current table
77          */
78         var curr_table_name = window.parent.table;
79         /**
80          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
81          */
82         var curr_column_name = $(this).parents('tr').children('th').children('label').text();
83         /**
84          * @var question    String containing the question to be asked for confirmation
85          */
86         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + curr_table_name + '` ADD PRIMARY KEY(`' + curr_column_name + '`)';
88         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
90             PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey']);
92             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
93                 if(data.success == true) {
94                     PMA_ajaxShowMessage(data.message);
95                     $(this).remove();
96                     if (typeof data.reload != 'undefined') {
97                         window.parent.frame_content.location.reload();
98                     }
99                 }
100                 else {
101                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
102                 }
103             }) // end $.get()
104         }) // end $.PMA_confirm()
105     })//end Add Primary Key
107     /**
108      * Ajax Event handler for 'Drop Primary Key/Index'
109      *
110      * @uses    $.PMA_confirm()
111      * @uses    PMA_ajaxShowMessage()
112      * (see $GLOBALS['cfg']['AjaxEnable'])
113      */
114     $('.drop_primary_key_index_anchor').live('click', function(event) {
115         event.preventDefault();
117         $anchor = $(this);
119         /**
120          * @var $curr_row    Object containing reference to the current field's row
121          */
122         var $curr_row = $anchor.parents('tr');
123         /** @var    Number of columns in the key */
124         var rows = $anchor.parents('td').attr('rowspan') || 1;
125         /** @var    Rows that should be hidden */
126         var $rows_to_hide = $curr_row;
127         for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
128             $rows_to_hide = $rows_to_hide.add($last_row);
129         }
131         var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
133         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
135             PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex']);
137             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
138                 if(data.success == true) {
139                     PMA_ajaxShowMessage(data.message);
140                     $rows_to_hide.hide("medium").remove();
141                 }
142                 else {
143                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
144                 }
145             }) // end $.get()
146         }) // end $.PMA_confirm()
147     }) //end Drop Primary Key/Index
149     /**
150      *Ajax event handler for muti column change
151     **/
152     $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
153         event.preventDefault();
155         /*Check whether atleast one row is selected for change*/
156         if ($("#tablestructure tbody tr").hasClass("marked")) {
157             /*Define the action and $url variabls for the post method*/
158             var $form = $("#fieldsForm");
159             var action = $form.attr('action');
160             var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
161             /*Calling for the changeColumns fucntion*/
162             changeColumns(action,url);
163         } else {
164             PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
165         }
166     });
168     /**
169      *Ajax event handler for single column change
170     **/
171     $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
172         event.preventDefault();
173         /*Define the action and $url variabls for the post method*/
174         var action = "tbl_alter.php";
175         var url = $(this).attr('href');
176         if (url.substring(0, 13) == "tbl_alter.php") {
177             url = url.substring(14, url.length);
178         }
179         url = url + "&ajax_request=true";
180         /*Calling for the changeColumns fucntion*/
181         changeColumns(action,url);
182      });
184     /**
185      *Ajax event handler for index edit
186     **/
187     $("#table_index tbody tr td.edit_index.ajax").live('click', function(event){
188         event.preventDefault();
189         var url = $(this).find("a").attr("href");
190         if (url.substring(0, 16) == "tbl_indexes.php?") {
191             url = url.substring(16, url.length );
192         }
193         url = url + "&ajax_request=true";
195         /*Remove the hidden dialogs if there are*/
196         if ($('#edit_index_dialog').length != 0) {
197             $('#edit_index_dialog').remove();
198         }
199         var $div = $('<div id="edit_index_dialog"></div>');
201         /**
202          *  @var    button_options  Object that stores the options passed to jQueryUI
203          *                          dialog
204          */
205         var button_options = {};
206         // in the following function we need to use $(this)
207         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
209         var button_options_error = {};
210         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
211         var $msgbox = PMA_ajaxShowMessage();
213         $.get( "tbl_indexes.php" , url ,  function(data) {
214             //in the case of an error, show the error message returned.
215             if (data.success != undefined && data.success == false) {
216                 $div
217                 .append(data.error)
218                 .dialog({
219                     title: PMA_messages['strEdit'],
220                     height: 230,
221                     width: 900,
222                     open: PMA_verifyTypeOfAllColumns,
223                     modal: true,
224                     buttons : button_options_error
225                 })// end dialog options
226             } else {
227                 $div
228                 .append(data)
229                 .dialog({
230                     title: PMA_messages['strEdit'],
231                     height: 600,
232                     width: 900,
233                     open: PMA_verifyTypeOfAllColumns,
234                     modal: true,
235                     buttons : button_options
236                 })
237                 //Remove the top menu container from the dialog
238                 .find("#topmenucontainer").hide()
239                 ; // end dialog options
240                 checkIndexType();
241                 checkIndexName("index_frm");
242             }
243             PMA_ajaxRemoveMessage($msgbox);
244         }) // end $.get()
245     });
247     /**
248      *Ajax action for submiting the index form
249     **/
250     $("#index_frm.ajax input[name=do_save_data]").live('click', function(event) {
251         event.preventDefault();
252         /**
253          *  @var    the_form    object referring to the export form
254          */
255         var $form = $("#index_frm");
257         PMA_prepareForAjaxRequest($form);
258         //User wants to submit the form
259         $.post($form.attr('action'), $form.serialize()+"&do_save_data=Save", function(data) {
260             if ($("#sqlqueryresults").length != 0) {
261                 $("#sqlqueryresults").remove();
262             }
263             if (data.success == true) {
264                 PMA_ajaxShowMessage(data.message);
265                 $("<div id='sqlqueryresults'></div>").insertAfter("#topmenucontainer");
266                 $("#sqlqueryresults").html(data.sql_query);
267                 $("#result_query .notice").remove();
268                 $("#result_query").prepend((data.message));
270                 /*Reload the field form*/
271                 $("#table_index").remove();
272                 var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
273                 $temp_div.find("#table_index").insertAfter("#index_header");
274                 if ($("#edit_index_dialog").length > 0) {
275                     $("#edit_index_dialog").dialog("close").remove();
276                 }
278             } else {
279                 if(data.error != undefined) {
280                     var $temp_div = $("<div id='temp_div'><div>").append(data.error);
281                     if ($temp_div.find(".error code").length != 0) {
282                         var $error = $temp_div.find(".error code").addClass("error");
283                     } else {
284                         var $error = $temp_div;
285                     }
286                 }
287                 PMA_ajaxShowMessage($error);
288             }
290         }) // end $.post()
291     }) // end insert table button "do_save_data"
293     /**
294      *Ajax action for submiting the index form for add more columns
295     **/
296     $("#index_frm.ajax input[name=add_fields]").live('click', function(event) {
297         event.preventDefault();
298         /**
299          *  @var    the_form    object referring to the export form
300          */
301         var $form = $("#index_frm");
303         PMA_prepareForAjaxRequest($form);
304         //User wants to submit the form
305         $.post($form.attr('action'), $form.serialize()+"&add_fields=Go", function(data) {
306             $("#index_columns").remove();
307             var $temp_div = $("<div id='temp_div'><div>").append(data);
308             $temp_div.find("#index_columns").appendTo("#index_edit_fields");
309         }) // end $.post()
310     }) // end insert table button "Go"
312     /**Add the show/hide index table option if the index is available*/
313     if ($("#index_div.ajax").find("#table_index").length != 0) {
314         /**
315          *Prepare a div containing a link for toggle the index table
316          */
317         $('<div id="toggletableindexdiv"><a id="toggletableindexlink"></a></div>')
318         .insertAfter('#index_div')
319         /** don't show it until we have index table on-screen */
320         .show();
322         /** Changing the displayed text according to the hide/show criteria in table index*/
324         $('#toggletableindexlink')
325         .html(PMA_messages['strHideIndexes'])
326         .bind('click', function() {
327              var $link = $(this);
328              $('#index_div').slideToggle();
329              if ($link.text() == PMA_messages['strHideIndexes']) {
330                  $link.text(PMA_messages['strShowIndexes']);
331              } else {
332                  $link.text(PMA_messages['strHideIndexes']);
333              }
334              /** avoid default click action */
335              return false;
336         });
337     } //end show/hide table index
339     /**
340      *Ajax event handler for Add column(s)
341     **/
342     $("#addColumns.ajax input[value=Go]").live('click', function(event){
343         event.preventDefault();
345         /*Remove the hidden dialogs if there are*/
346         if ($('#add_columns').length != 0) {
347             $('#add_columns').remove();
348         }
349         var $div = $('<div id="add_columns"></div>');
351         var $form = $("#addColumns");
353         /**
354          *  @var    button_options  Object that stores the options passed to jQueryUI
355          *                          dialog
356          */
357         var button_options = {};
358         // in the following function we need to use $(this)
359         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
361         var button_options_error = {};
362         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
363         var $msgbox = PMA_ajaxShowMessage();
365         $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" ,  function(data) {
366             //in the case of an error, show the error message returned.
367             if (data.success != undefined && data.success == false) {
368                 $div
369                 .append(data.error)
370                 .dialog({
371                     title: PMA_messages['strAddColumns'],
372                     height: 230,
373                     width: 900,
374                     open: PMA_verifyTypeOfAllColumns,
375                     modal: true,
376                     buttons : button_options_error
377                 })// end dialog options
378             } else {
379                 $div
380                 .append(data)
381                 .dialog({
382                     title: PMA_messages['strAddColumns'],
383                     height: 600,
384                     width: 900,
385                     open: PMA_verifyTypeOfAllColumns,
386                     modal: true,
387                     buttons : button_options
388                 })
389                 //Remove the top menu container from the dialog
390                 .find("#topmenucontainer").hide()
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_verifyTypeOfAllColumns,
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_verifyTypeOfAllColumns,
456                 buttons : button_options
457             })
458             //Remove the top menu container from the dialog
459             .find("#topmenucontainer").hide()
460             ; // end dialog options
461             $("#append_fields_form input[name=do_save_data]").addClass("ajax");
462             /*changed the z-index of the enum editor to allow the edit*/
463             $("#enum_editor").css("z-index", "1100");
464             $div = $("#change_column_dialog");
465             PMA_convertFootnotesToTooltips($div);
466         }
467         PMA_ajaxRemoveMessage($msgbox);
468     }) // end $.get()