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 $('a.real_row_count').off('click');
30 $('a.row_count_sum').off('click');
31 $('select[name=submit_mult]').unbind('change');
35 * Adjust number of rows and total size in the summary
36 * when truncating, creating, dropping or inserting into a table
38 function PMA_adjustTotals() {
39 var byteUnits = new Array(
49 * @var $allTr jQuery object that references all the rows in the list of tables
51 var $allTr = $("#tablesForm table.data tbody:first tr");
52 // New summary values for the table
53 var tableSum = $allTr.size();
57 var rowSumApproximated = false;
59 $allTr.each(function () {
62 // Get the number of rows for this SQL table
63 var strRows = $this.find('.tbl_rows').text();
64 // If the value is approximated
65 if (strRows.indexOf('~') === 0) {
66 rowSumApproximated = true;
67 // The approximated value contains a preceding ~ (Eg 100 --> ~100)
68 strRows = strRows.substring(1, strRows.length);
70 strRows = strRows.replace(/[,.]/g, '');
71 var intRow = parseInt(strRows, 10);
72 if (! isNaN(intRow)) {
75 // Extract the size and overhead
78 var strSize = $.trim($this.find('.tbl_size span:not(.unit)').text());
79 var strSizeUnit = $.trim($this.find('.tbl_size span.unit').text());
80 var strOverhead = $.trim($this.find('.tbl_overhead span:not(.unit)').text());
81 var strOverheadUnit = $.trim($this.find('.tbl_overhead span.unit').text());
82 // Given a value and a unit, such as 100 and KiB, for the table size
83 // and overhead calculate their numeric values in bytes, such as 102400
84 for (i = 0; i < byteUnits.length; i++) {
85 if (strSizeUnit == byteUnits[i]) {
86 tmpVal = parseFloat(strSize);
87 valSize = tmpVal * Math.pow(1024, i);
91 for (i = 0; i < byteUnits.length; i++) {
92 if (strOverheadUnit == byteUnits[i]) {
93 tmpVal = parseFloat(strOverhead);
94 valOverhead = tmpVal * Math.pow(1024, i);
99 overheadSum += valOverhead;
101 // Add some commas for readability:
102 // 1000000 becomes 1,000,000
103 var strRowSum = rowsSum + "";
104 var regex = /(\d+)(\d{3})/;
105 while (regex.test(strRowSum)) {
106 strRowSum = strRowSum.replace(regex, '$1' + ',' + '$2');
108 // If approximated total value add ~ in front
109 if (rowSumApproximated) {
110 strRowSum = "~" + strRowSum;
112 // Calculate the magnitude for the size and overhead values
113 var size_magnitude = 0, overhead_magnitude = 0;
114 while (sizeSum >= 1024) {
118 while (overheadSum >= 1024) {
120 overhead_magnitude++;
123 sizeSum = Math.round(sizeSum * 10) / 10;
124 overheadSum = Math.round(overheadSum * 10) / 10;
126 // Update summary with new data
127 var $summary = $("#tbl_summary_row");
128 $summary.find('.tbl_num').text(PMA_sprintf(PMA_messages.strNTables, tableSum));
129 $summary.find('.row_count_sum').text(strRowSum);
130 $summary.find('.tbl_size').text(sizeSum + " " + byteUnits[size_magnitude]);
131 $summary.find('.tbl_overhead').text(overheadSum + " " + byteUnits[overhead_magnitude]);
135 * Gets the real row count for a table or DB.
136 * @param object $target Target for appending the real count value.
138 function PMA_fetchRealRowCount($target)
140 var $throbber = $('#pma_navigation .throbber')
143 .css({visibility: 'visible', display: 'inline-block'})
145 $target.html($throbber);
148 url: $target.attr('href'),
151 success: function (response) {
152 if (response.success) {
153 // If to update all row counts for a DB.
154 if (response.real_row_count_all) {
155 $.each(JSON.parse(response.real_row_count_all),
156 function (index, table) {
157 // Update each table row count.
158 $('table.data td[data-table*="' + table.table + '"]')
159 .text(table.row_count);
163 // If to update a particular table's row count.
164 if (response.real_row_count) {
165 // Append the parent cell with real row count.
166 $target.parent().text(response.real_row_count);
168 // Adjust the 'Sum' displayed at the bottom.
171 PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
175 PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
180 AJAX.registerOnload('db_structure.js', function () {
182 * Handler for the print view multisubmit.
183 * All other multi submits can be handled via ajax, but this one needs
184 * special treatment as the results need to open in another browser window
186 $('#tablesForm').submit(function (event) {
188 if ($form.find('select[name=submit_mult]').val() === 'print') {
189 event.preventDefault();
190 event.stopPropagation();
191 $('form#clone').remove();
197 .find('select[name=submit_mult]')
200 .attr('target', 'printview')
207 * function to open the confirmation dialog for making table consistent with central list
209 * @param string msg message text to be displayed to user
210 * @param function success function to be called on success
213 var jqConfirm = function(msg, success) {
214 var dialogObj = $("<div style='display:none'>"+msg+"</div>");
215 $('body').append(dialogObj);
216 var buttonOptions = {};
217 buttonOptions[PMA_messages.strContinue] = function () {
219 $( this ).dialog( "close" );
221 buttonOptions[PMA_messages.strCancel] = function () {
222 $( this ).dialog( "close" );
223 $('#tablesForm')[0].reset();
225 $(dialogObj).dialog({
228 title: PMA_messages.confirmTitle,
229 buttons: buttonOptions
234 * Event handler on select of "Make consistent with central list"
236 $('select[name=submit_mult]').change(function(event) {
237 if($(this).val() === 'make_consistent_with_central_list') {
238 event.preventDefault();
239 event.stopPropagation();
240 jqConfirm(PMA_messages.makeConsistentMessage, function(){
241 $('#tablesForm').submit();
248 * Ajax Event handler for 'Truncate Table'
250 $(document).on('click', "a.truncate_table_anchor.ajax", function (event) {
251 event.preventDefault();
254 * @var $this_anchor Object referring to the anchor clicked
256 var $this_anchor = $(this);
258 //extract current table name and build the question string
260 * @var curr_table_name String containing the name of the table to be truncated
262 var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
264 * @var question String containing the question to be asked for confirmation
266 var question = PMA_messages.strTruncateTableStrongWarning + ' ' +
267 PMA_sprintf(PMA_messages.strDoYouReally, 'TRUNCATE ' + escapeHtml(curr_table_name));
269 $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
271 PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
273 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function (data) {
274 if (typeof data !== 'undefined' && data.success === true) {
275 PMA_ajaxShowMessage(data.message);
276 // Adjust table statistics
277 var $tr = $this_anchor.closest('tr');
278 $tr.find('.tbl_rows').text('0');
279 $tr.find('.tbl_size, .tbl_overhead').text('-');
280 //Fetch inner span of this anchor
281 //and replace the icon with its disabled version
282 var span = $this_anchor.html().replace(/b_empty/, 'bd_empty');
283 //To disable further attempts to truncate the table,
284 //replace the a element with its inner span (modified)
287 .removeClass('truncate_table_anchor');
290 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
293 }); //end $.PMA_confirm()
294 }); //end of Truncate Table Ajax action
297 * Ajax Event handler for 'Drop Table' or 'Drop View'
299 $(document).on('click', "a.drop_table_anchor.ajax", function (event) {
300 event.preventDefault();
302 var $this_anchor = $(this);
304 //extract current table name and build the question string
306 * @var $curr_row Object containing reference to the current row
308 var $curr_row = $this_anchor.parents('tr');
310 * @var curr_table_name String containing the name of the table to be truncated
312 var curr_table_name = $curr_row.children('th').children('a').text();
314 * @var is_view Boolean telling if we have a view
316 var is_view = $curr_row.hasClass('is_view') || $this_anchor.hasClass('view');
318 * @var question String containing the question to be asked for confirmation
322 question = PMA_messages.strDropTableStrongWarning + ' ' +
323 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP TABLE ' + escapeHtml(curr_table_name));
326 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP VIEW ' + escapeHtml(curr_table_name));
329 $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
331 var $msg = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
333 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function (data) {
334 if (typeof data !== 'undefined' && data.success === true) {
335 PMA_ajaxShowMessage(data.message);
336 toggleRowColors($curr_row.next());
337 $curr_row.hide("medium").remove();
339 PMA_reloadNavigation();
340 PMA_ajaxRemoveMessage($msg);
342 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
345 }); // end $.PMA_confirm()
346 }); //end of Drop Table Ajax action
348 //Calculate Real End for InnoDB
350 * Ajax Event handler for calculating the real end for a InnoDB table
353 $(document).on('click', '#real_end_input', function (event) {
354 event.preventDefault();
357 * @var question String containing the question to be asked for confirmation
359 var question = PMA_messages.strOperationTakesLongTime;
361 $(this).PMA_confirm(question, '', function () {
365 }); //end Calculate Real End for InnoDB
368 $("select[name*='funcs']"),
370 PMA_messages.strFunctionHint
372 // Add tooltip to favorite icons.
373 $(".favorite_table_anchor").each(function () {
377 $(this).attr("title")
381 // Get real row count via Ajax.
382 $('a.real_row_count').on('click', function (event) {
383 event.preventDefault();
384 PMA_fetchRealRowCount($(this));
386 // Get all real row count.
387 $('a.row_count_sum').on('click', function (event) {
388 event.preventDefault();
389 PMA_fetchRealRowCount($(this));