Files
wordpress-theme-aksrt/assets/js/rehydrate.js
T
kate522 1f4a39d8f8 feat(runtime): 全站前端脚本统一接入 AksrtFrontend 全局 DOM 生命周期
- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发
- aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件
- 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets)
  从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记
- section-navs 依赖改为 aksrt-runtime,保证注册时序
- 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本
- loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身
- build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤
- 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/
  public-bundle-parity),3 个既有 harness 适配注册表初始化方式
2026-09-02 21:48:56 +08:00

110 lines
3.2 KiB
JavaScript

/**
* AKSRT Rehydrate System
*
* Centralized component initialization and re-initialization system.
* Components register their init functions, and rehydrate() is called
* to initialize or re-initialize them on a given root element.
*
* Usage:
* AksrtFrontend.register('name', initFn); // Register a component
* AksrtFrontend.boot(document); // One-time initial page load
* AksrtFrontend.rehydrate(root, 'ajax'); // Initialize dynamic content
*
* @package AKSRT
* @since 5.0.0
*/
(function () {
'use strict';
var _registry = [];
var _booted = false;
var _bootRoot = null;
function invoke(entry, root, reason) {
try {
entry.initFn(root, reason);
} catch (err) {
console.error('[AKSRT Rehydrate] Error initializing "' + entry.name + '":', err);
}
}
function dispatch(root, reason, eventName) {
try {
root.dispatchEvent(new CustomEvent(eventName, {
bubbles: true,
detail: { reason: reason }
}));
} catch (e) { /* IE fallback */ }
}
window.AksrtFrontend = {
/**
* Register a component's init function.
*
* @param {string} name Unique component name
* @param {Function} initFn Function(root, reason) - root defaults to document
*/
register: function (name, initFn) {
if (typeof name !== 'string' || typeof initFn !== 'function') return;
// Prevent duplicate registrations
for (var i = 0; i < _registry.length; i++) {
if (_registry[i].name === name) {
_registry[i].initFn = initFn;
if (_booted) invoke(_registry[i], _bootRoot || document, 'init');
return;
}
}
var entry = { name: name, initFn: initFn };
_registry.push(entry);
if (_booted) invoke(entry, _bootRoot || document, 'init');
},
/**
* Run the one-time initial page boot.
*
* @param {Element|Document} root The document root to initialize
*/
boot: function (root) {
if (_booted) return;
_booted = true;
_bootRoot = root || document;
this.rehydrate(_bootRoot, 'init');
dispatch(_bootRoot, 'init', 'aksrt:ready');
},
/**
* Initialize or re-initialize all registered components on a root element.
*
* @param {Element|Document} root The root DOM element to search within
* @param {string} reason Why rehydrate is called:
* 'init' = initial page load
* 'ajax' = after AJAX content injection
* 'modal' = after modal content loads
* Custom string for other triggers
*/
rehydrate: function (root, reason) {
root = root || document;
reason = reason || 'init';
for (var i = 0; i < _registry.length; i++) {
invoke(_registry[i], root, reason);
}
dispatch(root, reason, 'aksrt:rehydrated');
},
/**
* Get the list of registered component names (for debugging).
*/
getRegistered: function () {
return _registry.map(function (r) { return r.name; });
},
isBooted: function () {
return _booted;
}
};
})();