1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used wherever an sql query form is used
6 * @requires js/functions.js
11 * decode a string URL_encoded
14 * @return string the URL-decoded string
16 function PMA_urldecode(str) {
17 return decodeURIComponent(str.replace(/\+/g, '%20'));
21 * Get the field name for the current field. Required to construct the query
24 * @param $this_field jQuery object that points to the current field's tr
25 * @param disp_mode string
27 function getFieldName($this_field, disp_mode) {
29 if(disp_mode == 'vertical') {
30 var field_name = $this_field.siblings('th').find('a').text();
31 // happens when just one row (headings contain no a)
32 if ("" == field_name) {
33 field_name = $this_field.siblings('th').text();
37 var this_field_index = $this_field.index();
38 // ltr or rtl direction does not impact how the DOM was generated
40 // 5 columns to account for the checkbox, edit, appended inline edit, copy and delete anchors but index is zero-based so substract 4
41 var field_name = $('#table_results').find('thead').find('th:nth('+ (this_field_index-4 )+') a').text();
42 // happens when just one row (headings contain no a)
43 if ("" == field_name) {
44 field_name = $('#table_results').find('thead').find('th:nth('+ (this_field_index-4 )+')').text();
48 field_name = $.trim(field_name);
54 * The function that iterates over each row in the table_results and appends a
55 * new inline edit anchor to each table row.
58 function appendInlineAnchor() {
59 var disp_mode = $("#top_direction_dropdown").val();
61 if (disp_mode == 'vertical') {
62 // there can be one or two tr containing this class, depending
63 // on the ModifyDeleteAtLeft and ModifyDeleteAtRight cfg parameters
64 $('#table_results tr')
65 .find('.edit_row_anchor')
66 .removeClass('.edit_row_anchor')
67 .parent().each(function() {
68 var $this_tr = $(this);
69 var $cloned_tr = $this_tr.clone();
71 var $img_object = $cloned_tr.find('img:first').attr('title', PMA_messages['strInlineEdit']);
72 var img_src = $img_object.attr('src').replace(/b_edit/,'b_inline_edit');
73 $img_object.attr('src', img_src);
76 .addClass('inline_edit_anchor')
77 .find('a').attr('href', '#')
79 .text(' ' + PMA_messages['strInlineEdit'])
80 .prepend($img_object);
82 $cloned_tr.insertAfter($this_tr);
85 $('#rowsDeleteForm').find('tbody').find('th').each(function() {
86 var $this_th = $(this);
87 if ($this_th.attr('rowspan') == 4) {
88 $this_th.attr('rowspan', '5');
93 $('.edit_row_anchor').each(function() {
96 $this_td.removeClass('edit_row_anchor');
98 var $cloned_anchor = $this_td.clone();
100 var $img_object = $cloned_anchor.find('img').attr('title', PMA_messages['strInlineEdit']);
101 var img_src = $img_object.attr('src').replace(/b_edit/,'b_inline_edit');
102 $img_object.attr('src', img_src);
104 $cloned_anchor.addClass('inline_edit_anchor')
105 .find('a').attr('href', '#')
107 .text(' ' + PMA_messages['strInlineEdit'])
108 .prepend($img_object);
110 $this_td.after($cloned_anchor);
113 $('#rowsDeleteForm').find('thead, tbody').find('th').each(function() {
114 var $this_th = $(this);
115 if ($this_th.attr('colspan') == 4) {
116 $this_th.attr('colspan', '5');
127 * @description <p>Ajax scripts for sql and browse pages</p>
129 * Actions ajaxified here:
131 * <li>Retrieve results of an SQL query</li>
132 * <li>Paginate the results table</li>
133 * <li>Sort the results table</li>
134 * <li>Change table according to display options</li>
135 * <li>Inline editing of data</li>
138 * @name document.ready
141 $(document).ready(function() {
144 * Set a parameter for all Ajax queries made on this page. Don't let the
145 * web server serve cached pages
152 * current value of the direction in which the table is displayed
157 var disp_mode = $("#top_direction_dropdown").val();
160 * Update value of {@link jQuery.disp_mode} everytime the direction dropdown changes value
162 * @name direction_dropdown_change
164 $("#top_direction_dropdown, #bottom_direction_dropdown").live('change', function(event) {
165 disp_mode = $(this).val();
169 * Attach the {@link appendInlineAnchor} function to a custom event, which
170 * will be triggered manually everytime the table of results is reloaded
173 $("#sqlqueryresults").live('appendAnchor',function() {
174 appendInlineAnchor();
178 * Trigger the appendAnchor event to prepare the first table for inline edit
179 * (see $GLOBALS['cfg']['AjaxEnable'])
181 * @name sqlqueryresults_trigger
183 $("#sqlqueryresults.ajax").trigger('appendAnchor');
186 * Append the "Show/Hide query box" message to the query input form
189 * @name appendToggleSpan
191 // do not add this link more than once
192 if (! $('#sqlqueryform').find('a').is('#togglequerybox')) {
193 $('<a id="togglequerybox"></a>')
194 .html(PMA_messages['strHideQueryBox'])
195 .appendTo("#sqlqueryform");
197 // Attach the toggling of the query box visibility to a click
198 $("#togglequerybox").bind('click', function() {
200 $link.siblings().slideToggle("medium");
201 if ($link.text() == PMA_messages['strHideQueryBox']) {
202 $link.text(PMA_messages['strShowQueryBox']);
204 $link.text(PMA_messages['strHideQueryBox']);
206 // avoid default click action
212 * Ajax Event handler for 'SQL Query Submit'
214 * @see PMA_ajaxShowMessage()
215 * @see $cfg['AjaxEnable']
217 * @name sqlqueryform_submit
219 $("#sqlqueryform.ajax").live('submit', function(event) {
220 event.preventDefault();
221 // remove any div containing a previous error message
222 $('.error').remove();
225 PMA_ajaxShowMessage();
227 if (! $form.find('input:hidden').is('#ajax_request_hidden')) {
228 $form.append('<input type="hidden" id="ajax_request_hidden" name="ajax_request" value="true" />');
231 $.post($(this).attr('action'), $(this).serialize() , function(data) {
232 if(data.success == true) {
233 // fade out previous success message, if any
234 $('.success').fadeOut();
235 // show a message that stays on screen
236 $('#sqlqueryform').before(data.message);
237 $('#sqlqueryresults').show();
238 // this happens if a USE command was typed
239 if (typeof data.reload != 'undefined') {
240 $form.find('input[name=db]').val(data.db);
241 // need to regenerate the whole upper part
242 $form.find('input[name=ajax_request]').remove();
243 $form.append('<input type="hidden" name="reload" value="true" />');
244 $.post('db_sql.php', $form.serialize(), function(data) {
245 $('body').html(data);
246 }); // end inner post
249 else if (data.success == false ) {
250 // show an error message that stays on screen
251 $('#sqlqueryform').before(data.error);
252 $('#sqlqueryresults').hide();
255 // real results are returned
256 $('#sqlqueryresults').show();
257 $("#sqlqueryresults").html(data);
258 $("#sqlqueryresults").trigger('appendAnchor');
259 if($("#togglequerybox").siblings(":visible").length > 0) {
260 $("#togglequerybox").trigger('click');
265 }) // end SQL Query submit
268 * Ajax Event handlers for Paginating the results table
272 * Paginate when we click any of the navigation buttons
273 * (only if the element has the ajax class, see $cfg['AjaxEnable'])
275 * @name paginate_nav_button_click
276 * @uses PMA_ajaxShowMessage()
277 * @see $cfg['AjaxEnable']
279 $("input[name=navig].ajax").live('click', function(event) {
281 event.preventDefault();
283 PMA_ajaxShowMessage();
286 * @var $the_form Object referring to the form element that paginates the results table
288 var $the_form = $(this).parent("form");
290 $the_form.append('<input type="hidden" name="ajax_request" value="true" />');
292 $.post($the_form.attr('action'), $the_form.serialize(), function(data) {
293 $("#sqlqueryresults").html(data);
294 $("#sqlqueryresults").trigger('appendAnchor');
297 })// end Paginate results table
300 * Paginate results with Page Selector dropdown
302 * @name paginate_dropdown_change
303 * @see $cfg['AjaxEnable']
305 $("#pageselector").live('change', function(event) {
306 var $the_form = $(this).parent("form");
308 if ($(this).hasClass('ajax')) {
309 event.preventDefault();
311 PMA_ajaxShowMessage();
313 $.post($the_form.attr('action'), $the_form.serialize() + '&ajax_request=true', function(data) {
314 $("#sqlqueryresults").html(data);
315 $("#sqlqueryresults").trigger('appendAnchor');
322 })// end Paginate results with Page Selector
325 * Ajax Event handler for sorting the results table
327 * @name table_results_sort_click
328 * @see $cfg['AjaxEnable']
330 $("#table_results.ajax").find("a[title=Sort]").live('click', function(event) {
331 event.preventDefault();
333 PMA_ajaxShowMessage();
337 $.get($anchor.attr('href'), $anchor.serialize() + '&ajax_request=true', function(data) {
338 $("#sqlqueryresults")
340 .trigger('appendAnchor');
342 })//end Sort results table
345 * Ajax Event handler for the display options
347 * @name displayOptionsForm_submit
348 * @see $cfg['AjaxEnable']
350 $("#displayOptionsForm.ajax").live('submit', function(event) {
351 event.preventDefault();
355 $.post($form.attr('action'), $form.serialize() + '&ajax_request=true' , function(data) {
356 $("#sqlqueryresults")
358 .trigger('appendAnchor');
362 //end displayOptionsForm handler
365 * Ajax Event handlers for Inline Editing
369 * On click, replace the fields of current row with an input/textarea
371 * @name inline_edit_start
372 * @see PMA_ajaxShowMessage()
373 * @see getFieldName()
375 $(".inline_edit_anchor").live('click', function(event) {
377 event.preventDefault();
379 $(this).removeClass('inline_edit_anchor').addClass('inline_edit_active');
381 // Initialize some variables
382 if(disp_mode == 'vertical') {
384 * @var this_row_index Index of the current <td> in the parent <tr>
385 * Current <td> is the inline edit anchor.
387 var this_row_index = $(this).index();
389 * @var $input_siblings Object referring to all inline editable events from same row
391 var $input_siblings = $(this).parents('tbody').find('tr').find('.inline_edit:nth('+this_row_index+')');
393 * @var where_clause String containing the WHERE clause to select this row
395 var where_clause = $(this).parents('tbody').find('tr').find('.where_clause:nth('+this_row_index+')').val();
398 var $input_siblings = $(this).parent('tr').find('.inline_edit');
399 var where_clause = $(this).parent('tr').find('.where_clause').val();
402 $input_siblings.each(function() {
405 * @var data_value Current value of this field
407 var data_value = $(this).html();
409 // We need to retrieve the value from the server for truncated/relation fields
410 // Find the field name
413 * @var this_field Object referring to this field (<td>)
415 var $this_field = $(this);
417 * @var field_name String containing the name of this field.
418 * @see getFieldName()
420 var field_name = getFieldName($this_field, disp_mode);
422 // In each input sibling, wrap the current value in a textarea
423 // and store the current value in a hidden span
424 if($this_field.is(':not(.truncated, .transformed, .relation, .enum, .null)')) {
425 // handle non-truncated, non-transformed, non-relation values
426 // We don't need to get any more data, just wrap the value
427 $this_field.html('<textarea>'+data_value+'</textarea>')
428 .append('<span class="original_data">'+data_value+'</span>');
429 $(".original_data").hide();
431 else if($this_field.is('.truncated, .transformed')) {
433 //handle truncated/transformed values values
436 * @var sql_query String containing the SQL query used to retrieve value of truncated/transformed data
438 var sql_query = 'SELECT ' + field_name + ' FROM ' + window.parent.table + ' WHERE ' + where_clause;
440 // Make the Ajax call and get the data, wrap it and insert it
442 'token' : window.parent.token,
443 'db' : window.parent.db,
444 'ajax_request' : true,
445 'sql_query' : sql_query,
448 if(data.success == true) {
449 $this_field.html('<textarea>'+data.value+'</textarea>')
450 .append('<span class="original_data">'+data_value+'</span>');
451 $(".original_data").hide();
454 PMA_ajaxShowMessage(data.error);
458 else if($this_field.is('.relation')) {
463 * @var curr_value String containing the current value of this relational field
465 var curr_value = $this_field.find('a').text();
468 * @var post_params Object containing parameters for the POST request
471 'ajax_request' : true,
472 'get_relational_values' : true,
473 'db' : window.parent.db,
474 'table' : window.parent.table,
475 'column' : field_name,
476 'token' : window.parent.token,
477 'curr_value' : curr_value
480 $.post('sql.php', post_params, function(data) {
481 $this_field.html(data.dropdown)
482 .append('<span class="original_data">'+data_value+'</span>');
483 $(".original_data").hide();
486 else if($this_field.is('.enum')) {
490 * @var curr_value String containing the current value of this relational field
492 var curr_value = $this_field.text();
495 * @var post_params Object containing parameters for the POST request
498 'ajax_request' : true,
499 'get_enum_values' : true,
500 'db' : window.parent.db,
501 'table' : window.parent.table,
502 'column' : field_name,
503 'token' : window.parent.token,
504 'curr_value' : curr_value
507 $.post('sql.php', post_params, function(data) {
508 $this_field.html(data.dropdown)
509 .append('<span class="original_data">'+data_value+'</span>');
510 $(".original_data").hide();
513 else if($this_field.is('.null')) {
515 $this_field.html('<textarea></textarea>')
516 .append('<span class="original_data">NULL</span>');
517 $(".original_data").hide();
520 }) // End On click, replace the current field with an input/textarea
523 * After editing, clicking again should post data
526 * @name inline_edit_save
527 * @see PMA_ajaxShowMessage()
528 * @see getFieldName()
530 $(".inline_edit_active").live('click', function(event) {
532 event.preventDefault();
535 * @var $this_td Object referring to the td containing the
536 * "Inline Edit" link that was clicked to save the row that is
540 var $this_td = $(this);
542 var $test_element = ''; // to test the presence of a element
544 // Initialize variables
545 if(disp_mode == 'vertical') {
547 * @var this_td_index Index of the current <td> in the parent <tr>
548 * Current <td> is the inline edit anchor.
550 var this_td_index = $this_td.index();
552 * @var $input_siblings Object referring to all inline editable events from same row
554 var $input_siblings = $this_td.parents('tbody').find('tr').find('.inline_edit:nth('+this_td_index+')');
556 * @var where_clause String containing the WHERE clause to select this row
558 var where_clause = $this_td.parents('tbody').find('tr').find('.where_clause:nth('+this_td_index+')').val();
561 var $input_siblings = $this_td.parent('tr').find('.inline_edit');
562 var where_clause = $this_td.parent('tr').find('.where_clause').val();
566 * @var nonunique Boolean, whether this row is unique or not
568 if($this_td.is('.nonunique')) {
575 // Collect values of all fields to submit, we don't know which changed
577 * @var params_to_submit Array containing the name/value pairs of all fields
579 var params_to_submit = {};
581 * @var relation_fields Array containing the name/value pairs of relational fields
583 var relation_fields = {};
585 * @var transform_fields Array containing the name/value pairs for transformed fields
587 var transform_fields = {};
589 * @var transformation_fields Boolean, if there are any transformed fields in this row
591 var transformation_fields = false;
593 $input_siblings.each(function() {
596 * @var this_field Object referring to this field (<td>)
598 var $this_field = $(this);
600 * @var field_name String containing the name of this field.
601 * @see getFieldName()
603 var field_name = getFieldName($this_field, disp_mode);
606 * @var this_field_params Array temporary storage for the name/value of current field
608 var this_field_params = {};
610 if($this_field.is('.transformed')) {
611 transformation_fields = true;
614 if($this_field.is(":not(.relation, .enum)")) {
615 this_field_params[field_name] = $this_field.find('textarea').val();
616 if($this_field.is('.transformed')) {
617 $.extend(transform_fields, this_field_params);
621 // results from a drop-down
622 $test_element = $this_field.find('select');
623 if ($test_element.length != 0) {
624 this_field_params[field_name] = $test_element.val();
627 // results from Browse foreign value
628 $test_element = $this_field.find('span.curr_value');
629 if ($test_element.length != 0) {
630 this_field_params[field_name] = $test_element.text();
633 if($this_field.is('.relation')) {
634 $.extend(relation_fields, this_field_params);
638 $.extend(params_to_submit, this_field_params);
642 * @var sql_query String containing the SQL query to update this row
644 var sql_query = 'UPDATE ' + window.parent.table + ' SET ';
646 $.each(params_to_submit, function(key, value) {
647 if(value.length == 0) {
650 sql_query += ' ' + key + "='" + value.replace(/'/g, "''") + "' , ";
652 //Remove the last ',' appended in the above loop
653 sql_query = sql_query.replace(/,\s$/, '');
654 sql_query += ' WHERE ' + PMA_urldecode(where_clause);
657 * @var rel_fields_list String, url encoded representation of {@link relations_fields}
659 var rel_fields_list = $.param(relation_fields);
662 * @var transform_fields_list String, url encoded representation of {@link transform_fields}
664 var transform_fields_list = $.param(transform_fields);
666 // Make the Ajax post after setting all parameters
668 * @var post_params Object containing parameters for the POST request
670 var post_params = {'ajax_request' : true,
671 'sql_query' : sql_query,
672 'disp_direction' : disp_mode,
673 'token' : window.parent.token,
674 'db' : window.parent.db,
675 'table' : window.parent.table,
676 'clause_is_unique' : nonunique,
677 'where_clause' : where_clause,
678 'rel_fields_list' : rel_fields_list,
679 'do_transformations' : transformation_fields,
680 'transform_fields_list' : transform_fields_list,
682 'submit_type' : 'save'
685 $.post('tbl_replace.php', post_params, function(data) {
686 if(data.success == true) {
687 PMA_ajaxShowMessage(data.message);
688 $this_td.removeClass('inline_edit_active').addClass('inline_edit_anchor');
690 $input_siblings.each(function() {
691 // Inline edit post has been successful.
692 $this_sibling = $(this);
693 if($this_sibling.is(':not(.relation, .enum)')) {
695 * @var new_html String containing value of the data field after edit
697 var new_html = $this_sibling.find('textarea').val();
699 if($this_sibling.is('.transformed')) {
700 var field_name = getFieldName($this_sibling, disp_mode);
701 $.each(data.transformations, function(key, value) {
702 if(key == field_name) {
703 if($this_sibling.is('.text_plain, .application_octetstream')) {
708 var new_value = $this_sibling.find('textarea').val();
709 new_html = $(value).append(new_value);
719 $test_element = $this_sibling.find('select');
720 if ($test_element.length != 0) {
721 new_value = $test_element.val();
724 $test_element = $this_sibling.find('span.curr_value');
725 if ($test_element.length != 0) {
726 new_value = $test_element.text();
730 if($this_sibling.is('.relation')) {
731 var field_name = getFieldName($this_sibling, disp_mode);
732 $.each(data.relations, function(key, value) {
733 if(key == field_name) {
734 new_html = $(value).append(new_value);
739 if($this_sibling.is('.enum')) {
740 new_html = new_value;
743 $this_sibling.html(new_html);
747 PMA_ajaxShowMessage(data.error);
750 }) // End After editing, clicking again should post data
751 }, 'top.frame_content') // end $(document).ready()
754 * Starting from some th, change the class of all td under it
756 function PMA_changeClassForColumn($this_th, newclass) {
757 // index 0 is the th containing the big T
758 var th_index = $this_th.index();
759 // .eq() is zero-based
761 var $tr_with_data = $this_th.closest('table').find('tbody tr ').has('td.data');
762 $tr_with_data.each(function() {
763 $(this).find('td.data:eq('+th_index+')').toggleClass(newclass);
767 $(document).ready(function() {
769 $('.browse_foreign').live('click', function(e) {
771 window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
773 $anchor.addClass('browse_foreign_clicked');
778 * vertical column highlighting in horizontal mode when hovering over the column header
780 $('.column_heading').live('hover', function() {
781 PMA_changeClassForColumn($(this), 'hover');
785 * vertical column marking in horizontal mode when clicking the column header
787 $('.column_heading').live('click', function() {
788 PMA_changeClassForColumn($(this), 'marked');