tbl_structure.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used on the table structure page
  4. * @name Table Structure
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @required js/functions.js
  9. */
  10. /**
  11. * AJAX scripts for tbl_structure.php
  12. *
  13. * Actions ajaxified here:
  14. * Drop Column
  15. * Add Primary Key
  16. * Drop Primary Key/Index
  17. *
  18. */
  19. /**
  20. * Unbind all event handlers before tearing down a page
  21. */
  22. AJAX.registerTeardown('tbl_structure.js', function() {
  23. $("a.change_column_anchor.ajax").die('click');
  24. $("button.change_columns_anchor.ajax, input.change_columns_anchor.ajax").die('click');
  25. $("a.drop_column_anchor.ajax").die('click');
  26. $("a.add_primary_key_anchor.ajax").die('click');
  27. $("#move_columns_anchor").die('click');
  28. $(".append_fields_form.ajax").unbind('submit');
  29. });
  30. AJAX.registerOnload('tbl_structure.js', function() {
  31. /**
  32. *Ajax action for submitting the "Column Change" and "Add Column" form
  33. */
  34. $(".append_fields_form.ajax").die().live('submit', function(event) {
  35. event.preventDefault();
  36. /**
  37. * @var the_form object referring to the export form
  38. */
  39. var $form = $(this);
  40. /*
  41. * First validate the form; if there is a problem, avoid submitting it
  42. *
  43. * checkTableEditForm() needs a pure element and not a jQuery object,
  44. * this is why we pass $form[0] as a parameter (the jQuery object
  45. * is actually an array of DOM elements)
  46. */
  47. if (checkTableEditForm($form[0], $form.find('input[name=orig_num_fields]').val())) {
  48. // OK, form passed validation step
  49. PMA_prepareForAjaxRequest($form);
  50. //User wants to submit the form
  51. $msg = PMA_ajaxShowMessage();
  52. $.post($form.attr('action'), $form.serialize() + '&do_save_data=1', function(data) {
  53. if ($("#sqlqueryresults").length != 0) {
  54. $("#sqlqueryresults").remove();
  55. } else if ($(".error:not(.tab)").length != 0) {
  56. $(".error:not(.tab)").remove();
  57. }
  58. if (data.success == true) {
  59. $("#page_content")
  60. .empty()
  61. .append(data.message)
  62. .append(data.sql_query)
  63. .show();
  64. $("#result_query .notice").remove();
  65. reloadFieldForm();
  66. $form.remove();
  67. PMA_ajaxRemoveMessage($msg);
  68. PMA_reloadNavigation();
  69. } else {
  70. PMA_ajaxShowMessage(data.error, false);
  71. }
  72. }); // end $.post()
  73. }
  74. }); // end change table button "do_save_data"
  75. /**
  76. * Attach Event Handler for 'Change Column'
  77. */
  78. $("a.change_column_anchor.ajax").live('click', function(event) {
  79. event.preventDefault();
  80. var $msg = PMA_ajaxShowMessage();
  81. $('#page_content').hide();
  82. $.get($(this).attr('href'), {'ajax_request': true}, function (data) {
  83. PMA_ajaxRemoveMessage($msg);
  84. if (data.success) {
  85. $('<div id="change_column_dialog" class="margin"></div>')
  86. .html(data.message)
  87. .insertBefore('#page_content');
  88. PMA_showHints();
  89. PMA_verifyColumnsProperties();
  90. } else {
  91. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  92. }
  93. });
  94. });
  95. /**
  96. * Attach Event Handler for 'Change multiple columns'
  97. */
  98. $("button.change_columns_anchor.ajax, input.change_columns_anchor.ajax").live('click', function(event) {
  99. event.preventDefault();
  100. var $msg = PMA_ajaxShowMessage();
  101. $('#page_content').hide();
  102. var $form = $(this).closest('form');
  103. var params = $form.serialize() + "&ajax_request=true&submit_mult=change";
  104. $.post($form.prop("action"), params, function (data) {
  105. PMA_ajaxRemoveMessage($msg);
  106. if (data.success) {
  107. $('#page_content')
  108. .empty()
  109. .append(
  110. $('<div id="change_column_dialog"></div>')
  111. .html(data.message)
  112. )
  113. .show();
  114. PMA_showHints();
  115. PMA_verifyColumnsProperties();
  116. } else {
  117. $('#page_content').show();
  118. PMA_ajaxShowMessage(data.error);
  119. }
  120. });
  121. });
  122. /**
  123. * Attach Event Handler for 'Drop Column'
  124. */
  125. $("a.drop_column_anchor.ajax").live('click', function(event) {
  126. event.preventDefault();
  127. /**
  128. * @var curr_table_name String containing the name of the current table
  129. */
  130. var curr_table_name = $(this).closest('form').find('input[name=table]').val();
  131. /**
  132. * @var curr_row Object reference to the currently selected row (i.e. field in the table)
  133. */
  134. var $curr_row = $(this).parents('tr');
  135. /**
  136. * @var curr_column_name String containing name of the field referred to by {@link curr_row}
  137. */
  138. var curr_column_name = $curr_row.children('th').children('label').text();
  139. curr_column_name = escapeHtml(curr_column_name);
  140. /**
  141. * @var $after_field_item Corresponding entry in the 'After' field.
  142. */
  143. var $after_field_item = $("select[name='after_field'] option[value='" + curr_column_name + "']");
  144. /**
  145. * @var question String containing the question to be asked for confirmation
  146. */
  147. var question = $.sprintf(PMA_messages['strDoYouReally'], 'ALTER TABLE `' + escapeHtml(curr_table_name) + '` DROP `' + escapeHtml(curr_column_name) + '`;');
  148. $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
  149. var $msg = PMA_ajaxShowMessage(PMA_messages['strDroppingColumn'], false);
  150. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true, 'ajax_page_request' : true}, function (data) {
  151. if (data.success == true) {
  152. PMA_ajaxRemoveMessage($msg);
  153. if ($('#result_query').length) {
  154. $('#result_query').remove();
  155. }
  156. if (data.sql_query) {
  157. $('<div id="result_query"></div>')
  158. .html(data.sql_query)
  159. .prependTo('#page_content');
  160. }
  161. toggleRowColors($curr_row.next());
  162. // Adjust the row numbers
  163. for (var $row = $curr_row.next(); $row.length > 0; $row = $row.next()) {
  164. var new_val = parseInt($row.find('td:nth-child(2)').text()) - 1;
  165. $row.find('td:nth-child(2)').text(new_val);
  166. }
  167. $after_field_item.remove();
  168. $curr_row.hide("medium").remove();
  169. //refresh table stats
  170. if (data.tableStat) {
  171. $('#tablestatistics').html(data.tableStat);
  172. }
  173. // refresh the list of indexes (comes from sql.php)
  174. $('.index_info').replaceWith(data.indexes_list);
  175. PMA_reloadNavigation();
  176. } else {
  177. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  178. }
  179. }); // end $.get()
  180. }); // end $.PMA_confirm()
  181. }) ; //end of Drop Column Anchor action
  182. /**
  183. * Ajax Event handler for 'Add Primary Key'
  184. */
  185. $("a.add_primary_key_anchor.ajax").live('click', function(event) {
  186. event.preventDefault();
  187. /**
  188. * @var curr_table_name String containing the name of the current table
  189. */
  190. var curr_table_name = $(this).closest('form').find('input[name=table]').val();
  191. /**
  192. * @var curr_column_name String containing name of the field referred to by {@link curr_row}
  193. */
  194. var curr_column_name = $(this).parents('tr').children('th').children('label').text();
  195. /**
  196. * @var question String containing the question to be asked for confirmation
  197. */
  198. var question = $.sprintf(PMA_messages['strDoYouReally'], 'ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`);');
  199. $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
  200. var $msg = PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
  201. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
  202. if (data.success == true) {
  203. PMA_ajaxRemoveMessage($msg);
  204. $(this).remove();
  205. if (typeof data.reload != 'undefined') {
  206. PMA_commonActions.refreshMain(false, function () {
  207. if ($('#result_query').length) {
  208. $('#result_query').remove();
  209. }
  210. if (data.sql_query) {
  211. $('<div id="result_query"></div>')
  212. .html(data.sql_query)
  213. .prependTo('#page_content');
  214. }
  215. });
  216. PMA_reloadNavigation();
  217. }
  218. } else {
  219. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  220. }
  221. }); // end $.get()
  222. }); // end $.PMA_confirm()
  223. }); //end Add Primary Key
  224. /**
  225. * Inline move columns
  226. **/
  227. $("#move_columns_anchor").live('click', function(e) {
  228. e.preventDefault();
  229. if ($(this).hasClass("move-active")) {
  230. return;
  231. }
  232. /**
  233. * @var button_options Object that stores the options passed to jQueryUI
  234. * dialog
  235. */
  236. var button_options = {};
  237. button_options[PMA_messages['strGo']] = function(event) {
  238. event.preventDefault();
  239. var $msgbox = PMA_ajaxShowMessage();
  240. var $this = $(this);
  241. var $form = $this.find("form");
  242. var serialized = $form.serialize();
  243. // check if any columns were moved at all
  244. if (serialized == $form.data("serialized-unmoved")) {
  245. PMA_ajaxRemoveMessage($msgbox);
  246. $this.dialog('close');
  247. return;
  248. }
  249. $.post($form.prop("action"), serialized + "&ajax_request=true", function (data) {
  250. if (data.success == false) {
  251. PMA_ajaxRemoveMessage($msgbox);
  252. $this
  253. .clone()
  254. .html(data.error)
  255. .dialog({
  256. title: $(this).prop("title"),
  257. height: 230,
  258. width: 900,
  259. modal: true,
  260. buttons: button_options_error
  261. }); // end dialog options
  262. } else {
  263. $('#fieldsForm ul.table-structure-actions').menuResizer('destroy');
  264. // sort the fields table
  265. var $fields_table = $("table#tablestructure tbody");
  266. // remove all existing rows and remember them
  267. var $rows = $fields_table.find("tr").remove();
  268. // loop through the correct order
  269. for (var i in data.columns) {
  270. var the_column = data.columns[i];
  271. var $the_row = $rows
  272. .find("input:checkbox[value=" + the_column + "]")
  273. .closest("tr");
  274. // append the row for this column to the table
  275. $fields_table.append($the_row);
  276. }
  277. var $firstrow = $fields_table.find("tr").eq(0);
  278. // Adjust the row numbers and colors
  279. for (var $row = $firstrow; $row.length > 0; $row = $row.next()) {
  280. $row
  281. .find('td:nth-child(2)')
  282. .text($row.index() + 1)
  283. .end()
  284. .removeClass("odd even")
  285. .addClass($row.index() % 2 == 0 ? "odd" : "even");
  286. }
  287. PMA_ajaxShowMessage(data.message);
  288. $this.dialog('close');
  289. $('#fieldsForm ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
  290. }
  291. });
  292. };
  293. button_options[PMA_messages['strCancel']] = function() {
  294. $(this).dialog('close');
  295. };
  296. var button_options_error = {};
  297. button_options_error[PMA_messages['strOK']] = function() {
  298. $(this).dialog('close').remove();
  299. };
  300. var columns = [];
  301. $("#tablestructure tbody tr").each(function () {
  302. var col_name = $(this).find("input:checkbox").eq(0).val();
  303. var hidden_input = $("<input/>")
  304. .prop({
  305. name: "move_columns[]",
  306. type: "hidden"
  307. })
  308. .val(col_name);
  309. columns[columns.length] = $("<li/>")
  310. .addClass("placeholderDrag")
  311. .text(col_name)
  312. .append(hidden_input);
  313. });
  314. var col_list = $("#move_columns_dialog ul")
  315. .find("li").remove().end();
  316. for (var i in columns) {
  317. col_list.append(columns[i]);
  318. }
  319. col_list.sortable({
  320. axis: 'y',
  321. containment: $("#move_columns_dialog div")
  322. }).disableSelection();
  323. var $form = $("#move_columns_dialog form");
  324. $form.data("serialized-unmoved", $form.serialize());
  325. $("#move_columns_dialog").dialog({
  326. modal: true,
  327. buttons: button_options,
  328. beforeClose: function () {
  329. $("#move_columns_anchor").removeClass("move-active");
  330. }
  331. });
  332. });
  333. });
  334. /**
  335. * Reload fields table
  336. */
  337. function reloadFieldForm() {
  338. $.post($("#fieldsForm").attr('action'), $("#fieldsForm").serialize()+"&ajax_request=true", function(form_data) {
  339. var $temp_div = $("<div id='temp_div'><div>").append(form_data.message);
  340. $("#fieldsForm").replaceWith($temp_div.find("#fieldsForm"));
  341. $("#addColumns").replaceWith($temp_div.find("#addColumns"));
  342. $('#move_columns_dialog ul').replaceWith($temp_div.find("#move_columns_dialog ul"));
  343. $("#moveColumns").removeClass("move-active");
  344. /* reinitialise the more options in table */
  345. $('#fieldsForm ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
  346. });
  347. $('#page_content').show();
  348. }
  349. /**
  350. * This function returns the horizontal space available for the menu in pixels.
  351. * To calculate this value we start we the width of the main panel, then we
  352. * substract the margin of the page content, then we substract any cellspacing
  353. * that the table may have (original theme only) and finally we substract the
  354. * width of all columns of the table except for the last one (which is where
  355. * the menu will go). What we should end up with is the distance between the
  356. * start of the last column on the table and the edge of the page, again this
  357. * is the space available for the menu.
  358. *
  359. * In the case where the table cell where the menu will be displayed is already
  360. * off-screen (the table is wider than the page), a negative value will be returned,
  361. * but this will be treated as a zero by the menuResizer plugin.
  362. *
  363. * @return int
  364. */
  365. function PMA_tbl_structure_menu_resizer_callback() {
  366. var pagewidth = $('body').width();
  367. var $page = $('#page_content');
  368. pagewidth -= $page.outerWidth(true) - $page.outerWidth();
  369. var columnsWidth = 0;
  370. var $columns = $('#tablestructure').find('tr:eq(1)').find('td,th');
  371. $columns.not(':last').each(function (){
  372. columnsWidth += $(this).outerWidth(true)
  373. });
  374. var totalCellSpacing = $('#tablestructure').width();
  375. $columns.each(function (){
  376. totalCellSpacing -= $(this).outerWidth(true);
  377. });
  378. return pagewidth - columnsWidth - totalCellSpacing - 15; // 15px extra margin
  379. }
  380. /** Handler for "More" dropdown in structure table rows */
  381. AJAX.registerOnload('tbl_structure.js', function() {
  382. if ($('#fieldsForm').hasClass('HideStructureActions')) {
  383. $('#fieldsForm ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
  384. }
  385. });
  386. AJAX.registerTeardown('tbl_structure.js', function() {
  387. $('#fieldsForm ul.table-structure-actions').menuResizer('destroy');
  388. });
  389. $(function () {
  390. $(window).resize($.throttle(function () {
  391. var $list = $('#fieldsForm ul.table-structure-actions');
  392. if ($list.length) {
  393. $list.menuResizer('resize');
  394. }
  395. }));
  396. });