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 * Unbind all event handlers before tearing down a page
24 AJAX.registerTeardown('db_structure.js', function () {
25 $(document).off('click', "a.truncate_table_anchor.ajax");
26 $(document).off('click', "a.drop_table_anchor.ajax");
27 $(document).off('click', '#real_end_input');
28 $(document).off('click', "a.favorite_table_anchor.ajax");
29 $(document).off('click', '#printView');
30 $('a.real_row_count').off('click');
31 $('a.row_count_sum').off('click');
32 $('select[name=submit_mult]').unbind('change');
36 * Adjust number of rows and total size in the summary
37 * when truncating, creating, dropping or inserting into a table
39 function PMA_adjustTotals() {
40 var byteUnits = new Array(
50 * @var $allTr jQuery object that references all the rows in the list of tables
52 var $allTr = $("#tablesForm").find("table.data tbody:first tr");
53 // New summary values for the table
54 var tableSum = $allTr.size();
58 var rowSumApproximated = false;
60 $allTr.each(function () {
63 // Get the number of rows for this SQL table
64 var strRows = $this.find('.tbl_rows').text();
65 // If the value is approximated
66 if (strRows.indexOf('~') === 0) {
67 rowSumApproximated = true;
68 // The approximated value contains a preceding ~ (Eg 100 --> ~100)
69 strRows = strRows.substring(1, strRows.length);
71 strRows = strRows.replace(/[,.]/g, '');
72 var intRow = parseInt(strRows, 10);
73 if (! isNaN(intRow)) {
76 // Extract the size and overhead
79 var strSize = $.trim($this.find('.tbl_size span:not(.unit)').text());
80 var strSizeUnit = $.trim($this.find('.tbl_size span.unit').text());
81 var strOverhead = $.trim($this.find('.tbl_overhead span:not(.unit)').text());
82 var strOverheadUnit = $.trim($this.find('.tbl_overhead span.unit').text());
83 // Given a value and a unit, such as 100 and KiB, for the table size
84 // and overhead calculate their numeric values in bytes, such as 102400
85 for (i = 0; i < byteUnits.length; i++) {
86 if (strSizeUnit == byteUnits[i]) {
87 tmpVal = parseFloat(strSize);
88 valSize = tmpVal * Math.pow(1024, i);
92 for (i = 0; i < byteUnits.length; i++) {
93 if (strOverheadUnit == byteUnits[i]) {
94 tmpVal = parseFloat(strOverhead);
95 valOverhead = tmpVal * Math.pow(1024, i);
100 overheadSum += valOverhead;
102 // Add some commas for readability:
103 // 1000000 becomes 1,000,000
104 var strRowSum = rowsSum + "";
105 var regex = /(\d+)(\d{3})/;
106 while (regex.test(strRowSum)) {
107 strRowSum = strRowSum.replace(regex, '$1' + ',' + '$2');
109 // If approximated total value add ~ in front
110 if (rowSumApproximated) {
111 strRowSum = "~" + strRowSum;
113 // Calculate the magnitude for the size and overhead values
114 var size_magnitude = 0, overhead_magnitude = 0;
115 while (sizeSum >= 1024) {
119 while (overheadSum >= 1024) {
121 overhead_magnitude++;
124 sizeSum = Math.round(sizeSum * 10) / 10;
125 overheadSum = Math.round(overheadSum * 10) / 10;
127 // Update summary with new data
128 var $summary = $("#tbl_summary_row");
129 $summary.find('.tbl_num').text(PMA_sprintf(PMA_messages.strNTables, tableSum));
130 if (rowSumApproximated) {
131 $summary.find('.row_count_sum').text(strRowSum);
133 $summary.find('.tbl_rows').text(strRowSum);
135 $summary.find('.tbl_size').text(sizeSum + " " + byteUnits[size_magnitude]);
136 $summary.find('.tbl_overhead').text(overheadSum + " " + byteUnits[overhead_magnitude]);
140 * Gets the real row count for a table or DB.
141 * @param object $target Target for appending the real count value.
143 function PMA_fetchRealRowCount($target)
145 var $throbber = $('#pma_navigation').find('.throbber')
148 .css({visibility: 'visible', display: 'inline-block'})
150 $target.html($throbber);
153 url: $target.attr('href'),
156 success: function (response) {
157 if (response.success) {
158 // If to update all row counts for a DB.
159 if (response.real_row_count_all) {
160 $.each(JSON.parse(response.real_row_count_all),
161 function (index, table) {
162 // Update each table row count.
163 $('table.data td[data-table*="' + table.table + '"]')
164 .text(table.row_count);
168 // If to update a particular table's row count.
169 if (response.real_row_count) {
170 // Append the parent cell with real row count.
171 $target.parent().text(response.real_row_count);
173 // Adjust the 'Sum' displayed at the bottom.
176 PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
180 PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
185 AJAX.registerOnload('db_structure.js', function () {
187 * function to open the confirmation dialog for making table consistent with central list
189 * @param string msg message text to be displayed to user
190 * @param function success function to be called on success
193 var jqConfirm = function(msg, success) {
194 var dialogObj = $("<div style='display:none'>"+msg+"</div>");
195 $('body').append(dialogObj);
196 var buttonOptions = {};
197 buttonOptions[PMA_messages.strContinue] = function () {
199 $( this ).dialog( "close" );
201 buttonOptions[PMA_messages.strCancel] = function () {
202 $( this ).dialog( "close" );
203 $('#tablesForm')[0].reset();
205 $(dialogObj).dialog({
208 title: PMA_messages.confirmTitle,
209 buttons: buttonOptions
214 * Event handler on select of "Make consistent with central list"
216 $('select[name=submit_mult]').change(function(event) {
217 if($(this).val() === 'make_consistent_with_central_list') {
218 event.preventDefault();
219 event.stopPropagation();
220 jqConfirm(PMA_messages.makeConsistentMessage, function(){
221 $('#tablesForm').submit();
228 * Ajax Event handler for 'Truncate Table'
230 $(document).on('click', "a.truncate_table_anchor.ajax", function (event) {
231 event.preventDefault();
234 * @var $this_anchor Object referring to the anchor clicked
236 var $this_anchor = $(this);
238 //extract current table name and build the question string
240 * @var curr_table_name String containing the name of the table to be truncated
242 var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
244 * @var question String containing the question to be asked for confirmation
246 var question = PMA_messages.strTruncateTableStrongWarning + ' ' +
247 PMA_sprintf(PMA_messages.strDoYouReally, 'TRUNCATE ' + escapeHtml(curr_table_name)) +
248 getForeignKeyCheckboxLoader();
250 $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
252 PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
254 var params = getJSConfirmCommonParam(this);
256 $.get(url, params, function (data) {
257 if (typeof data !== 'undefined' && data.success === true) {
258 PMA_ajaxShowMessage(data.message);
259 // Adjust table statistics
260 var $tr = $this_anchor.closest('tr');
261 $tr.find('.tbl_rows').text('0');
262 $tr.find('.tbl_size, .tbl_overhead').text('-');
263 //Fetch inner span of this anchor
264 //and replace the icon with its disabled version
265 var span = $this_anchor.html().replace(/b_empty/, 'bd_empty');
266 //To disable further attempts to truncate the table,
267 //replace the a element with its inner span (modified)
270 .removeClass('truncate_table_anchor');
273 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
276 }, loadForeignKeyCheckbox); //end $.PMA_confirm()
277 }); //end of Truncate Table Ajax action
280 * Ajax Event handler for 'Drop Table' or 'Drop View'
282 $(document).on('click', "a.drop_table_anchor.ajax", function (event) {
283 event.preventDefault();
285 var $this_anchor = $(this);
287 //extract current table name and build the question string
289 * @var $curr_row Object containing reference to the current row
291 var $curr_row = $this_anchor.parents('tr');
293 * @var curr_table_name String containing the name of the table to be truncated
295 var curr_table_name = $curr_row.children('th').children('a').text();
297 * @var is_view Boolean telling if we have a view
299 var is_view = $curr_row.hasClass('is_view') || $this_anchor.hasClass('view');
301 * @var question String containing the question to be asked for confirmation
305 question = PMA_messages.strDropTableStrongWarning + ' ' +
306 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP TABLE ' + escapeHtml(curr_table_name));
309 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP VIEW ' + escapeHtml(curr_table_name));
311 question += getForeignKeyCheckboxLoader();
313 $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
315 var $msg = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
317 var params = getJSConfirmCommonParam(this);
319 $.get(url, params, function (data) {
320 if (typeof data !== 'undefined' && data.success === true) {
321 PMA_ajaxShowMessage(data.message);
322 toggleRowColors($curr_row.next());
323 $curr_row.hide("medium").remove();
325 PMA_reloadNavigation();
326 PMA_ajaxRemoveMessage($msg);
328 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
331 }, loadForeignKeyCheckbox); // end $.PMA_confirm()
332 }); //end of Drop Table Ajax action
335 * Attach Event Handler for 'Print View'
337 $(document).on('click', "#printView", function (event) {
338 event.preventDefault();
342 }); //end of Print View action
344 //Calculate Real End for InnoDB
346 * Ajax Event handler for calculating the real end for a InnoDB table
349 $(document).on('click', '#real_end_input', function (event) {
350 event.preventDefault();
353 * @var question String containing the question to be asked for confirmation
355 var question = PMA_messages.strOperationTakesLongTime;
357 $(this).PMA_confirm(question, '', function () {
361 }); //end Calculate Real End for InnoDB
363 // Add tooltip to favorite icons.
364 $(".favorite_table_anchor").each(function () {
368 $(this).attr("title")
372 // Get real row count via Ajax.
373 $('a.real_row_count').on('click', function (event) {
374 event.preventDefault();
375 PMA_fetchRealRowCount($(this));
377 // Get all real row count.
378 $('a.row_count_sum').on('click', function (event) {
379 event.preventDefault();
380 PMA_fetchRealRowCount($(this));