server_status_processes.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * Server Status Processes
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. // object to store process list state information
  8. var processList = {
  9. // denotes whether auto refresh is on or off
  10. autoRefresh: false,
  11. // stores the GET request which refresh process list
  12. refreshRequest: null,
  13. // stores the timeout id returned by setTimeout
  14. refreshTimeout: null,
  15. // the refresh interval in seconds
  16. refreshInterval: null,
  17. // the refresh URL (required to save last used option)
  18. // i.e. full or sorting url
  19. refreshUrl: null,
  20. /**
  21. * Handles killing of a process
  22. *
  23. * @return void
  24. */
  25. init: function () {
  26. processList.setRefreshLabel();
  27. if (processList.refreshUrl === null) {
  28. processList.refreshUrl = 'server_status_processes.php' +
  29. PMA_commonParams.get('common_query');
  30. }
  31. if (processList.refreshInterval === null) {
  32. processList.refreshInterval = $('#id_refreshRate').val();
  33. } else {
  34. $('#id_refreshRate').val(processList.refreshInterval);
  35. }
  36. },
  37. /**
  38. * Handles killing of a process
  39. *
  40. * @param object the event object
  41. *
  42. * @return void
  43. */
  44. killProcessHandler: function (event) {
  45. event.preventDefault();
  46. var argSep = PMA_commonParams.get('arg_separator');
  47. var params = $(this).getPostData();
  48. params += argSep + 'ajax_request=1' + argSep + 'server=' + PMA_commonParams.get('server');
  49. // Get row element of the process to be killed.
  50. var $tr = $(this).closest('tr');
  51. $.post($(this).attr('href'), params, function (data) {
  52. // Check if process was killed or not.
  53. if (data.hasOwnProperty('success') && data.success) {
  54. // remove the row of killed process.
  55. $tr.remove();
  56. // As we just removed a row, reapply odd-even classes
  57. // to keep table stripes consistent
  58. var $tableProcessListTr = $('#tableprocesslist').find('> tbody > tr');
  59. $tableProcessListTr.filter(':even').removeClass('odd').addClass('even');
  60. $tableProcessListTr.filter(':odd').removeClass('even').addClass('odd');
  61. // Show process killed message
  62. PMA_ajaxShowMessage(data.message, false);
  63. } else {
  64. // Show process error message
  65. PMA_ajaxShowMessage(data.error, false);
  66. }
  67. }, 'json');
  68. },
  69. /**
  70. * Handles Auto Refreshing
  71. *
  72. * @param object the event object
  73. *
  74. * @return void
  75. */
  76. refresh: function (event) {
  77. // abort any previous pending requests
  78. // this is necessary, it may go into
  79. // multiple loops causing unnecessary
  80. // requests even after leaving the page.
  81. processList.abortRefresh();
  82. // if auto refresh is enabled
  83. if (processList.autoRefresh) {
  84. var interval = parseInt(processList.refreshInterval, 10) * 1000;
  85. var urlParams = processList.getUrlParams();
  86. processList.refreshRequest = $.post(processList.refreshUrl,
  87. urlParams,
  88. function (data) {
  89. if (data.hasOwnProperty('success') && data.success) {
  90. $newTable = $(data.message);
  91. $('#tableprocesslist').html($newTable.html());
  92. PMA_highlightSQL($('#tableprocesslist'));
  93. }
  94. processList.refreshTimeout = setTimeout(
  95. processList.refresh,
  96. interval
  97. );
  98. });
  99. }
  100. },
  101. /**
  102. * Stop current request and clears timeout
  103. *
  104. * @return void
  105. */
  106. abortRefresh: function () {
  107. if (processList.refreshRequest !== null) {
  108. processList.refreshRequest.abort();
  109. processList.refreshRequest = null;
  110. }
  111. clearTimeout(processList.refreshTimeout);
  112. },
  113. /**
  114. * Set label of refresh button
  115. * change between play & pause
  116. *
  117. * @return void
  118. */
  119. setRefreshLabel: function () {
  120. var img = 'play';
  121. var label = PMA_messages.strStartRefresh;
  122. if (processList.autoRefresh) {
  123. img = 'pause';
  124. label = PMA_messages.strStopRefresh;
  125. processList.refresh();
  126. }
  127. $('a#toggleRefresh').html(PMA_getImage(img) + escapeHtml(label));
  128. },
  129. /**
  130. * Return the Url Parameters
  131. * for autorefresh request,
  132. * includes showExecuting if the filter is checked
  133. *
  134. * @return urlParams - url parameters with autoRefresh request
  135. */
  136. getUrlParams: function () {
  137. var urlParams = { 'ajax_request': true, 'refresh': true };
  138. if ($('#showExecuting').is(':checked')) {
  139. urlParams.showExecuting = true;
  140. return urlParams;
  141. }
  142. return urlParams;
  143. }
  144. };
  145. AJAX.registerOnload('server_status_processes.js', function () {
  146. processList.init();
  147. // Bind event handler for kill_process
  148. $('#tableprocesslist').on(
  149. 'click',
  150. 'a.kill_process',
  151. processList.killProcessHandler
  152. );
  153. // Bind event handler for toggling refresh of process list
  154. $('a#toggleRefresh').on('click', function (event) {
  155. event.preventDefault();
  156. processList.autoRefresh = !processList.autoRefresh;
  157. processList.setRefreshLabel();
  158. });
  159. // Bind event handler for change in refresh rate
  160. $('#id_refreshRate').on('change', function (event) {
  161. processList.refreshInterval = $(this).val();
  162. processList.refresh();
  163. });
  164. // Bind event handler for table header links
  165. $('#tableprocesslist').on('click', 'thead a', function () {
  166. processList.refreshUrl = $(this).attr('href');
  167. });
  168. });
  169. /**
  170. * Unbind all event handlers before tearing down a page
  171. */
  172. AJAX.registerTeardown('server_status_processes.js', function () {
  173. $('#tableprocesslist').off('click', 'a.kill_process');
  174. $('a#toggleRefresh').off('click');
  175. $('#id_refreshRate').off('change');
  176. $('#tableprocesslist').off('click', 'thead a');
  177. // stop refreshing further
  178. processList.abortRefresh();
  179. });