tbl_operations.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * Unbind all event handlers before tearing down a page
  3. */
  4. AJAX.registerTeardown('tbl_operations.js', function () {
  5. $(document).off('submit', '#copyTable.ajax');
  6. $(document).off('submit', '#moveTableForm');
  7. $(document).off('submit', '#tableOptionsForm');
  8. $(document).off('submit', '#partitionsForm');
  9. $(document).off('click', '#tbl_maintenance li a.maintain_action.ajax');
  10. $(document).off('click', '#drop_tbl_anchor.ajax');
  11. $(document).off('click', '#drop_view_anchor.ajax');
  12. $(document).off('click', '#truncate_tbl_anchor.ajax');
  13. });
  14. /**
  15. * jQuery coding for 'Table operations'. Used on tbl_operations.php
  16. * Attach Ajax Event handlers for Table operations
  17. */
  18. AJAX.registerOnload('tbl_operations.js', function () {
  19. /**
  20. *Ajax action for submitting the "Copy table"
  21. **/
  22. $(document).on('submit', '#copyTable.ajax', function (event) {
  23. event.preventDefault();
  24. var $form = $(this);
  25. PMA_prepareForAjaxRequest($form);
  26. var argsep = PMA_commonParams.get('arg_separator');
  27. $.post($form.attr('action'), $form.serialize() + argsep + 'submit_copy=Go', function (data) {
  28. if (typeof data !== 'undefined' && data.success === true) {
  29. if ($form.find('input[name=\'switch_to_new\']').prop('checked')) {
  30. PMA_commonParams.set(
  31. 'db',
  32. $form.find('select[name=\'target_db\']').val()
  33. );
  34. PMA_commonParams.set(
  35. 'table',
  36. $form.find('input[name=\'new_name\']').val()
  37. );
  38. PMA_commonActions.refreshMain(false, function () {
  39. PMA_ajaxShowMessage(data.message);
  40. });
  41. } else {
  42. PMA_ajaxShowMessage(data.message);
  43. }
  44. // Refresh navigation when the table is copied
  45. PMA_reloadNavigation();
  46. } else {
  47. PMA_ajaxShowMessage(data.error, false);
  48. }
  49. }); // end $.post()
  50. });// end of copyTable ajax submit
  51. /**
  52. *Ajax action for submitting the "Move table"
  53. */
  54. $(document).on('submit', '#moveTableForm', function (event) {
  55. event.preventDefault();
  56. var $form = $(this);
  57. PMA_prepareForAjaxRequest($form);
  58. var argsep = PMA_commonParams.get('arg_separator');
  59. $.post($form.attr('action'), $form.serialize() + argsep + 'submit_move=1', function (data) {
  60. if (typeof data !== 'undefined' && data.success === true) {
  61. PMA_commonParams.set('db', data._params.db);
  62. PMA_commonParams.set('table', data._params.table);
  63. PMA_commonActions.refreshMain('tbl_sql.php', function () {
  64. PMA_ajaxShowMessage(data.message);
  65. });
  66. // Refresh navigation when the table is copied
  67. PMA_reloadNavigation();
  68. } else {
  69. PMA_ajaxShowMessage(data.error, false);
  70. }
  71. }); // end $.post()
  72. });
  73. /**
  74. * Ajax action for submitting the "Table options"
  75. */
  76. $(document).on('submit', '#tableOptionsForm', function (event) {
  77. event.preventDefault();
  78. event.stopPropagation();
  79. var $form = $(this);
  80. var $tblNameField = $form.find('input[name=new_name]');
  81. var $tblCollationField = $form.find('select[name=tbl_collation]');
  82. var collationOrigValue = $('select[name="tbl_collation"] option[selected]').val();
  83. var $changeAllColumnCollationsCheckBox = $('#checkbox_change_all_collations');
  84. var question = PMA_messages.strChangeAllColumnCollationsWarning;
  85. if ($tblNameField.val() !== $tblNameField[0].defaultValue) {
  86. // reload page and navigation if the table has been renamed
  87. PMA_prepareForAjaxRequest($form);
  88. if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
  89. $form.PMA_confirm(question, $form.attr('action'), function (url) {
  90. submitOptionsForm();
  91. });
  92. } else {
  93. submitOptionsForm();
  94. }
  95. } else {
  96. if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
  97. $form.PMA_confirm(question, $form.attr('action'), function (url) {
  98. $form.removeClass('ajax').submit().addClass('ajax');
  99. });
  100. } else {
  101. $form.removeClass('ajax').submit().addClass('ajax');
  102. }
  103. }
  104. function submitOptionsForm () {
  105. $.post($form.attr('action'), $form.serialize(), function (data) {
  106. if (typeof data !== 'undefined' && data.success === true) {
  107. PMA_commonParams.set('table', data._params.table);
  108. PMA_commonActions.refreshMain(false, function () {
  109. $('#page_content').html(data.message);
  110. PMA_highlightSQL($('#page_content'));
  111. });
  112. // Refresh navigation when the table is renamed
  113. PMA_reloadNavigation();
  114. } else {
  115. PMA_ajaxShowMessage(data.error, false);
  116. }
  117. }); // end $.post()
  118. }
  119. });
  120. /**
  121. *Ajax events for actions in the "Table maintenance"
  122. **/
  123. $(document).on('click', '#tbl_maintenance li a.maintain_action.ajax', function (event) {
  124. event.preventDefault();
  125. var $link = $(this);
  126. if ($('.sqlqueryresults').length !== 0) {
  127. $('.sqlqueryresults').remove();
  128. }
  129. if ($('.result_query').length !== 0) {
  130. $('.result_query').remove();
  131. }
  132. // variables which stores the common attributes
  133. var params = $.param({
  134. ajax_request: 1,
  135. server: PMA_commonParams.get('server')
  136. });
  137. var postData = $link.getPostData();
  138. if (postData) {
  139. params += PMA_commonParams.get('arg_separator') + postData;
  140. }
  141. $.post($link.attr('href'), params, function (data) {
  142. function scrollToTop () {
  143. $('html, body').animate({ scrollTop: 0 });
  144. }
  145. var $temp_div;
  146. if (typeof data !== 'undefined' && data.success === true && data.sql_query !== undefined) {
  147. PMA_ajaxShowMessage(data.message);
  148. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  149. $('.sqlqueryresults').html(data.sql_query);
  150. PMA_highlightSQL($('#page_content'));
  151. scrollToTop();
  152. } else if (typeof data !== 'undefined' && data.success === true) {
  153. $temp_div = $('<div id=\'temp_div\'></div>');
  154. $temp_div.html(data.message);
  155. var $success = $temp_div.find('.result_query .success');
  156. PMA_ajaxShowMessage($success);
  157. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  158. $('.sqlqueryresults').html(data.message);
  159. PMA_highlightSQL($('#page_content'));
  160. PMA_init_slider();
  161. $('.sqlqueryresults').children('fieldset,br').remove();
  162. scrollToTop();
  163. } else {
  164. $temp_div = $('<div id=\'temp_div\'></div>');
  165. $temp_div.html(data.error);
  166. var $error;
  167. if ($temp_div.find('.error code').length !== 0) {
  168. $error = $temp_div.find('.error code').addClass('error');
  169. } else {
  170. $error = $temp_div;
  171. }
  172. PMA_ajaxShowMessage($error, false);
  173. }
  174. }); // end $.post()
  175. });// end of table maintenance ajax click
  176. /**
  177. * Ajax action for submitting the "Partition Maintenance"
  178. * Also, asks for confirmation when DROP partition is submitted
  179. */
  180. $(document).on('submit', '#partitionsForm', function (event) {
  181. event.preventDefault();
  182. var $form = $(this);
  183. function submitPartitionMaintenance () {
  184. var argsep = PMA_commonParams.get('arg_separator');
  185. var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  186. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  187. AJAX.source = $form;
  188. $.post($form.attr('action'), submitData, AJAX.responseHandler);
  189. }
  190. if ($('#partition_operation_DROP').is(':checked')) {
  191. var question = PMA_messages.strDropPartitionWarning;
  192. $form.PMA_confirm(question, $form.attr('action'), function (url) {
  193. submitPartitionMaintenance();
  194. });
  195. } else if ($('#partition_operation_TRUNCATE').is(':checked')) {
  196. var question = PMA_messages.strTruncatePartitionWarning;
  197. $form.PMA_confirm(question, $form.attr('action'), function (url) {
  198. submitPartitionMaintenance();
  199. });
  200. } else {
  201. submitPartitionMaintenance();
  202. }
  203. });
  204. $(document).on('click', '#drop_tbl_anchor.ajax', function (event) {
  205. event.preventDefault();
  206. var $link = $(this);
  207. /**
  208. * @var question String containing the question to be asked for confirmation
  209. */
  210. var question = PMA_messages.strDropTableStrongWarning + ' ';
  211. question += PMA_sprintf(
  212. PMA_messages.strDoYouReally,
  213. 'DROP TABLE `' + escapeHtml(PMA_commonParams.get('db')) + '`.`' + escapeHtml(PMA_commonParams.get('table') + '`')
  214. ) + getForeignKeyCheckboxLoader();
  215. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  216. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  217. var params = getJSConfirmCommonParam(this, $link.getPostData());
  218. $.post(url, params, function (data) {
  219. if (typeof data !== 'undefined' && data.success === true) {
  220. PMA_ajaxRemoveMessage($msgbox);
  221. // Table deleted successfully, refresh both the frames
  222. PMA_reloadNavigation();
  223. PMA_commonParams.set('table', '');
  224. PMA_commonActions.refreshMain(
  225. PMA_commonParams.get('opendb_url'),
  226. function () {
  227. PMA_ajaxShowMessage(data.message);
  228. }
  229. );
  230. } else {
  231. PMA_ajaxShowMessage(data.error, false);
  232. }
  233. }); // end $.post()
  234. }, loadForeignKeyCheckbox); // end $.PMA_confirm()
  235. }); // end of Drop Table Ajax action
  236. $(document).on('click', '#drop_view_anchor.ajax', function (event) {
  237. event.preventDefault();
  238. var $link = $(this);
  239. /**
  240. * @var question String containing the question to be asked for confirmation
  241. */
  242. var question = PMA_messages.strDropTableStrongWarning + ' ';
  243. question += PMA_sprintf(
  244. PMA_messages.strDoYouReally,
  245. 'DROP VIEW `' + escapeHtml(PMA_commonParams.get('table') + '`')
  246. );
  247. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  248. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  249. var params = getJSConfirmCommonParam(this, $link.getPostData());
  250. $.post(url, params, function (data) {
  251. if (typeof data !== 'undefined' && data.success === true) {
  252. PMA_ajaxRemoveMessage($msgbox);
  253. // Table deleted successfully, refresh both the frames
  254. PMA_reloadNavigation();
  255. PMA_commonParams.set('table', '');
  256. PMA_commonActions.refreshMain(
  257. PMA_commonParams.get('opendb_url'),
  258. function () {
  259. PMA_ajaxShowMessage(data.message);
  260. }
  261. );
  262. } else {
  263. PMA_ajaxShowMessage(data.error, false);
  264. }
  265. }); // end $.post()
  266. }); // end $.PMA_confirm()
  267. }); // end of Drop View Ajax action
  268. $(document).on('click', '#truncate_tbl_anchor.ajax', function (event) {
  269. event.preventDefault();
  270. var $link = $(this);
  271. /**
  272. * @var question String containing the question to be asked for confirmation
  273. */
  274. var question = PMA_messages.strTruncateTableStrongWarning + ' ';
  275. question += PMA_sprintf(
  276. PMA_messages.strDoYouReally,
  277. 'TRUNCATE `' + escapeHtml(PMA_commonParams.get('db')) + '`.`' + escapeHtml(PMA_commonParams.get('table') + '`')
  278. ) + getForeignKeyCheckboxLoader();
  279. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  280. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  281. var params = getJSConfirmCommonParam(this, $link.getPostData());
  282. $.post(url, params, function (data) {
  283. if ($('.sqlqueryresults').length !== 0) {
  284. $('.sqlqueryresults').remove();
  285. }
  286. if ($('.result_query').length !== 0) {
  287. $('.result_query').remove();
  288. }
  289. if (typeof data !== 'undefined' && data.success === true) {
  290. PMA_ajaxShowMessage(data.message);
  291. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  292. $('.sqlqueryresults').html(data.sql_query);
  293. PMA_highlightSQL($('#page_content'));
  294. } else {
  295. PMA_ajaxShowMessage(data.error, false);
  296. }
  297. }); // end $.post()
  298. }, loadForeignKeyCheckbox); // end $.PMA_confirm()
  299. }); // end of Truncate Table Ajax action
  300. }); // end $(document).ready for 'Table operations'