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