server_status_processes.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 url = $(this).attr('href');
  47. // Get row element of the process to be killed.
  48. var $tr = $(this).closest('tr');
  49. $.getJSON(url, function (data) {
  50. // Check if process was killed or not.
  51. if (data.hasOwnProperty('success') && data.success) {
  52. // remove the row of killed process.
  53. $tr.remove();
  54. // As we just removed a row, reapply odd-even classes
  55. // to keep table stripes consistent
  56. var $tableProcessListTr = $('#tableprocesslist').find('> tbody > tr');
  57. $tableProcessListTr.filter(':even').removeClass('odd').addClass('even');
  58. $tableProcessListTr.filter(':odd').removeClass('even').addClass('odd');
  59. // Show process killed message
  60. PMA_ajaxShowMessage(data.message, false);
  61. } else {
  62. // Show process error message
  63. PMA_ajaxShowMessage(data.error, false);
  64. }
  65. });
  66. },
  67. /**
  68. * Handles Auto Refreshing
  69. *
  70. * @param object the event object
  71. *
  72. * @return void
  73. */
  74. refresh: function (event) {
  75. // abort any previous pending requests
  76. // this is necessary, it may go into
  77. // multiple loops causing unnecessary
  78. // requests even after leaving the page.
  79. processList.abortRefresh();
  80. // if auto refresh is enabled
  81. if (processList.autoRefresh) {
  82. var interval = parseInt(processList.refreshInterval, 10) * 1000;
  83. var urlParams = processList.getUrlParams();
  84. processList.refreshRequest = $.get(processList.refreshUrl,
  85. urlParams,
  86. function (data) {
  87. if (data.hasOwnProperty('success') && data.success) {
  88. $newTable = $(data.message);
  89. $('#tableprocesslist').html($newTable.html());
  90. PMA_highlightSQL($('#tableprocesslist'));
  91. }
  92. processList.refreshTimeout = setTimeout(
  93. processList.refresh,
  94. interval
  95. );
  96. });
  97. }
  98. },
  99. /**
  100. * Stop current request and clears timeout
  101. *
  102. * @return void
  103. */
  104. abortRefresh: function () {
  105. if (processList.refreshRequest !== null) {
  106. processList.refreshRequest.abort();
  107. processList.refreshRequest = null;
  108. }
  109. clearTimeout(processList.refreshTimeout);
  110. },
  111. /**
  112. * Set label of refresh button
  113. * change between play & pause
  114. *
  115. * @return void
  116. */
  117. setRefreshLabel: function () {
  118. var img = 'play';
  119. var label = PMA_messages.strStartRefresh;
  120. if (processList.autoRefresh) {
  121. img = 'pause';
  122. label = PMA_messages.strStopRefresh;
  123. processList.refresh();
  124. }
  125. $('a#toggleRefresh').html(PMA_getImage(img) + escapeHtml(label));
  126. },
  127. /**
  128. * Return the Url Parameters
  129. * for autorefresh request,
  130. * includes showExecuting if the filter is checked
  131. *
  132. * @return urlParams - url parameters with autoRefresh request
  133. */
  134. getUrlParams: function () {
  135. var urlParams = { 'ajax_request': true, 'refresh': true };
  136. if ($('#showExecuting').is(':checked')) {
  137. urlParams.showExecuting = true;
  138. return urlParams;
  139. }
  140. return urlParams;
  141. }
  142. };
  143. AJAX.registerOnload('server_status_processes.js', function () {
  144. processList.init();
  145. // Bind event handler for kill_process
  146. $('#tableprocesslist').on(
  147. 'click',
  148. 'a.kill_process',
  149. processList.killProcessHandler
  150. );
  151. // Bind event handler for toggling refresh of process list
  152. $('a#toggleRefresh').on('click', function (event) {
  153. event.preventDefault();
  154. processList.autoRefresh = !processList.autoRefresh;
  155. processList.setRefreshLabel();
  156. });
  157. // Bind event handler for change in refresh rate
  158. $('#id_refreshRate').on('change', function (event) {
  159. processList.refreshInterval = $(this).val();
  160. processList.refresh();
  161. });
  162. // Bind event handler for table header links
  163. $('#tableprocesslist').on('click', 'thead a', function () {
  164. processList.refreshUrl = $(this).attr('href');
  165. });
  166. });
  167. /**
  168. * Unbind all event handlers before tearing down a page
  169. */
  170. AJAX.registerTeardown('server_status_processes.js', function () {
  171. $('#tableprocesslist').off('click', 'a.kill_process');
  172. $('a#toggleRefresh').off('click');
  173. $('#id_refreshRate').off('change');
  174. $('#tableprocesslist').off('click', 'thead a');
  175. // stop refreshing further
  176. processList.abortRefresh();
  177. });