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();
67 // refresh the list of indexes (comes from sql.php)
68 $('#indexes').html(data.indexes_list);
71 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
74 }); // end $.PMA_confirm()
75 }) ; //end of Drop Column Anchor action
78 * Ajax Event handler for 'Add Primary Key'
80 * @uses $.PMA_confirm()
81 * @uses PMA_ajaxShowMessage()
82 * (see $GLOBALS['cfg']['AjaxEnable'])
84 $(".action_primary a").live('click', function(event) {
85 event.preventDefault();
88 * @var curr_table_name String containing the name of the current table
90 var curr_table_name = window.parent.table;
92 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
94 var curr_column_name = $(this).parents('tr').children('th').children('label').text();
96 * @var question String containing the question to be asked for confirmation
98 var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
100 $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
102 PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
104 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
105 if(data.success == true) {
106 PMA_ajaxShowMessage(data.message);
108 if (typeof data.reload != 'undefined') {
109 window.parent.frame_content.location.reload();
113 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
116 }) // end $.PMA_confirm()
117 })//end Add Primary Key
120 * Ajax Event handler for 'Drop Primary Key/Index'
122 * @uses $.PMA_confirm()
123 * @uses PMA_ajaxShowMessage()
124 * (see $GLOBALS['cfg']['AjaxEnable'])
126 $('.drop_primary_key_index_anchor').live('click', function(event) {
127 event.preventDefault();
132 * @var $curr_row Object containing reference to the current field's row
134 var $curr_row = $anchor.parents('tr');
135 /** @var Number of columns in the key */
136 var rows = $anchor.parents('td').attr('rowspan') || 1;
137 /** @var Rows that should be hidden */
138 var $rows_to_hide = $curr_row;
139 for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
140 $rows_to_hide = $rows_to_hide.add($last_row);
143 var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
145 $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
147 PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex'], false);
149 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
150 if(data.success == true) {
151 PMA_ajaxShowMessage(data.message);
152 var $table_ref = $rows_to_hide.closest('table');
153 if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
154 // We are about to remove all rows from the table
155 $table_ref.hide('medium', function() {
156 $('.no_indexes_defined').show('medium');
157 $rows_to_hide.remove();
159 $table_ref.siblings('div.notice').hide('medium');
161 // We are removing some of the rows only
162 toggleRowColors($rows_to_hide.last().next());
163 $rows_to_hide.hide("medium", function () {
169 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
172 }) // end $.PMA_confirm()
173 }) //end Drop Primary Key/Index
176 *Ajax event handler for multi column change
178 $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
179 event.preventDefault();
181 /*Check whether atleast one row is selected for change*/
182 if ($("#tablestructure tbody tr").hasClass("marked")) {
183 /*Define the action and $url variabls for the post method*/
184 var $form = $("#fieldsForm");
185 var action = $form.attr('action');
186 var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
187 /*Calling for the changeColumns fucntion*/
188 changeColumns(action,url);
190 PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
195 *Ajax event handler for single column change
197 $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
198 event.preventDefault();
199 /*Define the action and $url variabls for the post method*/
200 var action = "tbl_alter.php";
201 var url = $(this).attr('href');
202 if (url.substring(0, 13) == "tbl_alter.php") {
203 url = url.substring(14, url.length);
205 url = url + "&ajax_request=true";
206 /*Calling for the changeColumns fucntion*/
207 changeColumns(action,url);
211 *Ajax event handler for index edit
213 $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
214 event.preventDefault();
215 if ($(this).find("a").length == 0) {
217 var valid = checkFormElementInRange(
218 $(this).closest('form')[0],
220 'Column count has to be larger than zero.'
225 var url = $(this).closest('form').serialize();
226 var title = PMA_messages['strAddIndex'];
229 var url = $(this).find("a").attr("href");
230 if (url.substring(0, 16) == "tbl_indexes.php?") {
231 url = url.substring(16, url.length );
233 var title = PMA_messages['strEditIndex'];
235 url += "&ajax_request=true";
237 /*Remove the hidden dialogs if there are*/
238 if ($('#edit_index_dialog').length != 0) {
239 $('#edit_index_dialog').remove();
241 var $div = $('<div id="edit_index_dialog"></div>');
244 * @var button_options Object that stores the options
245 * passed to jQueryUI dialog
247 var button_options = {};
248 button_options[PMA_messages['strGo']] = function() {
250 * @var the_form object referring to the export form
252 var $form = $("#index_frm");
253 PMA_prepareForAjaxRequest($form);
254 //User wants to submit the form
255 $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
256 if ($("#sqlqueryresults").length != 0) {
257 $("#sqlqueryresults").remove();
259 if (data.success == true) {
260 PMA_ajaxShowMessage(data.message);
261 $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
262 $("#sqlqueryresults").html(data.sql_query);
263 $("#result_query .notice").remove();
264 $("#result_query").prepend(data.message);
266 /*Reload the field form*/
267 $("#table_index").remove();
268 var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
269 $temp_div.find("#table_index").insertAfter("#index_header");
270 if ($("#edit_index_dialog").length > 0) {
271 $("#edit_index_dialog").dialog("close");
273 $('.no_indexes_defined').hide();
274 } else if (data.error != undefined) {
275 var $temp_div = $("<div id='temp_div'><div>").append(data.error);
276 if ($temp_div.find(".error code").length != 0) {
277 var $error = $temp_div.find(".error code").addClass("error");
279 var $error = $temp_div;
281 PMA_ajaxShowMessage($error, false);
285 button_options[PMA_messages['strCancel']] = function() {
286 $(this).dialog('close');
288 var $msgbox = PMA_ajaxShowMessage();
289 $.get("tbl_indexes.php", url, function(data) {
291 //in the case of an error, show the error message returned.
292 PMA_ajaxShowMessage(data.error, false);
294 PMA_ajaxRemoveMessage($msgbox);
295 // Show dialog if the request was successful
301 open: PMA_verifyColumnsProperties,
303 buttons: button_options,
309 checkIndexName("index_frm");
310 PMA_convertFootnotesToTooltips($div);
311 // Add a slider for selecting how many columns to add to the index
312 $div.find('.slider').slider({
317 slide: function( event, ui ) {
318 $(this).closest('fieldset').find('input[type=submit]').val(
319 PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
323 // Focus the slider, otherwise it looks nearly transparent
324 $('.ui-slider-handle').addClass('ui-state-focus');
330 * Handler for adding more columns to an index in the editor
332 $('#index_frm input[type=submit]').live('click', function(event) {
333 event.preventDefault();
334 var rows_to_add = $(this)
338 while (rows_to_add--) {
339 var $newrow = $('#index_columns')
340 .find('tbody > tr:first')
343 $('#index_columns').find('tbody')
345 $newrow.find(':input').each(function() {
352 *Ajax event handler for Add column(s)
354 $("#addColumns.ajax input[type=submit]").live('click', function(event){
355 event.preventDefault();
357 /*Remove the hidden dialogs if there are*/
358 if ($('#add_columns').length != 0) {
359 $('#add_columns').remove();
361 var $div = $('<div id="add_columns"></div>');
363 var $form = $("#addColumns");
366 * @var button_options Object that stores the options passed to jQueryUI
369 var button_options = {};
370 // in the following function we need to use $(this)
371 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
373 var button_options_error = {};
374 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
375 var $msgbox = PMA_ajaxShowMessage();
377 $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" , function(data) {
378 //in the case of an error, show the error message returned.
379 if (data.success != undefined && data.success == false) {
383 title: PMA_messages['strAddColumns'],
386 open: PMA_verifyColumnsProperties,
388 buttons : button_options_error
389 })// end dialog options
394 title: PMA_messages['strAddColumns'],
397 open: PMA_verifyColumnsProperties,
399 buttons : button_options
400 }); // end dialog options
402 $div = $("#add_columns");
403 /*changed the z-index of the enum editor to allow the edit*/
404 $("#enum_editor").css("z-index", "1100");
405 PMA_convertFootnotesToTooltips($div);
407 PMA_ajaxRemoveMessage($msgbox);
413 }) // end $(document).ready()
416 * Loads the append_fields_form to the Change dialog allowing users
417 * to change the columns
418 * @param string action Variable which parses the name of the
420 * @param string $url Variable which parses the data for the
423 function changeColumns(action,url)
425 /*Remove the hidden dialogs if there are*/
426 if ($('#change_column_dialog').length != 0) {
427 $('#change_column_dialog').remove();
429 var $div = $('<div id="change_column_dialog"></div>');
432 * @var button_options Object that stores the options passed to jQueryUI
435 var button_options = {};
436 // in the following function we need to use $(this)
437 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
439 var button_options_error = {};
440 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
441 var $msgbox = PMA_ajaxShowMessage();
443 $.get( action , url , function(data) {
444 //in the case of an error, show the error message returned.
445 if (data.success != undefined && data.success == false) {
449 title: PMA_messages['strChangeTbl'],
453 open: PMA_verifyColumnsProperties,
454 buttons : button_options_error
455 })// end dialog options
460 title: PMA_messages['strChangeTbl'],
464 open: PMA_verifyColumnsProperties,
465 buttons : button_options
466 }); // end dialog options
467 $("#append_fields_form input[name=do_save_data]").addClass("ajax");
468 /*changed the z-index of the enum editor to allow the edit*/
469 $("#enum_editor").css("z-index", "1100");
470 $div = $("#change_column_dialog");
471 PMA_convertFootnotesToTooltips($div);
473 PMA_ajaxRemoveMessage($msgbox);