Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / js / db_structure.js
blobbb1e38e95956ef63292a3811bd6e7ec309e490b2
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                     $curr_row.hide("medium").remove();
342                     PMA_adjustTotals();
344                     if (window.parent && window.parent.frame_navigation) {
345                         window.parent.frame_navigation.location.reload();
346                     }
347                 } else {
348                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
349                 }
350             }); // end $.get()
351         }); // end $.PMA_confirm()
352     }); //end of Drop Table Ajax action
354     /**
355      * Ajax Event handler for 'Drop tracking'
356      *
357      * @uses    $.PMA_confirm()
358      * @uses    PMA_ajaxShowMessage()
359      * @see     $cfg['AjaxEnable']
360      */
361     $('.drop_tracking_anchor').live('click', function(event) {
362         event.preventDefault();
364         var $anchor = $(this);
366         /**
367          * @var curr_tracking_row   Object containing reference to the current tracked table's row
368          */
369         var curr_tracking_row = $anchor.parents('tr');
370          /**
371          * @var question    String containing the question to be asked for confirmation
372          */
373         var question = PMA_messages['strDeleteTrackingData'];
375         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
377             PMA_ajaxShowMessage(PMA_messages['strDeletingTrackingData']);
379             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
380                 if(data.success == true) {
381                     PMA_ajaxShowMessage(data.message);
382                     $(curr_tracking_row).hide("medium").remove();
383                 }
384                 else {
385                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
386                 }
387             }) // end $.get()
388         }) // end $.PMA_confirm()
389     }) //end Drop Tracking
391     //Calculate Real End for InnoDB
392     /**
393      * Ajax Event handler for calculatig the real end for a InnoDB table
394      *
395      * @uses    $.PMA_confirm
396      */
397     $('#real_end_input').live('click', function(event) {
398         event.preventDefault();
400         /**
401          * @var question    String containing the question to be asked for confirmation
402          */
403         var question = PMA_messages['strOperationTakesLongTime'];
405         $(this).PMA_confirm(question, '', function() {
406             return true;
407         })
408         return false;
409     }) //end Calculate Real End for InnoDB
411 }, 'top.frame_content'); // end $(document).ready()