PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/js/table/operations.js

http://github.com/phpmyadmin/phpmyadmin
JavaScript | 321 lines | 259 code | 23 blank | 39 comment | 46 complexity | 2160c3d35e31a038d2e32308900b80e3 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. /**
  2. * Unbind all event handlers before tearing down a page
  3. */
  4. AJAX.registerTeardown('table/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 /table/operations
  16. * Attach Ajax Event handlers for Table operations
  17. */
  18. AJAX.registerOnload('table/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. Functions.prepareForAjaxRequest($form);
  26. var argsep = 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. CommonParams.set(
  31. 'db',
  32. $form.find('select[name=\'target_db\']').val()
  33. );
  34. CommonParams.set(
  35. 'table',
  36. $form.find('input[name=\'new_name\']').val()
  37. );
  38. CommonActions.refreshMain(false, function () {
  39. Functions.ajaxShowMessage(data.message);
  40. });
  41. } else {
  42. Functions.ajaxShowMessage(data.message);
  43. }
  44. // Refresh navigation when the table is copied
  45. Navigation.reload();
  46. } else {
  47. Functions.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. Functions.prepareForAjaxRequest($form);
  58. var argsep = 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. CommonParams.set('db', data.params.db);
  62. CommonParams.set('table', data.params.table);
  63. CommonActions.refreshMain('index.php?route=/table/sql', function () {
  64. Functions.ajaxShowMessage(data.message);
  65. });
  66. // Refresh navigation when the table is copied
  67. Navigation.reload();
  68. } else {
  69. Functions.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 = Messages.strChangeAllColumnCollationsWarning;
  85. if ($tblNameField.val() !== $tblNameField[0].defaultValue) {
  86. // reload page and navigation if the table has been renamed
  87. Functions.prepareForAjaxRequest($form);
  88. if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
  89. $form.confirm(question, $form.attr('action'), function () {
  90. submitOptionsForm();
  91. });
  92. } else {
  93. submitOptionsForm();
  94. }
  95. } else {
  96. if ($tblCollationField.val() !== collationOrigValue && $changeAllColumnCollationsCheckBox.is(':checked')) {
  97. $form.confirm(question, $form.attr('action'), function () {
  98. $form.removeClass('ajax').trigger('submit').addClass('ajax');
  99. });
  100. } else {
  101. $form.removeClass('ajax').trigger('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. CommonParams.set('table', data.params.table);
  108. CommonActions.refreshMain(false, function () {
  109. $('#page_content').html(data.message);
  110. Functions.highlightSql($('#page_content'));
  111. });
  112. // Refresh navigation when the table is renamed
  113. Navigation.reload();
  114. } else {
  115. Functions.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': CommonParams.get('server')
  136. });
  137. var postData = $link.getPostData();
  138. if (postData) {
  139. params += 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 $tempDiv;
  146. if (typeof data !== 'undefined' && data.success === true && data.sql_query !== undefined) {
  147. Functions.ajaxShowMessage(data.message);
  148. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  149. $('.sqlqueryresults').html(data.sql_query);
  150. Functions.highlightSql($('#page_content'));
  151. scrollToTop();
  152. } else if (typeof data !== 'undefined' && data.success === true) {
  153. $tempDiv = $('<div id=\'temp_div\'></div>');
  154. $tempDiv.html(data.message);
  155. var $success = $tempDiv.find('.result_query .alert-success');
  156. Functions.ajaxShowMessage($success);
  157. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  158. $('.sqlqueryresults').html(data.message);
  159. Functions.highlightSql($('#page_content'));
  160. Functions.initSlider();
  161. $('.sqlqueryresults').children('fieldset,br').remove();
  162. scrollToTop();
  163. } else {
  164. $tempDiv = $('<div id=\'temp_div\'></div>');
  165. $tempDiv.html(data.error);
  166. var $error;
  167. if ($tempDiv.find('.error code').length !== 0) {
  168. $error = $tempDiv.find('.error code').addClass('error');
  169. } else {
  170. $error = $tempDiv;
  171. }
  172. Functions.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 = CommonParams.get('arg_separator');
  185. var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  186. Functions.ajaxShowMessage(Messages.strProcessingRequest);
  187. AJAX.source = $form;
  188. $.post($form.attr('action'), submitData, AJAX.responseHandler);
  189. }
  190. if ($('#partitionOperationRadioDrop').is(':checked')) {
  191. $form.confirm(Messages.strDropPartitionWarning, $form.attr('action'), function () {
  192. submitPartitionMaintenance();
  193. });
  194. } else if ($('#partitionOperationRadioTruncate').is(':checked')) {
  195. $form.confirm(Messages.strTruncatePartitionWarning, $form.attr('action'), function () {
  196. submitPartitionMaintenance();
  197. });
  198. } else {
  199. submitPartitionMaintenance();
  200. }
  201. });
  202. $(document).on('click', '#drop_tbl_anchor.ajax', function (event) {
  203. event.preventDefault();
  204. var $link = $(this);
  205. /**
  206. * @var question String containing the question to be asked for confirmation
  207. */
  208. var question = Messages.strDropTableStrongWarning + ' ';
  209. question += Functions.sprintf(
  210. Messages.strDoYouReally,
  211. 'DROP TABLE `' + Functions.escapeHtml(CommonParams.get('db')) + '`.`' + Functions.escapeHtml(CommonParams.get('table') + '`')
  212. ) + Functions.getForeignKeyCheckboxLoader();
  213. $(this).confirm(question, $(this).attr('href'), function (url) {
  214. var $msgbox = Functions.ajaxShowMessage(Messages.strProcessingRequest);
  215. var params = Functions.getJsConfirmCommonParam(this, $link.getPostData());
  216. $.post(url, params, function (data) {
  217. if (typeof data !== 'undefined' && data.success === true) {
  218. Functions.ajaxRemoveMessage($msgbox);
  219. // Table deleted successfully, refresh both the frames
  220. Navigation.reload();
  221. CommonParams.set('table', '');
  222. CommonActions.refreshMain(
  223. CommonParams.get('opendb_url'),
  224. function () {
  225. Functions.ajaxShowMessage(data.message);
  226. }
  227. );
  228. } else {
  229. Functions.ajaxShowMessage(data.error, false);
  230. }
  231. }); // end $.post()
  232. }, Functions.loadForeignKeyCheckbox);
  233. }); // end of Drop Table Ajax action
  234. $(document).on('click', '#drop_view_anchor.ajax', function (event) {
  235. event.preventDefault();
  236. var $link = $(this);
  237. /**
  238. * @var question String containing the question to be asked for confirmation
  239. */
  240. var question = Messages.strDropTableStrongWarning + ' ';
  241. question += Functions.sprintf(
  242. Messages.strDoYouReally,
  243. 'DROP VIEW `' + Functions.escapeHtml(CommonParams.get('table') + '`')
  244. );
  245. $(this).confirm(question, $(this).attr('href'), function (url) {
  246. var $msgbox = Functions.ajaxShowMessage(Messages.strProcessingRequest);
  247. var params = Functions.getJsConfirmCommonParam(this, $link.getPostData());
  248. $.post(url, params, function (data) {
  249. if (typeof data !== 'undefined' && data.success === true) {
  250. Functions.ajaxRemoveMessage($msgbox);
  251. // Table deleted successfully, refresh both the frames
  252. Navigation.reload();
  253. CommonParams.set('table', '');
  254. CommonActions.refreshMain(
  255. CommonParams.get('opendb_url'),
  256. function () {
  257. Functions.ajaxShowMessage(data.message);
  258. }
  259. );
  260. } else {
  261. Functions.ajaxShowMessage(data.error, false);
  262. }
  263. }); // end $.post()
  264. });
  265. }); // end of Drop View Ajax action
  266. $(document).on('click', '#truncate_tbl_anchor.ajax', function (event) {
  267. event.preventDefault();
  268. var $link = $(this);
  269. /**
  270. * @var question String containing the question to be asked for confirmation
  271. */
  272. var question = Messages.strTruncateTableStrongWarning + ' ';
  273. question += Functions.sprintf(
  274. Messages.strDoYouReally,
  275. 'TRUNCATE `' + Functions.escapeHtml(CommonParams.get('db')) + '`.`' + Functions.escapeHtml(CommonParams.get('table') + '`')
  276. ) + Functions.getForeignKeyCheckboxLoader();
  277. $(this).confirm(question, $(this).attr('href'), function (url) {
  278. Functions.ajaxShowMessage(Messages.strProcessingRequest);
  279. var params = Functions.getJsConfirmCommonParam(this, $link.getPostData());
  280. $.post(url, params, function (data) {
  281. if ($('.sqlqueryresults').length !== 0) {
  282. $('.sqlqueryresults').remove();
  283. }
  284. if ($('.result_query').length !== 0) {
  285. $('.result_query').remove();
  286. }
  287. if (typeof data !== 'undefined' && data.success === true) {
  288. Functions.ajaxShowMessage(data.message);
  289. $('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
  290. $('.sqlqueryresults').html(data.sql_query);
  291. Functions.highlightSql($('#page_content'));
  292. } else {
  293. Functions.ajaxShowMessage(data.error, false);
  294. }
  295. }); // end $.post()
  296. }, Functions.loadForeignKeyCheckbox);
  297. }); // end of Truncate Table Ajax action
  298. }); // end $(document).ready for 'Table operations'