operations.js 12 KB

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