| 1 |
jQuery( function ( $ ) { |
| 2 |
|
| 3 |
var uwpSystemStatus = { |
| 4 |
init: function() { |
| 5 |
$( document.body ) |
| 6 |
.on( 'click', 'a.debug-report', this.generateReport ) |
| 7 |
.on( 'click', '#copy-for-support', this.copyReport ) |
| 8 |
.on( 'aftercopy', '#copy-for-support', this.copySuccess ) |
| 9 |
}, |
| 10 |
|
| 11 |
/** |
| 12 |
* Generate system status report. |
| 13 |
* |
| 14 |
* @return {Bool} |
| 15 |
*/ |
| 16 |
generateReport: function() { |
| 17 |
var report = ''; |
| 18 |
|
| 19 |
$( '.uwp-status-table thead, .uwp-status-table tbody' ).each( function() { |
| 20 |
if ( $( this ).is( 'thead' ) ) { |
| 21 |
var label = $( this ).find( 'th:eq(0)' ).data( 'export-label' ) || $( this ).text(); |
| 22 |
report = report + '\n### ' + $.trim( label ) + ' ###\n\n'; |
| 23 |
} else { |
| 24 |
$( 'tr', $( this ) ).each( function() { |
| 25 |
var label = $( this ).find( 'td:eq(0)' ).data( 'export-label' ) || $( this ).find( 'td:eq(0)' ).text(); |
| 26 |
var the_name = $.trim( label ).replace( /(<([^>]+)>)/ig, '' ); // Remove HTML. |
| 27 |
|
| 28 |
// Find value |
| 29 |
var $value_html = $( this ).find( 'td:eq(1)' ).clone(); |
| 30 |
$value_html.find( '.private' ).remove(); |
| 31 |
$value_html.find( '.dashicons-yes' ).replaceWith( '✔' ); |
| 32 |
$value_html.find( '.dashicons-no-alt, .dashicons-warning' ).replaceWith( '❌' ); |
| 33 |
|
| 34 |
// Format value |
| 35 |
var the_value = $.trim( $value_html.text() ); |
| 36 |
var value_array = the_value.split( ', ' ); |
| 37 |
|
| 38 |
if ( value_array.length > 1 ) { |
| 39 |
// If value have a list of plugins ','. |
| 40 |
// Split to add new line. |
| 41 |
var temp_line =''; |
| 42 |
$.each( value_array, function( key, line ) { |
| 43 |
temp_line = temp_line + line + '\n'; |
| 44 |
}); |
| 45 |
|
| 46 |
the_value = temp_line; |
| 47 |
} |
| 48 |
|
| 49 |
report = report + '' + the_name + ': ' + the_value + '\n'; |
| 50 |
}); |
| 51 |
} |
| 52 |
}); |
| 53 |
|
| 54 |
try { |
| 55 |
$( '#debug-report' ).slideDown(); |
| 56 |
$( '#debug-report' ).find( 'textarea' ).val( '`' + report + '`' ).focus().select(); |
| 57 |
$( this ).fadeOut(); |
| 58 |
return false; |
| 59 |
} catch ( e ) { |
| 60 |
console.log( e ); |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
}, |
| 65 |
|
| 66 |
/** |
| 67 |
* Copy for report. |
| 68 |
* |
| 69 |
* @param {Object} evt Copy event. |
| 70 |
*/ |
| 71 |
copyReport: function( evt ) { |
| 72 |
uwpClearClipboard(); |
| 73 |
uwpSetClipboard( $( '#debug-report' ).find( 'textarea' ).val(), $( this ) ); |
| 74 |
evt.preventDefault(); |
| 75 |
}, |
| 76 |
|
| 77 |
/** |
| 78 |
* Display a "Copied!" alert when success copying |
| 79 |
*/ |
| 80 |
copySuccess: function() { |
| 81 |
alert('copied!'); |
| 82 |
}, |
| 83 |
|
| 84 |
/** |
| 85 |
* Displays the copy error message when failure copying. |
| 86 |
*/ |
| 87 |
copyFail: function() { |
| 88 |
$( '.copy-error' ).removeClass( 'hidden' ); |
| 89 |
$( '#debug-report' ).find( 'textarea' ).focus().select(); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
uwpSystemStatus.init(); |
| 94 |
|
| 95 |
function uwpSetClipboard( data, $el ) { |
| 96 |
if ( 'undefined' === typeof $el ) { |
| 97 |
$el = jQuery( document ); |
| 98 |
} |
| 99 |
var $temp_input = jQuery( '<textarea style="opacity:0">' ); |
| 100 |
jQuery( 'body' ).append( $temp_input ); |
| 101 |
$temp_input.val( data ).select(); |
| 102 |
|
| 103 |
$el.trigger( 'beforecopy' ); |
| 104 |
try { |
| 105 |
document.execCommand( 'copy' ); |
| 106 |
$el.trigger( 'aftercopy' ); |
| 107 |
} catch ( err ) { |
| 108 |
$el.trigger( 'aftercopyfailure' ); |
| 109 |
} |
| 110 |
|
| 111 |
$temp_input.remove(); |
| 112 |
} |
| 113 |
|
| 114 |
function uwpClearClipboard() { |
| 115 |
uwpSetClipboard( '' ); |
| 116 |
} |
| 117 |
}); |