db_operations.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview function used in server privilege pages
  4. * @name Database Operations
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @requires js/functions.js
  9. *
  10. */
  11. /**
  12. * Ajax event handlers here for db_operations.php
  13. *
  14. * Actions Ajaxified here:
  15. * Rename Database
  16. * Copy Database
  17. * Change Charset
  18. * Drop Database
  19. */
  20. /**
  21. * Unbind all event handlers before tearing down a page
  22. */
  23. AJAX.registerTeardown('db_operations.js', function () {
  24. $(document).off('submit', '#rename_db_form.ajax');
  25. $(document).off('submit', '#copy_db_form.ajax');
  26. $(document).off('submit', '#change_db_charset_form.ajax');
  27. $(document).off('click', '#drop_db_anchor.ajax');
  28. });
  29. AJAX.registerOnload('db_operations.js', function () {
  30. /**
  31. * Ajax event handlers for 'Rename Database'
  32. */
  33. $(document).on('submit', '#rename_db_form.ajax', function (event) {
  34. event.preventDefault();
  35. var old_db_name = PMA_commonParams.get('db');
  36. var new_db_name = $('#new_db_name').val();
  37. if (new_db_name === old_db_name) {
  38. PMA_ajaxShowMessage(PMA_messages.strDatabaseRenameToSameName, false, 'error');
  39. return false;
  40. }
  41. var $form = $(this);
  42. var question = escapeHtml('CREATE DATABASE ' + new_db_name + ' / DROP DATABASE ' + old_db_name);
  43. PMA_prepareForAjaxRequest($form);
  44. $form.PMA_confirm(question, $form.attr('action'), function (url) {
  45. PMA_ajaxShowMessage(PMA_messages.strRenamingDatabases, false);
  46. $.post(url, $('#rename_db_form').serialize() + PMA_commonParams.get('arg_separator') + 'is_js_confirmed=1', function (data) {
  47. if (typeof data !== 'undefined' && data.success === true) {
  48. PMA_ajaxShowMessage(data.message);
  49. PMA_commonParams.set('db', data.newname);
  50. PMA_reloadNavigation(function () {
  51. $('#pma_navigation_tree')
  52. .find('a:not(\'.expander\')')
  53. .each(function (index) {
  54. var $thisAnchor = $(this);
  55. if ($thisAnchor.text() === data.newname) {
  56. // simulate a click on the new db name
  57. // in navigation
  58. $thisAnchor.trigger('click');
  59. }
  60. });
  61. });
  62. } else {
  63. PMA_ajaxShowMessage(data.error, false);
  64. }
  65. }); // end $.post()
  66. });
  67. }); // end Rename Database
  68. /**
  69. * Ajax Event Handler for 'Copy Database'
  70. */
  71. $(document).on('submit', '#copy_db_form.ajax', function (event) {
  72. event.preventDefault();
  73. PMA_ajaxShowMessage(PMA_messages.strCopyingDatabase, false);
  74. var $form = $(this);
  75. PMA_prepareForAjaxRequest($form);
  76. $.post($form.attr('action'), $form.serialize(), function (data) {
  77. // use messages that stay on screen
  78. $('div.success, div.error').fadeOut();
  79. if (typeof data !== 'undefined' && data.success === true) {
  80. if ($('#checkbox_switch').is(':checked')) {
  81. PMA_commonParams.set('db', data.newname);
  82. PMA_commonActions.refreshMain(false, function () {
  83. PMA_ajaxShowMessage(data.message);
  84. });
  85. } else {
  86. PMA_commonParams.set('db', data.db);
  87. PMA_ajaxShowMessage(data.message);
  88. }
  89. PMA_reloadNavigation();
  90. } else {
  91. PMA_ajaxShowMessage(data.error, false);
  92. }
  93. }); // end $.post()
  94. }); // end copy database
  95. /**
  96. * Change tables columns visible only if change tables is checked
  97. */
  98. $('#span_change_all_tables_columns_collations').hide();
  99. $('#checkbox_change_all_tables_collations').on('click', function () {
  100. $('#span_change_all_tables_columns_collations').toggle();
  101. });
  102. /**
  103. * Ajax Event handler for 'Change Charset' of the database
  104. */
  105. $(document).on('submit', '#change_db_charset_form.ajax', function (event) {
  106. event.preventDefault();
  107. var $form = $(this);
  108. PMA_prepareForAjaxRequest($form);
  109. PMA_ajaxShowMessage(PMA_messages.strChangingCharset);
  110. $.post($form.attr('action'), $form.serialize() + PMA_commonParams.get('arg_separator') + 'submitcollation=1', function (data) {
  111. if (typeof data !== 'undefined' && data.success === true) {
  112. PMA_ajaxShowMessage(data.message);
  113. } else {
  114. PMA_ajaxShowMessage(data.error, false);
  115. }
  116. }); // end $.post()
  117. }); // end change charset
  118. /**
  119. * Ajax event handlers for Drop Database
  120. */
  121. $(document).on('click', '#drop_db_anchor.ajax', function (event) {
  122. event.preventDefault();
  123. var $link = $(this);
  124. /**
  125. * @var question String containing the question to be asked for confirmation
  126. */
  127. var question = PMA_messages.strDropDatabaseStrongWarning + ' ';
  128. question += PMA_sprintf(
  129. PMA_messages.strDoYouReally,
  130. 'DROP DATABASE `' + escapeHtml(PMA_commonParams.get('db') + '`')
  131. );
  132. var params = getJSConfirmCommonParam(this, $link.getPostData());
  133. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  134. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  135. $.post(url, params, function (data) {
  136. if (typeof data !== 'undefined' && data.success) {
  137. // Database deleted successfully, refresh both the frames
  138. PMA_reloadNavigation();
  139. PMA_commonParams.set('db', '');
  140. PMA_commonActions.refreshMain(
  141. 'server_databases.php',
  142. function () {
  143. PMA_ajaxShowMessage(data.message);
  144. }
  145. );
  146. } else {
  147. PMA_ajaxShowMessage(data.error, false);
  148. }
  149. });
  150. });
  151. });
  152. });