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 $after_field_item.remove();
60 $(curr_row).hide("medium").remove();
63 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
66 }); // end $.PMA_confirm()
67 }) ; //end of Drop Column Anchor action
70 * Ajax Event handler for 'Add Primary Key'
72 * @uses $.PMA_confirm()
73 * @uses PMA_ajaxShowMessage()
74 * (see $GLOBALS['cfg']['AjaxEnable'])
76 $(".action_primary a").live('click', function(event) {
77 event.preventDefault();
80 * @var curr_table_name String containing the name of the current table
82 var curr_table_name = window.parent.table;
84 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
86 var curr_column_name = $(this).parents('tr').children('th').children('label').text();
88 * @var question String containing the question to be asked for confirmation
90 var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
92 $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
94 PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey'], false);
96 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
97 if(data.success == true) {
98 PMA_ajaxShowMessage(data.message);
100 if (typeof data.reload != 'undefined') {
101 window.parent.frame_content.location.reload();
105 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
108 }) // end $.PMA_confirm()
109 })//end Add Primary Key
112 * Ajax Event handler for 'Drop Primary Key/Index'
114 * @uses $.PMA_confirm()
115 * @uses PMA_ajaxShowMessage()
116 * (see $GLOBALS['cfg']['AjaxEnable'])
118 $('.drop_primary_key_index_anchor').live('click', function(event) {
119 event.preventDefault();
124 * @var $curr_row Object containing reference to the current field's row
126 var $curr_row = $anchor.parents('tr');
127 /** @var Number of columns in the key */
128 var rows = $anchor.parents('td').attr('rowspan') || 1;
129 /** @var Rows that should be hidden */
130 var $rows_to_hide = $curr_row;
131 for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
132 $rows_to_hide = $rows_to_hide.add($last_row);
135 var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
137 $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
139 PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex'], false);
141 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
142 if(data.success == true) {
143 PMA_ajaxShowMessage(data.message);
144 var $table_ref = $rows_to_hide.closest('table');
145 if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
146 // We are about to remove all rows from the table
147 $table_ref.hide('medium', function() {
148 $('.no_indexes_defined').show('medium');
149 $rows_to_hide.remove();
151 $table_ref.siblings('div.notice').hide('medium');
153 // We are removing some of the rows only
154 $rows_to_hide.hide("medium", function () {
160 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
163 }) // end $.PMA_confirm()
164 }) //end Drop Primary Key/Index
167 *Ajax event handler for multi column change
169 $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
170 event.preventDefault();
172 /*Check whether atleast one row is selected for change*/
173 if ($("#tablestructure tbody tr").hasClass("marked")) {
174 /*Define the action and $url variabls for the post method*/
175 var $form = $("#fieldsForm");
176 var action = $form.attr('action');
177 var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
178 /*Calling for the changeColumns fucntion*/
179 changeColumns(action,url);
181 PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
186 *Ajax event handler for single column change
188 $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
189 event.preventDefault();
190 /*Define the action and $url variabls for the post method*/
191 var action = "tbl_alter.php";
192 var url = $(this).attr('href');
193 if (url.substring(0, 13) == "tbl_alter.php") {
194 url = url.substring(14, url.length);
196 url = url + "&ajax_request=true";
197 /*Calling for the changeColumns fucntion*/
198 changeColumns(action,url);
202 *Ajax event handler for index edit
204 $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
205 event.preventDefault();
206 if ($(this).find("a").length == 0) {
208 var valid = checkFormElementInRange(
209 $(this).closest('form')[0],
211 'Column count has to be larger than zero.'
216 var url = $(this).closest('form').serialize();
217 var title = PMA_messages['strAddIndex'];
220 var url = $(this).find("a").attr("href");
221 if (url.substring(0, 16) == "tbl_indexes.php?") {
222 url = url.substring(16, url.length );
224 var title = PMA_messages['strEditIndex'];
226 url += "&ajax_request=true";
228 /*Remove the hidden dialogs if there are*/
229 if ($('#edit_index_dialog').length != 0) {
230 $('#edit_index_dialog').remove();
232 var $div = $('<div id="edit_index_dialog"></div>');
235 * @var button_options Object that stores the options
236 * passed to jQueryUI dialog
238 var button_options = {};
239 button_options[PMA_messages['strGo']] = function() {
241 * @var the_form object referring to the export form
243 var $form = $("#index_frm");
244 PMA_prepareForAjaxRequest($form);
245 //User wants to submit the form
246 $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
247 if ($("#sqlqueryresults").length != 0) {
248 $("#sqlqueryresults").remove();
250 if (data.success == true) {
251 PMA_ajaxShowMessage(data.message);
252 $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
253 $("#sqlqueryresults").html(data.sql_query);
254 $("#result_query .notice").remove();
255 $("#result_query").prepend(data.message);
257 /*Reload the field form*/
258 $("#table_index").remove();
259 var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
260 $temp_div.find("#table_index").insertAfter("#index_header");
261 if ($("#edit_index_dialog").length > 0) {
262 $("#edit_index_dialog").dialog("close");
264 $('.no_indexes_defined').hide();
265 } else if (data.error != undefined) {
266 var $temp_div = $("<div id='temp_div'><div>").append(data.error);
267 if ($temp_div.find(".error code").length != 0) {
268 var $error = $temp_div.find(".error code").addClass("error");
270 var $error = $temp_div;
272 PMA_ajaxShowMessage($error, false);
276 button_options[PMA_messages['strCancel']] = function() {
277 $(this).dialog('close');
279 var $msgbox = PMA_ajaxShowMessage();
280 $.get("tbl_indexes.php", url, function(data) {
282 //in the case of an error, show the error message returned.
283 PMA_ajaxShowMessage(data.error, false);
285 PMA_ajaxRemoveMessage($msgbox);
286 // Show dialog if the request was successful
292 open: PMA_verifyColumnsProperties,
294 buttons: button_options,
300 checkIndexName("index_frm");
301 PMA_convertFootnotesToTooltips($div);
302 // Add a slider for selecting how many columns to add to the index
303 $div.find('.slider').slider({
308 slide: function( event, ui ) {
309 $(this).closest('fieldset').find('input[type=submit]').val(
310 PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
314 // Focus the slider, otherwise it looks nearly transparent
315 $('.ui-slider-handle').addClass('ui-state-focus');
321 * Handler for adding more columns to an index in the editor
323 $('#index_frm input[type=submit]').live('click', function(event) {
324 event.preventDefault();
325 var rows_to_add = $(this)
329 while (rows_to_add--) {
330 var $newrow = $('#index_columns')
331 .find('tbody > tr:first')
334 $('#index_columns').find('tbody')
336 $newrow.find(':input').each(function() {
343 *Ajax event handler for Add column(s)
345 $("#addColumns.ajax input[type=submit]").live('click', function(event){
346 event.preventDefault();
348 /*Remove the hidden dialogs if there are*/
349 if ($('#add_columns').length != 0) {
350 $('#add_columns').remove();
352 var $div = $('<div id="add_columns"></div>');
354 var $form = $("#addColumns");
357 * @var button_options Object that stores the options passed to jQueryUI
360 var button_options = {};
361 // in the following function we need to use $(this)
362 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
364 var button_options_error = {};
365 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
366 var $msgbox = PMA_ajaxShowMessage();
368 $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" , function(data) {
369 //in the case of an error, show the error message returned.
370 if (data.success != undefined && data.success == false) {
374 title: PMA_messages['strAddColumns'],
377 open: PMA_verifyColumnsProperties,
379 buttons : button_options_error
380 })// end dialog options
385 title: PMA_messages['strAddColumns'],
388 open: PMA_verifyColumnsProperties,
390 buttons : button_options
391 }); // end dialog options
393 $div = $("#add_columns");
394 /*changed the z-index of the enum editor to allow the edit*/
395 $("#enum_editor").css("z-index", "1100");
396 PMA_convertFootnotesToTooltips($div);
398 PMA_ajaxRemoveMessage($msgbox);
404 }) // end $(document).ready()
407 * Loads the append_fields_form to the Change dialog allowing users
408 * to change the columns
409 * @param string action Variable which parses the name of the
411 * @param string $url Variable which parses the data for the
414 function changeColumns(action,url)
416 /*Remove the hidden dialogs if there are*/
417 if ($('#change_column_dialog').length != 0) {
418 $('#change_column_dialog').remove();
420 var $div = $('<div id="change_column_dialog"></div>');
423 * @var button_options Object that stores the options passed to jQueryUI
426 var button_options = {};
427 // in the following function we need to use $(this)
428 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
430 var button_options_error = {};
431 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
432 var $msgbox = PMA_ajaxShowMessage();
434 $.get( action , url , function(data) {
435 //in the case of an error, show the error message returned.
436 if (data.success != undefined && data.success == false) {
440 title: PMA_messages['strChangeTbl'],
444 open: PMA_verifyColumnsProperties,
445 buttons : button_options_error
446 })// end dialog options
451 title: PMA_messages['strChangeTbl'],
455 open: PMA_verifyColumnsProperties,
456 buttons : button_options
457 }); // end dialog options
458 $("#append_fields_form input[name=do_save_data]").addClass("ajax");
459 /*changed the z-index of the enum editor to allow the edit*/
460 $("#enum_editor").css("z-index", "1100");
461 $div = $("#change_column_dialog");
462 PMA_convertFootnotesToTooltips($div);
464 PMA_ajaxRemoveMessage($msgbox);