Merge branch 'origin/QA_4_8' into Weblate.
[phpmyadmin.git] / js / db_search.js
blobc1c5dd6b2b5a8c503f3ac7dbbf273be9410dec62
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * JavaScript functions used on Database Search page
4  *
5  * @requires    jQuery
6  * @requires    js/functions.js
7  *
8  * @package PhpMyAdmin
9  */
11 /**
12  * AJAX script for the Database Search page.
13  *
14  * Actions ajaxified here:
15  * Retrieve result of SQL query
16  */
18 /**
19  * Unbind all event handlers before tearing down a page
20  */
21 AJAX.registerTeardown('db_search.js', function () {
22     $('a.browse_results').off('click');
23     $('a.delete_results').off('click');
24     $('#buttonGo').off('click');
25     $('#togglesearchresultlink').off('click');
26     $('#togglequerybox').off('click');
27     $('#togglesearchformlink').off('click');
28     $(document).off('submit', '#db_search_form.ajax');
29 });
31 AJAX.registerOnload('db_search.js', function () {
32     /** Hide the table link in the initial search result */
33     var icon = PMA_getImage('s_tbl', '', { 'id': 'table-image' }).toString();
34     $('#table-info').prepend(icon).hide();
36     /** Hide the browse and deleted results in the new search criteria */
37     $('#buttonGo').click(function () {
38         $('#table-info').hide();
39         $('#browse-results').hide();
40         $('#sqlqueryform').hide();
41         $('#togglequerybox').hide();
42     });
43     /**
44      * Prepare a div containing a link for toggle the search results
45      */
46     $('#togglesearchresultsdiv')
47     /** don't show it until we have results on-screen */
48         .hide();
50     /**
51      * Changing the displayed text according to
52      * the hide/show criteria in search result forms
53      */
54     $('#togglesearchresultlink')
55         .html(PMA_messages.strHideSearchResults)
56         .on('click', function () {
57             var $link = $(this);
58             $('#searchresults').slideToggle();
59             if ($link.text() === PMA_messages.strHideSearchResults) {
60                 $link.text(PMA_messages.strShowSearchResults);
61             } else {
62                 $link.text(PMA_messages.strHideSearchResults);
63             }
64             /** avoid default click action */
65             return false;
66         });
68     /**
69      * Prepare a div containing a link for toggle the search form,
70      * otherwise it's incorrectly displayed after a couple of clicks
71      */
72     $('#togglesearchformdiv')
73         .hide(); // don't show it until we have results on-screen
75     /**
76      * Changing the displayed text according to
77      * the hide/show criteria in search form
78      */
79     $('#togglequerybox')
80         .hide()
81         .on('click', function () {
82             var $link = $(this);
83             $('#sqlqueryform').slideToggle('medium');
84             if ($link.text() === PMA_messages.strHideQueryBox) {
85                 $link.text(PMA_messages.strShowQueryBox);
86             } else {
87                 $link.text(PMA_messages.strHideQueryBox);
88             }
89             /** avoid default click action */
90             return false;
91         });
93     /** don't show it until we have results on-screen */
95     /**
96      * Changing the displayed text according to
97      * the hide/show criteria in search criteria form
98      */
99     $('#togglesearchformlink')
100         .html(PMA_messages.strShowSearchCriteria)
101         .on('click', function () {
102             var $link = $(this);
103             $('#db_search_form').slideToggle();
104             if ($link.text() === PMA_messages.strHideSearchCriteria) {
105                 $link.text(PMA_messages.strShowSearchCriteria);
106             } else {
107                 $link.text(PMA_messages.strHideSearchCriteria);
108             }
109             /** avoid default click action */
110             return false;
111         });
113     /*
114      * Ajax Event handler for retrieving the results from a table
115      */
116     $(document).on('click', 'a.browse_results', function (e) {
117         e.preventDefault();
118         /**   Hides the results shown by the delete criteria */
119         var $msg = PMA_ajaxShowMessage(PMA_messages.strBrowsing, false);
120         $('#sqlqueryform').hide();
121         $('#togglequerybox').hide();
122         /**  Load the browse results to the page */
123         $('#table-info').show();
124         var table_name = $(this).data('table-name');
125         $('#table-link').attr({ 'href' : $(this).attr('href') }).text(table_name);
127         var url = $(this).attr('href') + '#searchresults';
128         var browse_sql = $(this).data('browse-sql');
129         var params = {
130             'ajax_request': true,
131             'is_js_confirmed': true,
132             'sql_query' : browse_sql
133         };
134         $.post(url, params, function (data) {
135             if (typeof data !== 'undefined' && data.success) {
136                 $('#browse-results').html(data.message);
137                 PMA_ajaxRemoveMessage($msg);
138                 $('.table_results').each(function () {
139                     PMA_makegrid(this, true, true, true, true);
140                 });
141                 $('#browse-results').show();
142                 PMA_highlightSQL($('#browse-results'));
143                 $('html, body')
144                     .animate({
145                         scrollTop: $('#browse-results').offset().top
146                     }, 1000);
147             } else {
148                 PMA_ajaxShowMessage(data.error, false);
149             }
150         });
151     });
153     /*
154      * Ajax Event handler for deleting the results from a table
155      */
156     $(document).on('click', 'a.delete_results', function (e) {
157         e.preventDefault();
158         /**  Hides the results shown by the browse criteria */
159         $('#table-info').hide();
160         $('#sqlqueryform').hide();
161         $('#togglequerybox').hide();
162         /** Conformation message for deletion */
163         var msg = PMA_sprintf(
164             PMA_messages.strConfirmDeleteResults,
165             $(this).data('table-name')
166         );
167         if (confirm(msg)) {
168             var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false);
169             /** Load the deleted option to the page*/
170             $('#sqlqueryform').html('');
171             var params = {
172                 'ajax_request': true,
173                 'is_js_confirmed': true,
174                 'sql_query': $(this).data('delete-sql')
175             };
176             var url = $(this).attr('href');
178             $.post(url, params, function (data) {
179                 if (typeof data === 'undefined' || !data.success) {
180                     PMA_ajaxShowMessage(data.error, false);
181                     return;
182                 }
184                 $('#sqlqueryform').html(data.sql_query);
185                 /** Refresh the search results after the deletion */
186                 document.getElementById('buttonGo').click();
187                 $('#togglequerybox').html(PMA_messages.strHideQueryBox);
188                 /** Show the results of the deletion option */
189                 $('#browse-results').hide();
190                 $('#sqlqueryform').show();
191                 $('#togglequerybox').show();
192                 $('html, body')
193                     .animate({
194                         scrollTop: $('#browse-results').offset().top
195                     }, 1000);
196                 PMA_ajaxRemoveMessage($msg);
197             });
198         }
199     });
201     /**
202      * Ajax Event handler for retrieving the result of an SQL Query
203      */
204     $(document).on('submit', '#db_search_form.ajax', function (event) {
205         event.preventDefault();
207         var $msgbox = PMA_ajaxShowMessage(PMA_messages.strSearching, false);
208         // jQuery object to reuse
209         var $form = $(this);
211         PMA_prepareForAjaxRequest($form);
213         var url = $form.serialize() + PMA_commonParams.get('arg_separator') + 'submit_search=' + $('#buttonGo').val();
214         $.post($form.attr('action'), url, function (data) {
215             if (typeof data !== 'undefined' && data.success === true) {
216                 // found results
217                 $('#searchresults').html(data.message);
219                 $('#togglesearchresultlink')
220                 // always start with the Show message
221                     .text(PMA_messages.strHideSearchResults);
222                 $('#togglesearchresultsdiv')
223                 // now it's time to show the div containing the link
224                     .show();
225                 $('#searchresults').show();
228                 $('#db_search_form')
229                     // workaround for Chrome problem (bug #3168569)
230                     .slideToggle()
231                     .hide();
232                 $('#togglesearchformlink')
233                     // always start with the Show message
234                     .text(PMA_messages.strShowSearchCriteria);
235                 $('#togglesearchformdiv')
236                     // now it's time to show the div containing the link
237                     .show();
238             } else {
239                 // error message (zero rows)
240                 $('#searchresults').html(data.error).show();
241             }
243             PMA_ajaxRemoveMessage($msgbox);
244         });
245     });
246 }); // end $()