Add OK and Cancel button for grid editing
[phpmyadmin/arisferyanto.git] / js / tbl_structure.js
blobf02572f8adb7ad987004778c019cc54a3f926945
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3 * @fileoverview functions used on the table structure page
4 * @name Table Structure
6 * @requires jQuery
7 * @requires jQueryUI
8 * @required js/functions.js
9 */
11 /**
12 * AJAX scripts for tbl_structure.php
14 * Actions ajaxified here:
15 * Drop Column
16 * Add Primary Key
17 * Drop Primary Key/Index
20 $(document).ready(function() {
21 /**
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();
31 /**
32 * @var curr_table_name String containing the name of the current table
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)
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}
42 var curr_column_name = $(curr_row).children('th').children('label').text();
43 /**
44 * @var question String containing the question to be asked for confirmation
46 var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` DROP `' + escapeHtml(curr_column_name) + '`';
48 $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
50 PMA_ajaxShowMessage(PMA_messages['strDroppingColumn']);
52 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
53 if(data.success == true) {
54 PMA_ajaxShowMessage(data.message);
55 $(curr_row).hide("medium").remove();
57 else {
58 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
60 }) // end $.get()
61 }); // end $.PMA_confirm()
62 }) ; //end of Drop Column Anchor action
64 /**
65 * Ajax Event handler for 'Add Primary Key'
67 * @uses $.PMA_confirm()
68 * @uses PMA_ajaxShowMessage()
69 * (see $GLOBALS['cfg']['AjaxEnable'])
71 $(".action_primary a").live('click', function(event) {
72 event.preventDefault();
74 /**
75 * @var curr_table_name String containing the name of the current table
77 var curr_table_name = window.parent.table;
78 /**
79 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
81 var curr_column_name = $(this).parents('tr').children('th').children('label').text();
82 /**
83 * @var question String containing the question to be asked for confirmation
85 var question = PMA_messages['strDoYouReally'] + ' :\n ALTER TABLE `' + escapeHtml(curr_table_name) + '` ADD PRIMARY KEY(`' + escapeHtml(curr_column_name) + '`)';
87 $(this).PMA_confirm(question, $(this).attr('href'), function(url) {
89 PMA_ajaxShowMessage(PMA_messages['strAddingPrimaryKey']);
91 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
92 if(data.success == true) {
93 PMA_ajaxShowMessage(data.message);
94 $(this).remove();
95 if (typeof data.reload != 'undefined') {
96 window.parent.frame_content.location.reload();
99 else {
100 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
102 }) // end $.get()
103 }) // end $.PMA_confirm()
104 })//end Add Primary Key
107 * Ajax Event handler for 'Drop Primary Key/Index'
109 * @uses $.PMA_confirm()
110 * @uses PMA_ajaxShowMessage()
111 * (see $GLOBALS['cfg']['AjaxEnable'])
113 $('.drop_primary_key_index_anchor').live('click', function(event) {
114 event.preventDefault();
116 $anchor = $(this);
119 * @var $curr_row Object containing reference to the current field's row
121 var $curr_row = $anchor.parents('tr');
122 /** @var Number of columns in the key */
123 var rows = $anchor.parents('td').attr('rowspan') || 1;
124 /** @var Rows that should be hidden */
125 var $rows_to_hide = $curr_row;
126 for (var i = 1, $last_row = $curr_row.next(); i < rows; i++, $last_row = $last_row.next()) {
127 $rows_to_hide = $rows_to_hide.add($last_row);
130 var question = $curr_row.children('td').children('.drop_primary_key_index_msg').val();
132 $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
134 PMA_ajaxShowMessage(PMA_messages['strDroppingPrimaryKeyIndex']);
136 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
137 if(data.success == true) {
138 PMA_ajaxShowMessage(data.message);
139 var $table_ref = $rows_to_hide.closest('table');
140 if ($rows_to_hide.length == $table_ref.find('tbody > tr').length) {
141 // We are about to remove all rows from the table
142 $table_ref.hide('medium', function() {
143 $('.no_indexes_defined').show('medium');
144 $rows_to_hide.remove();
146 $table_ref.siblings('div.notice').hide('medium');
147 } else {
148 // We are removing some of the rows only
149 $rows_to_hide.hide("medium", function () {
150 $(this).remove();
154 else {
155 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
157 }) // end $.get()
158 }) // end $.PMA_confirm()
159 }) //end Drop Primary Key/Index
162 *Ajax event handler for multi column change
164 $("#fieldsForm.ajax .mult_submit[value=change]").live('click', function(event){
165 event.preventDefault();
167 /*Check whether atleast one row is selected for change*/
168 if ($("#tablestructure tbody tr").hasClass("marked")) {
169 /*Define the action and $url variabls for the post method*/
170 var $form = $("#fieldsForm");
171 var action = $form.attr('action');
172 var url = $form.serialize()+"&ajax_request=true&submit_mult=change";
173 /*Calling for the changeColumns fucntion*/
174 changeColumns(action,url);
175 } else {
176 PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
181 *Ajax event handler for single column change
183 $("#fieldsForm.ajax #tablestructure tbody tr td.edit a").live('click', function(event){
184 event.preventDefault();
185 /*Define the action and $url variabls for the post method*/
186 var action = "tbl_alter.php";
187 var url = $(this).attr('href');
188 if (url.substring(0, 13) == "tbl_alter.php") {
189 url = url.substring(14, url.length);
191 url = url + "&ajax_request=true";
192 /*Calling for the changeColumns fucntion*/
193 changeColumns(action,url);
197 *Ajax event handler for index edit
199 $("#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax").live('click', function(event){
200 event.preventDefault();
201 if ($(this).find("a").length == 0) {
202 // Add index
203 var valid = checkFormElementInRange(
204 $(this).closest('form')[0],
205 'added_fields',
206 'Column count has to be larger than zero.'
208 if (! valid) {
209 return;
211 var url = $(this).closest('form').serialize();
212 var title = PMA_messages['strAddIndex'];
213 } else {
214 // Edit index
215 var url = $(this).find("a").attr("href");
216 if (url.substring(0, 16) == "tbl_indexes.php?") {
217 url = url.substring(16, url.length );
219 var title = PMA_messages['strEditIndex'];
221 url += "&ajax_request=true";
223 /*Remove the hidden dialogs if there are*/
224 if ($('#edit_index_dialog').length != 0) {
225 $('#edit_index_dialog').remove();
227 var $div = $('<div id="edit_index_dialog"></div>');
230 * @var button_options Object that stores the options
231 * passed to jQueryUI dialog
233 var button_options = {};
234 button_options[PMA_messages['strGo']] = function() {
236 * @var the_form object referring to the export form
238 var $form = $("#index_frm");
239 PMA_prepareForAjaxRequest($form);
240 //User wants to submit the form
241 $.post($form.attr('action'), $form.serialize()+"&do_save_data=1", function(data) {
242 if ($("#sqlqueryresults").length != 0) {
243 $("#sqlqueryresults").remove();
245 if (data.success == true) {
246 PMA_ajaxShowMessage(data.message);
247 $("<div id='sqlqueryresults'></div>").insertAfter("#floating_menubar");
248 $("#sqlqueryresults").html(data.sql_query);
249 $("#result_query .notice").remove();
250 $("#result_query").prepend(data.message);
252 /*Reload the field form*/
253 $("#table_index").remove();
254 var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
255 $temp_div.find("#table_index").insertAfter("#index_header");
256 if ($("#edit_index_dialog").length > 0) {
257 $("#edit_index_dialog").dialog("close");
259 } else if (data.error != undefined) {
260 var $temp_div = $("<div id='temp_div'><div>").append(data.error);
261 if ($temp_div.find(".error code").length != 0) {
262 var $error = $temp_div.find(".error code").addClass("error");
263 } else {
264 var $error = $temp_div;
266 PMA_ajaxShowMessage($error, false);
268 }) // end $.post()
270 button_options[PMA_messages['strCancel']] = function() {
271 $(this).dialog('close');
273 var $msgbox = PMA_ajaxShowMessage();
274 $.get("tbl_indexes.php", url, function(data) {
275 if (data.error) {
276 //in the case of an error, show the error message returned.
277 PMA_ajaxShowMessage(data.error, false);
278 } else {
279 PMA_ajaxRemoveMessage($msgbox);
280 // Show dialog if the request was successful
281 $div
282 .append(data)
283 .dialog({
284 title: title,
285 width: 450,
286 open: PMA_verifyColumnsProperties,
287 modal: true,
288 buttons: button_options,
289 close: function () {
290 $(this).remove();
293 checkIndexType();
294 checkIndexName("index_frm");
295 PMA_convertFootnotesToTooltips($div);
296 // Add a slider for selecting how many columns to add to the index
297 $div.find('.slider').slider({
298 animate: true,
299 value: 1,
300 min: 1,
301 max: 16,
302 slide: function( event, ui ) {
303 $(this).closest('fieldset').find('input[type=submit]').val(
304 PMA_messages['strAddToIndex'].replace(/%d/, ui.value)
308 // Focus the slider, otherwise it looks nearly transparent
309 $('.ui-slider-handle').addClass('ui-state-focus');
311 }) // end $.get()
315 * Handler for adding more columns to an index in the editor
317 $('#index_frm input[type=submit]').live('click', function(event) {
318 event.preventDefault();
319 var rows_to_add = $(this)
320 .closest('fieldset')
321 .find('.slider')
322 .slider('value');
323 while (rows_to_add--) {
324 var $newrow = $('#index_columns')
325 .find('tbody > tr:first')
326 .clone()
327 .appendTo(
328 $('#index_columns').find('tbody')
330 $newrow.find(':input').each(function() {
331 $(this).val('');
337 *Ajax event handler for Add column(s)
339 $("#addColumns.ajax input[type=submit]").live('click', function(event){
340 event.preventDefault();
342 /*Remove the hidden dialogs if there are*/
343 if ($('#add_columns').length != 0) {
344 $('#add_columns').remove();
346 var $div = $('<div id="add_columns"></div>');
348 var $form = $("#addColumns");
351 * @var button_options Object that stores the options passed to jQueryUI
352 * dialog
354 var button_options = {};
355 // in the following function we need to use $(this)
356 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
358 var button_options_error = {};
359 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
360 var $msgbox = PMA_ajaxShowMessage();
362 $.get( $form.attr('action') , $form.serialize()+"&ajax_request=true" , function(data) {
363 //in the case of an error, show the error message returned.
364 if (data.success != undefined && data.success == false) {
365 $div
366 .append(data.error)
367 .dialog({
368 title: PMA_messages['strAddColumns'],
369 height: 230,
370 width: 900,
371 open: PMA_verifyColumnsProperties,
372 modal: true,
373 buttons : button_options_error
374 })// end dialog options
375 } else {
376 $div
377 .append(data)
378 .dialog({
379 title: PMA_messages['strAddColumns'],
380 height: 600,
381 width: 900,
382 open: PMA_verifyColumnsProperties,
383 modal: true,
384 buttons : button_options
385 }); // end dialog options
387 $div = $("#add_columns");
388 /*changed the z-index of the enum editor to allow the edit*/
389 $("#enum_editor").css("z-index", "1100");
390 PMA_convertFootnotesToTooltips($div);
392 PMA_ajaxRemoveMessage($msgbox);
393 }) // end $.get()
398 }) // end $(document).ready()
401 * Loads the append_fields_form to the Change dialog allowing users
402 * to change the columns
403 * @param string action Variable which parses the name of the
404 * destination file
405 * @param string $url Variable which parses the data for the
406 * post action
408 function changeColumns(action,url)
410 /*Remove the hidden dialogs if there are*/
411 if ($('#change_column_dialog').length != 0) {
412 $('#change_column_dialog').remove();
414 var $div = $('<div id="change_column_dialog"></div>');
417 * @var button_options Object that stores the options passed to jQueryUI
418 * dialog
420 var button_options = {};
421 // in the following function we need to use $(this)
422 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();}
424 var button_options_error = {};
425 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();}
426 var $msgbox = PMA_ajaxShowMessage();
428 $.get( action , url , function(data) {
429 //in the case of an error, show the error message returned.
430 if (data.success != undefined && data.success == false) {
431 $div
432 .append(data.error)
433 .dialog({
434 title: PMA_messages['strChangeTbl'],
435 height: 230,
436 width: 900,
437 modal: true,
438 open: PMA_verifyColumnsProperties,
439 buttons : button_options_error
440 })// end dialog options
441 } else {
442 $div
443 .append(data)
444 .dialog({
445 title: PMA_messages['strChangeTbl'],
446 height: 600,
447 width: 900,
448 modal: true,
449 open: PMA_verifyColumnsProperties,
450 buttons : button_options
451 }); // end dialog options
452 $("#append_fields_form input[name=do_save_data]").addClass("ajax");
453 /*changed the z-index of the enum editor to allow the edit*/
454 $("#enum_editor").css("z-index", "1100");
455 $div = $("#change_column_dialog");
456 PMA_convertFootnotesToTooltips($div);
458 PMA_ajaxRemoveMessage($msgbox);
459 }) // end $.get()