db_structure.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. */
  10. /**
  11. * AJAX scripts for db_structure.php
  12. *
  13. * Actions ajaxified here:
  14. * Drop Database
  15. * Truncate Table
  16. * Drop Table
  17. *
  18. */
  19. /**
  20. * Unbind all event handlers before tearing down a page
  21. */
  22. AJAX.registerTeardown('db_structure.js', function () {
  23. $("span.fkc_switch").unbind('click');
  24. $('#fkc_checkbox').unbind('change');
  25. $("a.truncate_table_anchor.ajax").die('click');
  26. $("a.drop_table_anchor.ajax").die('click');
  27. $('a.drop_tracking_anchor.ajax').die('click');
  28. $('#real_end_input').die('click');
  29. $("a.favorite_table_anchor.ajax").die('click');
  30. });
  31. /**
  32. * Adjust number of rows and total size in the summary
  33. * when truncating, creating, dropping or inserting into a table
  34. */
  35. function PMA_adjustTotals() {
  36. var byteUnits = new Array(
  37. PMA_messages.strB,
  38. PMA_messages.strKiB,
  39. PMA_messages.strMiB,
  40. PMA_messages.strGiB,
  41. PMA_messages.strTiB,
  42. PMA_messages.strPiB,
  43. PMA_messages.strEiB
  44. );
  45. /**
  46. * @var $allTr jQuery object that references all the rows in the list of tables
  47. */
  48. var $allTr = $("#tablesForm table.data tbody:first tr");
  49. // New summary values for the table
  50. var tableSum = $allTr.size();
  51. var rowsSum = 0;
  52. var sizeSum = 0;
  53. var overheadSum = 0;
  54. var rowSumApproximated = false;
  55. $allTr.each(function () {
  56. var $this = $(this);
  57. var i, tmpVal;
  58. // Get the number of rows for this SQL table
  59. var strRows = $this.find('.tbl_rows').text();
  60. // If the value is approximated
  61. if (strRows.indexOf('~') === 0) {
  62. rowSumApproximated = true;
  63. // The approximated value contains a preceding ~ (Eg 100 --> ~100)
  64. strRows = strRows.substring(1, strRows.length);
  65. }
  66. strRows = strRows.replace(/[,.]/g, '');
  67. var intRow = parseInt(strRows, 10);
  68. if (! isNaN(intRow)) {
  69. rowsSum += intRow;
  70. }
  71. // Extract the size and overhead
  72. var valSize = 0;
  73. var valOverhead = 0;
  74. var strSize = $.trim($this.find('.tbl_size span:not(.unit)').text());
  75. var strSizeUnit = $.trim($this.find('.tbl_size span.unit').text());
  76. var strOverhead = $.trim($this.find('.tbl_overhead span:not(.unit)').text());
  77. var strOverheadUnit = $.trim($this.find('.tbl_overhead span.unit').text());
  78. // Given a value and a unit, such as 100 and KiB, for the table size
  79. // and overhead calculate their numeric values in bytes, such as 102400
  80. for (i = 0; i < byteUnits.length; i++) {
  81. if (strSizeUnit == byteUnits[i]) {
  82. tmpVal = parseFloat(strSize);
  83. valSize = tmpVal * Math.pow(1024, i);
  84. break;
  85. }
  86. }
  87. for (i = 0; i < byteUnits.length; i++) {
  88. if (strOverheadUnit == byteUnits[i]) {
  89. tmpVal = parseFloat(strOverhead);
  90. valOverhead = tmpVal * Math.pow(1024, i);
  91. break;
  92. }
  93. }
  94. sizeSum += valSize;
  95. overheadSum += valOverhead;
  96. });
  97. // Add some commas for readablility:
  98. // 1000000 becomes 1,000,000
  99. var strRowSum = rowsSum + "";
  100. var regex = /(\d+)(\d{3})/;
  101. while (regex.test(strRowSum)) {
  102. strRowSum = strRowSum.replace(regex, '$1' + ',' + '$2');
  103. }
  104. // If approximated total value add ~ in front
  105. if (rowSumApproximated) {
  106. strRowSum = "~" + strRowSum;
  107. }
  108. // Calculate the magnitude for the size and overhead values
  109. var size_magnitude = 0, overhead_magnitude = 0;
  110. while (sizeSum >= 1024) {
  111. sizeSum /= 1024;
  112. size_magnitude++;
  113. }
  114. while (overheadSum >= 1024) {
  115. overheadSum /= 1024;
  116. overhead_magnitude++;
  117. }
  118. sizeSum = Math.round(sizeSum * 10) / 10;
  119. overheadSum = Math.round(overheadSum * 10) / 10;
  120. // Update summary with new data
  121. var $summary = $("#tbl_summary_row");
  122. $summary.find('.tbl_num').text($.sprintf(PMA_messages.strTables, tableSum));
  123. $summary.find('.tbl_rows').text(strRowSum);
  124. $summary.find('.tbl_size').text(sizeSum + " " + byteUnits[size_magnitude]);
  125. $summary.find('.tbl_overhead').text(overheadSum + " " + byteUnits[overhead_magnitude]);
  126. }
  127. AJAX.registerOnload('db_structure.js', function () {
  128. /**
  129. * Handler for the print view multisubmit.
  130. * All other multi submits can be handled via ajax, but this one needs
  131. * special treatment as the results need to open in another browser window
  132. */
  133. $('#tablesForm').submit(function (event) {
  134. var $form = $(this);
  135. if ($form.find('select[name=submit_mult]').val() === 'print') {
  136. event.preventDefault();
  137. event.stopPropagation();
  138. $('form#clone').remove();
  139. var $clone = $form
  140. .clone()
  141. .hide()
  142. .appendTo('body');
  143. $clone
  144. .find('select[name=submit_mult]')
  145. .val('print');
  146. $clone
  147. .attr('target', 'printview')
  148. .attr('id', 'clone')
  149. .submit();
  150. }
  151. });
  152. /**
  153. * Event handler for 'Foreign Key Checks' disabling option
  154. * in the drop table confirmation form
  155. */
  156. $("span.fkc_switch").click(function (event) {
  157. if ($("#fkc_checkbox").prop('checked')) {
  158. $("#fkc_checkbox").prop('checked', false);
  159. $("#fkc_status").html(PMA_messages.strForeignKeyCheckDisabled);
  160. return;
  161. }
  162. $("#fkc_checkbox").prop('checked', true);
  163. $("#fkc_status").html(PMA_messages.strForeignKeyCheckEnabled);
  164. });
  165. $('#fkc_checkbox').change(function () {
  166. if ($(this).prop("checked")) {
  167. $("#fkc_status").html(PMA_messages.strForeignKeyCheckEnabled);
  168. return;
  169. }
  170. $("#fkc_status").html(PMA_messages.strForeignKeyCheckDisabled);
  171. }); // End of event handler for 'Foreign Key Check'
  172. /**
  173. * Ajax Event handler for 'Truncate Table'
  174. */
  175. $("a.truncate_table_anchor.ajax").live('click', function (event) {
  176. event.preventDefault();
  177. /**
  178. * @var $this_anchor Object referring to the anchor clicked
  179. */
  180. var $this_anchor = $(this);
  181. //extract current table name and build the question string
  182. /**
  183. * @var curr_table_name String containing the name of the table to be truncated
  184. */
  185. var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
  186. /**
  187. * @var question String containing the question to be asked for confirmation
  188. */
  189. var question = PMA_messages.strTruncateTableStrongWarning + ' ' +
  190. $.sprintf(PMA_messages.strDoYouReally, 'TRUNCATE ' + escapeHtml(curr_table_name));
  191. $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
  192. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  193. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function (data) {
  194. if (data.success === true) {
  195. PMA_ajaxShowMessage(data.message);
  196. // Adjust table statistics
  197. var $tr = $this_anchor.closest('tr');
  198. $tr.find('.tbl_rows').text('0');
  199. $tr.find('.tbl_size, .tbl_overhead').text('-');
  200. //Fetch inner span of this anchor
  201. //and replace the icon with its disabled version
  202. var span = $this_anchor.html().replace(/b_empty/, 'bd_empty');
  203. //To disable further attempts to truncate the table,
  204. //replace the a element with its inner span (modified)
  205. $this_anchor
  206. .replaceWith(span)
  207. .removeClass('truncate_table_anchor');
  208. PMA_adjustTotals();
  209. } else {
  210. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
  211. }
  212. }); // end $.get()
  213. }); //end $.PMA_confirm()
  214. }); //end of Truncate Table Ajax action
  215. /**
  216. * Ajax Event handler for 'Drop Table' or 'Drop View'
  217. */
  218. $("a.drop_table_anchor.ajax").live('click', function (event) {
  219. event.preventDefault();
  220. var $this_anchor = $(this);
  221. //extract current table name and build the question string
  222. /**
  223. * @var $curr_row Object containing reference to the current row
  224. */
  225. var $curr_row = $this_anchor.parents('tr');
  226. /**
  227. * @var curr_table_name String containing the name of the table to be truncated
  228. */
  229. var curr_table_name = $curr_row.children('th').children('a').text();
  230. /**
  231. * @var is_view Boolean telling if we have a view
  232. */
  233. var is_view = $curr_row.hasClass('is_view') || $this_anchor.hasClass('view');
  234. /**
  235. * @var question String containing the question to be asked for confirmation
  236. */
  237. var question;
  238. if (! is_view) {
  239. question = PMA_messages.strDropTableStrongWarning + ' ' +
  240. $.sprintf(PMA_messages.strDoYouReally, 'DROP TABLE ' + escapeHtml(curr_table_name));
  241. } else {
  242. question =
  243. $.sprintf(PMA_messages.strDoYouReally, 'DROP VIEW ' + escapeHtml(curr_table_name));
  244. }
  245. $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function (url) {
  246. var $msg = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  247. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function (data) {
  248. if (data.success === true) {
  249. PMA_ajaxShowMessage(data.message);
  250. toggleRowColors($curr_row.next());
  251. $curr_row.hide("medium").remove();
  252. PMA_adjustTotals();
  253. PMA_reloadNavigation();
  254. PMA_ajaxRemoveMessage($msg);
  255. } else {
  256. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
  257. }
  258. }); // end $.get()
  259. }); // end $.PMA_confirm()
  260. }); //end of Drop Table Ajax action
  261. /**
  262. * Ajax Event handler for 'Drop tracking'
  263. */
  264. $('a.drop_tracking_anchor.ajax').live('click', function (event) {
  265. event.preventDefault();
  266. var $anchor = $(this);
  267. /**
  268. * @var curr_tracking_row Object containing reference to the current tracked table's row
  269. */
  270. var $curr_tracking_row = $anchor.parents('tr');
  271. /**
  272. * @var question String containing the question to be asked for confirmation
  273. */
  274. var question = PMA_messages.strDeleteTrackingData;
  275. $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
  276. PMA_ajaxShowMessage(PMA_messages.strDeletingTrackingData);
  277. $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function (data) {
  278. if (data.success === true) {
  279. var $tracked_table = $curr_tracking_row.parents('table');
  280. var table_name = $curr_tracking_row.find('td:nth-child(2)').text();
  281. // Check how many rows will be left after we remove
  282. if ($tracked_table.find('tbody tr').length === 1) {
  283. // We are removing the only row it has
  284. $('#tracked_tables').hide("slow").remove();
  285. } else {
  286. // There are more rows left after the deletion
  287. toggleRowColors($curr_tracking_row.next());
  288. $curr_tracking_row.hide("slow", function () {
  289. $(this).remove();
  290. });
  291. }
  292. // Make the removed table visible in the list of 'Untracked tables'.
  293. var $untracked_table = $('table#noversions');
  294. // This won't work if no untracked tables are there.
  295. if ($untracked_table.length > 0) {
  296. var $rows = $untracked_table.find('tbody tr');
  297. $rows.each(function (index) {
  298. var $row = $(this);
  299. var tmp_tbl_name = $row.find('td:first-child').text();
  300. var is_last_iteration = (index == ($rows.length - 1));
  301. if (tmp_tbl_name > table_name || is_last_iteration) {
  302. var $cloned = $row.clone();
  303. // Change the table name of the cloned row.
  304. $cloned.find('td:first-child').text(table_name);
  305. // Change the link of the cloned row.
  306. var new_url = $cloned
  307. .find('td:nth-child(2) a')
  308. .attr('href')
  309. .replace('table=' + tmp_tbl_name, 'table=' + encodeURIComponent(table_name));
  310. $cloned.find('td:nth-child(2) a').attr('href', new_url);
  311. // Insert the cloned row in an appropriate location.
  312. if (tmp_tbl_name > table_name) {
  313. $cloned.insertBefore($row);
  314. toggleRowColors($row);
  315. return false;
  316. } else {
  317. $cloned.insertAfter($row);
  318. toggleRowColors($cloned);
  319. }
  320. }
  321. });
  322. }
  323. PMA_ajaxShowMessage(data.message);
  324. } else {
  325. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
  326. }
  327. }); // end $.get()
  328. }); // end $.PMA_confirm()
  329. }); //end Drop Tracking
  330. //Calculate Real End for InnoDB
  331. /**
  332. * Ajax Event handler for calculatig the real end for a InnoDB table
  333. *
  334. */
  335. $('#real_end_input').live('click', function (event) {
  336. event.preventDefault();
  337. /**
  338. * @var question String containing the question to be asked for confirmation
  339. */
  340. var question = PMA_messages.strOperationTakesLongTime;
  341. $(this).PMA_confirm(question, '', function () {
  342. return true;
  343. });
  344. return false;
  345. }); //end Calculate Real End for InnoDB
  346. PMA_tooltip(
  347. $("select[name*='funcs']"),
  348. 'select',
  349. PMA_messages.strFunctionHint
  350. );
  351. // Add tooltip to favorite icons.
  352. $(".favorite_table_anchor").each(function () {
  353. PMA_tooltip(
  354. $(this),
  355. 'a',
  356. $(this).attr("title")
  357. );
  358. });
  359. }); // end $()