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').unbind('click');
23 $('a.delete_results').unbind('click');
24 $('#buttonGo').unbind('click');
25 $('#togglesearchresultlink').unbind('click');
26 $("#togglequerybox").unbind('click');
27 $('#togglesearchformlink').unbind('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.png', '', {'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 .bind('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 .bind('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 .bind('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,
133 'token' : PMA_commonParams.get('token')
135 $.post(url, params, function (data) {
136 if (typeof data !== 'undefined' && data.success) {
137 $('#browse-results').html(data.message);
138 PMA_ajaxRemoveMessage($msg);
139 $('.table_results').each(function () {
140 PMA_makegrid(this, true, true, true, true);
142 $('#browse-results').show();
143 PMA_highlightSQL($('#browse-results'));
146 scrollTop: $("#browse-results").offset().top
149 PMA_ajaxShowMessage(data.error, false);
155 * Ajax Event handler for deleting the results from a table
157 $(document).on('click', 'a.delete_results', function(e){
159 /** Hides the results shown by the browse criteria */
160 $("#table-info").hide();
161 $('#sqlqueryform').hide();
162 $('#togglequerybox').hide();
163 /** Conformation message for deletion */
164 var msg = PMA_sprintf(
165 PMA_messages.strConfirmDeleteResults,
166 $(this).data('table-name')
169 var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false);
170 /** Load the deleted option to the page*/
171 $('#sqlqueryform').html('');
173 'ajax_request': true,
174 'is_js_confirmed': true,
175 'sql_query': $(this).data('delete-sql'),
176 'token' : PMA_commonParams.get('token')
178 var url = $(this).attr('href');
180 $.post(url, params, function (data) {
181 if (typeof data === 'undefined' || !data.success) {
182 PMA_ajaxShowMessage(data.error, false);
186 $('#sqlqueryform').html(data.sql_query);
187 /** Refresh the search results after the deletion */
188 document.getElementById('buttonGo').click();
189 $('#togglequerybox').html(PMA_messages.strHideQueryBox);
190 /** Show the results of the deletion option */
191 $('#browse-results').hide();
192 $('#sqlqueryform').show();
193 $('#togglequerybox').show();
196 scrollTop: $("#browse-results").offset().top
198 PMA_ajaxRemoveMessage($msg);
204 * Ajax Event handler for retrieving the result of an SQL Query
206 $(document).on('submit', "#db_search_form.ajax", function (event) {
207 event.preventDefault();
209 var $msgbox = PMA_ajaxShowMessage(PMA_messages.strSearching, false);
210 // jQuery object to reuse
213 PMA_prepareForAjaxRequest($form);
215 var url = $form.serialize() + "&submit_search=" + $("#buttonGo").val();
216 $.post($form.attr('action'), url, function (data) {
217 if (typeof data !== 'undefined' && data.success === true) {
219 $("#searchresults").html(data.message);
221 $('#togglesearchresultlink')
222 // always start with the Show message
223 .text(PMA_messages.strHideSearchResults);
224 $('#togglesearchresultsdiv')
225 // now it's time to show the div containing the link
227 $('#searchresults').show();
231 // workaround for Chrome problem (bug #3168569)
234 $('#togglesearchformlink')
235 // always start with the Show message
236 .text(PMA_messages.strShowSearchCriteria);
237 $('#togglesearchformdiv')
238 // now it's time to show the div containing the link
241 // error message (zero rows)
242 $("#searchresults").html(data.error).show();
245 PMA_ajaxRemoveMessage($msgbox);