indexes.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. * Returns the array of indexes based on the index choice
  12. *
  13. * @param index_choice index choice
  14. */
  15. function PMA_getIndexArray (index_choice) {
  16. var source_array = null;
  17. switch (index_choice.toLowerCase()) {
  18. case 'primary':
  19. source_array = primary_indexes;
  20. break;
  21. case 'unique':
  22. source_array = unique_indexes;
  23. break;
  24. case 'index':
  25. source_array = indexes;
  26. break;
  27. case 'fulltext':
  28. source_array = fulltext_indexes;
  29. break;
  30. case 'spatial':
  31. source_array = spatial_indexes;
  32. break;
  33. default:
  34. return null;
  35. }
  36. return source_array;
  37. }
  38. /**
  39. * Hides/shows the inputs and submits appropriately depending
  40. * on whether the index type chosen is 'SPATIAL' or not.
  41. */
  42. function checkIndexType () {
  43. /**
  44. * @var Object Dropdown to select the index choice.
  45. */
  46. var $select_index_choice = $('#select_index_choice');
  47. /**
  48. * @var Object Dropdown to select the index type.
  49. */
  50. var $select_index_type = $('#select_index_type');
  51. /**
  52. * @var Object Table header for the size column.
  53. */
  54. var $size_header = $('#index_columns').find('thead tr th:nth-child(2)');
  55. /**
  56. * @var Object Inputs to specify the columns for the index.
  57. */
  58. var $column_inputs = $('select[name="index[columns][names][]"]');
  59. /**
  60. * @var Object Inputs to specify sizes for columns of the index.
  61. */
  62. var $size_inputs = $('input[name="index[columns][sub_parts][]"]');
  63. /**
  64. * @var Object Footer containg the controllers to add more columns
  65. */
  66. var $add_more = $('#index_frm').find('.add_more');
  67. if ($select_index_choice.val() === 'SPATIAL') {
  68. // Disable and hide the size column
  69. $size_header.hide();
  70. $size_inputs.each(function () {
  71. $(this)
  72. .prop('disabled', true)
  73. .parent('td').hide();
  74. });
  75. // Disable and hide the columns of the index other than the first one
  76. var initial = true;
  77. $column_inputs.each(function () {
  78. $column_input = $(this);
  79. if (! initial) {
  80. $column_input
  81. .prop('disabled', true)
  82. .parent('td').hide();
  83. } else {
  84. initial = false;
  85. }
  86. });
  87. // Hide controllers to add more columns
  88. $add_more.hide();
  89. } else {
  90. // Enable and show the size column
  91. $size_header.show();
  92. $size_inputs.each(function () {
  93. $(this)
  94. .prop('disabled', false)
  95. .parent('td').show();
  96. });
  97. // Enable and show the columns of the index
  98. $column_inputs.each(function () {
  99. $(this)
  100. .prop('disabled', false)
  101. .parent('td').show();
  102. });
  103. // Show controllers to add more columns
  104. $add_more.show();
  105. }
  106. if ($select_index_choice.val() === 'SPATIAL' ||
  107. $select_index_choice.val() === 'FULLTEXT') {
  108. $select_index_type.val('').prop('disabled', true);
  109. } else {
  110. $select_index_type.prop('disabled', false);
  111. }
  112. }
  113. /**
  114. * Sets current index information into form parameters.
  115. *
  116. * @param array source_array Array containing index columns
  117. * @param string index_choice Choice of index
  118. *
  119. * @return void
  120. */
  121. function PMA_setIndexFormParameters (source_array, index_choice) {
  122. if (index_choice === 'index') {
  123. $('input[name="indexes"]').val(JSON.stringify(source_array));
  124. } else {
  125. $('input[name="' + index_choice + '_indexes"]').val(JSON.stringify(source_array));
  126. }
  127. }
  128. /**
  129. * Removes a column from an Index.
  130. *
  131. * @param string col_index Index of column in form
  132. *
  133. * @return void
  134. */
  135. function PMA_removeColumnFromIndex (col_index) {
  136. // Get previous index details.
  137. var previous_index = $('select[name="field_key[' + col_index + ']"]')
  138. .attr('data-index');
  139. if (previous_index.length) {
  140. previous_index = previous_index.split(',');
  141. var source_array = PMA_getIndexArray(previous_index[0]);
  142. if (source_array === null) {
  143. return;
  144. }
  145. // Remove column from index array.
  146. var source_length = source_array[previous_index[1]].columns.length;
  147. for (var i = 0; i < source_length; i++) {
  148. if (source_array[previous_index[1]].columns[i].col_index === col_index) {
  149. source_array[previous_index[1]].columns.splice(i, 1);
  150. }
  151. }
  152. // Remove index completely if no columns left.
  153. if (source_array[previous_index[1]].columns.length === 0) {
  154. source_array.splice(previous_index[1], 1);
  155. }
  156. // Update current index details.
  157. $('select[name="field_key[' + col_index + ']"]').attr('data-index', '');
  158. // Update form index parameters.
  159. PMA_setIndexFormParameters(source_array, previous_index[0].toLowerCase());
  160. }
  161. }
  162. /**
  163. * Adds a column to an Index.
  164. *
  165. * @param array source_array Array holding corresponding indexes
  166. * @param string array_index Index of an INDEX in array
  167. * @param string index_choice Choice of Index
  168. * @param string col_index Index of column on form
  169. *
  170. * @return void
  171. */
  172. function PMA_addColumnToIndex (source_array, array_index, index_choice, col_index) {
  173. if (col_index >= 0) {
  174. // Remove column from other indexes (if any).
  175. PMA_removeColumnFromIndex(col_index);
  176. }
  177. var index_name = $('input[name="index[Key_name]"]').val();
  178. var index_comment = $('input[name="index[Index_comment]"]').val();
  179. var key_block_size = $('input[name="index[Key_block_size]"]').val();
  180. var parser = $('input[name="index[Parser]"]').val();
  181. var index_type = $('select[name="index[Index_type]"]').val();
  182. var columns = [];
  183. $('#index_columns').find('tbody').find('tr').each(function () {
  184. // Get columns in particular order.
  185. var col_index = $(this).find('select[name="index[columns][names][]"]').val();
  186. var size = $(this).find('input[name="index[columns][sub_parts][]"]').val();
  187. columns.push({
  188. 'col_index': col_index,
  189. 'size': size
  190. });
  191. });
  192. // Update or create an index.
  193. source_array[array_index] = {
  194. 'Key_name': index_name,
  195. 'Index_comment': index_comment,
  196. 'Index_choice': index_choice.toUpperCase(),
  197. 'Key_block_size': key_block_size,
  198. 'Parser': parser,
  199. 'Index_type': index_type,
  200. 'columns': columns
  201. };
  202. // Display index name (or column list)
  203. var displayName = index_name;
  204. if (displayName === '') {
  205. var columnNames = [];
  206. $.each(columns, function () {
  207. columnNames.push($('input[name="field_name[' + this.col_index + ']"]').val());
  208. });
  209. displayName = '[' + columnNames.join(', ') + ']';
  210. }
  211. $.each(columns, function () {
  212. var id = 'index_name_' + this.col_index + '_8';
  213. var $name = $('#' + id);
  214. if ($name.length === 0) {
  215. $name = $('<a id="' + id + '" href="#" class="ajax show_index_dialog"></a>');
  216. $name.insertAfter($('select[name="field_key[' + this.col_index + ']"]'));
  217. }
  218. var $text = $('<small>').text(displayName);
  219. $name.html($text);
  220. });
  221. if (col_index >= 0) {
  222. // Update index details on form.
  223. $('select[name="field_key[' + col_index + ']"]')
  224. .attr('data-index', index_choice + ',' + array_index);
  225. }
  226. PMA_setIndexFormParameters(source_array, index_choice.toLowerCase());
  227. }
  228. /**
  229. * Get choices list for a column to create a composite index with.
  230. *
  231. * @param string index_choice Choice of index
  232. * @param array source_array Array hodling columns for particular index
  233. *
  234. * @return jQuery Object
  235. */
  236. function PMA_getCompositeIndexList (source_array, col_index) {
  237. // Remove any previous list.
  238. if ($('#composite_index_list').length) {
  239. $('#composite_index_list').remove();
  240. }
  241. // Html list.
  242. var $composite_index_list = $(
  243. '<ul id="composite_index_list">' +
  244. '<div>' + PMA_messages.strCompositeWith + '</div>' +
  245. '</ul>'
  246. );
  247. // Add each column to list available for composite index.
  248. var source_length = source_array.length;
  249. var already_present = false;
  250. for (var i = 0; i < source_length; i++) {
  251. var sub_array_len = source_array[i].columns.length;
  252. var column_names = [];
  253. for (var j = 0; j < sub_array_len; j++) {
  254. column_names.push(
  255. $('input[name="field_name[' + source_array[i].columns[j].col_index + ']"]').val()
  256. );
  257. if (col_index === source_array[i].columns[j].col_index) {
  258. already_present = true;
  259. }
  260. }
  261. $composite_index_list.append(
  262. '<li>' +
  263. '<input type="radio" name="composite_with" ' +
  264. (already_present ? 'checked="checked"' : '') +
  265. ' id="composite_index_' + i + '" value="' + i + '">' +
  266. '<label for="composite_index_' + i + '">' + column_names.join(', ') +
  267. '</lablel>' +
  268. '</li>'
  269. );
  270. }
  271. return $composite_index_list;
  272. }
  273. /**
  274. * Shows 'Add Index' dialog.
  275. *
  276. * @param array source_array Array holding particluar index
  277. * @param string array_index Index of an INDEX in array
  278. * @param array target_columns Columns for an INDEX
  279. * @param string col_index Index of column on form
  280. * @param object index Index detail object
  281. *
  282. * @return void
  283. */
  284. function PMA_showAddIndexDialog (source_array, array_index, target_columns, col_index, index) {
  285. // Prepare post-data.
  286. var $table = $('input[name="table"]');
  287. var table = $table.length > 0 ? $table.val() : '';
  288. var post_data = {
  289. server: PMA_commonParams.get('server'),
  290. db: $('input[name="db"]').val(),
  291. table: table,
  292. ajax_request: 1,
  293. create_edit_table: 1,
  294. index: index
  295. };
  296. var columns = {};
  297. for (var i = 0; i < target_columns.length; i++) {
  298. var column_name = $('input[name="field_name[' + target_columns[i] + ']"]').val();
  299. var column_type = $('select[name="field_type[' + target_columns[i] + ']"]').val().toLowerCase();
  300. columns[column_name] = [column_type, target_columns[i]];
  301. }
  302. post_data.columns = JSON.stringify(columns);
  303. var button_options = {};
  304. button_options[PMA_messages.strGo] = function () {
  305. var is_missing_value = false;
  306. $('select[name="index[columns][names][]"]').each(function () {
  307. if ($(this).val() === '') {
  308. is_missing_value = true;
  309. }
  310. });
  311. if (! is_missing_value) {
  312. PMA_addColumnToIndex(
  313. source_array,
  314. array_index,
  315. index.Index_choice,
  316. col_index
  317. );
  318. } else {
  319. PMA_ajaxShowMessage(
  320. '<div class="error"><img src="themes/dot.gif" title="" alt=""' +
  321. ' class="icon ic_s_error" /> ' + PMA_messages.strMissingColumn +
  322. ' </div>', false
  323. );
  324. return false;
  325. }
  326. $(this).dialog('close');
  327. };
  328. button_options[PMA_messages.strCancel] = function () {
  329. if (col_index >= 0) {
  330. // Handle state on 'Cancel'.
  331. var $select_list = $('select[name="field_key[' + col_index + ']"]');
  332. if (! $select_list.attr('data-index').length) {
  333. $select_list.find('option[value*="none"]').attr('selected', 'selected');
  334. } else {
  335. var previous_index = $select_list.attr('data-index').split(',');
  336. $select_list.find('option[value*="' + previous_index[0].toLowerCase() + '"]')
  337. .attr('selected', 'selected');
  338. }
  339. }
  340. $(this).dialog('close');
  341. };
  342. var $msgbox = PMA_ajaxShowMessage();
  343. $.post('tbl_indexes.php', post_data, function (data) {
  344. if (data.success === false) {
  345. // in the case of an error, show the error message returned.
  346. PMA_ajaxShowMessage(data.error, false);
  347. } else {
  348. PMA_ajaxRemoveMessage($msgbox);
  349. // Show dialog if the request was successful
  350. var $div = $('<div/>');
  351. $div
  352. .append(data.message)
  353. .dialog({
  354. title: PMA_messages.strAddIndex,
  355. width: 450,
  356. minHeight: 250,
  357. open: function () {
  358. checkIndexName('index_frm');
  359. PMA_showHints($div);
  360. PMA_init_slider();
  361. $('#index_columns').find('td').each(function () {
  362. $(this).css('width', $(this).width() + 'px');
  363. });
  364. $('#index_columns').find('tbody').sortable({
  365. axis: 'y',
  366. containment: $('#index_columns').find('tbody'),
  367. tolerance: 'pointer'
  368. });
  369. // We dont need the slider at this moment.
  370. $(this).find('fieldset.tblFooters').remove();
  371. },
  372. modal: true,
  373. buttons: button_options,
  374. close: function () {
  375. $(this).remove();
  376. }
  377. });
  378. }
  379. });
  380. }
  381. /**
  382. * Creates a advanced index type selection dialog.
  383. *
  384. * @param array source_array Array holding a particular type of indexes
  385. * @param string index_choice Choice of index
  386. * @param string col_index Index of new column on form
  387. *
  388. * @return void
  389. */
  390. function PMA_indexTypeSelectionDialog (source_array, index_choice, col_index) {
  391. var $single_column_radio = $('<input type="radio" id="single_column" name="index_choice"' +
  392. ' checked="checked">' +
  393. '<label for="single_column">' + PMA_messages.strCreateSingleColumnIndex + '</label>');
  394. var $composite_index_radio = $('<input type="radio" id="composite_index"' +
  395. ' name="index_choice">' +
  396. '<label for="composite_index">' + PMA_messages.strCreateCompositeIndex + '</label>');
  397. var $dialog_content = $('<fieldset id="advance_index_creator"></fieldset>');
  398. $dialog_content.append('<legend>' + index_choice.toUpperCase() + '</legend>');
  399. // For UNIQUE/INDEX type, show choice for single-column and composite index.
  400. $dialog_content.append($single_column_radio);
  401. $dialog_content.append($composite_index_radio);
  402. var button_options = {};
  403. // 'OK' operation.
  404. button_options[PMA_messages.strGo] = function () {
  405. if ($('#single_column').is(':checked')) {
  406. var index = {
  407. 'Key_name': (index_choice === 'primary' ? 'PRIMARY' : ''),
  408. 'Index_choice': index_choice.toUpperCase()
  409. };
  410. PMA_showAddIndexDialog(source_array, (source_array.length), [col_index], col_index, index);
  411. }
  412. if ($('#composite_index').is(':checked')) {
  413. if ($('input[name="composite_with"]').length !== 0 && $('input[name="composite_with"]:checked').length === 0
  414. ) {
  415. PMA_ajaxShowMessage(
  416. '<div class="error"><img src="themes/dot.gif" title=""' +
  417. ' alt="" class="icon ic_s_error" /> ' +
  418. PMA_messages.strFormEmpty +
  419. ' </div>',
  420. false
  421. );
  422. return false;
  423. }
  424. var array_index = $('input[name="composite_with"]:checked').val();
  425. var source_length = source_array[array_index].columns.length;
  426. var target_columns = [];
  427. for (var i = 0; i < source_length; i++) {
  428. target_columns.push(source_array[array_index].columns[i].col_index);
  429. }
  430. target_columns.push(col_index);
  431. PMA_showAddIndexDialog(source_array, array_index, target_columns, col_index,
  432. source_array[array_index]);
  433. }
  434. $(this).remove();
  435. };
  436. button_options[PMA_messages.strCancel] = function () {
  437. // Handle state on 'Cancel'.
  438. var $select_list = $('select[name="field_key[' + col_index + ']"]');
  439. if (! $select_list.attr('data-index').length) {
  440. $select_list.find('option[value*="none"]').attr('selected', 'selected');
  441. } else {
  442. var previous_index = $select_list.attr('data-index').split(',');
  443. $select_list.find('option[value*="' + previous_index[0].toLowerCase() + '"]')
  444. .attr('selected', 'selected');
  445. }
  446. $(this).remove();
  447. };
  448. var $dialog = $('<div/>').append($dialog_content).dialog({
  449. minWidth: 525,
  450. minHeight: 200,
  451. modal: true,
  452. title: PMA_messages.strAddIndex,
  453. resizable: false,
  454. buttons: button_options,
  455. open: function () {
  456. $('#composite_index').on('change', function () {
  457. if ($(this).is(':checked')) {
  458. $dialog_content.append(PMA_getCompositeIndexList(source_array, col_index));
  459. }
  460. });
  461. $('#single_column').on('change', function () {
  462. if ($(this).is(':checked')) {
  463. if ($('#composite_index_list').length) {
  464. $('#composite_index_list').remove();
  465. }
  466. }
  467. });
  468. },
  469. close: function () {
  470. $('#composite_index').off('change');
  471. $('#single_column').off('change');
  472. $(this).remove();
  473. }
  474. });
  475. }
  476. /**
  477. * Unbind all event handlers before tearing down a page
  478. */
  479. AJAX.registerTeardown('indexes.js', function () {
  480. $(document).off('click', '#save_index_frm');
  481. $(document).off('click', '#preview_index_frm');
  482. $(document).off('change', '#select_index_choice');
  483. $(document).off('click', 'a.drop_primary_key_index_anchor.ajax');
  484. $(document).off('click', '#table_index tbody tr td.edit_index.ajax, #index_div .add_index.ajax');
  485. $(document).off('click', '#index_frm input[type=submit]');
  486. $('body').off('change', 'select[name*="field_key"]');
  487. $(document).off('click', '.show_index_dialog');
  488. });
  489. /**
  490. * @description <p>Ajax scripts for table index page</p>
  491. *
  492. * Actions ajaxified here:
  493. * <ul>
  494. * <li>Showing/hiding inputs depending on the index type chosen</li>
  495. * <li>create/edit/drop indexes</li>
  496. * </ul>
  497. */
  498. AJAX.registerOnload('indexes.js', function () {
  499. // Re-initialize variables.
  500. primary_indexes = [];
  501. unique_indexes = [];
  502. indexes = [];
  503. fulltext_indexes = [];
  504. spatial_indexes = [];
  505. // for table creation form
  506. var $engine_selector = $('.create_table_form select[name=tbl_storage_engine]');
  507. if ($engine_selector.length) {
  508. PMA_hideShowConnection($engine_selector);
  509. }
  510. var $form = $('#index_frm');
  511. if ($form.length > 0) {
  512. showIndexEditDialog($form);
  513. }
  514. $(document).on('click', '#save_index_frm', function (event) {
  515. event.preventDefault();
  516. var $form = $('#index_frm');
  517. var argsep = PMA_commonParams.get('arg_separator');
  518. var submitData = $form.serialize() + argsep + 'do_save_data=1' + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  519. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  520. AJAX.source = $form;
  521. $.post($form.attr('action'), submitData, AJAX.responseHandler);
  522. });
  523. $(document).on('click', '#preview_index_frm', function (event) {
  524. event.preventDefault();
  525. PMA_previewSQL($('#index_frm'));
  526. });
  527. $(document).on('change', '#select_index_choice', function (event) {
  528. event.preventDefault();
  529. checkIndexType();
  530. checkIndexName('index_frm');
  531. });
  532. /**
  533. * Ajax Event handler for 'Drop Index'
  534. */
  535. $(document).on('click', 'a.drop_primary_key_index_anchor.ajax', function (event) {
  536. event.preventDefault();
  537. var $anchor = $(this);
  538. /**
  539. * @var $curr_row Object containing reference to the current field's row
  540. */
  541. var $curr_row = $anchor.parents('tr');
  542. /** @var Number of columns in the key */
  543. var rows = $anchor.parents('td').attr('rowspan') || 1;
  544. /** @var Rows that should be hidden */
  545. var $rows_to_hide = $curr_row;
  546. for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
  547. $rows_to_hide = $rows_to_hide.add($last_row);
  548. }
  549. var question = escapeHtml(
  550. $curr_row.children('td')
  551. .children('.drop_primary_key_index_msg')
  552. .val()
  553. );
  554. PMA_confirmPreviewSQL(question, $anchor.attr('href'), function (url) {
  555. var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingPrimaryKeyIndex, false);
  556. var params = getJSConfirmCommonParam(this, $anchor.getPostData());
  557. $.post(url, params, function (data) {
  558. if (typeof data !== 'undefined' && data.success === true) {
  559. PMA_ajaxRemoveMessage($msg);
  560. var $table_ref = $rows_to_hide.closest('table');
  561. if ($rows_to_hide.length === $table_ref.find('tbody > tr').length) {
  562. // We are about to remove all rows from the table
  563. $table_ref.hide('medium', function () {
  564. $('div.no_indexes_defined').show('medium');
  565. $rows_to_hide.remove();
  566. });
  567. $table_ref.siblings('div.notice').hide('medium');
  568. } else {
  569. // We are removing some of the rows only
  570. $rows_to_hide.hide('medium', function () {
  571. $(this).remove();
  572. });
  573. }
  574. if ($('.result_query').length) {
  575. $('.result_query').remove();
  576. }
  577. if (data.sql_query) {
  578. $('<div class="result_query"></div>')
  579. .html(data.sql_query)
  580. .prependTo('#structure_content');
  581. PMA_highlightSQL($('#page_content'));
  582. }
  583. PMA_commonActions.refreshMain(false, function () {
  584. $('a.ajax[href^=#indexes]').click();
  585. });
  586. PMA_reloadNavigation();
  587. } else {
  588. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
  589. }
  590. }); // end $.post()
  591. }); // end $.PMA_confirm()
  592. }); // end Drop Primary Key/Index
  593. /**
  594. *Ajax event handler for index edit
  595. **/
  596. $(document).on('click', '#table_index tbody tr td.edit_index.ajax, #index_div .add_index.ajax', function (event) {
  597. event.preventDefault();
  598. var url;
  599. var title;
  600. if ($(this).find('a').length === 0) {
  601. // Add index
  602. var valid = checkFormElementInRange(
  603. $(this).closest('form')[0],
  604. 'added_fields',
  605. 'Column count has to be larger than zero.'
  606. );
  607. if (! valid) {
  608. return;
  609. }
  610. url = $(this).closest('form').serialize();
  611. title = PMA_messages.strAddIndex;
  612. } else {
  613. // Edit index
  614. url = $(this).find('a').getPostData();
  615. title = PMA_messages.strEditIndex;
  616. }
  617. url += PMA_commonParams.get('arg_separator') + 'ajax_request=true';
  618. indexEditorDialog(url, title, function () {
  619. // refresh the page using ajax
  620. PMA_commonActions.refreshMain(false, function () {
  621. $('a.ajax[href^=#indexes]').click();
  622. });
  623. });
  624. });
  625. /**
  626. * Ajax event handler for advanced index creation during table creation
  627. * and column addition.
  628. */
  629. $('body').on('change', 'select[name*="field_key"]', function () {
  630. // Index of column on Table edit and create page.
  631. var col_index = /\d+/.exec($(this).attr('name'));
  632. col_index = col_index[0];
  633. // Choice of selected index.
  634. var index_choice = /[a-z]+/.exec($(this).val());
  635. index_choice = index_choice[0];
  636. // Array containing corresponding indexes.
  637. var source_array = null;
  638. if (index_choice === 'none') {
  639. PMA_removeColumnFromIndex(col_index);
  640. var id = 'index_name_' + '0' + '_8';
  641. var $name = $('#' + id);
  642. if ($name.length === 0) {
  643. $name = $('<a id="' + id + '" href="#" class="ajax show_index_dialog"></a>');
  644. $name.insertAfter($('select[name="field_key[' + '0' + ']"]'));
  645. }
  646. $name.html('');
  647. return false;
  648. }
  649. // Select a source array.
  650. source_array = PMA_getIndexArray(index_choice);
  651. if (source_array === null) {
  652. return;
  653. }
  654. if (source_array.length === 0) {
  655. var index = {
  656. 'Key_name': (index_choice === 'primary' ? 'PRIMARY' : ''),
  657. 'Index_choice': index_choice.toUpperCase()
  658. };
  659. PMA_showAddIndexDialog(source_array, 0, [col_index], col_index, index);
  660. } else {
  661. if (index_choice === 'primary') {
  662. var array_index = 0;
  663. var source_length = source_array[array_index].columns.length;
  664. var target_columns = [];
  665. for (var i = 0; i < source_length; i++) {
  666. target_columns.push(source_array[array_index].columns[i].col_index);
  667. }
  668. target_columns.push(col_index);
  669. PMA_showAddIndexDialog(source_array, array_index, target_columns, col_index,
  670. source_array[array_index]);
  671. } else {
  672. // If there are multiple columns selected for an index, show advanced dialog.
  673. PMA_indexTypeSelectionDialog(source_array, index_choice, col_index);
  674. }
  675. }
  676. });
  677. $(document).on('click', '.show_index_dialog', function (e) {
  678. e.preventDefault();
  679. // Get index details.
  680. var previous_index = $(this).prev('select')
  681. .attr('data-index')
  682. .split(',');
  683. var index_choice = previous_index[0];
  684. var array_index = previous_index[1];
  685. var source_array = PMA_getIndexArray(index_choice);
  686. if (source_array !== null) {
  687. var source_length = source_array[array_index].columns.length;
  688. var target_columns = [];
  689. for (var i = 0; i < source_length; i++) {
  690. target_columns.push(source_array[array_index].columns[i].col_index);
  691. }
  692. PMA_showAddIndexDialog(source_array, array_index, target_columns, -1, source_array[array_index]);
  693. }
  694. });
  695. $('#index_frm').on('submit', function () {
  696. if (typeof(this.elements['index[Key_name]'].disabled) !== 'undefined') {
  697. this.elements['index[Key_name]'].disabled = false;
  698. }
  699. });
  700. });