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