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
13 * decode a string URL_encoded
16 * @return string the URL-decoded string
18 function PMA_urldecode(str)
20 return decodeURIComponent(str.replace(/\+/g, '%20'));
23 function PMA_urlencode(str)
25 return encodeURIComponent(str).replace(/\%20/g, '+');
29 * Get the field name for the current field. Required to construct the query
32 * @param $this_field jQuery object that points to the current field's tr
34 function getFieldName($this_field)
37 var this_field_index = $this_field.index();
38 // ltr or rtl direction does not impact how the DOM was generated
39 // check if the action column in the left exist
40 var left_action_exist = !$('#table_results').find('th:first').hasClass('draggable');
41 // number of column span for checkbox and Actions
42 var left_action_skip = left_action_exist ? $('#table_results').find('th:first').attr('colspan') - 1 : 0;
43 var field_name = $('#table_results').find('thead').find('th:eq('+ (this_field_index - left_action_skip) + ') a').text();
44 // happens when just one row (headings contain no a)
45 if ("" == field_name) {
46 field_name = $('#table_results').find('thead').find('th:eq('+ (this_field_index - left_action_skip) + ')').text();
49 field_name = $.trim(field_name);
60 * @description <p>Ajax scripts for sql and browse pages</p>
62 * Actions ajaxified here:
64 * <li>Retrieve results of an SQL query</li>
65 * <li>Paginate the results table</li>
66 * <li>Sort the results table</li>
67 * <li>Change table according to display options</li>
68 * <li>Grid editing of data</li>
71 * @name document.ready
74 $(document).ready(function() {
75 /* Hides the bookmarkoptions checkboxes when the bookmark label is empty */
76 $('input#bkm_label').keyup(function() {
77 $('input#id_bkm_all_users, input#id_bkm_replace')
79 .toggle($(this).attr('value').length > 0);
83 * Attach the {@link makegrid} function to a custom event, which will be
84 * triggered manually everytime the table of results is reloaded
87 $("#sqlqueryresults").live('makegrid', function() {
88 PMA_makegrid($('#table_results')[0]);
92 * Append the "Show/Hide query box" message to the query input form
95 * @name appendToggleSpan
97 // do not add this link more than once
98 if (! $('#sqlqueryform').find('a').is('#togglequerybox')) {
99 $('<a id="togglequerybox"></a>')
100 .html(PMA_messages['strHideQueryBox'])
101 .appendTo("#sqlqueryform")
102 // initially hidden because at this point, nothing else
103 // appears under the link
106 // Attach the toggling of the query box visibility to a click
107 $("#togglequerybox").bind('click', function() {
109 $link.siblings().slideToggle("fast");
110 if ($link.text() == PMA_messages['strHideQueryBox']) {
111 $link.text(PMA_messages['strShowQueryBox']);
112 // cheap trick to add a spacer between the menu tabs
113 // and "Show query box"; feel free to improve!
114 $('#togglequerybox_spacer').remove();
115 $link.before('<br id="togglequerybox_spacer" />');
117 $link.text(PMA_messages['strHideQueryBox']);
119 // avoid default click action
126 * Event handler for sqlqueryform.ajax button_submit_query
130 $("#button_submit_query").live('click', function(event) {
131 var $form = $(this).closest("form");
132 // the Go button related to query submission was clicked,
133 // instead of the one related to Bookmarks, so empty the
134 // id_bookmark selector to avoid misinterpretation in
135 // import.php about what needs to be done
136 $form.find("select[name=id_bookmark]").attr("value","");
137 // let normal event propagation happen
141 * Ajax Event handler for 'SQL Query Submit'
143 * @see PMA_ajaxShowMessage()
144 * @see $cfg['AjaxEnable']
146 * @name sqlqueryform_submit
148 $("#sqlqueryform.ajax").live('submit', function(event) {
149 event.preventDefault();
152 if (! checkSqlQuery($form[0])) {
156 // remove any div containing a previous error message
157 $('.error').remove();
159 var $msgbox = PMA_ajaxShowMessage();
160 var $sqlqueryresults = $('#sqlqueryresults');
162 PMA_prepareForAjaxRequest($form);
164 $.post($form.attr('action'), $form.serialize() , function(data) {
165 if (data.success == true) {
166 // success happens if the query returns rows or not
168 // fade out previous messages, if any
169 $('.success').fadeOut();
170 $('.sqlquery_message').fadeOut();
171 // show a message that stays on screen
172 if (typeof data.action_bookmark != 'undefined') {
174 if ('1' == data.action_bookmark) {
175 $('#sqlquery').text(data.sql_query);
176 // send to codemirror if possible
177 setQuery(data.sql_query);
180 if ('2' == data.action_bookmark) {
181 $("#id_bookmark option[value='" + data.id_bookmark + "']").remove();
183 $('#sqlqueryform').before(data.message);
184 } else if (typeof data.sql_query != 'undefined') {
185 $('<div class="sqlquery_message"></div>')
186 .html(data.sql_query)
187 .insertBefore('#sqlqueryform');
188 // unnecessary div that came from data.sql_query
189 $('.notice').remove();
191 $('#sqlqueryform').before(data.message);
193 $sqlqueryresults.show();
194 // this happens if a USE command was typed
195 if (typeof data.reload != 'undefined') {
196 // Unbind the submit event before reloading. See bug #3295529
197 $("#sqlqueryform.ajax").die('submit');
198 $form.find('input[name=db]').val(data.db);
199 // need to regenerate the whole upper part
200 $form.find('input[name=ajax_request]').remove();
201 $form.append('<input type="hidden" name="reload" value="true" />');
202 $.post('db_sql.php', $form.serialize(), function(data) {
203 $('body').html(data);
204 }); // end inner post
206 } else if (data.success == false ) {
207 // show an error message that stays on screen
208 $('#sqlqueryform').before(data.error);
209 $sqlqueryresults.hide();
211 // real results are returned
212 // fade out previous messages, if any
213 $('.success').fadeOut();
214 $('.sqlquery_message').fadeOut();
215 var $received_data = $(data);
216 var $zero_row_results = $received_data.find('textarea[name="sql_query"]');
217 // if zero rows are returned from the query execution
218 if ($zero_row_results.length > 0) {
219 $('#sqlquery').val($zero_row_results.val());
220 setQuery($('#sqlquery').val());
225 .trigger('makegrid');
226 $('#togglequerybox').show();
227 if ($("#togglequerybox").siblings(":visible").length > 0) {
228 $("#togglequerybox").trigger('click');
233 PMA_ajaxRemoveMessage($msgbox);
235 }); // end SQL Query submit
238 * Ajax Event handlers for Paginating the results table
242 * Paginate when we click any of the navigation buttons
243 * (only if the element has the ajax class, see $cfg['AjaxEnable'])
245 * @name paginate_nav_button_click
246 * @uses PMA_ajaxShowMessage()
247 * @see $cfg['AjaxEnable']
249 $("input[name=navig].ajax").live('click', function(event) {
251 event.preventDefault();
253 var $msgbox = PMA_ajaxShowMessage();
256 * @var $form Object referring to the form element that paginates the results table
258 var $form = $(this).parent("form");
260 PMA_prepareForAjaxRequest($form);
262 $.post($form.attr('action'), $form.serialize(), function(data) {
263 $("#sqlqueryresults")
265 .trigger('makegrid');
268 PMA_ajaxRemoveMessage($msgbox);
270 }); // end Paginate results table
273 * Paginate results with Page Selector dropdown
275 * @name paginate_dropdown_change
276 * @see $cfg['AjaxEnable']
278 $("#pageselector").live('change', function(event) {
279 var $form = $(this).parent("form");
281 if ($(this).hasClass('ajax')) {
282 event.preventDefault();
284 var $msgbox = PMA_ajaxShowMessage();
286 $.post($form.attr('action'), $form.serialize() + '&ajax_request=true', function(data) {
287 $("#sqlqueryresults")
289 .trigger('makegrid');
291 PMA_ajaxRemoveMessage($msgbox);
297 }); // end Paginate results with Page Selector
300 * Ajax Event handler for sorting the results table
302 * @name table_results_sort_click
303 * @see $cfg['AjaxEnable']
305 $("#table_results.ajax").find("a[title=Sort]").live('click', function(event) {
306 event.preventDefault();
308 var $msgbox = PMA_ajaxShowMessage();
312 $.get($anchor.attr('href'), $anchor.serialize() + '&ajax_request=true', function(data) {
313 $("#sqlqueryresults")
315 .trigger('makegrid');
316 PMA_ajaxRemoveMessage($msgbox);
318 }); //end Sort results table
321 * Ajax Event handler for the display options
323 * @name displayOptionsForm_submit
324 * @see $cfg['AjaxEnable']
326 $("#displayOptionsForm.ajax").live('submit', function(event) {
327 event.preventDefault();
331 $.post($form.attr('action'), $form.serialize() + '&ajax_request=true' , function(data) {
332 $("#sqlqueryresults")
334 .trigger('makegrid');
337 }); //end displayOptionsForm handler
340 * Ajax Event for table row change
342 $("#resultsForm.ajax .mult_submit[value=edit]").live('click', function(event){
343 event.preventDefault();
345 /*Check whether atleast one row is selected for change*/
346 if ($("#table_results tbody tr, #table_results tbody tr td").hasClass("marked")) {
347 var div = $('<div id="change_row_dialog"></div>');
350 * @var button_options Object that stores the options passed to jQueryUI
353 var button_options = {};
354 // in the following function we need to use $(this)
355 button_options[PMA_messages['strCancel']] = function() {$(this).dialog('close').remove();};
357 var button_options_error = {};
358 button_options_error[PMA_messages['strOK']] = function() {$(this).dialog('close').remove();};
359 var $form = $("#resultsForm");
360 var $msgbox = PMA_ajaxShowMessage();
362 $.get( $form.attr('action'), $form.serialize()+"&ajax_request=true&submit_mult=row_edit", function(data) {
363 //in the case of an error, show the error message returned.
364 if (data.success != undefined && data.success == false) {
368 title: PMA_messages['strChangeTbl'],
371 open: PMA_verifyColumnsProperties,
372 close: function(event, ui) {
373 $('#change_row_dialog').remove();
375 buttons : button_options_error
376 }); // end dialog options
381 title: PMA_messages['strChangeTbl'],
384 open: PMA_verifyColumnsProperties,
385 close: function(event, ui) {
386 $('#change_row_dialog').remove();
388 buttons : button_options
390 //Remove the top menu container from the dialog
391 .find("#topmenucontainer").hide()
392 ; // end dialog options
393 $(".insertRowTable").addClass("ajax");
394 $("#buttonYes").addClass("ajax");
396 PMA_ajaxRemoveMessage($msgbox);
399 PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
404 * Click action for "Go" button in ajax dialog insertForm -> insertRowTable
406 $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function(event) {
407 event.preventDefault();
409 * @var the_form object referring to the insert form
411 var $form = $("#insertForm");
412 PMA_prepareForAjaxRequest($form);
413 //User wants to submit the form
414 $.post($form.attr('action'), $form.serialize(), function(data) {
415 if (data.success == true) {
416 PMA_ajaxShowMessage(data.message);
417 if ($("#pageselector").length != 0) {
418 $("#pageselector").trigger('change');
420 $("input[name=navig].ajax").trigger('click');
424 PMA_ajaxShowMessage(data.error, false);
425 $("#table_results tbody tr.marked .multi_checkbox " +
426 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
427 $("#table_results tbody tr.marked .multi_checkbox " +
428 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
429 $("#table_results tbody tr" +
430 ", #table_results tbody tr td").removeClass("marked");
432 if ($("#change_row_dialog").length > 0) {
433 $("#change_row_dialog").dialog("close").remove();
435 /**Update the row count at the tableForm*/
436 $("#result_query").remove();
437 $("#sqlqueryresults").prepend(data.sql_query);
438 $("#result_query .notice").remove();
439 $("#result_query").prepend((data.message));
441 }); // end insert table button "Go"
443 /**$("#buttonYes.ajax").live('click'
444 * Click action for #buttonYes button in ajax dialog insertForm
446 $("#buttonYes.ajax").live('click', function(event){
447 event.preventDefault();
449 * @var the_form object referring to the insert form
451 var $form = $("#insertForm");
452 /**Get the submit type and the after insert type in the form*/
453 var selected_submit_type = $("#insertForm").find("#actions_panel .control_at_footer option:selected").attr('value');
454 var selected_after_insert = $("#insertForm").find("#actions_panel select[name=after_insert] option:selected").attr('value');
455 $("#result_query").remove();
456 PMA_prepareForAjaxRequest($form);
457 //User wants to submit the form
458 $.post($form.attr('action'), $form.serialize() , function(data) {
459 if (data.success == true) {
460 PMA_ajaxShowMessage(data.message);
461 if (selected_submit_type == "showinsert") {
462 $("#sqlqueryresults").prepend(data.sql_query);
463 $("#result_query .notice").remove();
464 $("#result_query").prepend(data.message);
465 $("#table_results tbody tr.marked .multi_checkbox " +
466 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
467 $("#table_results tbody tr.marked .multi_checkbox " +
468 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
469 $("#table_results tbody tr" +
470 ", #table_results tbody tr td").removeClass("marked");
472 if ($("#pageselector").length != 0) {
473 $("#pageselector").trigger('change');
475 $("input[name=navig].ajax").trigger('click');
477 $("#result_query").remove();
478 $("#sqlqueryresults").prepend(data.sql_query);
479 $("#result_query .notice").remove();
480 $("#result_query").prepend((data.message));
483 PMA_ajaxShowMessage(data.error, false);
484 $("#table_results tbody tr.marked .multi_checkbox " +
485 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
486 $("#table_results tbody tr.marked .multi_checkbox " +
487 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
488 $("#table_results tbody tr" +
489 ", #table_results tbody tr td").removeClass("marked");
491 if ($("#change_row_dialog").length > 0) {
492 $("#change_row_dialog").dialog("close").remove();
497 }, 'top.frame_content'); // end $(document).ready()
501 * Starting from some th, change the class of all td under it.
502 * If isAddClass is specified, it will be used to determine whether to add or remove the class.
504 function PMA_changeClassForColumn($this_th, newclass, isAddClass)
506 // index 0 is the th containing the big T
507 var th_index = $this_th.index();
508 var has_big_t = !$this_th.closest('tr').children(':first').hasClass('column_heading');
509 // .eq() is zero-based
513 var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq('+th_index+')');
514 if (isAddClass == undefined) {
515 $tds.toggleClass(newclass);
517 $tds.toggleClass(newclass, isAddClass);
521 $(document).ready(function() {
523 $('.browse_foreign').live('click', function(e) {
525 window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
527 $anchor.addClass('browse_foreign_clicked');
532 * vertical column highlighting in horizontal mode when hovering over the column header
534 $('.column_heading.pointer').live('hover', function(e) {
535 PMA_changeClassForColumn($(this), 'hover', e.type == 'mouseenter');
539 * vertical column marking in horizontal mode when clicking the column header
541 $('.column_heading.marker').live('click', function() {
542 PMA_changeClassForColumn($(this), 'marked');
546 * create resizable table
548 $("#sqlqueryresults").trigger('makegrid');
554 function makeProfilingChart()
556 if ($('#profilingchart').length == 0) {
560 var data = new Array();
561 $.each(jQuery.parseJSON($('#profilingchart').html()),function(key,value) {
562 data.push([key,parseFloat(value)]);
565 // Prevent the user from seeing the JSON code
566 $('div#profilingchart').html('').show();
568 PMA_createProfilingChart(data);