1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used on the table structure page
4 * @name Table Structure
8 * @required js/functions.js
12 * AJAX scripts for tbl_structure.php
14 * Actions ajaxified here:
17 * Drop Primary Key/Index
20 $(document).ready(function() {
22 * Attach Event Handler for 'Drop Column'
24 * @uses $.PMA_confirm()
25 * @uses PMA_ajaxShowMessage()
26 * (see $GLOBALS['cfg']['AjaxEnable'])
28 $(".drop_column_anchor").live('click', function(event) {
29 event.preventDefault();
32 * @var curr_table_name String containing the name of the current table
34 var curr_table_name = window.parent.table;
36 * @var curr_row Object reference to the currently selected row (i.e. field in the table)
38 var $curr_row = $(this).parents('tr');
40 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
42 var curr_column_name = $curr_row.children('th').children('label').text();
44 * @var $after_field_item Corresponding entry in the 'After' field.
46 var $after_field_item = $("select[name='after_field'] option[value='" + curr_column_name + "']");
48 * @var question String containing the question to be asked for confirmation
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);
65 $after_field_item.remove();
66 $curr_row.hide("medium").remove();
69 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
72 }); // end $.PMA_confirm()
73 }) ; //end of Drop Column Anchor action
76 * Ajax Event handler for 'Add Primary Key'
78 * @uses $.PMA_confirm()
79 * @uses PMA_ajaxShowMessage()
80 * (see $GLOBALS['cfg']['AjaxEnable'])
82 $(".action_primary a").live('click', function(event) {
83 event.preventDefault();
86 * @var curr_table_name String containing the name of the current table
88 var curr_table_name = window.parent.table;
90 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
92 var curr_column_name = $(this).parents('tr').children('th').children('label').text();
94 * @var question String containing the question to be asked for confirmation
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);
106 if (typeof data.reload != 'undefined') {
107 window.parent.frame_content.location.reload();
111 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
114 }) // end $.PMA_confirm()
115 })//end Add Primary Key
118 * Ajax Event handler for 'Drop Primary Key/Index'
120 * @uses $.PMA_confirm()
121 * @uses PMA_ajaxShowMessage()
122 * (see $GLOBALS['cfg']['AjaxEnable'])
124 $('.drop_primary_key_index_anchor').live('click', function(event) {
125 event.preventDefault();
130 * @var $curr_row Object containing reference to the current field's row
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);
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();
157 $table_ref.siblings('div.notice').hide('medium');
159 // We are removing some of the rows only
160 toggleRowColors($rows_to_hide.last().next());
161 $rows_to_hide.hide("medium", function () {
167 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
170 }) // end $.PMA_confirm()
171 }) //end Drop Primary Key/Index
174 *Ajax event handler for multi column change
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);
188 PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
193 *Ajax event handler for single column change
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);
203 url = url + "&ajax_request=true";
204 /*Calling for the changeColumns fucntion*/
205 changeColumns(action,url);
209 *Ajax event handler for index edit
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) {
215 var valid = checkFormElementInRange(
216 $(this).closest('form')[0],
218 'Column count has to be larger than zero.'
223 var url = $(this).closest('form').serialize();
224 var title = PMA_messages['strAddIndex'];
227 var url = $(this).find("a").attr("href");
228 if (url.substring(0, 16) == "tbl_indexes.php?") {
229 url = url.substring(16, url.length );
231 var title = PMA_messages['strEditIndex'];
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();
239 var $div = $('<div id="edit_index_dialog"></div>');
242 * @var button_options Object that stores the options
243 * passed to jQueryUI dialog
245 var button_options = {};
246 button_options[PMA_messages['strGo']] = function() {
248 * @var the_form object referring to the export form
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();
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");
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");
277 var $error = $temp_div;
279 PMA_ajaxShowMessage($error, false);
283 button_options[PMA_messages['strCancel']] = function() {
284 $(this).dialog('close');
286 var $msgbox = PMA_ajaxShowMessage();
287 $.get("tbl_indexes.php", url, function(data) {
289 //in the case of an error, show the error message returned.
290 PMA_ajaxShowMessage(data.error, false);
292 PMA_ajaxRemoveMessage($msgbox);
293 // Show dialog if the request was successful
299 open: PMA_verifyColumnsProperties,
301 buttons: button_options,
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({
315 slide: function( event, ui ) {
316 $(this).closest('fieldset').find('input[type=submit]').val(
317 PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
321 // Focus the slider, otherwise it looks nearly transparent
322 $('.ui-slider-handle').addClass('ui-state-focus');
328 * Handler for adding more columns to an index in the editor
330 $('#index_frm input[type=submit]').live('click', function(event) {
331 event.preventDefault();
332 var rows_to_add = $(this)
336 while (rows_to_add--) {
337 var $newrow = $('#index_columns')
338 .find('tbody > tr:first')
341 $('#index_columns').find('tbody')
343 $newrow.find(':input').each(function() {
350 *Ajax event handler for Add column(s)
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();
359 var $div = $('<div id="add_columns"></div>');
361 var $form = $("#addColumns");
364 * @var button_options Object that stores the options passed to jQueryUI
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) {
381 title: PMA_messages['strAddColumns'],
384 open: PMA_verifyColumnsProperties,
386 buttons : button_options_error
387 })// end dialog options
392 title: PMA_messages['strAddColumns'],
395 open: PMA_verifyColumnsProperties,
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);
405 PMA_ajaxRemoveMessage($msgbox);
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
418 * @param string $url Variable which parses the data for the
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();
427 var $div = $('<div id="change_column_dialog"></div>');
430 * @var button_options Object that stores the options passed to jQueryUI
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) {
447 title: PMA_messages['strChangeTbl'],
451 open: PMA_verifyColumnsProperties,
452 buttons : button_options_error
453 })// end dialog options
458 title: PMA_messages['strChangeTbl'],
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);
471 PMA_ajaxRemoveMessage($msgbox);