Translation update done using Pootle.
[phpmyadmin.git] / js / sql.js
blobabd0be77d1d3dab2be85744f847b67cde8b1afaf
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 function PMA_urlencode(str)
25     return encodeURIComponent(str).replace(/\%20/g, '+');
28 /**
29  * Get the field name for the current field.  Required to construct the query
30  * for grid editing
31  *
32  * @param   $this_field  jQuery object that points to the current field's tr
33  */
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();
47     }
49     field_name = $.trim(field_name);
51     return field_name;
55 /**#@+
56  * @namespace   jQuery
57  */
59 /**
60  * @description <p>Ajax scripts for sql and browse pages</p>
61  *
62  * Actions ajaxified here:
63  * <ul>
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>
69  * </ul>
70  *
71  * @name        document.ready
72  * @memberOf    jQuery
73  */
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')
78             .parent()
79             .toggle($(this).attr('value').length > 0);
80     }).trigger('keyup');
82     /**
83      * Attach the {@link makegrid} function to a custom event, which will be
84      * triggered manually everytime the table of results is reloaded
85      * @memberOf    jQuery
86      */
87     $("#sqlqueryresults").live('makegrid', function() {
88         PMA_makegrid($('#table_results')[0]);
89     });
91     /**
92      * Append the "Show/Hide query box" message to the query input form
93      *
94      * @memberOf jQuery
95      * @name    appendToggleSpan
96      */
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
104         .hide();
106         // Attach the toggling of the query box visibility to a click
107         $("#togglequerybox").bind('click', function() {
108             var $link = $(this);
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" />');
116             } else {
117                 $link.text(PMA_messages['strHideQueryBox']);
118             }
119             // avoid default click action
120             return false;
121         });
122     }
125     /**
126      * Event handler for sqlqueryform.ajax button_submit_query 
127      *
128      * @memberOf    jQuery
129      */
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
138     });
140     /**
141      * Ajax Event handler for 'SQL Query Submit'
142      *
143      * @see         PMA_ajaxShowMessage()
144      * @see         $cfg['AjaxEnable']
145      * @memberOf    jQuery
146      * @name        sqlqueryform_submit
147      */
148     $("#sqlqueryform.ajax").live('submit', function(event) {
149         event.preventDefault();
151         var $form = $(this);
152         if (! checkSqlQuery($form[0])) {
153             return false;
154         }
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
167                 //
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') {
173                     // view only
174                     if ('1' == data.action_bookmark) {
175                         $('#sqlquery').text(data.sql_query);
176                         // send to codemirror if possible
177                         setQuery(data.sql_query);
178                     }
179                     // delete
180                     if ('2' == data.action_bookmark) {
181                         $("#id_bookmark option[value='" + data.id_bookmark + "']").remove();
182                     }
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();
190                 } else {
191                     $('#sqlqueryform').before(data.message);
192                 }
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
205                 }
206             } else if (data.success == false ) {
207                 // show an error message that stays on screen
208                 $('#sqlqueryform').before(data.error);
209                 $sqlqueryresults.hide();
210             }  else {
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());
221                 } else {
222                     $sqlqueryresults
223                      .show()
224                      .html(data)
225                      .trigger('makegrid');
226                     $('#togglequerybox').show();
227                     if ($("#togglequerybox").siblings(":visible").length > 0) {
228                         $("#togglequerybox").trigger('click');
229                     }
230                     PMA_init_slider();
231                 }
232             }
233             PMA_ajaxRemoveMessage($msgbox);
234         }); // end $.post()
235     }); // end SQL Query submit
237     /**
238      * Ajax Event handlers for Paginating the results table
239      */
241     /**
242      * Paginate when we click any of the navigation buttons
243      * (only if the element has the ajax class, see $cfg['AjaxEnable'])
244      * @memberOf    jQuery
245      * @name        paginate_nav_button_click
246      * @uses        PMA_ajaxShowMessage()
247      * @see         $cfg['AjaxEnable']
248      */
249     $("input[name=navig].ajax").live('click', function(event) {
250         /** @lends jQuery */
251         event.preventDefault();
253         var $msgbox = PMA_ajaxShowMessage();
255         /**
256          * @var $form    Object referring to the form element that paginates the results table
257          */
258         var $form = $(this).parent("form");
260         PMA_prepareForAjaxRequest($form);
262         $.post($form.attr('action'), $form.serialize(), function(data) {
263             $("#sqlqueryresults")
264              .html(data)
265              .trigger('makegrid');
266             PMA_init_slider();
268             PMA_ajaxRemoveMessage($msgbox);
269         }); // end $.post()
270     }); // end Paginate results table
272     /**
273      * Paginate results with Page Selector dropdown
274      * @memberOf    jQuery
275      * @name        paginate_dropdown_change
276      * @see         $cfg['AjaxEnable']
277      */
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")
288                  .html(data)
289                  .trigger('makegrid');
290                 PMA_init_slider();
291                 PMA_ajaxRemoveMessage($msgbox);
292             }); // end $.post()
293         } else {
294             $form.submit();
295         }
297     }); // end Paginate results with Page Selector
299     /**
300      * Ajax Event handler for sorting the results table
301      * @memberOf    jQuery
302      * @name        table_results_sort_click
303      * @see         $cfg['AjaxEnable']
304      */
305     $("#table_results.ajax").find("a[title=Sort]").live('click', function(event) {
306         event.preventDefault();
308         var $msgbox = PMA_ajaxShowMessage();
310         $anchor = $(this);
312         $.get($anchor.attr('href'), $anchor.serialize() + '&ajax_request=true', function(data) {
313             $("#sqlqueryresults")
314              .html(data)
315              .trigger('makegrid');
316             PMA_ajaxRemoveMessage($msgbox);
317         }); // end $.get()
318     }); //end Sort results table
320     /**
321      * Ajax Event handler for the display options
322      * @memberOf    jQuery
323      * @name        displayOptionsForm_submit
324      * @see         $cfg['AjaxEnable']
325      */
326     $("#displayOptionsForm.ajax").live('submit', function(event) {
327         event.preventDefault();
329         $form = $(this);
331         $.post($form.attr('action'), $form.serialize() + '&ajax_request=true' , function(data) {
332             $("#sqlqueryresults")
333              .html(data)
334              .trigger('makegrid');
335             PMA_init_slider();
336         }); // end $.post()
337     }); //end displayOptionsForm handler
340  * Ajax Event for table row change
341  * */
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>');
349             /**
350              *  @var    button_options  Object that stores the options passed to jQueryUI
351              *                          dialog
352              */
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) {
365                     div
366                     .append(data.error)
367                     .dialog({
368                         title: PMA_messages['strChangeTbl'],
369                         height: 230,
370                         width: 900,
371                         open: PMA_verifyColumnsProperties,
372                         close: function(event, ui) {
373                             $('#change_row_dialog').remove();
374                         },
375                         buttons : button_options_error
376                     }); // end dialog options
377                 } else {
378                     div
379                     .append(data)
380                     .dialog({
381                         title: PMA_messages['strChangeTbl'],
382                         height: 600,
383                         width: 900,
384                         open: PMA_verifyColumnsProperties,
385                         close: function(event, ui) {
386                             $('#change_row_dialog').remove();
387                         },
388                         buttons : button_options
389                     })
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");
395                 }
396                 PMA_ajaxRemoveMessage($msgbox);
397             }); // end $.get()
398         } else {
399             PMA_ajaxShowMessage(PMA_messages['strNoRowSelected']);
400         }
401     });
404  * Click action for "Go" button in ajax dialog insertForm -> insertRowTable
405  */
406     $("#insertForm .insertRowTable.ajax input[type=submit]").live('click', function(event) {
407         event.preventDefault();
408         /**
409          *  @var    the_form    object referring to the insert form
410          */
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');
419                 } else {
420                     $("input[name=navig].ajax").trigger('click');
421                 }
423             } else {
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");
431             }
432             if ($("#change_row_dialog").length > 0) {
433                 $("#change_row_dialog").dialog("close").remove();
434             }
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));
440         }); // end $.post()
441     }); // end insert table button "Go"
443 /**$("#buttonYes.ajax").live('click'
444  * Click action for #buttonYes button in ajax dialog insertForm
445  */
446     $("#buttonYes.ajax").live('click', function(event){
447         event.preventDefault();
448         /**
449          *  @var    the_form    object referring to the insert form
450          */
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");
471                 } else {
472                     if ($("#pageselector").length != 0) {
473                         $("#pageselector").trigger('change');
474                     } else {
475                         $("input[name=navig].ajax").trigger('click');
476                     }
477                     $("#result_query").remove();
478                     $("#sqlqueryresults").prepend(data.sql_query);
479                     $("#result_query .notice").remove();
480                     $("#result_query").prepend((data.message));
481                 }
482             } else {
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");
490             }
491             if ($("#change_row_dialog").length > 0) {
492                 $("#change_row_dialog").dialog("close").remove();
493             }
494         }); // end $.post()
495     });
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.
503  */
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
510     if (has_big_t) {
511         th_index--;
512     }
513     var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq('+th_index+')');
514     if (isAddClass == undefined) {
515         $tds.toggleClass(newclass);
516     } else {
517         $tds.toggleClass(newclass, isAddClass);
518     }
521 $(document).ready(function() {
523     $('.browse_foreign').live('click', function(e) {
524         e.preventDefault();
525         window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
526         $anchor = $(this);
527         $anchor.addClass('browse_foreign_clicked');
528         return false;
529     });
531     /**
532      * vertical column highlighting in horizontal mode when hovering over the column header
533      */
534     $('.column_heading.pointer').live('hover', function(e) {
535         PMA_changeClassForColumn($(this), 'hover', e.type == 'mouseenter');
536         });
538     /**
539      * vertical column marking in horizontal mode when clicking the column header
540      */
541     $('.column_heading.marker').live('click', function() {
542         PMA_changeClassForColumn($(this), 'marked');
543         });
545     /**
546      * create resizable table
547      */
548     $("#sqlqueryresults").trigger('makegrid');
552  * Profiling Chart
553  */
554 function makeProfilingChart()
556     if ($('#profilingchart').length == 0) {
557         return;
558     }
560     var data = new Array();
561     $.each(jQuery.parseJSON($('#profilingchart').html()),function(key,value) {
562         data.push([key,parseFloat(value)]);
563     });
565     // Prevent the user from seeing the JSON code
566     $('div#profilingchart').html('').show();
568     PMA_createProfilingChart(data);
572 /**#@- */