db_search.js 8.6 KB

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