/** * Google Reader theme JS for the Friends plugin. * * Keyboard shortcuts: * j/k - Select next/previous item (expands it) * n/p - Select next/previous item without opening * space - Page down, or next item if at bottom of current * shift-space - Page up * o/enter - Toggle open/close current item * s - Star (navigate to author page) * v - Open original in new tab * shift-a - Mark all as read (trash all visible) * u - Toggle sidebar * ? - Show keyboard shortcuts help */ ( function( $ ) { var currentIndex = -1; function getItems() { return $( 'section.posts article.card' ); } function scrollToItem( $item ) { if ( ! $item.length ) { return; } $( 'html, body' ).animate( { scrollTop: $item.offset().top - 80 }, 150 ); } function expandItem( $item ) { if ( ! $item.hasClass( 'uncollapsed' ) ) { $item.addClass( 'uncollapsed' ); } } function collapseItem( $item ) { $item.removeClass( 'uncollapsed' ); } function collapseAll() { getItems().removeClass( 'uncollapsed' ); } function selectItem( index, expand ) { var items = getItems(); if ( index < 0 || index >= items.length ) { return; } if ( expand ) { collapseAll(); } currentIndex = index; var $item = items.eq( index ); items.removeClass( 'gr-current' ); $item.addClass( 'gr-current' ); scrollToItem( $item ); if ( expand ) { expandItem( $item ); } } function isItemBottomVisible( $item ) { var itemBottom = $item.offset().top + $item.outerHeight(); var viewBottom = $( window ).scrollTop() + $( window ).height(); return itemBottom <= viewBottom; } // Keyboard shortcuts. $( document ).on( 'keydown', function( e ) { if ( $( e.target ).is( 'input, textarea, select, [contenteditable]' ) ) { return; } var items = getItems(); if ( ! items.length && e.key !== '?' && e.key !== 'u' ) { return; } switch ( e.key ) { case 'j': // Next item, expand. e.preventDefault(); selectItem( currentIndex + 1, true ); break; case 'k': // Previous item, expand. e.preventDefault(); if ( currentIndex > 0 ) { selectItem( currentIndex - 1, true ); } break; case 'n': // Next item, don't expand. e.preventDefault(); selectItem( currentIndex + 1, false ); break; case 'p': // Previous item, don't expand. e.preventDefault(); if ( currentIndex > 0 ) { selectItem( currentIndex - 1, false ); } break; case ' ': // Space: page down or next item. e.preventDefault(); if ( e.shiftKey ) { // Page up. window.scrollBy( 0, -$( window ).height() * 0.8 ); } else if ( currentIndex >= 0 && items.eq( currentIndex ).hasClass( 'uncollapsed' ) && ! isItemBottomVisible( items.eq( currentIndex ) ) ) { // Page down within current expanded item. window.scrollBy( 0, $( window ).height() * 0.8 ); } else { // Move to next item. selectItem( currentIndex + 1, true ); } break; case 'o': // Toggle current item. case 'Enter': if ( currentIndex >= 0 ) { e.preventDefault(); var $item = items.eq( currentIndex ); if ( $item.hasClass( 'uncollapsed' ) ) { collapseItem( $item ); } else { collapseAll(); expandItem( $item ); } } break; case 's': // Star: toggle ⭐ reaction on current post. if ( currentIndex >= 0 ) { e.preventDefault(); var $star = items.eq( currentIndex ).find( 'button.friends-reaction[data-emoji="2b50"], button.friends-reaction-picker[data-emoji="2b50"]' ).first(); if ( $star.length ) { $star.trigger( 'click' ); } } break; case 'v': // Open original in new tab. if ( currentIndex >= 0 ) { e.preventDefault(); var $link = items.eq( currentIndex ).find( '.card-title a, h4 a' ).first(); if ( $link.length ) { window.open( $link.attr( 'href' ), '_blank' ); } } break; case 'u': // Toggle sidebar. e.preventDefault(); $( '.off-canvas-sidebar' ).toggle(); $( '.off-canvas-content' ).toggleClass( 'gr-sidebar-hidden' ); break; case '?': // Show shortcuts help. e.preventDefault(); var $help = $( '#gr-shortcuts-help' ); if ( $help.length ) { $help.toggle(); } else { $( 'body' ).append( '
| j / k | Next / previous item |
| n / p | Next / previous without opening |
| space | Page down or next item |
| shift-space | Page up |
| o / enter | Open / close item |
| s | Toggle ⭐ reaction |
| v | Open original in new tab |
| u | Toggle sidebar |
| ? | Show this help |
Press any key to close
' + '