| 1 |
/* globals jQuery, wp, wp_stream_live_updates, wp_stream_regenerate_alt_rows */ |
| 2 |
jQuery( |
| 3 |
function( $ ) { |
| 4 |
$( document ).ready( |
| 5 |
function() { |
| 6 |
// Only run on wp_stream when page is 1 and the order is desc |
| 7 |
if ( 'toplevel_page_wp_stream' !== wp_stream_live_updates.current_screen || '1' !== wp_stream_live_updates.current_page || 'asc' === wp_stream_live_updates.current_order ) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
// Do not run if there are filters in use |
| 12 |
if ( parseInt( wp_stream_live_updates.current_query_count, 10 ) > 1 ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
var list_sel = '.toplevel_page_wp_stream #the-list'; |
| 17 |
|
| 18 |
// Set initial beat to fast. WP is designed to slow this to 15 seconds after 2.5 minutes. |
| 19 |
wp.heartbeat.interval( 'fast' ); |
| 20 |
|
| 21 |
$( document ).on( |
| 22 |
'heartbeat-send.stream', function( e, data ) { |
| 23 |
data['wp-stream-heartbeat'] = 'live-update'; |
| 24 |
|
| 25 |
var last_item = $( list_sel + ' tr:first .column-date time' ), |
| 26 |
last_time = 1; |
| 27 |
|
| 28 |
if ( 0 !== last_item.length ) { |
| 29 |
last_time = ( '' === last_item.attr( 'datetime' ) ) ? 1 : last_item.attr( 'datetime' ); |
| 30 |
} |
| 31 |
|
| 32 |
data['wp-stream-heartbeat-last-time'] = last_time; |
| 33 |
data['wp-stream-heartbeat-query'] = wp_stream_live_updates.current_query; |
| 34 |
} |
| 35 |
); |
| 36 |
|
| 37 |
// Listen for "heartbeat-tick" on $(document). |
| 38 |
$( document ).on( |
| 39 |
'heartbeat-tick.stream', function( e, data ) { |
| 40 |
// If this no rows return then we kill the script |
| 41 |
if ( ! data['wp-stream-heartbeat'] || 0 === data['wp-stream-heartbeat'].length ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
var show_on_screen = $( '#edit_stream_per_page' ).val(), |
| 46 |
$current_items = $( list_sel + ' tr' ), |
| 47 |
$new_items = $( data['wp-stream-heartbeat'] ); |
| 48 |
|
| 49 |
// Remove all default classes and add class to highlight new rows |
| 50 |
$new_items.addClass( 'new-row' ); |
| 51 |
|
| 52 |
// Check if first tr has the alternate class |
| 53 |
var has_class = ( $current_items.first().hasClass( 'alternate' ) ); |
| 54 |
|
| 55 |
// Apply the good class to the list |
| 56 |
if ( 1 === $new_items.length && ! has_class ) { |
| 57 |
$new_items.addClass( 'alternate' ); |
| 58 |
} else { |
| 59 |
var even_or_odd = ( 0 === $new_items.length % 2 && ! has_class ) ? 'even' : 'odd'; |
| 60 |
// Add class to nth child because there is more than one element |
| 61 |
$new_items.filter( ':nth-child(' + even_or_odd + ')' ).addClass( 'alternate' ); |
| 62 |
} |
| 63 |
|
| 64 |
// Add element to the dom |
| 65 |
$( list_sel ).prepend( $new_items ); |
| 66 |
|
| 67 |
$( '.metabox-prefs input' ).each( |
| 68 |
function() { |
| 69 |
if ( true !== $( this ).prop( 'checked' ) ) { |
| 70 |
var label = $( this ).val(); |
| 71 |
$( 'td.column-' + label ).hide(); |
| 72 |
} |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
// Remove the number of element added to the end of the list table |
| 77 |
var slice_rows = show_on_screen - ( $new_items.length + $current_items.length ); |
| 78 |
|
| 79 |
if ( slice_rows < 0 ) { |
| 80 |
$( list_sel + ' tr' ).slice( slice_rows ).remove(); |
| 81 |
} |
| 82 |
|
| 83 |
// Remove the no items row |
| 84 |
$( list_sel + ' tr.no-items' ).remove(); |
| 85 |
|
| 86 |
// Update pagination |
| 87 |
var total_items_i18n = data.total_items_i18n || ''; |
| 88 |
|
| 89 |
if ( total_items_i18n ) { |
| 90 |
$( '.displaying-num' ).text( total_items_i18n ); |
| 91 |
$( '.total-pages' ).text( data.total_pages_i18n ); |
| 92 |
$( '.tablenav-pages' ).find( '.next-page, .last-page' ).toggleClass( 'disabled', data.total_pages === $( '.current-page' ).val() ); |
| 93 |
$( '.tablenav-pages .last-page' ).attr( 'href', data.last_page_link ); |
| 94 |
} |
| 95 |
|
| 96 |
// Allow others to hook in, ie: timeago |
| 97 |
$( list_sel ).parent().trigger( 'updated' ); |
| 98 |
|
| 99 |
// Regenerate alternating row classes |
| 100 |
wp_stream_regenerate_alt_rows( $( list_sel + ' tr' ) ); |
| 101 |
|
| 102 |
// Remove background after a certain amount of time |
| 103 |
setTimeout( |
| 104 |
function() { |
| 105 |
$( '.new-row' ).addClass( 'fadeout' ); |
| 106 |
setTimeout( |
| 107 |
function() { |
| 108 |
$( list_sel + ' tr' ).removeClass( 'new-row fadeout' ); |
| 109 |
}, 500 |
| 110 |
); |
| 111 |
}, 3000 |
| 112 |
); |
| 113 |
} |
| 114 |
); |
| 115 |
} |
| 116 |
); |
| 117 |
} |
| 118 |
); |
| 119 |
|