Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / js / db_structure.js
blob559819e11c9a24f71e9b426f998d5ce6f27f9a09
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used on the database structure page
4  * @name            Database Structure
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @required    js/functions.js
9  */
11 /**
12  * AJAX scripts for db_structure.php
13  *
14  * Actions ajaxified here:
15  * Drop Database
16  * Truncate Table
17  * Drop Table
18  *
19  */
21 /**
22  * Adjust number of rows and total size in the summary
23  * when emptying or dropping a table
24  *
25  * @param jQuery object     $this_anchor
26  */
27 function PMA_adjustTotals($this_anchor) {
28     var $parent_tr = $this_anchor.closest('tr');
29     var $rows_td = $parent_tr.find('.tbl_rows');
30     var $size_td = $parent_tr.find('.tbl_size');
31     var num_rows = parseInt($rows_td.text());
32     // set number of rows to 0
33     // (not really needed in case we are dropping the table)
34     $rows_td.text('0');
35     // set size to unknown (not sure how to get the exact
36     // value here, as an empty InnoDB table would have a size)
37     $size_td.text('-');
39     // try to compute a new total row number
40     if (! isNaN(num_rows)) {
41         $total_rows_td = $('#tbl_summary_row').find('.tbl_rows');
42         var total_rows = parseInt($total_rows_td.text());
43         if (! isNaN(total_rows)) {
44             $total_rows_td.text(total_rows - num_rows);
45         }
46     }
48     // prefix total size with "~"
49     var $total_size_td = $('#tbl_summary_row').find('.tbl_size');
50     $total_size_td.text($total_size_td.text().replace(/^/,'~'));
53 $(document).ready(function() {
54     /**
55      * Ajax Event handler for 'Insert Table'
56      *
57      * @uses    PMA_ajaxShowMessage()
58      * @see     $cfg['AjaxEnable']
59      */
60     var current_insert_table;
61     $("td.insert_table a.ajax").live('click', function(event){
62         event.preventDefault();
63         current_insert_table = $(this);
64         var $url = $(this).attr("href");
65         if ($url.substring(0, 15) == "tbl_change.php?") {
66             $url = $url.substring(15);
67         }
69         if ($("#insert_table_dialog").length > 0) {
70             $("#insert_table_dialog").remove();
71         }
72            var $div = $('<div id="insert_table_dialog"></div>');
73            var target = "tbl_change.php";
75         /**
76          *  @var    button_options  Object that stores the options passed to jQueryUI
77          *                          dialog
78          */
79         var button_options = {};
80         // in the following function we need to use $(this)
81         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
83         var button_options_error = {};
84         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
86         var $msgbox = PMA_ajaxShowMessage();
88         $.get( target , $url+"&ajax_request=true" ,  function(data) {
89             //in the case of an error, show the error message returned.
90             if (data.success != undefined && data.success == false) {
91                 $div
92                 .append(data.error)
93                 .dialog({
94                     title: PMA_messages['strInsertTable'],
95                     height: 230,
96                     width: 900,
97                     modal: true,
98                     open: PMA_verifyTypeOfAllColumns,
99                     buttons : button_options_error
100                 })// end dialog options
101             } else {
102                 var $dialog = $div
103                     .append(data)
104                     .dialog({
105                         title: PMA_messages['strInsertTable'],
106                         height: 600,
107                         width: 900,
108                         modal: true,
109                         open: PMA_verifyTypeOfAllColumns,
110                         buttons : button_options
111                     });// end dialog options
112                 //Remove the top menu container from the dialog
113                 $dialog.find("#topmenucontainer").hide();
114                 //Adding the datetime pikers for the dialog
115                 $dialog.find('.datefield, .datetimefield').each(function () {
116                     PMA_addDatepicker($(this));
117                 });
118                 $(".insertRowTable").addClass("ajax");
119                 $("#buttonYes").addClass("ajax");
120                 $div = $("#insert_table_dialog");
121                 PMA_convertFootnotesToTooltips($div);
122             }
123             PMA_ajaxRemoveMessage($msgbox);
124         }) // end $.get()
126     });
128     $("#insertForm .insertRowTable.ajax input[value=Go]").live('click', function(event) {
129         event.preventDefault();
130         /**
131          *  @var    the_form    object referring to the insert form
132          */
133         var $form = $("#insertForm");
134         $("#result_query").remove();
135         PMA_prepareForAjaxRequest($form);
136         //User wants to submit the form
137         $.post($form.attr('action'), $form.serialize() , function(data) {
138             if(data.success == true) {
139                 PMA_ajaxShowMessage(data.message);
140             } else {
141                 PMA_ajaxShowMessage(data.error);
142             }
143             if ($("#insert_table_dialog").length > 0) {
144                 $("#insert_table_dialog").dialog("close").remove();
145             }
146             /**Update the row count at the tableForm*/
147             current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
148         }) // end $.post()
149     }) // end insert table button "Go"
151     $("#buttonYes.ajax").live('click', function(event){
152         event.preventDefault();
153         /**
154          *  @var    the_form    object referring to the insert form
155          */
156         var $form = $("#insertForm");
157         /**Get the submit type and the after insert type in the form*/
158         var selected_submit_type = $("#insertForm").find("#actions_panel .control_at_footer option:selected").attr('value');
159         var selected_after_insert = $("#insertForm").find("#actions_panel select[name=after_insert] option:selected").attr('value');
160         $("#result_query").remove();
161         PMA_prepareForAjaxRequest($form);
162         //User wants to submit the form
163         $.post($form.attr('action'), $form.serialize() , function(data) {
164             if(data.success == true) {
165                 PMA_ajaxShowMessage(data.message);
166                 if (selected_submit_type == "showinsert") {
167                     $(data.sql_query).insertAfter("#topmenucontainer");
168                     $("#result_query .notice").remove();
169                     $("#result_query").prepend((data.message));
170                 }
171                 if (selected_after_insert == "new_insert") {
172                     /**Trigger the insert dialog for new_insert option*/
173                     current_insert_table.trigger('click');
174                 }
176             } else {
177                 PMA_ajaxShowMessage(data.error);
178             }
179             if ($("#insert_table_dialog").length > 0) {
180                 $("#insert_table_dialog").dialog("close").remove();
181             }
182             /**Update the row count at the tableForm*/
183             current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
184         }) // end $.post()
185     });
187     /**
188      * Ajax Event handler for 'Truncate Table'
189      *
190      * @uses    $.PMA_confirm()
191      * @uses    PMA_ajaxShowMessage()
192      * @see     $cfg['AjaxEnable']
193      */
194     $(".truncate_table_anchor").live('click', function(event) {
195         event.preventDefault();
197         /**
198          * @var $this_anchor Object  referring to the anchor clicked
199          */
200         var $this_anchor = $(this);
202         //extract current table name and build the question string
203         /**
204          * @var curr_table_name String containing the name of the table to be truncated
205          */
206         var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
207         /**
208          * @var question    String containing the question to be asked for confirmation
209          */
210         var question = 'TRUNCATE ' + curr_table_name;
212         $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
214             PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
216             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
217                 if (data.success == true) {
218                     PMA_ajaxShowMessage(data.message);
219                     //Fetch inner span of this anchor
220                     //and replace the icon with its disabled version
221                     var span = $this_anchor.html().replace(/ic_b_empty/, 'ic_bd_empty');
222                     PMA_adjustTotals($this_anchor);
224                     //To disable further attempts to truncate the table,
225                     //replace the a element with its inner span (modified)
226                     $this_anchor
227                         .replaceWith(span)
228                         .removeClass('truncate_table_anchor');
229                 } else {
230                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
231                 }
232             }) // end $.get()
233         }) //end $.PMA_confirm()
234     }); //end of Truncate Table Ajax action
236     /**
237      * Ajax Event handler for 'Drop Table'
238      *
239      * @uses    $.PMA_confirm()
240      * @uses    PMA_ajaxShowMessage()
241      * @see     $cfg['AjaxEnable']
242      */
243     $(".drop_table_anchor").live('click', function(event) {
244         event.preventDefault();
246         var $this_anchor = $(this);
248         //extract current table name and build the question string
249         /**
250          * @var $curr_row    Object containing reference to the current row
251          */
252         var $curr_row = $this_anchor.parents('tr');
253         /**
254          * @var curr_table_name String containing the name of the table to be truncated
255          */
256         var curr_table_name = $curr_row.children('th').children('a').text();
257         /**
258          * @var question    String containing the question to be asked for confirmation
259          */
260         var question = 'DROP TABLE ' + curr_table_name;
262         $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
264             PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
266             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
267                 if (data.success == true) {
268                     PMA_ajaxShowMessage(data.message);
269                     PMA_adjustTotals($this_anchor);
270                     $curr_row.hide("medium").remove();
272                     if (window.parent && window.parent.frame_navigation) {
273                         window.parent.frame_navigation.location.reload();
274                     }
275                 } else {
276                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
277                 }
278             }); // end $.get()
279         }); // end $.PMA_confirm()
280     }); //end of Drop Table Ajax action
282     /**
283      * Ajax Event handler for 'Drop tracking'
284      *
285      * @uses    $.PMA_confirm()
286      * @uses    PMA_ajaxShowMessage()
287      * @see     $cfg['AjaxEnable']
288      */
289     $('.drop_tracking_anchor').live('click', function(event) {
290         event.preventDefault();
292         var $anchor = $(this);
294         /**
295          * @var curr_tracking_row   Object containing reference to the current tracked table's row
296          */
297         var curr_tracking_row = $anchor.parents('tr');
298          /**
299          * @var question    String containing the question to be asked for confirmation
300          */
301         var question = PMA_messages['strDeleteTrackingData'];
303         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
305             PMA_ajaxShowMessage(PMA_messages['strDeletingTrackingData']);
307             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
308                 if(data.success == true) {
309                     PMA_ajaxShowMessage(data.message);
310                     $(curr_tracking_row).hide("medium").remove();
311                 }
312                 else {
313                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error);
314                 }
315             }) // end $.get()
316         }) // end $.PMA_confirm()
317     }) //end Drop Tracking
319     //Calculate Real End for InnoDB
320     /**
321      * Ajax Event handler for calculatig the real end for a InnoDB table
322      *
323      * @uses    $.PMA_confirm
324      */
325     $('#real_end_input').live('click', function(event) {
326         event.preventDefault();
328         /**
329          * @var question    String containing the question to be asked for confirmation
330          */
331         var question = PMA_messages['strOperationTakesLongTime'];
333         $(this).PMA_confirm(question, '', function() {
334             return true;
335         })
336         return false;
337     }) //end Calculate Real End for InnoDB
339 }, 'top.frame_content'); // end $(document).ready()