indexes.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview function used for index manipulation pages
  4. * @name Table Structure
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @required js/functions.js
  9. */
  10. /**
  11. * Hides/shows the inputs and submits appropriately depending
  12. * on whether the index type chosen is 'SPATIAL' or not.
  13. */
  14. function checkIndexType()
  15. {
  16. /**
  17. * @var Object Dropdown to select the index type.
  18. */
  19. $select_index_type = $('#select_index_type');
  20. /**
  21. * @var Object Table header for the size column.
  22. */
  23. $size_header = $('#index_columns thead tr th:nth-child(2)');
  24. /**
  25. * @var Object Inputs to specify the columns for the index.
  26. */
  27. $column_inputs = $('select[name="index[columns][names][]"]');
  28. /**
  29. * @var Object Inputs to specify sizes for columns of the index.
  30. */
  31. $size_inputs = $('input[name="index[columns][sub_parts][]"]');
  32. /**
  33. * @var Object Footer containg the controllers to add more columns
  34. */
  35. $add_more = $('#index_frm .tblFooters');
  36. if ($select_index_type.val() == 'SPATIAL') {
  37. // Disable and hide the size column
  38. $size_header.hide();
  39. $size_inputs.each(function () {
  40. $(this)
  41. .prop('disabled', true)
  42. .parent('td').hide();
  43. });
  44. // Disable and hide the columns of the index other than the first one
  45. var initial = true;
  46. $column_inputs.each(function () {
  47. $column_input = $(this);
  48. if (! initial) {
  49. $column_input
  50. .prop('disabled', true)
  51. .parent('td').hide();
  52. } else {
  53. initial = false;
  54. }
  55. });
  56. // Hide controllers to add more columns
  57. $add_more.hide();
  58. } else {
  59. // Enable and show the size column
  60. $size_header.show();
  61. $size_inputs.each(function () {
  62. $(this)
  63. .prop('disabled', false)
  64. .parent('td').show();
  65. });
  66. // Enable and show the columns of the index
  67. $column_inputs.each(function () {
  68. $(this)
  69. .prop('disabled', false)
  70. .parent('td').show();
  71. });
  72. // Show controllers to add more columns
  73. $add_more.show();
  74. }
  75. }
  76. /**
  77. * Unbind all event handlers before tearing down a page
  78. */
  79. AJAX.registerTeardown('indexes.js', function () {
  80. $('#select_index_type').die('change');
  81. $('a.drop_primary_key_index_anchor.ajax').die('click');
  82. $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").die('click');
  83. $('#index_frm input[type=submit]').die('click');
  84. });
  85. /**
  86. * @description <p>Ajax scripts for table index page</p>
  87. *
  88. * Actions ajaxified here:
  89. * <ul>
  90. * <li>Showing/hiding inputs depending on the index type chosen</li>
  91. * <li>create/edit/drop indexes</li>
  92. * </ul>
  93. */
  94. AJAX.registerOnload('indexes.js', function () {
  95. checkIndexType();
  96. checkIndexName("index_frm");
  97. $('#select_index_type').live('change', function (event) {
  98. event.preventDefault();
  99. checkIndexType();
  100. checkIndexName("index_frm");
  101. });
  102. /**
  103. * Ajax Event handler for 'Drop Index'
  104. */
  105. $('a.drop_primary_key_index_anchor.ajax').live('click', function (event) {
  106. event.preventDefault();
  107. var $anchor = $(this);
  108. /**
  109. * @var $curr_row Object containing reference to the current field's row
  110. */
  111. var $curr_row = $anchor.parents('tr');
  112. /** @var Number of columns in the key */
  113. var rows = $anchor.parents('td').attr('rowspan') || 1;
  114. /** @var Rows that should be hidden */
  115. var $rows_to_hide = $curr_row;
  116. for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
  117. $rows_to_hide = $rows_to_hide.add($last_row);
  118. }
  119. var question = escapeHtml(
  120. $curr_row.children('td')
  121. .children('.drop_primary_key_index_msg')
  122. .val()
  123. );
  124. $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
  125. var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingPrimaryKeyIndex, false);
  126. $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function (data) {
  127. if (data.success === true) {
  128. PMA_ajaxRemoveMessage($msg);
  129. var $table_ref = $rows_to_hide.closest('table');
  130. if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
  131. // We are about to remove all rows from the table
  132. $table_ref.hide('medium', function () {
  133. $('div.no_indexes_defined').show('medium');
  134. $rows_to_hide.remove();
  135. });
  136. $table_ref.siblings('div.notice').hide('medium');
  137. } else {
  138. // We are removing some of the rows only
  139. toggleRowColors($rows_to_hide.last().next());
  140. $rows_to_hide.hide("medium", function () {
  141. $(this).remove();
  142. });
  143. }
  144. if ($('#result_query').length) {
  145. $('#result_query').remove();
  146. }
  147. if (data.sql_query) {
  148. $('<div id="result_query"></div>')
  149. .html(data.sql_query)
  150. .prependTo('#page_content');
  151. PMA_highlightSQL($('#page_content'));
  152. }
  153. PMA_commonActions.refreshMain(false, function () {
  154. $("a.ajax[href^=#indexes]").click();
  155. });
  156. PMA_reloadNavigation();
  157. } else {
  158. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
  159. }
  160. }); // end $.get()
  161. }); // end $.PMA_confirm()
  162. }); //end Drop Primary Key/Index
  163. /**
  164. *Ajax event handler for index edit
  165. **/
  166. $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function (event) {
  167. event.preventDefault();
  168. var url, title;
  169. if ($(this).find("a").length === 0) {
  170. // Add index
  171. var valid = checkFormElementInRange(
  172. $(this).closest('form')[0],
  173. 'added_fields',
  174. 'Column count has to be larger than zero.'
  175. );
  176. if (! valid) {
  177. return;
  178. }
  179. url = $(this).closest('form').serialize();
  180. title = PMA_messages.strAddIndex;
  181. } else {
  182. // Edit index
  183. url = $(this).find("a").attr("href");
  184. if (url.substring(0, 16) == "tbl_indexes.php?") {
  185. url = url.substring(16, url.length);
  186. }
  187. title = PMA_messages.strEditIndex;
  188. }
  189. url += "&ajax_request=true";
  190. indexEditorDialog(url, title, function () {
  191. // refresh the page using ajax
  192. PMA_commonActions.refreshMain(false, function () {
  193. $("a.ajax[href^=#indexes]").click();
  194. });
  195. });
  196. });
  197. });