/** * PRAutoBlogger — Kanban Board Poller * * Live-polls the board status AJAX endpoint and updates the four columns * (Generating | In Review | Published | Failed) in-place without a full * page reload. * * Poll behaviour: * - Polls at `prabBoard.pollInterval` ms when any article is generating. * - Doubles the interval (backoff) when `has_active_runs` is false, capping * at 4× the base interval — prevents needless AJAX on an idle board. * - Resets to base interval as soon as an active run is detected again. * * Click-throughs (M1): * - Generating → Activity Log * - In Review → Review Queue * - Published → Post edit screen * - Failed → Activity Log * (M2 rewires all to Article Dossier) * * Localised config: `prabBoard` object (ajaxUrl, nonce, pollInterval, action, * strings) injected by PRAutoBlogger_Board_Page::on_enqueue_assets(). * * @see includes/admin/class-board-page.php — AJAX handler + localise. * @see assets/css/board.css — Card and column styles. * @see templates/admin/board-page.php — Initial server-rendered HTML. */ ( function ( $ ) { 'use strict'; var cfg = window.prabBoard || {}; var baseInterval = cfg.pollInterval || 5000; var maxBackoff = 4; var backoffFactor = 1; var pollTimer = null; var $board = null; var $errorBanner = null; /** Escape HTML for safe DOM insertion. */ function escHtml( str ) { return $( '
' + escHtml( cfg.strings.empty ) + '
' ); return; } // Build new card HTML. var newHtml = ''; for ( var i = 0; i < cards.length; i++ ) { newHtml += buildCardHtml( cards[ i ], colKey ); } $cards.html( newHtml ); } /** * Apply a server snapshot to all four columns. * * @param {Object} snapshot Board snapshot from AJAX. * @returns {void} */ function applySnapshot( snapshot ) { renderColumn( 'generating', snapshot.generating || [] ); renderColumn( 'in_review', snapshot.in_review || [] ); renderColumn( 'published', snapshot.published || [] ); renderColumn( 'failed', snapshot.failed || [] ); // Re-enable board Generate button once no active runs remain. if ( ! snapshot.has_active_runs ) { $( '#prab-board-generate-now' ).prop( 'disabled', false ) .text( ( cfg.strings && cfg.strings.newArticle ) || 'New Article' ); } } /** * Compute the next poll delay based on whether there are active runs. * * @param {boolean} hasActiveRuns * @returns {number} Milliseconds until next poll. */ function nextDelay( hasActiveRuns ) { if ( hasActiveRuns ) { backoffFactor = 1; return baseInterval; } backoffFactor = Math.min( backoffFactor + 1, maxBackoff ); return baseInterval * backoffFactor; } /** * Schedule the next poll after a given delay. * * @param {number} delay Milliseconds. * @returns {void} */ function schedulePoll( delay ) { clearTimeout( pollTimer ); pollTimer = setTimeout( doPoll, delay ); } /** * Execute one board poll: AJAX POST → apply snapshot → reschedule. * * @returns {void} */ function doPoll() { $.ajax( { url: cfg.ajaxUrl, method: 'POST', data: { action: cfg.action, nonce: cfg.nonce }, success: function ( response ) { if ( $errorBanner ) { $errorBanner.removeClass( 'is-visible' ); } if ( response && response.success && response.data ) { applySnapshot( response.data ); schedulePoll( nextDelay( !! response.data.has_active_runs ) ); } else { schedulePoll( nextDelay( false ) ); } }, error: function () { if ( $errorBanner ) { $errorBanner.addClass( 'is-visible' ); } schedulePoll( nextDelay( false ) ); } } ); } } ); }( jQuery ) );