/* global jQuery */ ( function ( $, wp, friends ) { const $document = $( document ); let alreadySearching = ''; let alreadyLoading = false; const bottomOffsetLoadNext = 2000; let searchResultFocused = false; wp = wp || { ajax: { send() {}, post() {} } }; $document.on( 'keydown', 'input#master-search, input.master-search, .form-autocomplete a', function ( e ) { const code = e.keyCode ? e.keyCode : e.which; const results = $( this ) .closest( '.form-autocomplete' ) .find( '.menu .menu-item a' ); if ( 40 === code ) { if ( false === searchResultFocused ) { searchResultFocused = 0; } else if ( searchResultFocused + 1 < results.length ) { searchResultFocused += 1; } } else if ( 38 === code ) { if ( false === searchResultFocused ) { searchResultFocused = -1; } else { searchResultFocused -= 1; if ( -1 === searchResultFocused ) { $( this ).closest( '.form-autocomplete' ).find( 'input.master-search, input#master-search' ).focus(); return false; } } } else { return; } searchResultFocused = ( results.length + searchResultFocused ) % results.length; const el = results.get( searchResultFocused ); if ( el ) { el.focus(); } else { searchResultFocused = results.length; } return false; } ); $document.on( 'keyup', 'input#master-search, input.master-search', function () { const input = $( this ); const query = input.val().trim(); if ( alreadySearching === query ) { return; } const menu = input.closest( '.form-autocomplete' ).find( '.menu' ); if ( ! query ) { menu.hide(); return; } const searchIndicator = input .closest( '.form-autocomplete-input' ) .find( '.form-icon' ); wp.ajax.send( 'friends-autocomplete', { data: { _ajax_nonce: input.data( 'nonce' ), q: query, }, beforeSend() { searchIndicator.addClass( 'loading' ); alreadySearching = query; }, success( results ) { searchIndicator.removeClass( 'loading' ); searchResultFocused = false; if ( results ) { menu.html( results ).show(); } else { menu.hide(); } }, } ); } ); $document.on( 'keydown', function ( e ) { const searchDialog = $( '.search-dialog' ); if ( searchDialog.length ) { if ( (e.metaKey && 75 === e.keyCode ) // cmd-k. || ( e.ctrlKey && 75 === e.keyCode ) // ctrl-p. ) { // move all the childnodes of section.navbar-section search into the .search-dialog: searchDialog.append( $( '.navbar-section.search' ).children() ); // create a new dialog if ( searchDialog.is( ':visible' ) ) { searchDialog.hide(); $( '.navbar-section.search' ).append( searchDialog.children() ); } else { searchDialog.show(); } $( 'input#master-search' ).focus(); return false; } if ( 27 === e.keyCode ) { searchDialog.hide(); $( '.navbar-section.search' ).append( $( '.search-dialog' ).children() ); } } if ( 69 === e.keyCode && ! e.metaKey && ! e.ctrlKey && ! $( e.target ).is( 'input, textarea' ) ) { const links = $( 'header .post-edit-link' ); if ( 1 === links.length ) { links[ 0 ].click(); } } } ); const refresh_feeds_now = function() { let $this = $( this ); function set_status( t ) { if ( ! $this.length ) { $this = $( 'a.friends-refresh' ); } if ( ! $this.find( 'span' ).length ) { $this.html( ' ' ); $this.find( 'i' ).css( 'margin-left', '1em' ); $this.find( 'i' ).css( 'margin-right', '1em' ); } $this.find( 'span' ).text( t ); } set_status( friends.text_refreshing ); $.ajax( { url: friends.rest_base + 'get-feeds', beforeSend: function(xhr){ xhr.setRequestHeader( 'X-WP-Nonce', friends.rest_nonce ); }, } ).done( function( feeds ) { const feed_count = feeds.length; let alreadyLoading = false; function update_page() { if ( alreadyLoading ) { return; } wp.ajax.send( 'friends-load-next-page', { data: { query_vars: friends.query_vars, page: friends.current_page - 1, qv_sign: friends.qv_sign, }, beforeSend() { alreadyLoading = true; }, success( newPosts ) { alreadyLoading = false; if ( newPosts ) { $( 'section.posts' ) .html( newPosts ); } }, } ); } function fetch_next() { if ( ! feeds.length ) { $this.html( friends.text_refreshed + '' ); alreadyLoading = false; update_page(); return; } const feed = feeds.shift(); $.ajax( { url: friends.rest_base + 'refresh-feed', method: 'POST', data: { id: feed.id }, beforeSend: function(xhr){ xhr.setRequestHeader( 'X-WP-Nonce', friends.rest_nonce ); }, } ).always( function( data ) { set_status( ( feed_count - feeds.length ) + ' / ' + feed_count ); if ( data.new_posts ) { update_page(); } setTimeout( fetch_next, 1 ); } ); } fetch_next(); } ); return false; }; $document.on( 'click', 'a.friends-refresh', refresh_feeds_now ); if ( 'true' === friends.refresh_now ) { refresh_feeds_now.apply( $( 'a.friends-refresh' ) ); } $( function () { $( '.activitypub-follower-check' ).each( function () { const $pill = $( this ); wp.ajax.send( 'friends_check_activitypub_follower', { data: { _ajax_nonce: $pill.data( 'nonce' ), actor_url: $pill.data( 'actor-url' ), }, success( result ) { $pill .removeClass( 'is-loading is-error' ) .toggleClass( 'is-following', !! result.follows ) .toggleClass( 'is-not-following', ! result.follows ) .text( result.label ) .attr( 'title', result.title ); }, error( result ) { const message = Array.isArray( result ) ? result.map( function( error ) { return error.message || error.code; } ).join( ', ' ) : friends.text_error; $pill .removeClass( 'is-loading is-following is-not-following' ) .addClass( 'is-error' ) .text( friends.text_error ) .attr( 'title', message ); }, } ); } ); const standard_count = $( '.chip.post-count-standard' ); if ( standard_count.text().substr( 0, 3 ) === '...' || standard_count.text().substr( 0, 1 ) === '…' ) { wp.ajax.send( 'friends-get-post-counts', { data: { _ajax_nonce: standard_count.data( 'nonce' ) }, success( r ) { for ( const i in r ) { if ( ! r[ i ] ) { $( '.chip.post-count-' + i ).remove(); continue; } $( '.chip.post-count-' + i ).text( r[ i ] ); } }, } ); return false; } }); $( function () { $( '#only_subscribe' ).on( 'change', function () { $( this ) .closest( 'form' ) .find( '#submit' ) .text( this.checked ? '1' : '2' ); } ); $( '#post-format-switcher select' ).on( 'change', function () { const re = new RegExp( '/type/[^/$]+' ), v = $( this ).val(); let url; const urlPart = v ? '/type/' + v + '/' : '/'; if ( window.location.href.match( re ) ) { url = window.location.href.replace( re, urlPart ); } else { url = window.location.href.replace( /\/$/, '' ) + urlPart; } window.location.href = url; } ); } ); $document.on( 'click', 'a.friends-trash-post', function () { const button = $( this ); wp.ajax.send( 'trash-post', { data: { _ajax_nonce: button.data( 'trash-nonce' ), id: button.data( 'id' ), }, success() { button .text( friends.text_undo ) .attr( 'class', 'friends-untrash-post' ); }, } ); return false; } ); $document.on( 'click', 'a.friends-untrash-post', function () { const button = $( this ); wp.ajax.send( 'untrash-post', { data: { _ajax_nonce: button.data( 'untrash-nonce' ), id: button.data( 'id' ), }, success() { button .text( friends.text_trash_post ) .attr( 'class', 'friends-trash-post' ); }, } ); return false; } ); let openMenu = null; $document.on( 'click', 'a.friends-dropdown-toggle', function ( e ) { const $this = $( this ); const dropdown = $this.next( '.menu' ); if ( dropdown.is( ':visible' ) ) { dropdown.hide(); openMenu = null; } else { $( '.menu:not(.menu-nav)' ).hide(); dropdown.show(); openMenu = dropdown; } e.stopPropagation(); return false; }); $document.on( 'click', function ( e ) { if ( e.target.closest( '.friends-dropdown' ) ) { return true; } if ( openMenu ) { openMenu.hide(); } } ); function loadComments( commentsLink, callback ) { const $this = $( commentsLink ); let content = $this.closest( 'article' ).find( '.comments-content' ); if ( ! content.length ) { content = $this.closest( '.wp-block-friends-post-comments' ).find( '.comments-content' ); } if ( ! content.length ) { content = $this.closest( 'li' ).find( '.comments-content' ); } if ( content.data( 'loaded' ) ) { content.toggle(); } else { content.show(); content.html( ' ' ); content.find( 'i' ).css( 'margin-left', '1em' ); content.find( 'i' ).css( 'margin-right', '1em' ); content.find( 'span' ).text( friends.text_loading_comments ); let stillLoading = setTimeout( function () { content.find( 'span' ).text( friends.text_still_loading ); }, 2000 ); wp.ajax.send( 'friends-load-comments', { data: { _ajax_nonce: $this.data( 'cnonce' ), post_id: $this.data( 'id' ), }, success( comments ) { content.html( comments ).data( 'loaded', true ); clearTimeout( stillLoading ); callback(); }, error( message ) { content.html( message ).data( 'loaded', true ); clearTimeout( stillLoading ); }, } ); } } $document.on( 'click', 'article a.comments, .wp-block-friends-post-comments a.comments', function ( e ) { if ( e.metaKey || e.altKey || e.shiftKey ) { return; } loadComments.call( this ); return false; } ); $document.on( 'change', 'select.friends-change-post-format', function () { const select = $( this ); wp.ajax.send( 'friends-change-post-format', { data: { _ajax_nonce: select.data( 'change-post-format-nonce' ), id: select.data( 'id' ), format: select.val(), }, success() { // TODO: add some visual indication. }, } ); return false; } ); $( window ).scroll( function () { if ( $( document ).scrollTop() < $( document ).height() - bottomOffsetLoadNext ) { return; } nextPage(); } ); $( document ).on( 'click', '.nav-previous', function () { nextPage(); return false; } ); function nextPage() { if ( alreadyLoading ) { return; } if ( friends.current_page >= friends.max_page ) { return; } wp.ajax.send( 'friends-load-next-page', { data: { query_vars: friends.query_vars, page: friends.current_page, qv_sign: friends.qv_sign, }, beforeSend() { $( 'section.posts .posts-navigation .nav-previous' ).addClass( 'loading' ); alreadyLoading = true; }, success( newPosts ) { $( 'section.posts .posts-navigation .nav-previous' ).removeClass( 'loading' ); if ( newPosts ) { $( 'section.posts > article' ).last().after( newPosts ); if ( ++friends.current_page < friends.max_page && /