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'));
24 * endecode a string URL_decoded
27 * @return string the URL-encoded string
29 function PMA_urlencode(str)
31 return encodeURIComponent(str).replace(/\%20/g, '+');
35 * Get the field name for the current field. Required to construct the query
38 * @param $this_field jQuery object that points to the current field's tr
40 function getFieldName($this_field)
43 var this_field_index = $this_field.index();
44 // ltr or rtl direction does not impact how the DOM was generated
45 // check if the action column in the left exist
46 var left_action_exist = !$('#table_results').find('th:first').hasClass('draggable');
47 // number of column span for checkbox and Actions
48 var left_action_skip = left_action_exist ? $('#table_results').find('th:first').attr('colspan') - 1 : 0;
49 var field_name = $('#table_results').find('thead').find('th:eq(' + (this_field_index - left_action_skip) + ') a').text();
50 // happens when just one row (headings contain no a)
51 if (field_name === '') {
52 var $heading = $('#table_results').find('thead').find('th:eq(' + (this_field_index - left_action_skip) + ')').children('span');
53 // may contain column comment enclosed in a span - detach it temporarily to read the column name
54 var $tempColComment = $heading.children().detach();
55 field_name = $heading.text();
56 // re-attach the column comment
57 $heading.append($tempColComment);
60 field_name = $.trim(field_name);
66 * Unbind all event handlers before tearing down a page
68 AJAX.registerTeardown('sql.js', function () {
69 $('a.delete_row.ajax').die('click');
70 $('#bookmarkQueryForm').die('submit');
71 $('input#bkm_label').unbind('keyup');
72 $("#sqlqueryresults").die('makegrid');
73 $("#togglequerybox").unbind('click');
74 $("#button_submit_query").die('click');
75 $("input[name=bookmark_variable]").unbind("keypress");
76 $("#sqlqueryform.ajax").die('submit');
77 $("input[name=navig].ajax").die('click');
78 $("#pageselector").die('change');
79 $("#table_results.ajax").find("a[title=Sort]").die('click');
80 $("#displayOptionsForm.ajax").die('submit');
81 $("#resultsForm.ajax .mult_submit[value=edit]").die('click');
82 $("#insertForm .insertRowTable.ajax input[type=submit]").die('click');
83 $("#buttonYes.ajax").die('click');
84 $('a.browse_foreign').die('click');
85 $('th.column_heading.pointer').die('hover');
86 $('th.column_heading.marker').die('click');
90 * @description <p>Ajax scripts for sql and browse pages</p>
92 * Actions ajaxified here:
94 * <li>Retrieve results of an SQL query</li>
95 * <li>Paginate the results table</li>
96 * <li>Sort the results table</li>
97 * <li>Change table according to display options</li>
98 * <li>Grid editing of data</li>
99 * <li>Saving a bookmark</li>
102 * @name document.ready
105 AJAX.registerOnload('sql.js', function () {
106 // Delete row from SQL results
107 $('a.delete_row.ajax').live('click', function (e) {
109 var question = $.sprintf(PMA_messages.strDoYouReally, $(this).closest('td').find('div').text());
111 $link.PMA_confirm(question, $link.attr('href'), function (url) {
112 $msgbox = PMA_ajaxShowMessage();
113 $.get(url, {'ajax_request': true, 'is_js_confirmed': true}, function (data) {
115 PMA_ajaxShowMessage(data.message);
116 $link.closest('tr').remove();
118 PMA_ajaxShowMessage(data.error, false);
124 // Ajaxification for 'Bookmark this SQL query'
125 $('#bookmarkQueryForm').live('submit', function (e) {
127 PMA_ajaxShowMessage();
128 $.post($(this).attr('action'), 'ajax_request=1&' + $(this).serialize(), function (data) {
130 PMA_ajaxShowMessage(data.message);
132 PMA_ajaxShowMessage(data.error, false);
137 /* Hides the bookmarkoptions checkboxes when the bookmark label is empty */
138 $('input#bkm_label').keyup(function () {
139 $('input#id_bkm_all_users, input#id_bkm_replace')
141 .toggle($(this).val().length > 0);
145 * Attach the {@link makegrid} function to a custom event, which will be
146 * triggered manually everytime the table of results is reloaded
149 $("#sqlqueryresults").live('makegrid', function () {
150 PMA_makegrid($('#table_results')[0]);
154 * Append the "Show/Hide query box" message to the query input form
157 * @name appendToggleSpan
159 // do not add this link more than once
160 if (! $('#sqlqueryform').find('a').is('#togglequerybox')) {
161 $('<a id="togglequerybox"></a>')
162 .html(PMA_messages.strHideQueryBox)
163 .appendTo("#sqlqueryform")
164 // initially hidden because at this point, nothing else
165 // appears under the link
168 // Attach the toggling of the query box visibility to a click
169 $("#togglequerybox").bind('click', function () {
171 $link.siblings().slideToggle("fast");
172 if ($link.text() == PMA_messages.strHideQueryBox) {
173 $link.text(PMA_messages.strShowQueryBox);
174 // cheap trick to add a spacer between the menu tabs
175 // and "Show query box"; feel free to improve!
176 $('#togglequerybox_spacer').remove();
177 $link.before('<br id="togglequerybox_spacer" />');
179 $link.text(PMA_messages.strHideQueryBox);
181 // avoid default click action
188 * Event handler for sqlqueryform.ajax button_submit_query
192 $("#button_submit_query").live('click', function (event) {
193 var $form = $(this).closest("form");
194 // the Go button related to query submission was clicked,
195 // instead of the one related to Bookmarks, so empty the
196 // id_bookmark selector to avoid misinterpretation in
197 // import.php about what needs to be done
198 $form.find("select[name=id_bookmark]").val("");
199 // let normal event propagation happen
203 * Event handler for hitting enter on sqlqueryform bookmark_variable
204 * (the Variable textfield in Bookmarked SQL query section)
208 $("input[name=bookmark_variable]").bind("keypress", function (event) {
209 // force the 'Enter Key' to implicitly click the #button_submit_bookmark
210 var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
211 if (keycode == 13) { // keycode for enter key
212 // When you press enter in the sqlqueryform, which
213 // has 2 submit buttons, the default is to run the
214 // #button_submit_query, because of the tabindex
216 // This submits #button_submit_bookmark instead,
217 // because when you are in the Bookmarked SQL query
218 // section and hit enter, you expect it to do the
219 // same action as the Go button in that section.
220 $("#button_submit_bookmark").click();
228 * Ajax Event handler for 'SQL Query Submit'
230 * @see PMA_ajaxShowMessage()
232 * @name sqlqueryform_submit
234 $("#sqlqueryform.ajax").live('submit', function (event) {
235 event.preventDefault();
238 if (! checkSqlQuery($form[0])) {
242 // remove any div containing a previous error message
243 $('div.error').remove();
245 var $msgbox = PMA_ajaxShowMessage();
246 var $sqlqueryresults = $('#sqlqueryresults');
248 PMA_prepareForAjaxRequest($form);
250 $.post($form.attr('action'), $form.serialize(), function (data) {
251 if (data.success === true) {
252 // success happens if the query returns rows or not
254 // fade out previous messages, if any
255 $('div.success, div.sqlquery_message').fadeOut();
256 if ($('#result_query').length) {
257 $('#result_query').remove();
260 // show a message that stays on screen
261 if (typeof data.action_bookmark != 'undefined') {
263 if ('1' == data.action_bookmark) {
264 $('#sqlquery').text(data.sql_query);
265 // send to codemirror if possible
266 setQuery(data.sql_query);
269 if ('2' == data.action_bookmark) {
270 $("#id_bookmark option[value='" + data.id_bookmark + "']").remove();
271 // if there are no bookmarked queries now (only the empty option),
272 // remove the bookmark section
273 if ($('#id_bookmark option').length == 1) {
274 $('#fieldsetBookmarkOptions').hide();
275 $('#fieldsetBookmarkOptionsFooter').hide();
281 } else if (typeof data.sql_query != 'undefined') {
282 $('<div class="sqlquery_message"></div>')
283 .html(data.sql_query)
284 .insertBefore('#sqlqueryform');
285 // unnecessary div that came from data.sql_query
286 $('div.notice').remove();
293 if (typeof data.ajax_reload != 'undefined') {
294 if (data.ajax_reload.reload) {
295 if (data.ajax_reload.table_name) {
296 PMA_commonParams.set('table', data.ajax_reload.table_name);
297 PMA_commonActions.refreshMain();
299 PMA_reloadNavigation();
302 } else if (typeof data.reload != 'undefined') {
303 // this happens if a USE or DROP command was typed
304 PMA_commonActions.setDb(data.db);
308 url = 'table_sql.php';
313 url = 'server_sql.php';
315 PMA_commonActions.refreshMain(url, function () {
316 if ($('#result_query').length) {
317 $('#result_query').remove();
319 if (data.sql_query) {
320 $('<div id="result_query"></div>')
321 .html(data.sql_query)
322 .prependTo('#page_content');
323 PMA_highlightSQL($('#page_content'));
328 $sqlqueryresults.show().trigger('makegrid');
329 $('#togglequerybox').show();
332 if (typeof data.action_bookmark == 'undefined') {
333 if ($('#sqlqueryform input[name="retain_query_box"]').is(':checked') !== true) {
334 if ($("#togglequerybox").siblings(":visible").length > 0) {
335 $("#togglequerybox").trigger('click');
339 } else if (data.success === false) {
340 // show an error message that stays on screen
341 $('#sqlqueryform').before(data.error);
342 $sqlqueryresults.hide();
344 PMA_ajaxRemoveMessage($msgbox);
346 }); // end SQL Query submit
349 * Paginate results with Page Selector dropdown
351 * @name paginate_dropdown_change
353 $("#pageselector").live('change', function (event) {
354 var $form = $(this).parent("form");
356 }); // end Paginate results with Page Selector
359 * Ajax Event handler for the display options
361 * @name displayOptionsForm_submit
363 $("#displayOptionsForm.ajax").live('submit', function (event) {
364 event.preventDefault();
368 $.post($form.attr('action'), $form.serialize() + '&ajax_request=true', function (data) {
369 $("#sqlqueryresults")
371 .trigger('makegrid');
374 }); //end displayOptionsForm handler
377 * Ajax Event for table row change
379 $("#resultsForm.ajax .mult_submit[value=edit]").live('click', function (event) {
380 event.preventDefault();
382 /*Check whether at least one row is selected*/
383 if ($("#table_results tbody tr, #table_results tbody tr td").hasClass("marked")) {
384 var $div = $('<div id="change_row_dialog"></div>');
387 * @var button_options Object that stores the options passed to jQueryUI
390 var button_options = {};
391 // in the following function we need to use $(this)
392 button_options[PMA_messages.strCancel] = function () {
393 $(this).dialog('close');
396 var button_options_error = {};
397 button_options_error[PMA_messages.strOK] = function () {
398 $(this).dialog('close');
400 var $form = $("#resultsForm");
401 var $msgbox = PMA_ajaxShowMessage();
403 $.get($form.attr('action'), $form.serialize() + "&ajax_request=true&submit_mult=row_edit", function (data) {
404 //in the case of an error, show the error message returned.
405 if (data.success !== undefined && data.success === false) {
409 title: PMA_messages.strChangeTbl,
412 open: PMA_verifyColumnsProperties,
413 close: function (event, ui) {
416 buttons : button_options_error
417 }); // end dialog options
420 .append(data.message)
422 title: PMA_messages.strChangeTbl,
425 open: PMA_verifyColumnsProperties,
426 close: function (event, ui) {
429 buttons : button_options
431 //Remove the top menu container from the dialog
432 .find("#topmenucontainer").hide()
433 ; // end dialog options
434 $("table.insertRowTable").addClass("ajax");
435 $("#buttonYes").addClass("ajax");
437 PMA_ajaxRemoveMessage($msgbox);
440 PMA_ajaxShowMessage(PMA_messages.strNoRowSelected);
445 * Checks whether at least one row is selected for deletion or export
447 $("#resultsForm.ajax .mult_submit[value=delete]," +
448 "#resultsForm.ajax .mult_submit[value=export]").live('click', function (event) {
449 /*Check whether at least one row is selected*/
450 if (!$("#table_results tbody tr, #table_results tbody tr td").hasClass("marked")) {
451 event.preventDefault();
452 PMA_ajaxShowMessage(PMA_messages.strNoRowSelected);
456 * Click action for "Go" button in ajax dialog insertForm -> insertRowTable
458 $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function (event) {
459 event.preventDefault();
461 * @var the_form object referring to the insert form
463 var $form = $("#insertForm");
464 PMA_prepareForAjaxRequest($form);
465 //User wants to submit the form
466 $.post($form.attr('action'), $form.serialize(), function (data) {
467 if (data.success === true) {
468 PMA_ajaxShowMessage(data.message);
469 if ($("#pageselector").length !== 0) {
470 $("#pageselector").trigger('change');
472 $("input[name=navig].ajax").trigger('click');
476 PMA_ajaxShowMessage(data.error, false);
477 $("#table_results tbody tr.marked .multi_checkbox " +
478 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
479 $("#table_results tbody tr.marked .multi_checkbox " +
480 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
481 $("#table_results tbody tr" +
482 ", #table_results tbody tr td").removeClass("marked");
484 if ($("#change_row_dialog").length > 0) {
485 $("#change_row_dialog").dialog("close").remove();
487 /**Update the row count at the tableForm*/
488 $("#result_query").remove();
489 $("#sqlqueryresults").prepend(data.sql_query);
490 $("#result_query .notice").remove();
491 $("#result_query").prepend((data.message));
493 }); // end insert table button "Go"
495 /**$("#buttonYes.ajax").live('click'
496 * Click action for #buttonYes button in ajax dialog insertForm
499 $("#buttonYes.ajax").live('click', function (event) {
500 event.preventDefault();
502 * @var the_form object referring to the insert form
504 var $form = $("#insertForm");
505 /**Get the submit type in the form*/
506 var selected_submit_type = $("#insertForm").find("#actions_panel .control_at_footer option:selected").val();
507 $("#result_query").remove();
508 PMA_prepareForAjaxRequest($form);
509 //User wants to submit the form
510 $.post($form.attr('action'), $form.serialize(), function (data) {
511 if (data.success === true) {
512 PMA_ajaxShowMessage(data.message);
513 if (selected_submit_type == "showinsert") {
514 $("#sqlqueryresults").prepend(data.sql_query);
515 $("#result_query .notice").remove();
516 $("#result_query").prepend(data.message);
517 $("#table_results tbody tr.marked .multi_checkbox " +
518 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
519 $("#table_results tbody tr.marked .multi_checkbox " +
520 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
521 $("#table_results tbody tr" +
522 ", #table_results tbody tr td").removeClass("marked");
524 if ($("#pageselector").length !== 0) {
525 $("#pageselector").trigger('change');
527 $("input[name=navig].ajax").trigger('click');
529 $("#result_query").remove();
530 $("#sqlqueryresults").prepend(data.sql_query);
531 $("#result_query .notice").remove();
532 $("#result_query").prepend((data.message));
535 PMA_ajaxShowMessage(data.error, false);
536 $("#table_results tbody tr.marked .multi_checkbox " +
537 ", #table_results tbody tr td.marked .multi_checkbox").prop("checked", false);
538 $("#table_results tbody tr.marked .multi_checkbox " +
539 ", #table_results tbody tr td.marked .multi_checkbox").removeClass("last_clicked");
540 $("#table_results tbody tr" +
541 ", #table_results tbody tr td").removeClass("marked");
543 if ($("#change_row_dialog").length > 0) {
544 $("#change_row_dialog").dialog("close").remove();
553 * Starting from some th, change the class of all td under it.
554 * If isAddClass is specified, it will be used to determine whether to add or remove the class.
556 function PMA_changeClassForColumn($this_th, newclass, isAddClass)
558 // index 0 is the th containing the big T
559 var th_index = $this_th.index();
560 var has_big_t = !$this_th.closest('tr').children(':first').hasClass('column_heading');
561 // .eq() is zero-based
565 var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq(' + th_index + ')');
566 if (isAddClass === undefined) {
567 $tds.toggleClass(newclass);
569 $tds.toggleClass(newclass, isAddClass);
573 AJAX.registerOnload('sql.js', function () {
575 $('a.browse_foreign').live('click', function (e) {
577 window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
579 $anchor.addClass('browse_foreign_clicked');
583 * vertical column highlighting in horizontal mode when hovering over the column header
585 $('th.column_heading.pointer').live('hover', function (e) {
586 PMA_changeClassForColumn($(this), 'hover', e.type == 'mouseenter');
590 * vertical column marking in horizontal mode when clicking the column header
592 $('th.column_heading.marker').live('click', function () {
593 PMA_changeClassForColumn($(this), 'marked');
597 * create resizable table
599 $("#sqlqueryresults").trigger('makegrid');
605 function makeProfilingChart()
607 if ($('#profilingchart').length === 0 ||
608 $('#profilingchart').html().length !== 0 ||
609 !$.jqplot || !$.jqplot.Highlighter || !$.jqplot.PieRenderer
615 $.each(jQuery.parseJSON($('#profilingChartData').html()), function (key, value) {
616 data.push([key, parseFloat(value)]);
619 // Remove chart and data divs contents
620 $('#profilingchart').html('').show();
621 $('#profilingChartData').html('');
623 PMA_createProfilingChartJqplot('profilingchart', data);
627 * initialize profiling data tables
629 function initProfilingTables()
631 if (!$.tablesorter) {
635 $('#profiletable').tablesorter({
638 textExtraction: function (node) {
639 if (node.children.length > 0) {
640 return node.children[0].innerHTML;
642 return node.innerHTML;
647 $('#profilesummarytable').tablesorter({
650 textExtraction: function (node) {
651 if (node.children.length > 0) {
652 return node.children[0].innerHTML;
654 return node.innerHTML;
660 AJAX.registerOnload('sql.js', function () {
661 makeProfilingChart();
662 initProfilingTables();