1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * JavaScript functions used on Database Search page
6 * @requires js/functions.js
12 * AJAX script for the Database Search page.
14 * Actions ajaxified here:
15 * Retrieve result of SQL query
19 * Unbind all event handlers before tearing down a page
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');
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();
44 * Prepare a div containing a link for toggle the search results
46 $('#togglesearchresultsdiv')
47 /** don't show it until we have results on-screen */
51 * Changing the displayed text according to
52 * the hide/show criteria in search result forms
54 $('#togglesearchresultlink')
55 .html(PMA_messages.strHideSearchResults)
56 .on('click', function () {
58 $('#searchresults').slideToggle();
59 if ($link.text() === PMA_messages.strHideSearchResults) {
60 $link.text(PMA_messages.strShowSearchResults);
62 $link.text(PMA_messages.strHideSearchResults);
64 /** avoid default click action */
69 * Prepare a div containing a link for toggle the search form,
70 * otherwise it's incorrectly displayed after a couple of clicks
72 $('#togglesearchformdiv')
73 .hide(); // don't show it until we have results on-screen
76 * Changing the displayed text according to
77 * the hide/show criteria in search form
81 .on('click', function () {
83 $('#sqlqueryform').slideToggle('medium');
84 if ($link.text() === PMA_messages.strHideQueryBox) {
85 $link.text(PMA_messages.strShowQueryBox);
87 $link.text(PMA_messages.strHideQueryBox);
89 /** avoid default click action */
93 /** don't show it until we have results on-screen */
96 * Changing the displayed text according to
97 * the hide/show criteria in search criteria form
99 $('#togglesearchformlink')
100 .html(PMA_messages.strShowSearchCriteria)
101 .on('click', function () {
103 $('#db_search_form').slideToggle();
104 if ($link.text() === PMA_messages.strHideSearchCriteria) {
105 $link.text(PMA_messages.strShowSearchCriteria);
107 $link.text(PMA_messages.strHideSearchCriteria);
109 /** avoid default click action */
114 * Ajax Event handler for retrieving the results from a table
116 $(document).on('click', 'a.browse_results', function (e) {
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');
130 'ajax_request': true,
131 'is_js_confirmed': true,
132 'sql_query' : browse_sql
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);
141 $('#browse-results').show();
142 PMA_highlightSQL($('#browse-results'));
145 scrollTop: $('#browse-results').offset().top
148 PMA_ajaxShowMessage(data.error, false);
154 * Ajax Event handler for deleting the results from a table
156 $(document).on('click', 'a.delete_results', function (e) {
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')
168 var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false);
169 /** Load the deleted option to the page*/
170 $('#sqlqueryform').html('');
172 'ajax_request': true,
173 'is_js_confirmed': true,
174 'sql_query': $(this).data('delete-sql')
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);
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();
194 scrollTop: $('#browse-results').offset().top
196 PMA_ajaxRemoveMessage($msg);
202 * Ajax Event handler for retrieving the result of an SQL Query
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
211 PMA_prepareForAjaxRequest($form);
213 var url = $form.serialize() + '&submit_search=' + $('#buttonGo').val();
214 $.post($form.attr('action'), url, function (data) {
215 if (typeof data !== 'undefined' && data.success === true) {
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
225 $('#searchresults').show();
229 // workaround for Chrome problem (bug #3168569)
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
239 // error message (zero rows)
240 $('#searchresults').html(data.error).show();
243 PMA_ajaxRemoveMessage($msgbox);