indexes.js 26 KB

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