Translation update done using Pootle.
[phpmyadmin.git] / js / db_structure.js
blob5151bcaf38840b74a756f7f32e485f413b03b7dc
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 truncating, creating, dropping or inserting into a table
24  */
25 function PMA_adjustTotals() {
26     var byteUnits = new Array(
27         PMA_messages['strB'],
28         PMA_messages['strKiB'],
29         PMA_messages['strMiB'],
30         PMA_messages['strGiB'],
31         PMA_messages['strTiB'],
32         PMA_messages['strPiB'],
33         PMA_messages['strEiB']
34     );
35     /**
36      * @var $allTr jQuery object that references all the rows in the list of tables
37      */
38     var $allTr = $("#tablesForm table.data tbody:first tr");
39     // New summary values for the table
40     var tableSum = $allTr.size();
41     var rowsSum = 0;
42     var sizeSum = 0;
43     var overheadSum = 0;
44     var rowSumApproximated = false;
45     
46     $allTr.each(function () {
47         var $this = $(this);
48         // Get the number of rows for this SQL table
49         var strRows = $this.find('.tbl_rows').text();
50         // If the value is approximated
51         if (strRows.indexOf('~') == 0) {
52             rowSumApproximated = true;
53             // The approximated value contains a preceding ~ and a following 2 (Eg 100 --> ~1002)
54             strRows = strRows.substring(1, strRows.length - 1);
55         }
56         strRows = strRows.replace(/[,.]/g , '');
57         var intRow = parseInt(strRows, 10);
58         if (! isNaN(intRow)) {
59             rowsSum += intRow;
60         }
61         // Extract the size and overhead
62         var valSize         = 0;
63         var valOverhead     = 0;
64         var strSize         = $.trim($this.find('.tbl_size span:not(.unit)').text());
65         var strSizeUnit     = $.trim($this.find('.tbl_size span.unit').text());
66         var strOverhead     = $.trim($this.find('.tbl_overhead span:not(.unit)').text());
67         var strOverheadUnit = $.trim($this.find('.tbl_overhead span.unit').text());
68         // Given a value and a unit, such as 100 and KiB, for the table size
69         // and overhead calculate their numeric values in bytes, such as 102400
70         for (var i = 0; i < byteUnits.length; i++) {
71             if (strSizeUnit == byteUnits[i]) {
72                 var tmpVal = parseFloat(strSize);
73                 valSize = tmpVal * Math.pow(1024, i);
74                 break;
75             }
76         }
77         for (var i = 0; i < byteUnits.length; i++) {
78             if (strOverheadUnit == byteUnits[i]) {
79                 var tmpVal = parseFloat(strOverhead);
80                 valOverhead = tmpVal * Math.pow(1024, i);
81                 break;
82             }
83         }
84         sizeSum += valSize;
85         overheadSum += valOverhead;
86     });
87     // Add some commas for readablility:
88     // 1000000 becomes 1,000,000
89     var strRowSum = rowsSum + "";
90     var regex = /(\d+)(\d{3})/;
91     while (regex.test(strRowSum)) {
92         strRowSum = strRowSum.replace(regex, '$1' + ',' + '$2');
93     }
94     // If approximated total value add ~ in front
95     if (rowSumApproximated) {
96         strRowSum = "~" + strRowSum;
97     }
98     // Calculate the magnitude for the size and overhead values
99     var size_magnitude = 0, overhead_magnitude = 0;
100     while (sizeSum >= 1024) {
101         sizeSum /= 1024;
102         size_magnitude++;
103     }
104     while (overheadSum >= 1024) {
105         overheadSum /= 1024;
106         overhead_magnitude++;
107     }
109     sizeSum = Math.round(sizeSum * 10) / 10;
110     overheadSum = Math.round(overheadSum * 10) / 10;
112     // Update summary with new data
113     var $summary = $("#tbl_summary_row");
114     $summary.find('.tbl_num').text($.sprintf(PMA_messages['strTables'], tableSum));
115     $summary.find('.tbl_rows').text(strRowSum);
116     $summary.find('.tbl_size').text(sizeSum + " " + byteUnits[size_magnitude]);
117     $summary.find('.tbl_overhead').text(overheadSum + " " + byteUnits[overhead_magnitude]);
120 $(document).ready(function() {
121     /**
122      * Ajax Event handler for 'Insert Table'
123      *
124      * @uses    PMA_ajaxShowMessage()
125      * @see     $cfg['AjaxEnable']
126      */
127     var current_insert_table;
128     $("td.insert_table a.ajax").live('click', function(event){
129         event.preventDefault();
130         current_insert_table = $(this);
131         var $url = $(this).attr("href");
132         if ($url.substring(0, 15) == "tbl_change.php?") {
133             $url = $url.substring(15);
134         }
136         if ($("#insert_table_dialog").length > 0) {
137             $("#insert_table_dialog").remove();
138         }
139            var $div = $('<div id="insert_table_dialog"></div>');
140            var target = "tbl_change.php";
142         /**
143          *  @var    button_options  Object that stores the options passed to jQueryUI
144          *                          dialog
145          */
146         var button_options = {};
147         // in the following function we need to use $(this)
148         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
150         var button_options_error = {};
151         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
153         var $msgbox = PMA_ajaxShowMessage();
155         $.get( target , $url+"&ajax_request=true" ,  function(data) {
156             //in the case of an error, show the error message returned.
157             if (data.success != undefined && data.success == false) {
158                 $div
159                 .append(data.error)
160                 .dialog({
161                     title: PMA_messages['strInsertTable'],
162                     height: 230,
163                     width: 900,
164                     modal: true,
165                     open: PMA_verifyColumnsProperties,
166                     buttons : button_options_error
167                 })// end dialog options
168             } else {
169                 var $dialog = $div
170                     .append(data)
171                     .dialog({
172                         title: PMA_messages['strInsertTable'],
173                         height: 600,
174                         width: 900,
175                         modal: true,
176                         open: PMA_verifyColumnsProperties,
177                         buttons : button_options
178                     });// end dialog options
179                 //Remove the top menu container from the dialog
180                 $dialog.find("#topmenucontainer").hide();
181                 //Adding the datetime pikers for the dialog
182                 $dialog.find('.datefield, .datetimefield').each(function () {
183                     PMA_addDatepicker($(this));
184                 });
185                 $(".insertRowTable").addClass("ajax");
186                 $("#buttonYes").addClass("ajax");
187                 $div = $("#insert_table_dialog");
188                 PMA_convertFootnotesToTooltips($div);
189             }
190             PMA_ajaxRemoveMessage($msgbox);
191         }) // end $.get()
193     });
195     $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function(event) {
196         event.preventDefault();
197         /**
198          *  @var    the_form    object referring to the insert form
199          */
200         var $form = $("#insertForm");
201         $("#result_query").remove();
202         PMA_prepareForAjaxRequest($form);
203         //User wants to submit the form
204         $.post($form.attr('action'), $form.serialize() , function(data) {
205             if(data.success == true) {
206                 PMA_ajaxShowMessage(data.message);
207             } else {
208                 PMA_ajaxShowMessage(data.error, false);
209             }
210             if ($("#insert_table_dialog").length > 0) {
211                 $("#insert_table_dialog").dialog("close").remove();
212             }
213             /**Update the row count at the tableForm*/
214             current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
215             PMA_adjustTotals();
216         }) // end $.post()
217     }) // end insert table button "Go"
219     $("#buttonYes.ajax").live('click', function(event){
220         event.preventDefault();
221         /**
222          *  @var    the_form    object referring to the insert form
223          */
224         var $form = $("#insertForm");
225         /**Get the submit type and the after insert type in the form*/
226         var selected_submit_type = $("#insertForm").find("#actions_panel .control_at_footer option:selected").attr('value');
227         var selected_after_insert = $("#insertForm").find("#actions_panel select[name=after_insert] option:selected").attr('value');
228         $("#result_query").remove();
229         PMA_prepareForAjaxRequest($form);
230         //User wants to submit the form
231         $.post($form.attr('action'), $form.serialize() , function(data) {
232             if(data.success == true) {
233                 PMA_ajaxShowMessage(data.message);
234                 if (selected_submit_type == "showinsert") {
235                     $(data.sql_query).insertAfter("#floating_menubar");
236                     $("#result_query .notice").remove();
237                     $("#result_query").prepend((data.message));
238                 }
239                 if (selected_after_insert == "new_insert") {
240                     /**Trigger the insert dialog for new_insert option*/
241                     current_insert_table.trigger('click');
242                 }
244             } else {
245                 PMA_ajaxShowMessage(data.error, false);
246             }
247             if ($("#insert_table_dialog").length > 0) {
248                 $("#insert_table_dialog").dialog("close").remove();
249             }
250             /**Update the row count at the tableForm*/
251             current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
252             PMA_adjustTotals();
253         }) // end $.post()
254     });
256     /**
257      * Ajax Event handler for 'Truncate Table'
258      *
259      * @uses    $.PMA_confirm()
260      * @uses    PMA_ajaxShowMessage()
261      * @see     $cfg['AjaxEnable']
262      */
263     $(".truncate_table_anchor").live('click', function(event) {
264         event.preventDefault();
266         /**
267          * @var $this_anchor Object  referring to the anchor clicked
268          */
269         var $this_anchor = $(this);
271         //extract current table name and build the question string
272         /**
273          * @var curr_table_name String containing the name of the table to be truncated
274          */
275         var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
276         /**
277          * @var question    String containing the question to be asked for confirmation
278          */
279         var question = 'TRUNCATE ' + curr_table_name;
281         $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
283             PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
285             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
286                 if (data.success == true) {
287                     PMA_ajaxShowMessage(data.message);
288                     // Adjust table statistics
289                     var $tr = $this_anchor.closest('tr');
290                     $tr.find('.tbl_rows').text('0');
291                     $tr.find('.tbl_size, .tbl_overhead').text('-');
292                     //Fetch inner span of this anchor
293                     //and replace the icon with its disabled version
294                     var span = $this_anchor.html().replace(/b_empty/, 'bd_empty');
295                     //To disable further attempts to truncate the table,
296                     //replace the a element with its inner span (modified)
297                     $this_anchor
298                         .replaceWith(span)
299                         .removeClass('truncate_table_anchor');
300                     PMA_adjustTotals();
301                 } else {
302                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
303                 }
304             }) // end $.get()
305         }) //end $.PMA_confirm()
306     }); //end of Truncate Table Ajax action
308     /**
309      * Ajax Event handler for 'Drop Table'
310      *
311      * @uses    $.PMA_confirm()
312      * @uses    PMA_ajaxShowMessage()
313      * @see     $cfg['AjaxEnable']
314      */
315     $(".drop_table_anchor").live('click', function(event) {
316         event.preventDefault();
318         var $this_anchor = $(this);
320         //extract current table name and build the question string
321         /**
322          * @var $curr_row    Object containing reference to the current row
323          */
324         var $curr_row = $this_anchor.parents('tr');
325         /**
326          * @var curr_table_name String containing the name of the table to be truncated
327          */
328         var curr_table_name = $curr_row.children('th').children('a').text();
329         /**
330          * @var question    String containing the question to be asked for confirmation
331          */
332         var question = 'DROP TABLE ' + curr_table_name;
334         $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
336             PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
338             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
339                 if (data.success == true) {
340                     PMA_ajaxShowMessage(data.message);
341                     toggleRowColors($curr_row.next());
342                     $curr_row.hide("medium").remove();
343                     PMA_adjustTotals();
345                     if (window.parent && window.parent.frame_navigation) {
346                         window.parent.frame_navigation.location.reload();
347                     }
348                 } else {
349                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
350                 }
351             }); // end $.get()
352         }); // end $.PMA_confirm()
353     }); //end of Drop Table Ajax action
355     /**
356      * Ajax Event handler for 'Drop tracking'
357      *
358      * @uses    $.PMA_confirm()
359      * @uses    PMA_ajaxShowMessage()
360      * @see     $cfg['AjaxEnable']
361      */
362     $('.drop_tracking_anchor').live('click', function(event) {
363         event.preventDefault();
365         var $anchor = $(this);
367         /**
368          * @var curr_tracking_row   Object containing reference to the current tracked table's row
369          */
370         var $curr_tracking_row = $anchor.parents('tr');
371          /**
372          * @var question    String containing the question to be asked for confirmation
373          */
374         var question = PMA_messages['strDeleteTrackingData'];
376         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
378             PMA_ajaxShowMessage(PMA_messages['strDeletingTrackingData']);
380             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
381                 if(data.success == true) {
382                     var $tracked_table = $curr_tracking_row.parents('table');
383                     var table_name = $curr_tracking_row.find('td:nth-child(2)').text();
385                     // Check how many rows will be left after we remove
386                     if ($tracked_table.find('tbody tr').length === 1) {
387                         // We are removing the only row it has
388                         $('#tracked_tables').hide("slow").remove();
389                     } else {
390                         // There are more rows left after the deletion
391                         toggleRowColors($curr_tracking_row.next());
392                         $curr_tracking_row.hide("slow", function() {
393                             $(this).remove();
394                         });
395                     }
397                     // Make the removed table visible in the list of 'Untracked tables'.
398                     $untracked_table = $('table#noversions');
400                     // This won't work if no untracked tables are there.
401                     if ($untracked_table.length > 0) {
402                         var $rows = $untracked_table.find('tbody tr');
404                         $rows.each(function(index) {
405                             var $row = $(this);
406                             var tmp_tbl_name = $row.find('td:first-child').text();
407                             var is_last_iteration = (index == ($rows.length - 1));
409                             if (tmp_tbl_name > table_name || is_last_iteration) {
410                                 var $cloned = $row.clone();
412                                 // Change the table name of the cloned row.
413                                 $cloned.find('td:first-child').text(table_name);
415                                 // Change the link of the cloned row.
416                                 var new_url = $cloned
417                                     .find('td:nth-child(2) a')
418                                     .attr('href')
419                                     .replace('table=' + tmp_tbl_name, 'table=' + encodeURIComponent(table_name));
420                                 $cloned.find('td:nth-child(2) a').attr('href', new_url);
422                                 // Insert the cloned row in an appropriate location.
423                                 if (tmp_tbl_name > table_name) {
424                                     $cloned.insertBefore($row);
425                                     toggleRowColors($row);
426                                     return false;
427                                 } else {
428                                     $cloned.insertAfter($row);
429                                     toggleRowColors($cloned);
430                                 }
431                             }
432                         });
433                     }
435                     PMA_ajaxShowMessage(data.message);
436                 } else {
437                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
438                 }
439             }); // end $.get()
440         }); // end $.PMA_confirm()
441     }); //end Drop Tracking
443     //Calculate Real End for InnoDB
444     /**
445      * Ajax Event handler for calculatig the real end for a InnoDB table
446      *
447      * @uses    $.PMA_confirm
448      */
449     $('#real_end_input').live('click', function(event) {
450         event.preventDefault();
452         /**
453          * @var question    String containing the question to be asked for confirmation
454          */
455         var question = PMA_messages['strOperationTakesLongTime'];
457         $(this).PMA_confirm(question, '', function() {
458             return true;
459         })
460         return false;
461     }) //end Calculate Real End for InnoDB
463 }, 'top.frame_content'); // end $(document).ready()