Translated using Weblate (Slovenian)
[phpmyadmin.git] / js / db_structure.js
blob6f991ae9fda7b0cc9bbce94b8360c0bcc5827cf7
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  * Unbind all event handlers before tearing down a page
23  */
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');
33 });
35 /**
36  * Adjust number of rows and total size in the summary
37  * when truncating, creating, dropping or inserting into a table
38  */
39 function PMA_adjustTotals() {
40     var byteUnits = new Array(
41         PMA_messages.strB,
42         PMA_messages.strKiB,
43         PMA_messages.strMiB,
44         PMA_messages.strGiB,
45         PMA_messages.strTiB,
46         PMA_messages.strPiB,
47         PMA_messages.strEiB
48     );
49     /**
50      * @var $allTr jQuery object that references all the rows in the list of tables
51      */
52     var $allTr = $("#tablesForm table.data tbody:first tr");
53     // New summary values for the table
54     var tableSum = $allTr.size();
55     var rowsSum = 0;
56     var sizeSum = 0;
57     var overheadSum = 0;
58     var rowSumApproximated = false;
60     $allTr.each(function () {
61         var $this = $(this);
62         var i, tmpVal;
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);
70         }
71         strRows = strRows.replace(/[,.]/g, '');
72         var intRow = parseInt(strRows, 10);
73         if (! isNaN(intRow)) {
74             rowsSum += intRow;
75         }
76         // Extract the size and overhead
77         var valSize         = 0;
78         var valOverhead     = 0;
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);
89                 break;
90             }
91         }
92         for (i = 0; i < byteUnits.length; i++) {
93             if (strOverheadUnit == byteUnits[i]) {
94                 tmpVal = parseFloat(strOverhead);
95                 valOverhead = tmpVal * Math.pow(1024, i);
96                 break;
97             }
98         }
99         sizeSum += valSize;
100         overheadSum += valOverhead;
101     });
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');
108     }
109     // If approximated total value add ~ in front
110     if (rowSumApproximated) {
111         strRowSum = "~" + strRowSum;
112     }
113     // Calculate the magnitude for the size and overhead values
114     var size_magnitude = 0, overhead_magnitude = 0;
115     while (sizeSum >= 1024) {
116         sizeSum /= 1024;
117         size_magnitude++;
118     }
119     while (overheadSum >= 1024) {
120         overheadSum /= 1024;
121         overhead_magnitude++;
122     }
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);
132     } else {
133         $summary.find('.tbl_rows').text(strRowSum);
134     }
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.
142  */
143 function PMA_fetchRealRowCount($target)
145     var $throbber = $('#pma_navigation .throbber')
146         .first()
147         .clone()
148         .css({visibility: 'visible', display: 'inline-block'})
149         .click(false);
150     $target.html($throbber);
151     $.ajax({
152         type: 'GET',
153         url: $target.attr('href'),
154         cache: false,
155         dataType: 'json',
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);
165                         }
166                     );
167                 }
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);
172                 }
173                 // Adjust the 'Sum' displayed at the bottom.
174                 PMA_adjustTotals();
175             } else {
176                 PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
177             }
178         },
179         error: function () {
180             PMA_ajaxShowMessage(PMA_messages.strErrorRealRowCount);
181         }
182     });
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
192  */
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 () {
198             success();
199             $( this ).dialog( "close" );
200         };
201         buttonOptions[PMA_messages.strCancel] = function () {
202             $( this ).dialog( "close" );
203             $('#tablesForm')[0].reset();
204         };
205         $(dialogObj).dialog({
206             resizable: false,
207             modal: true,
208             title: PMA_messages.confirmTitle,
209             buttons: buttonOptions
210         });
211     };
214  *  Event handler on select of "Make consistent with central list"
215  */
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();
222                     });
223             return false;
224         }
225     });
227     /**
228      * Ajax Event handler for 'Truncate Table'
229      */
230     $(document).on('click', "a.truncate_table_anchor.ajax", function (event) {
231         event.preventDefault();
233         /**
234          * @var $this_anchor Object  referring to the anchor clicked
235          */
236         var $this_anchor = $(this);
238         //extract current table name and build the question string
239         /**
240          * @var curr_table_name String containing the name of the table to be truncated
241          */
242         var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
243         /**
244          * @var question    String containing the question to be asked for confirmation
245          */
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)
268                     $this_anchor
269                         .replaceWith(span)
270                         .removeClass('truncate_table_anchor');
271                     PMA_adjustTotals();
272                 } else {
273                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
274                 }
275             }); // end $.get()
276         }, loadForeignKeyCheckbox); //end $.PMA_confirm()
277     }); //end of Truncate Table Ajax action
279     /**
280      * Ajax Event handler for 'Drop Table' or 'Drop View'
281      */
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
288         /**
289          * @var $curr_row    Object containing reference to the current row
290          */
291         var $curr_row = $this_anchor.parents('tr');
292         /**
293          * @var curr_table_name String containing the name of the table to be truncated
294          */
295         var curr_table_name = $curr_row.children('th').children('a').text();
296         /**
297          * @var is_view Boolean telling if we have a view
298          */
299         var is_view = $curr_row.hasClass('is_view') || $this_anchor.hasClass('view');
300         /**
301          * @var question    String containing the question to be asked for confirmation
302          */
303         var question;
304         if (! is_view) {
305             question = PMA_messages.strDropTableStrongWarning + ' ' +
306                 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP TABLE ' + escapeHtml(curr_table_name));
307         } else {
308             question =
309                 PMA_sprintf(PMA_messages.strDoYouReally, 'DROP VIEW ' + escapeHtml(curr_table_name));
310         }
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();
324                     PMA_adjustTotals();
325                     PMA_reloadNavigation();
326                     PMA_ajaxRemoveMessage($msg);
327                 } else {
328                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
329                 }
330             }); // end $.get()
331         }, loadForeignKeyCheckbox); // end $.PMA_confirm()
332     }); //end of Drop Table Ajax action
334     /**
335      * Attach Event Handler for 'Print View'
336      */
337     $(document).on('click', "#printView", function (event) {
338         event.preventDefault();
340         // Print the page
341         printPage();
342     }); //end of Print View action
344     //Calculate Real End for InnoDB
345     /**
346      * Ajax Event handler for calculating the real end for a InnoDB table
347      *
348      */
349     $(document).on('click', '#real_end_input', function (event) {
350         event.preventDefault();
352         /**
353          * @var question    String containing the question to be asked for confirmation
354          */
355         var question = PMA_messages.strOperationTakesLongTime;
357         $(this).PMA_confirm(question, '', function () {
358             return true;
359         });
360         return false;
361     }); //end Calculate Real End for InnoDB
363     // Add tooltip to favorite icons.
364     $(".favorite_table_anchor").each(function () {
365         PMA_tooltip(
366             $(this),
367             'a',
368             $(this).attr("title")
369         );
370     });
372     // Get real row count via Ajax.
373     $('a.real_row_count').on('click', function (event) {
374         event.preventDefault();
375         PMA_fetchRealRowCount($(this));
376     });
377     // Get all real row count.
378     $('a.row_count_sum').on('click', function (event) {
379         event.preventDefault();
380         PMA_fetchRealRowCount($(this));
381     });
382 }); // end $()