search.js 7.3 KB

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