system-status.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* global jQuery, woocommerce_admin_system_status, wcSetClipboard, wcClearClipboard */
  2. jQuery( function ( $ ) {
  3. /**
  4. * Users country and state fields
  5. */
  6. var wcSystemStatus = {
  7. init: function() {
  8. $( document.body )
  9. .on( 'click', 'a.help_tip, a.woocommerce-help-tip', this.preventTipTipClick )
  10. .on( 'click', 'a.debug-report', this.generateReport )
  11. .on( 'click', '#copy-for-support', this.copyReport )
  12. .on( 'aftercopy', '#copy-for-support', this.copySuccess )
  13. .on( 'aftercopyfailure', '#copy-for-support', this.copyFail );
  14. },
  15. /**
  16. * Prevent anchor behavior when click on TipTip.
  17. *
  18. * @return {Bool}
  19. */
  20. preventTipTipClick: function() {
  21. return false;
  22. },
  23. /**
  24. * Generate system status report.
  25. *
  26. * @return {Bool}
  27. */
  28. generateReport: function() {
  29. var report = '';
  30. $( '.wc_status_table thead, .wc_status_table tbody' ).each( function() {
  31. if ( $( this ).is( 'thead' ) ) {
  32. var label = $( this ).find( 'th:eq(0)' ).data( 'export-label' ) || $( this ).text();
  33. report = report + '\n### ' + $.trim( label ) + ' ###\n\n';
  34. } else {
  35. $( 'tr', $( this ) ).each( function() {
  36. var label = $( this ).find( 'td:eq(0)' ).data( 'export-label' ) || $( this ).find( 'td:eq(0)' ).text();
  37. var the_name = $.trim( label ).replace( /(<([^>]+)>)/ig, '' ); // Remove HTML.
  38. // Find value
  39. var $value_html = $( this ).find( 'td:eq(2)' ).clone();
  40. $value_html.find( '.private' ).remove();
  41. $value_html.find( '.dashicons-yes' ).replaceWith( '&#10004;' );
  42. $value_html.find( '.dashicons-no-alt, .dashicons-warning' ).replaceWith( '&#10060;' );
  43. // Format value
  44. var the_value = $.trim( $value_html.text() );
  45. var value_array = the_value.split( ', ' );
  46. if ( value_array.length > 1 ) {
  47. // If value have a list of plugins ','.
  48. // Split to add new line.
  49. var temp_line ='';
  50. $.each( value_array, function( key, line ) {
  51. temp_line = temp_line + line + '\n';
  52. });
  53. the_value = temp_line;
  54. }
  55. report = report + '' + the_name + ': ' + the_value + '\n';
  56. });
  57. }
  58. });
  59. try {
  60. $( '#debug-report' ).slideDown();
  61. $( '#debug-report' ).find( 'textarea' ).val( '`' + report + '`' ).focus().select();
  62. $( this ).fadeOut();
  63. return false;
  64. } catch ( e ) {
  65. /* jshint devel: true */
  66. console.log( e );
  67. }
  68. return false;
  69. },
  70. /**
  71. * Copy for report.
  72. *
  73. * @param {Object} evt Copy event.
  74. */
  75. copyReport: function( evt ) {
  76. wcClearClipboard();
  77. wcSetClipboard( $( '#debug-report' ).find( 'textarea' ).val(), $( this ) );
  78. evt.preventDefault();
  79. },
  80. /**
  81. * Display a "Copied!" tip when success copying
  82. */
  83. copySuccess: function() {
  84. $( '#copy-for-support' ).tipTip({
  85. 'attribute': 'data-tip',
  86. 'activation': 'focus',
  87. 'fadeIn': 50,
  88. 'fadeOut': 50,
  89. 'delay': 0
  90. }).focus();
  91. },
  92. /**
  93. * Displays the copy error message when failure copying.
  94. */
  95. copyFail: function() {
  96. $( '.copy-error' ).removeClass( 'hidden' );
  97. $( '#debug-report' ).find( 'textarea' ).focus().select();
  98. }
  99. };
  100. wcSystemStatus.init();
  101. $( '#log-viewer-select' ).on( 'click', 'h2 a.page-title-action', function( evt ) {
  102. evt.stopImmediatePropagation();
  103. return window.confirm( woocommerce_admin_system_status.delete_log_confirmation );
  104. });
  105. });