server_databases.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used on the server databases list page
  4. * @name Server Databases
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @required js/functions.js
  9. */
  10. /**
  11. * Unbind all event handlers before tearing down a page
  12. */
  13. AJAX.registerTeardown('server_databases.js', function () {
  14. $(document).off('submit', '#dbStatsForm');
  15. $(document).off('submit', '#create_database_form.ajax');
  16. });
  17. /**
  18. * AJAX scripts for server_databases.php
  19. *
  20. * Actions ajaxified here:
  21. * Drop Databases
  22. *
  23. */
  24. AJAX.registerOnload('server_databases.js', function () {
  25. /**
  26. * Attach Event Handler for 'Drop Databases'
  27. */
  28. $(document).on('submit', '#dbStatsForm', function (event) {
  29. event.preventDefault();
  30. var $form = $(this);
  31. /**
  32. * @var selected_dbs Array containing the names of the checked databases
  33. */
  34. var selected_dbs = [];
  35. // loop over all checked checkboxes, except the .checkall_box checkbox
  36. $form.find('input:checkbox:checked:not(.checkall_box)').each(function () {
  37. $(this).closest('tr').addClass('removeMe');
  38. selected_dbs[selected_dbs.length] = 'DROP DATABASE `' + escapeHtml($(this).val()) + '`;';
  39. });
  40. if (! selected_dbs.length) {
  41. PMA_ajaxShowMessage(
  42. $('<div class="notice" />').text(
  43. PMA_messages.strNoDatabasesSelected
  44. ),
  45. 2000
  46. );
  47. return;
  48. }
  49. /**
  50. * @var question String containing the question to be asked for confirmation
  51. */
  52. var question = PMA_messages.strDropDatabaseStrongWarning + ' ' +
  53. PMA_sprintf(PMA_messages.strDoYouReally, selected_dbs.join('<br />'));
  54. var argsep = PMA_commonParams.get('arg_separator');
  55. $(this).PMA_confirm(
  56. question,
  57. $form.prop('action') + '?' + $(this).serialize() +
  58. argsep + 'drop_selected_dbs=1',
  59. function (url) {
  60. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest, false);
  61. var parts = url.split('?');
  62. var params = getJSConfirmCommonParam(this, parts[1]);
  63. $.post(parts[0], params, function (data) {
  64. if (typeof data !== 'undefined' && data.success === true) {
  65. PMA_ajaxShowMessage(data.message);
  66. var $rowsToRemove = $form.find('tr.removeMe');
  67. var $databasesCount = $('#filter-rows-count');
  68. var newCount = parseInt($databasesCount.text(), 10) - $rowsToRemove.length;
  69. $databasesCount.text(newCount);
  70. $rowsToRemove.remove();
  71. $form.find('tbody').PMA_sort_table('.name');
  72. if ($form.find('tbody').find('tr').length === 0) {
  73. // user just dropped the last db on this page
  74. PMA_commonActions.refreshMain();
  75. }
  76. PMA_reloadNavigation();
  77. } else {
  78. $form.find('tr.removeMe').removeClass('removeMe');
  79. PMA_ajaxShowMessage(data.error, false);
  80. }
  81. }); // end $.post()
  82. }
  83. ); // end $.PMA_confirm()
  84. }); // end of Drop Database action
  85. /**
  86. * Attach Ajax event handlers for 'Create Database'.
  87. */
  88. $(document).on('submit', '#create_database_form.ajax', function (event) {
  89. event.preventDefault();
  90. var $form = $(this);
  91. // TODO Remove this section when all browsers support HTML5 "required" property
  92. var newDbNameInput = $form.find('input[name=new_db]');
  93. if (newDbNameInput.val() === '') {
  94. newDbNameInput.focus();
  95. alert(PMA_messages.strFormEmpty);
  96. return;
  97. }
  98. // end remove
  99. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  100. PMA_prepareForAjaxRequest($form);
  101. $.post($form.attr('action'), $form.serialize(), function (data) {
  102. if (typeof data !== 'undefined' && data.success === true) {
  103. PMA_ajaxShowMessage(data.message);
  104. var $databases_count_object = $('#filter-rows-count');
  105. var databases_count = parseInt($databases_count_object.text(), 10) + 1;
  106. $databases_count_object.text(databases_count);
  107. PMA_reloadNavigation();
  108. // make ajax request to load db structure page - taken from ajax.js
  109. var dbStruct_url = data.url_query;
  110. dbStruct_url = dbStruct_url.replace(/amp;/ig, '');
  111. var params = 'ajax_request=true' + PMA_commonParams.get('arg_separator') + 'ajax_page_request=true';
  112. if (! (history && history.pushState)) {
  113. params += PMA_MicroHistory.menus.getRequestParam();
  114. }
  115. $.get(dbStruct_url, params, AJAX.responseHandler);
  116. } else {
  117. PMA_ajaxShowMessage(data.error, false);
  118. }
  119. }); // end $.post()
  120. }); // end $(document).on()
  121. /* Don't show filter if number of databases are very few */
  122. var databasesCount = $('#filter-rows-count').html();
  123. if (databasesCount <= 10) {
  124. $('#tableFilter').hide();
  125. }
  126. var tableRows = $('.server_databases');
  127. $.each(tableRows, function (index, item) {
  128. $(this).click(function () {
  129. PMA_commonActions.setDb($(this).attr('data'));
  130. });
  131. });
  132. }); // end $()