Fixed failing test
[phpmyadmin.git] / js / sql.js
blob6e334430808e7dee4460a71946f4143a19dee171
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used wherever an sql query form is used
4  *
5  * @requires    jQuery
6  * @requires    js/functions.js
7  *
8  */
10 var $data_a;
12 /**
13  * decode a string URL_encoded
14  *
15  * @param string str
16  * @return string the URL-decoded string
17  */
18 function PMA_urldecode(str)
20     return decodeURIComponent(str.replace(/\+/g, '%20'));
23 /**
24  * endecode a string URL_decoded
25  *
26  * @param string str
27  * @return string the URL-encoded string
28  */
29 function PMA_urlencode(str)
31     return encodeURIComponent(str).replace(/\%20/g, '+');
34 /**
35  * Get the field name for the current field.  Required to construct the query
36  * for grid editing
37  *
38  * @param $this_field  jQuery object that points to the current field's tr
39  */
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);
58     }
60     field_name = $.trim(field_name);
62     return field_name;
65 /**
66  * Unbind all event handlers before tearing down a page
67  */
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');
87 });
89 /**
90  * @description <p>Ajax scripts for sql and browse pages</p>
91  *
92  * Actions ajaxified here:
93  * <ul>
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>
100  * </ul>
102  * @name        document.ready
103  * @memberOf    jQuery
104  */
105 AJAX.registerOnload('sql.js', function () {
106     // Delete row from SQL results
107     $('a.delete_row.ajax').live('click', function (e) {
108         e.preventDefault();
109         var question = $.sprintf(PMA_messages.strDoYouReally, $(this).closest('td').find('div').text());
110         var $link = $(this);
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) {
114                 if (data.success) {
115                     PMA_ajaxShowMessage(data.message);
116                     $link.closest('tr').remove();
117                 } else {
118                     PMA_ajaxShowMessage(data.error, false);
119                 }
120             });
121         });
122     });
124     // Ajaxification for 'Bookmark this SQL query'
125     $('#bookmarkQueryForm').live('submit', function (e) {
126         e.preventDefault();
127         PMA_ajaxShowMessage();
128         $.post($(this).attr('action'), 'ajax_request=1&' + $(this).serialize(), function (data) {
129             if (data.success) {
130                 PMA_ajaxShowMessage(data.message);
131             } else {
132                 PMA_ajaxShowMessage(data.error, false);
133             }
134         });
135     });
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')
140             .parent()
141             .toggle($(this).val().length > 0);
142     }).trigger('keyup');
144     /**
145      * Attach the {@link makegrid} function to a custom event, which will be
146      * triggered manually everytime the table of results is reloaded
147      * @memberOf    jQuery
148      */
149     $("#sqlqueryresults").live('makegrid', function () {
150         PMA_makegrid($('#table_results')[0]);
151     });
153     /**
154      * Append the "Show/Hide query box" message to the query input form
155      *
156      * @memberOf jQuery
157      * @name    appendToggleSpan
158      */
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
166         .hide();
168         // Attach the toggling of the query box visibility to a click
169         $("#togglequerybox").bind('click', function () {
170             var $link = $(this);
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" />');
178             } else {
179                 $link.text(PMA_messages.strHideQueryBox);
180             }
181             // avoid default click action
182             return false;
183         });
184     }
187     /**
188      * Event handler for sqlqueryform.ajax button_submit_query
189      *
190      * @memberOf    jQuery
191      */
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
200     });
202     /**
203      * Event handler for hitting enter on sqlqueryform bookmark_variable
204      * (the Variable textfield in Bookmarked SQL query section)
205      *
206      * @memberOf    jQuery
207      */
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
215             // attribute.
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();
221             return false;
222         } else  {
223             return true;
224         }
225     });
227     /**
228      * Ajax Event handler for 'SQL Query Submit'
229      *
230      * @see         PMA_ajaxShowMessage()
231      * @memberOf    jQuery
232      * @name        sqlqueryform_submit
233      */
234     $("#sqlqueryform.ajax").live('submit', function (event) {
235         event.preventDefault();
237         var $form = $(this);
238         if (! checkSqlQuery($form[0])) {
239             return false;
240         }
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
253                 //
254                 // fade out previous messages, if any
255                 $('div.success, div.sqlquery_message').fadeOut();
256                 if ($('#result_query').length) {
257                     $('#result_query').remove();
258                 }
260                 // show a message that stays on screen
261                 if (typeof data.action_bookmark != 'undefined') {
262                     // view only
263                     if ('1' == data.action_bookmark) {
264                         $('#sqlquery').text(data.sql_query);
265                         // send to codemirror if possible
266                         setQuery(data.sql_query);
267                     }
268                     // delete
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();
276                         }
277                     }
278                     $sqlqueryresults
279                      .show()
280                      .html(data.message);
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();
287                 } else {
288                     $sqlqueryresults
289                      .show()
290                      .html(data.message);
291                 }
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();
298                         } else {
299                             PMA_reloadNavigation();
300                         }
301                     }
302                 } else if (typeof data.reload != 'undefined') {
303                     // this happens if a USE or DROP command was typed
304                     PMA_commonActions.setDb(data.db);
305                     var url;
306                     if (data.db) {
307                         if (data.table) {
308                             url = 'table_sql.php';
309                         } else {
310                             url = 'db_sql.php';
311                         }
312                     } else {
313                         url = 'server_sql.php';
314                     }
315                     PMA_commonActions.refreshMain(url, function () {
316                         if ($('#result_query').length) {
317                             $('#result_query').remove();
318                         }
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'));
324                         }
325                     });
326                 }
328                 $sqlqueryresults.show().trigger('makegrid');
329                 $('#togglequerybox').show();
330                 PMA_init_slider();
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');
336                         }
337                     }
338                 }
339             } else if (data.success === false) {
340                 // show an error message that stays on screen
341                 $('#sqlqueryform').before(data.error);
342                 $sqlqueryresults.hide();
343             }
344             PMA_ajaxRemoveMessage($msgbox);
345         }); // end $.post()
346     }); // end SQL Query submit
348     /**
349      * Paginate results with Page Selector dropdown
350      * @memberOf    jQuery
351      * @name        paginate_dropdown_change
352      */
353     $("#pageselector").live('change', function (event) {
354         var $form = $(this).parent("form");
355         $form.submit();
356     }); // end Paginate results with Page Selector
358     /**
359      * Ajax Event handler for the display options
360      * @memberOf    jQuery
361      * @name        displayOptionsForm_submit
362      */
363     $("#displayOptionsForm.ajax").live('submit', function (event) {
364         event.preventDefault();
366         $form = $(this);
368         $.post($form.attr('action'), $form.serialize() + '&ajax_request=true', function (data) {
369             $("#sqlqueryresults")
370              .html(data.message)
371              .trigger('makegrid');
372             PMA_init_slider();
373         }); // end $.post()
374     }); //end displayOptionsForm handler
377  * Ajax Event for table row change
378  * */
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>');
386             /**
387              * @var    button_options  Object that stores the options passed to jQueryUI
388              *                          dialog
389              */
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');
394             };
396             var button_options_error = {};
397             button_options_error[PMA_messages.strOK] = function () {
398                 $(this).dialog('close');
399             };
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) {
406                     $div
407                     .append(data.error)
408                     .dialog({
409                         title: PMA_messages.strChangeTbl,
410                         height: 230,
411                         width: 900,
412                         open: PMA_verifyColumnsProperties,
413                         close: function (event, ui) {
414                             $(this).remove();
415                         },
416                         buttons : button_options_error
417                     }); // end dialog options
418                 } else {
419                     $div
420                     .append(data.message)
421                     .dialog({
422                         title: PMA_messages.strChangeTbl,
423                         height: 600,
424                         width: 900,
425                         open: PMA_verifyColumnsProperties,
426                         close: function (event, ui) {
427                             $(this).remove();
428                         },
429                         buttons : button_options
430                     })
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");
436                 }
437                 PMA_ajaxRemoveMessage($msgbox);
438             }); // end $.get()
439         } else {
440             PMA_ajaxShowMessage(PMA_messages.strNoRowSelected);
441         }
442     });
444     /**
445      * Checks whether at least one row is selected for deletion or export
446      */
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);
453         }
454     });
456  * Click action for "Go" button in ajax dialog insertForm -> insertRowTable
457  */
458     $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function (event) {
459         event.preventDefault();
460         /**
461          * @var    the_form    object referring to the insert form
462          */
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');
471                 } else {
472                     $("input[name=navig].ajax").trigger('click');
473                 }
475             } else {
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");
483             }
484             if ($("#change_row_dialog").length > 0) {
485                 $("#change_row_dialog").dialog("close").remove();
486             }
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));
492         }); // end $.post()
493     }); // end insert table button "Go"
495 /**$("#buttonYes.ajax").live('click'
496  * Click action for #buttonYes button in ajax dialog insertForm
497  */
499     $("#buttonYes.ajax").live('click', function (event) {
500         event.preventDefault();
501         /**
502          * @var    the_form    object referring to the insert form
503          */
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");
523                 } else {
524                     if ($("#pageselector").length !== 0) {
525                         $("#pageselector").trigger('change');
526                     } else {
527                         $("input[name=navig].ajax").trigger('click');
528                     }
529                     $("#result_query").remove();
530                     $("#sqlqueryresults").prepend(data.sql_query);
531                     $("#result_query .notice").remove();
532                     $("#result_query").prepend((data.message));
533                 }
534             } else {
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");
542             }
543             if ($("#change_row_dialog").length > 0) {
544                 $("#change_row_dialog").dialog("close").remove();
545             }
546         }); // end $.post()
547     });
549 }); // end $()
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.
555  */
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
562     if (has_big_t) {
563         th_index--;
564     }
565     var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq(' + th_index + ')');
566     if (isAddClass === undefined) {
567         $tds.toggleClass(newclass);
568     } else {
569         $tds.toggleClass(newclass, isAddClass);
570     }
573 AJAX.registerOnload('sql.js', function () {
575     $('a.browse_foreign').live('click', function (e) {
576         e.preventDefault();
577         window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
578         $anchor = $(this);
579         $anchor.addClass('browse_foreign_clicked');
580     });
582     /**
583      * vertical column highlighting in horizontal mode when hovering over the column header
584      */
585     $('th.column_heading.pointer').live('hover', function (e) {
586         PMA_changeClassForColumn($(this), 'hover', e.type == 'mouseenter');
587     });
589     /**
590      * vertical column marking in horizontal mode when clicking the column header
591      */
592     $('th.column_heading.marker').live('click', function () {
593         PMA_changeClassForColumn($(this), 'marked');
594     });
596     /**
597      * create resizable table
598      */
599     $("#sqlqueryresults").trigger('makegrid');
603  * Profiling Chart
604  */
605 function makeProfilingChart()
607     if ($('#profilingchart').length === 0 ||
608         $('#profilingchart').html().length !== 0 ||
609         !$.jqplot || !$.jqplot.Highlighter || !$.jqplot.PieRenderer
610     ) {
611         return;
612     }
614     var data = [];
615     $.each(jQuery.parseJSON($('#profilingChartData').html()), function (key, value) {
616         data.push([key, parseFloat(value)]);
617     });
619     // Remove chart and data divs contents
620     $('#profilingchart').html('').show();
621     $('#profilingChartData').html('');
623     PMA_createProfilingChartJqplot('profilingchart', data);
627  * initialize profiling data tables
628  */
629 function initProfilingTables()
631     if (!$.tablesorter) {
632         return;
633     }
635     $('#profiletable').tablesorter({
636         widgets: ['zebra'],
637         sortList: [[0, 0]],
638         textExtraction: function (node) {
639             if (node.children.length > 0) {
640                 return node.children[0].innerHTML;
641             } else {
642                 return node.innerHTML;
643             }
644         }
645     });
647     $('#profilesummarytable').tablesorter({
648         widgets: ['zebra'],
649         sortList: [[1, 1]],
650         textExtraction: function (node) {
651             if (node.children.length > 0) {
652                 return node.children[0].innerHTML;
653             } else {
654                 return node.innerHTML;
655             }
656         }
657     });
660 AJAX.registerOnload('sql.js', function () {
661     makeProfilingChart();
662     initProfilingTables();