1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used on the database structure page
4 * @name Database Structure
8 * @required js/functions.js
12 * AJAX scripts for db_structure.php
14 * Actions ajaxified here:
22 * Adjust number of rows and total size in the summary
23 * when truncating, creating, dropping or inserting into a table
25 function PMA_adjustTotals() {
26 var byteUnits = new Array(
28 PMA_messages['strKiB'],
29 PMA_messages['strMiB'],
30 PMA_messages['strGiB'],
31 PMA_messages['strTiB'],
32 PMA_messages['strPiB'],
33 PMA_messages['strEiB']
36 * @var $allTr jQuery object that references all the rows in the list of tables
38 var $allTr = $("#tablesForm table.data tbody:first tr");
39 // New summary values for the table
40 var tableSum = $allTr.size();
44 var rowSumApproximated = false;
46 $allTr.each(function () {
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);
56 strRows = strRows.replace(/[,.]/g , '');
57 var intRow = parseInt(strRows, 10);
58 if (! isNaN(intRow)) {
61 // Extract the size and overhead
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);
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);
85 overheadSum += valOverhead;
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');
94 // If approximated total value add ~ in front
95 if (rowSumApproximated) {
96 strRowSum = "~" + strRowSum;
98 // Calculate the magnitude for the size and overhead values
99 var size_magnitude = 0, overhead_magnitude = 0;
100 while (sizeSum >= 1024) {
104 while (overheadSum >= 1024) {
106 overhead_magnitude++;
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() {
122 * Ajax Event handler for 'Insert Table'
124 * @uses PMA_ajaxShowMessage()
125 * @see $cfg['AjaxEnable']
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);
136 if ($("#insert_table_dialog").length > 0) {
137 $("#insert_table_dialog").remove();
139 var $div = $('<div id="insert_table_dialog"></div>');
140 var target = "tbl_change.php";
143 * @var button_options Object that stores the options passed to jQueryUI
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) {
161 title: PMA_messages['strInsertTable'],
165 open: PMA_verifyColumnsProperties,
166 buttons : button_options_error
167 })// end dialog options
172 title: PMA_messages['strInsertTable'],
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));
185 $(".insertRowTable").addClass("ajax");
186 $("#buttonYes").addClass("ajax");
187 $div = $("#insert_table_dialog");
188 PMA_convertFootnotesToTooltips($div);
190 PMA_ajaxRemoveMessage($msgbox);
195 $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function(event) {
196 event.preventDefault();
198 * @var the_form object referring to the insert form
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);
208 PMA_ajaxShowMessage(data.error, false);
210 if ($("#insert_table_dialog").length > 0) {
211 $("#insert_table_dialog").dialog("close").remove();
213 /**Update the row count at the tableForm*/
214 current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
217 }) // end insert table button "Go"
219 $("#buttonYes.ajax").live('click', function(event){
220 event.preventDefault();
222 * @var the_form object referring to the insert form
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));
239 if (selected_after_insert == "new_insert") {
240 /**Trigger the insert dialog for new_insert option*/
241 current_insert_table.trigger('click');
245 PMA_ajaxShowMessage(data.error, false);
247 if ($("#insert_table_dialog").length > 0) {
248 $("#insert_table_dialog").dialog("close").remove();
250 /**Update the row count at the tableForm*/
251 current_insert_table.closest('tr').find('.value.tbl_rows').html(data.row_count);
257 * Ajax Event handler for 'Truncate Table'
259 * @uses $.PMA_confirm()
260 * @uses PMA_ajaxShowMessage()
261 * @see $cfg['AjaxEnable']
263 $(".truncate_table_anchor").live('click', function(event) {
264 event.preventDefault();
267 * @var $this_anchor Object referring to the anchor clicked
269 var $this_anchor = $(this);
271 //extract current table name and build the question string
273 * @var curr_table_name String containing the name of the table to be truncated
275 var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
277 * @var question String containing the question to be asked for confirmation
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)
299 .removeClass('truncate_table_anchor');
302 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
305 }) //end $.PMA_confirm()
306 }); //end of Truncate Table Ajax action
309 * Ajax Event handler for 'Drop Table'
311 * @uses $.PMA_confirm()
312 * @uses PMA_ajaxShowMessage()
313 * @see $cfg['AjaxEnable']
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
322 * @var $curr_row Object containing reference to the current row
324 var $curr_row = $this_anchor.parents('tr');
326 * @var curr_table_name String containing the name of the table to be truncated
328 var curr_table_name = $curr_row.children('th').children('a').text();
330 * @var question String containing the question to be asked for confirmation
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();
345 if (window.parent && window.parent.frame_navigation) {
346 window.parent.frame_navigation.location.reload();
349 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
352 }); // end $.PMA_confirm()
353 }); //end of Drop Table Ajax action
356 * Ajax Event handler for 'Drop tracking'
358 * @uses $.PMA_confirm()
359 * @uses PMA_ajaxShowMessage()
360 * @see $cfg['AjaxEnable']
362 $('.drop_tracking_anchor').live('click', function(event) {
363 event.preventDefault();
365 var $anchor = $(this);
368 * @var curr_tracking_row Object containing reference to the current tracked table's row
370 var $curr_tracking_row = $anchor.parents('tr');
372 * @var question String containing the question to be asked for confirmation
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();
390 // There are more rows left after the deletion
391 toggleRowColors($curr_tracking_row.next());
392 $curr_tracking_row.hide("slow", function() {
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) {
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')
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);
428 $cloned.insertAfter($row);
429 toggleRowColors($cloned);
435 PMA_ajaxShowMessage(data.message);
437 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
440 }); // end $.PMA_confirm()
441 }); //end Drop Tracking
443 //Calculate Real End for InnoDB
445 * Ajax Event handler for calculatig the real end for a InnoDB table
447 * @uses $.PMA_confirm
449 $('#real_end_input').live('click', function(event) {
450 event.preventDefault();
453 * @var question String containing the question to be asked for confirmation
455 var question = PMA_messages['strOperationTakesLongTime'];
457 $(this).PMA_confirm(question, '', function() {
461 }) //end Calculate Real End for InnoDB
463 }, 'top.frame_content'); // end $(document).ready()