1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview function used for index manipulation pages
4 * @name Table Structure
8 * @required js/functions.js
12 * Hides/shows the inputs and submits appropriately depending
13 * on whether the index type chosen is 'SPATIAL' or not.
15 function checkIndexType()
18 * @var Object Dropdown to select the index choice.
20 var $select_index_choice = $('#select_index_choice');
22 * @var Object Dropdown to select the index type.
24 var $select_index_type = $('#select_index_type');
26 * @var Object Table header for the size column.
28 var $size_header = $('#index_columns thead tr th:nth-child(2)');
30 * @var Object Inputs to specify the columns for the index.
32 var $column_inputs = $('select[name="index[columns][names][]"]');
34 * @var Object Inputs to specify sizes for columns of the index.
36 var $size_inputs = $('input[name="index[columns][sub_parts][]"]');
38 * @var Object Footer containg the controllers to add more columns
40 var $add_more = $('#index_frm .add_more');
42 if ($select_index_choice.val() == 'SPATIAL') {
43 // Disable and hide the size column
45 $size_inputs.each(function () {
47 .prop('disabled', true)
51 // Disable and hide the columns of the index other than the first one
53 $column_inputs.each(function () {
54 $column_input = $(this);
57 .prop('disabled', true)
64 // Hide controllers to add more columns
67 // Enable and show the size column
69 $size_inputs.each(function () {
71 .prop('disabled', false)
75 // Enable and show the columns of the index
76 $column_inputs.each(function () {
78 .prop('disabled', false)
82 // Show controllers to add more columns
86 if ($select_index_choice.val() == 'SPATIAL' ||
87 $select_index_choice.val() == 'FULLTEXT') {
88 $select_index_type.val('').prop('disabled', true);
90 $select_index_type.prop('disabled', false)
95 * Sets current index information into form parameters.
97 * @param array source_array Array containing index columns
98 * @param string index_choice Choice of index
102 function PMA_setIndexFormParameters(source_array, index_choice)
104 if (index_choice == 'index') {
105 $('input[name="indexes"]').val(JSON.stringify(source_array));
107 $('input[name="' + index_choice + '_indexes"]').val(JSON.stringify(source_array));
112 * Removes a column from an Index.
114 * @param string col_index Index of column in form
118 function PMA_removeColumnFromIndex(col_index)
120 // Get previous index details.
121 var previous_index = $('select[name="field_key[' + col_index + ']"]')
123 if (previous_index.length) {
124 previous_index = previous_index.split(',');
125 var source_array = PMA_getIndexArray(previous_index[0]);
126 if (source_array == null) {
130 // Remove column from index array.
131 var source_length = source_array[previous_index[1]].columns.length;
132 for (var i=0; i<source_length; i++) {
133 if (source_array[previous_index[1]].columns[i].col_index == col_index) {
134 source_array[previous_index[1]].columns.splice(i, 1);
138 // Remove index completely if no columns left.
139 if (source_array[previous_index[1]].columns.length === 0) {
140 source_array.splice(previous_index[1], 1);
143 // Update current index details.
144 $('select[name="field_key[' + col_index + ']"]').attr('data-index', '');
145 // Update form index parameters.
146 PMA_setIndexFormParameters(source_array, previous_index[0].toLowerCase());
151 * Adds a column to an Index.
153 * @param array source_array Array holding corresponding indexes
154 * @param string array_index Index of an INDEX in array
155 * @param string index_choice Choice of Index
156 * @param string col_index Index of column on form
160 function PMA_addColumnToIndex(source_array, array_index, index_choice, col_index)
162 if (col_index >= 0) {
163 // Remove column from other indexes (if any).
164 PMA_removeColumnFromIndex(col_index);
166 var index_name = $('input[name="index[Key_name]"]').val();
167 var index_comment = $('input[name="index[Index_comment]"]').val();
168 var key_block_size = $('input[name="index[Key_block_size]"]').val();
169 var parser = $('input[name="index[Parser]"]').val();
170 var index_type = $('select[name="index[Index_type]"]').val();
172 $('#index_columns tbody').find('tr').each(function () {
173 // Get columns in particular order.
174 var col_index = $(this).find('select[name="index[columns][names][]"]').val();
175 var size = $(this).find('input[name="index[columns][sub_parts][]"]').val();
177 'col_index': col_index,
182 // Update or create an index.
183 source_array[array_index] = {
184 'Key_name': index_name,
185 'Index_comment': index_comment,
186 'Index_choice': index_choice.toUpperCase(),
187 'Key_block_size': key_block_size,
189 'Index_type': index_type,
193 // Display index name (or column list)
194 var displayName = index_name;
195 if (displayName == '') {
196 var columnNames = [];
197 $.each(columns, function () {
198 columnNames.push($('input[name="field_name[' + this.col_index + ']"]').val());
200 displayName = '[' + columnNames.join(', ') + ']';
202 $.each(columns, function () {
203 var id = 'index_name_' + this.col_index + '_8';
204 var $name = $('#' + id);
205 if ($name.length == 0) {
206 $name = $('<a id="' + id + '" href="#" class="ajax show_index_dialog"></a>');
207 $name.insertAfter($('select[name="field_key[' + this.col_index + ']"]'));
209 var $text = $('<small>').text(displayName);
213 if (col_index >= 0) {
214 // Update index details on form.
215 $('select[name="field_key[' + col_index + ']"]')
216 .attr('data-index', index_choice + ',' + array_index);
218 PMA_setIndexFormParameters(source_array, index_choice.toLowerCase());
222 * Get choices list for a column to create a composite index with.
224 * @param string index_choice Choice of index
225 * @param array source_array Array hodling columns for particular index
227 * @return jQuery Object
229 function PMA_getCompositeIndexList(source_array, col_index)
231 // Remove any previous list.
232 if ($('#composite_index_list').length) {
233 $('#composite_index_list').remove();
237 var $composite_index_list = $(
238 '<ul id="composite_index_list">' +
239 '<div>' + PMA_messages.strCompositeWith + '</div>' +
243 // Add each column to list available for composite index.
244 var source_length = source_array.length;
245 var already_present = false;
246 for (var i=0; i<source_length; i++) {
247 var sub_array_len = source_array[i].columns.length;
248 var column_names = [];
249 for (var j=0; j<sub_array_len; j++) {
251 $('input[name="field_name[' + source_array[i].columns[j].col_index + ']"]').val()
254 if (col_index == source_array[i].columns[j].col_index) {
255 already_present = true;
259 $composite_index_list.append(
261 '<input type="radio" name="composite_with" ' +
262 (already_present ? 'checked="checked"' : '') +
263 ' id="composite_index_' + i + '" value="' + i + '">' +
264 '<label for="composite_index_' + i + '">' + column_names.join(', ') +
270 return $composite_index_list;
274 * Shows 'Add Index' dialog.
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
284 function PMA_showAddIndexDialog(source_array, array_index, target_columns, col_index, index)
286 // Prepare post-data.
287 var $table = $('input[name="table"]');
288 var table = $table.length > 0 ? $table.val() : '';
290 token: $('input[name="token"]').val(),
291 server: $('input[name="server"]').val(),
292 db: $('input[name="db"]').val(),
295 create_edit_table: 1,
300 for (var i=0; i<target_columns.length; i++) {
301 var column_name = $('input[name="field_name[' + target_columns[i] + ']"]').val();
302 var column_type = $('select[name="field_type[' + target_columns[i] + ']"]').val().toLowerCase();
303 columns[column_name] = [column_type, target_columns[i]];
305 post_data.columns = JSON.stringify(columns);
307 var button_options = {};
308 button_options[PMA_messages.strGo] = function () {
309 var is_missing_value = false;
310 $('select[name="index[columns][names][]"]').each(function () {
311 if ($(this).val() === '') {
312 is_missing_value = true;
316 if (! is_missing_value) {
317 PMA_addColumnToIndex(
325 '<div class="error"><img src="themes/dot.gif" title="" alt=""' +
326 ' class="icon ic_s_error" /> ' + PMA_messages.strMissingColumn +
333 $(this).dialog('close');
335 button_options[PMA_messages.strCancel] = function () {
336 if (col_index >= 0) {
337 // Handle state on 'Cancel'.
338 var $select_list = $('select[name="field_key[' + col_index + ']"]');
339 if (! $select_list.attr('data-index').length) {
340 $select_list.find('option[value*="none"]').attr('selected', 'selected');
342 var previous_index = $select_list.attr('data-index').split(',');
343 $select_list.find('option[value*="' + previous_index[0].toLowerCase() + '"]')
344 .attr('selected', 'selected');
347 $(this).dialog('close');
349 var $msgbox = PMA_ajaxShowMessage();
350 $.post("tbl_indexes.php", post_data, function (data) {
351 if (data.success === false) {
352 //in the case of an error, show the error message returned.
353 PMA_ajaxShowMessage(data.error, false);
355 PMA_ajaxRemoveMessage($msgbox);
356 // Show dialog if the request was successful
357 var $div = $('<div/>');
359 .append(data.message)
361 title: PMA_messages.strAddIndex,
365 checkIndexName("index_frm");
368 $('#index_columns td').each(function () {
369 $(this).css("width", $(this).width() + 'px');
371 $('#index_columns tbody').sortable({
373 containment: $("#index_columns tbody"),
376 // We dont need the slider at this moment.
377 $(this).find('fieldset.tblFooters').remove();
380 buttons: button_options,
390 * Creates a advanced index type selection dialog.
392 * @param array source_array Array holding a particular type of indexes
393 * @param string index_choice Choice of index
394 * @param string col_index Index of new column on form
398 function PMA_indexTypeSelectionDialog(source_array, index_choice, col_index)
400 var $single_column_radio = $('<input type="radio" id="single_column" name="index_choice"' +
401 ' checked="checked">' +
402 '<label for="single_column">' + PMA_messages.strCreateSingleColumnIndex + '</label>');
403 var $composite_index_radio = $('<input type="radio" id="composite_index"' +
404 ' name="index_choice">' +
405 '<label for="composite_index">' + PMA_messages.strCreateCompositeIndex + '</label>');
406 var $dialog_content = $('<fieldset id="advance_index_creator"></fieldset>');
407 $dialog_content.append('<legend>' + index_choice.toUpperCase() + '</legend>');
410 // For UNIQUE/INDEX type, show choice for single-column and composite index.
411 $dialog_content.append($single_column_radio);
412 $dialog_content.append($composite_index_radio);
414 var button_options = {};
416 button_options[PMA_messages.strGo] = function () {
417 if ($('#single_column').is(':checked')) {
419 'Key_name': (index_choice == 'primary' ? 'PRIMARY' : ''),
420 'Index_choice': index_choice.toUpperCase()
422 PMA_showAddIndexDialog(source_array, (source_array.length), [col_index], col_index, index);
425 if ($('#composite_index').is(':checked')) {
426 if ($('input[name="composite_with"]').length !== 0 && $('input[name="composite_with"]:checked').length === 0
429 '<div class="error"><img src="themes/dot.gif" title=""' +
430 ' alt="" class="icon ic_s_error" /> ' +
431 PMA_messages.strFormEmpty +
438 var array_index = $('input[name="composite_with"]:checked').val();
439 var source_length = source_array[array_index].columns.length;
440 var target_columns = [];
441 for (var i=0; i<source_length; i++) {
442 target_columns.push(source_array[array_index].columns[i].col_index);
444 target_columns.push(col_index);
446 PMA_showAddIndexDialog(source_array, array_index, target_columns, col_index,
447 source_array[array_index]);
452 button_options[PMA_messages.strCancel] = function () {
453 // Handle state on 'Cancel'.
454 var $select_list = $('select[name="field_key[' + col_index + ']"]');
455 if (! $select_list.attr('data-index').length) {
456 $select_list.find('option[value*="none"]').attr('selected', 'selected');
458 var previous_index = $select_list.attr('data-index').split(',');
459 $select_list.find('option[value*="' + previous_index[0].toLowerCase() + '"]')
460 .attr('selected', 'selected');
464 var $dialog = $('<div/>').append($dialog_content).dialog({
468 title: PMA_messages.strAddIndex,
470 buttons: button_options,
472 $('#composite_index').on('change', function () {
473 if ($(this).is(':checked')) {
474 $dialog_content.append(PMA_getCompositeIndexList(source_array, col_index));
477 $('#single_column').on('change', function () {
478 if ($(this).is(':checked')) {
479 if ($('#composite_index_list').length) {
480 $('#composite_index_list').remove();
486 $('#composite_index').off('change');
487 $('#single_column').off('change');
494 * Returns the array of indexes based on the index choice
496 * @param index_choice index choice
498 function PMA_getIndexArray(index_choice)
500 var source_array = null;
502 switch (index_choice.toLowerCase()) {
504 source_array = primary_indexes;
507 source_array = unique_indexes;
510 source_array = indexes;
513 source_array = fulltext_indexes;
516 source_array = spatial_indexes;
526 * Unbind all event handlers before tearing down a page
528 AJAX.registerTeardown('indexes.js', function () {
529 $(document).off('click', '#save_index_frm');
530 $(document).off('click', '#preview_index_frm');
531 $(document).off('change', '#select_index_choice');
532 $(document).off('click', 'a.drop_primary_key_index_anchor.ajax');
533 $(document).off('click', "#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax");
534 $(document).off('click', '#index_frm input[type=submit]');
535 $('body').off('change', 'select[name*="field_key"]');
536 $(document).off('click', '.show_index_dialog');
540 * @description <p>Ajax scripts for table index page</p>
542 * Actions ajaxified here:
544 * <li>Showing/hiding inputs depending on the index type chosen</li>
545 * <li>create/edit/drop indexes</li>
548 AJAX.registerOnload('indexes.js', function () {
549 // Re-initialize variables.
550 primary_indexes = [];
553 fulltext_indexes = [];
554 spatial_indexes = [];
556 // for table creation form
557 var $engine_selector = $('.create_table_form select[name=tbl_storage_engine]');
558 if ($engine_selector.length) {
559 PMA_hideShowConnection($engine_selector);
562 var $form = $("#index_frm");
563 if ($form.length > 0) {
564 showIndexEditDialog($form);
567 $(document).on('click', '#save_index_frm', function (event) {
568 event.preventDefault();
569 var $form = $("#index_frm");
570 var submitData = $form.serialize() + '&do_save_data=1&ajax_request=true&ajax_page_request=true';
571 var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
573 $.post($form.attr('action'), submitData, AJAX.responseHandler);
576 $(document).on('click', '#preview_index_frm', function (event) {
577 event.preventDefault();
578 PMA_previewSQL($('#index_frm'));
581 $(document).on('change', '#select_index_choice', function (event) {
582 event.preventDefault();
584 checkIndexName("index_frm");
588 * Ajax Event handler for 'Drop Index'
590 $(document).on('click', 'a.drop_primary_key_index_anchor.ajax', function (event) {
591 event.preventDefault();
592 var $anchor = $(this);
594 * @var $curr_row Object containing reference to the current field's row
596 var $curr_row = $anchor.parents('tr');
597 /** @var Number of columns in the key */
598 var rows = $anchor.parents('td').attr('rowspan') || 1;
599 /** @var Rows that should be hidden */
600 var $rows_to_hide = $curr_row;
601 for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
602 $rows_to_hide = $rows_to_hide.add($last_row);
605 var question = escapeHtml(
606 $curr_row.children('td')
607 .children('.drop_primary_key_index_msg')
611 $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
612 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingPrimaryKeyIndex, false);
613 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function (data) {
614 if (typeof data !== 'undefined' && data.success === true) {
615 PMA_ajaxRemoveMessage($msg);
616 var $table_ref = $rows_to_hide.closest('table');
617 if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
618 // We are about to remove all rows from the table
619 $table_ref.hide('medium', function () {
620 $('div.no_indexes_defined').show('medium');
621 $rows_to_hide.remove();
623 $table_ref.siblings('div.notice').hide('medium');
625 // We are removing some of the rows only
626 toggleRowColors($rows_to_hide.last().next());
627 $rows_to_hide.hide("medium", function () {
631 if ($('.result_query').length) {
632 $('.result_query').remove();
634 if (data.sql_query) {
635 $('<div class="result_query"></div>')
636 .html(data.sql_query)
637 .prependTo('#structure_content');
638 PMA_highlightSQL($('#page_content'));
640 PMA_commonActions.refreshMain(false, function () {
641 $("a.ajax[href^=#indexes]").click();
643 PMA_reloadNavigation();
645 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
648 }); // end $.PMA_confirm()
649 }); //end Drop Primary Key/Index
652 *Ajax event handler for index edit
654 $(document).on('click', "#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax", function (event) {
655 event.preventDefault();
657 if ($(this).find("a").length === 0) {
659 var valid = checkFormElementInRange(
660 $(this).closest('form')[0],
662 'Column count has to be larger than zero.'
667 url = $(this).closest('form').serialize();
668 title = PMA_messages.strAddIndex;
671 url = $(this).find("a").attr("href");
672 if (url.substring(0, 16) == "tbl_indexes.php?") {
673 url = url.substring(16, url.length);
675 title = PMA_messages.strEditIndex;
677 url += "&ajax_request=true";
678 indexEditorDialog(url, title, function () {
679 // refresh the page using ajax
680 PMA_commonActions.refreshMain(false, function () {
681 $("a.ajax[href^=#indexes]").click();
687 * Ajax event handler for advanced index creation during table creation
688 * and column addition.
690 $('body').on('change', 'select[name*="field_key"]', function () {
691 // Index of column on Table edit and create page.
692 var col_index = /\d+/.exec($(this).attr('name'));
693 col_index = col_index[0];
694 // Choice of selected index.
695 var index_choice = /[a-z]+/.exec($(this).val());
696 index_choice = index_choice[0];
697 // Array containing corresponding indexes.
698 var source_array = null;
700 if (index_choice == 'none') {
701 PMA_removeColumnFromIndex(col_index);
705 // Select a source array.
706 var source_array = PMA_getIndexArray(index_choice);
707 if (source_array == null) {
711 if (source_array.length === 0) {
713 'Key_name': (index_choice == 'primary' ? 'PRIMARY' : ''),
714 'Index_choice': index_choice.toUpperCase()
716 PMA_showAddIndexDialog(source_array, 0, [col_index], col_index, index);
718 if (index_choice == 'primary') {
720 var source_length = source_array[array_index].columns.length;
721 var target_columns = [];
722 for (var i=0; i<source_length; i++) {
723 target_columns.push(source_array[array_index].columns[i].col_index);
725 target_columns.push(col_index);
727 PMA_showAddIndexDialog(source_array, array_index, target_columns, col_index,
728 source_array[array_index]);
730 // If there are multiple columns selected for an index, show advanced dialog.
731 PMA_indexTypeSelectionDialog(source_array, index_choice, col_index);
736 $(document).on('click', '.show_index_dialog', function (e) {
739 // Get index details.
740 var previous_index = $(this).prev('select')
744 var index_choice = previous_index[0];
745 var array_index = previous_index[1];
747 var source_array = PMA_getIndexArray(index_choice);
748 var source_length = source_array[array_index].columns.length;
750 var target_columns = [];
751 for (var i = 0; i < source_length; i++) {
752 target_columns.push(source_array[array_index].columns[i].col_index);
755 PMA_showAddIndexDialog(source_array, array_index, target_columns, -1, source_array[array_index]);