Speed up PMA_getImage()
[phpmyadmin/roccivic.git] / js / tbl_structure.js
blob5564bc0b4021e1c22a0ee553e99900124986b626
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used on the table structure page
4  * @name            Table Structure
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @required    js/functions.js
9  */
11 /**
12  * AJAX scripts for tbl_structure.php
13  *
14  * Actions ajaxified here:
15  * Drop Column
16  * Add Primary Key
17  * Drop Primary Key/Index
18  *
19  */
20 $(document).ready(function() {
21     /**
22      * Attach Event Handler for 'Drop Column'
23      *
24      * @uses    $.PMA_confirm()
25      * @uses    PMA_ajaxShowMessage()
26      * (see $GLOBALS['cfg']['AjaxEnable'])
27      */
28     $(".drop_column_anchor").live('click', function(event) {
29         event.preventDefault();
31         /**
32          * @var curr_table_name String containing the name of the current table
33          */
34         var curr_table_name = window.parent.table;
35         /**
36          * @var curr_row    Object reference to the currently selected row (i.e. field in the table)
37          */
38         var $curr_row = $(this).parents('tr');
39         /**
40          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
41          */
42         var curr_column_name = $curr_row.children('th').children('label').text();
43         /**
44          * @var $after_field_item    Corresponding entry in the 'After' field.
45          */
46         var $after_field_item = $("select[name='after_field'] option[value='" + curr_column_name + "']");
47         /**
48          * @var question    String containing the question to be asked for confirmation
49          */
50         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` DROP `' + escapeHtml(curr_column_name) + '`';
52         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
54             PMA_ajaxShowMessage(PMA_messages['strDroppingColumn'], false);
56             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
57                 if(data.success == true) {
58                     PMA_ajaxShowMessage(data.message);
59                     toggleRowColors($curr_row.next());
60                     // Adjust the row numbers
61                     for (var $row = $curr_row.next(); $row.length > 0; $row = $row.next()) {
62                         var new_val = parseInt($row.find('td:nth-child(2)').text()) - 1;
63                         $row.find('td:nth-child(2)').text(new_val);
64                     }
65                     $after_field_item.remove();
66                     $curr_row.hide("medium").remove();
67                 }
68                 else {
69                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
70                 }
71             }) // end $.get()
72         }); // end $.PMA_confirm()
73     }) ; //end of Drop Column Anchor action
75     /**
76      * Ajax Event handler for 'Add Primary Key'
77      *
78      * @uses    $.PMA_confirm()
79      * @uses    PMA_ajaxShowMessage()
80      * (see $GLOBALS['cfg']['AjaxEnable'])
81      */
82     $(".action_primary a").live('click', function(event) {
83         event.preventDefault();
85         /**
86          * @var curr_table_name String containing the name of the current table
87          */
88         var curr_table_name = window.parent.table;
89         /**
90          * @var curr_column_name    String containing name of the field referred to by {@link curr_row}
91          */
92         var curr_column_name = $(this).parents('tr').children('th').children('label').text();
93         /**
94          * @var question    String containing the question to be asked for confirmation
95          */
96         var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
98         $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
100             PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
102             $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
103                 if(data.success == true) {
104                     PMA_ajaxShowMessage(data.message);
105                     $(this).remove();
106                     if (typeof data.reload != 'undefined') {
107                         window.parent.frame_content.location.reload();
108                     }
109                 }
110                 else {
111                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
112                 }
113             }) // end $.get()
114         }) // end $.PMA_confirm()
115     })//end Add Primary Key
117     /**
118      * Ajax Event handler for 'Drop Primary Key/Index'
119      *
120      * @uses    $.PMA_confirm()
121      * @uses    PMA_ajaxShowMessage()
122      * (see $GLOBALS['cfg']['AjaxEnable'])
123      */
124     $('.drop_primary_key_index_anchor').live('click', function(event) {
125         event.preventDefault();
127         $anchor = $(this);
129         /**
130          * @var $curr_row    Object containing reference to the current field's row
131          */
132         var $curr_row = $anchor.parents('tr');
133         /** @var    Number of columns in the key */
134         var rows = $anchor.parents('td').attr('rowspan') || 1;
135         /** @var    Rows that should be hidden */
136         var $rows_to_hide = $curr_row;
137         for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
138             $rows_to_hide = $rows_to_hide.add($last_row);
139         }
141         var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
143         $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
145             PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex'], false);
147             $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
148                 if(data.success == true) {
149                     PMA_ajaxShowMessage(data.message);
150                     var $table_ref = $rows_to_hide.closest('table');
151                     if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
152                         // We are about to remove all rows from the table
153                         $table_ref.hide('medium', function() {
154                             $('.no_indexes_defined').show('medium');
155                             $rows_to_hide.remove();
156                         });
157                         $table_ref.siblings('div.notice').hide('medium');
158                     } else {
159                         // We are removing some of the rows only
160                         toggleRowColors($rows_to_hide.last().next());
161                         $rows_to_hide.hide("medium", function () {
162                             $(this).remove();
163                         });
164                     }
165                 }
166                 else {
167                     PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
168                 }
169             }) // end $.get()
170         }) // end $.PMA_confirm()
171     }) //end Drop Primary Key/Index
173     /**
174      *Ajax event handler for multi column change
175     **/
176     $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
177         event.preventDefault();
179         /*Check whether atleast one row is selected for change*/
180         if ($("#tablestructure tbody tr").hasClass("marked")) {
181             /*Define the action and $url variabls for the post method*/
182             var $form = $("#fieldsForm");
183             var action = $form.attr('action');
184             var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
185             /*Calling for the changeColumns fucntion*/
186             changeColumns(action,url);
187         } else {
188             PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
189         }
190     });
192     /**
193      *Ajax event handler for single column change
194     **/
195     $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
196         event.preventDefault();
197         /*Define the action and $url variabls for the post method*/
198         var action = "tbl_alter.php";
199         var url = $(this).attr('href');
200         if (url.substring(0, 13) == "tbl_alter.php") {
201             url = url.substring(14, url.length);
202         }
203         url = url + "&ajax_request=true";
204         /*Calling for the changeColumns fucntion*/
205         changeColumns(action,url);
206      });
208     /**
209      *Ajax event handler for index edit
210     **/
211     $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
212         event.preventDefault();
213         if ($(this).find("a").length == 0) {
214             // Add index
215             var valid = checkFormElementInRange(
216                 $(this).closest('form')[0],
217                 'added_fields',
218                 'Column count has to be larger than zero.'
219             );
220             if (! valid) {
221                 return;
222             }
223             var url = $(this).closest('form').serialize();
224             var title = PMA_messages['strAddIndex'];
225         } else {
226             // Edit index
227             var url = $(this).find("a").attr("href");
228             if (url.substring(0, 16) == "tbl_indexes.php?") {
229                 url = url.substring(16, url.length );
230             }
231             var title = PMA_messages['strEditIndex'];
232         }
233         url += "&ajax_request=true";
235         /*Remove the hidden dialogs if there are*/
236         if ($('#edit_index_dialog').length != 0) {
237             $('#edit_index_dialog').remove();
238         }
239         var $div = $('<div id="edit_index_dialog"></div>');
241         /**
242          * @var button_options Object that stores the options
243          *                     passed to jQueryUI dialog
244          */
245         var button_options = {};
246         button_options[PMA_messages['strGo']] = function() {
247             /**
248              *  @var    the_form    object referring to the export form
249              */
250             var $form = $("#index_frm");
251             PMA_prepareForAjaxRequest($form);
252             //User wants to submit the form
253             $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
254                 if ($("#sqlqueryresults").length != 0) {
255                     $("#sqlqueryresults").remove();
256                 }
257                 if (data.success == true) {
258                     PMA_ajaxShowMessage(data.message);
259                     $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
260                     $("#sqlqueryresults").html(data.sql_query);
261                     $("#result_query .notice").remove();
262                     $("#result_query").prepend(data.message);
264                     /*Reload the field form*/
265                     $("#table_index").remove();
266                     var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
267                     $temp_div.find("#table_index").insertAfter("#index_header");
268                     if ($("#edit_index_dialog").length > 0) {
269                         $("#edit_index_dialog").dialog("close");
270                     }
271                     $('.no_indexes_defined').hide();
272                 } else if (data.error != undefined) {
273                     var $temp_div = $("<div id='temp_div'><div>").append(data.error);
274                     if ($temp_div.find(".error code").length != 0) {
275                         var $error = $temp_div.find(".error code").addClass("error");
276                     } else {
277                         var $error = $temp_div;
278                     }
279                     PMA_ajaxShowMessage($error, false);
280                 }
281             }) // end $.post()
282         }
283         button_options[PMA_messages['strCancel']] = function() {
284             $(this).dialog('close');
285         }
286         var $msgbox = PMA_ajaxShowMessage();
287         $.get("tbl_indexes.php", url, function(data) {
288             if (data.error) {
289                 //in the case of an error, show the error message returned.
290                 PMA_ajaxShowMessage(data.error, false);
291             } else {
292                 PMA_ajaxRemoveMessage($msgbox);
293                 // Show dialog if the request was successful
294                 $div
295                 .append(data)
296                 .dialog({
297                     title: title,
298                     width: 450,
299                     open: PMA_verifyColumnsProperties,
300                     modal: true,
301                     buttons: button_options,
302                     close: function () {
303                         $(this).remove();
304                     }
305                 });
306                 checkIndexType();
307                 checkIndexName("index_frm");
308                 PMA_convertFootnotesToTooltips($div);
309                 // Add a slider for selecting how many columns to add to the index
310                 $div.find('.slider').slider({
311                     animate: true,
312                     value: 1,
313                     min: 1,
314                     max: 16,
315                     slide: function( event, ui ) {
316                         $(this).closest('fieldset').find('input[type=submit]').val(
317                             PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
318                         );
319                     }
320                 });
321                 // Focus the slider, otherwise it looks nearly transparent
322                 $('.ui-slider-handle').addClass('ui-state-focus');
323             }
324         }) // end $.get()
325     });
327     /**
328      * Handler for adding more columns to an index in the editor
329      */
330     $('#index_frm input[type=submit]').live('click', function(event) {
331         event.preventDefault();
332         var rows_to_add = $(this)
333             .closest('fieldset')
334             .find('.slider')
335             .slider('value');
336         while (rows_to_add--) {
337             var $newrow = $('#index_columns')
338                 .find('tbody > tr:first')
339                 .clone()
340                 .appendTo(
341                     $('#index_columns').find('tbody')
342                 );
343             $newrow.find(':input').each(function() {
344                 $(this).val('');
345             });
346         }
347     });
349     /**
350      *Ajax event handler for Add column(s)
351     **/
352     $("#addColumns.ajax input[type=submit]").live('click', function(event){
353         event.preventDefault();
355         /*Remove the hidden dialogs if there are*/
356         if ($('#add_columns').length != 0) {
357             $('#add_columns').remove();
358         }
359         var $div = $('<div id="add_columns"></div>');
361         var $form = $("#addColumns");
363         /**
364          *  @var    button_options  Object that stores the options passed to jQueryUI
365          *                          dialog
366          */
367         var button_options = {};
368         // in the following function we need to use $(this)
369         button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
371         var button_options_error = {};
372         button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
373         var $msgbox = PMA_ajaxShowMessage();
375         $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" ,  function(data) {
376             //in the case of an error, show the error message returned.
377             if (data.success != undefined && data.success == false) {
378                 $div
379                 .append(data.error)
380                 .dialog({
381                     title: PMA_messages['strAddColumns'],
382                     height: 230,
383                     width: 900,
384                     open: PMA_verifyColumnsProperties,
385                     modal: true,
386                     buttons : button_options_error
387                 })// end dialog options
388             } else {
389                 $div
390                 .append(data)
391                 .dialog({
392                     title: PMA_messages['strAddColumns'],
393                     height: 600,
394                     width: 900,
395                     open: PMA_verifyColumnsProperties,
396                     modal: true,
397                     buttons : button_options
398                 }); // end dialog options
400                 $div = $("#add_columns");
401                 /*changed the z-index of the enum editor to allow the edit*/
402                 $("#enum_editor").css("z-index", "1100");
403                 PMA_convertFootnotesToTooltips($div);
404             }
405             PMA_ajaxRemoveMessage($msgbox);
406         }) // end $.get()
407     });
411 }) // end $(document).ready()
414  * Loads the append_fields_form to the Change dialog allowing users
415  * to change the columns
416  * @param   string    action  Variable which parses the name of the
417  *                             destination file
418  * @param   string    $url    Variable which parses the data for the
419  *                             post action
420  */
421 function changeColumns(action,url)
423     /*Remove the hidden dialogs if there are*/
424     if ($('#change_column_dialog').length != 0) {
425         $('#change_column_dialog').remove();
426     }
427     var $div = $('<div id="change_column_dialog"></div>');
429     /**
430      *  @var    button_options  Object that stores the options passed to jQueryUI
431      *                          dialog
432      */
433     var button_options = {};
434     // in the following function we need to use $(this)
435     button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
437     var button_options_error = {};
438     button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
439     var $msgbox = PMA_ajaxShowMessage();
441     $.get( action , url ,  function(data) {
442         //in the case of an error, show the error message returned.
443         if (data.success != undefined && data.success == false) {
444             $div
445             .append(data.error)
446             .dialog({
447                 title: PMA_messages['strChangeTbl'],
448                 height: 230,
449                 width: 900,
450                 modal: true,
451                 open: PMA_verifyColumnsProperties,
452                 buttons : button_options_error
453             })// end dialog options
454         } else {
455             $div
456             .append(data)
457             .dialog({
458                 title: PMA_messages['strChangeTbl'],
459                 height: 600,
460                 width: 900,
461                 modal: true,
462                 open: PMA_verifyColumnsProperties,
463                 buttons : button_options
464             }); // end dialog options
465             $("#append_fields_form input[name=do_save_data]").addClass("ajax");
466             /*changed the z-index of the enum editor to allow the edit*/
467             $("#enum_editor").css("z-index", "1100");
468             $div = $("#change_column_dialog");
469             PMA_convertFootnotesToTooltips($div);
470         }
471         PMA_ajaxRemoveMessage($msgbox);
472     }) // end $.get()