113 lines
3.3 KiB
JavaScript
113 lines
3.3 KiB
JavaScript
(function (root, factory) {
|
|
// 待开发页面状态模块同时支持浏览器页面和 Node 单元测试。
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory(root);
|
|
return;
|
|
}
|
|
|
|
root.PageAvailability = factory(root);
|
|
if (root.document) {
|
|
root.document.addEventListener('DOMContentLoaded', function () {
|
|
root.PageAvailability.init();
|
|
});
|
|
}
|
|
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
|
'use strict';
|
|
|
|
var documentRef = root.document;
|
|
|
|
function escapeHtml(value) {
|
|
return String(value === undefined || value === null ? '' : value)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
function buildBanner(message) {
|
|
return '<section class="feature-status-banner" role="status">' +
|
|
'<strong>功能开发中</strong>' +
|
|
'<p>' + escapeHtml(message || '该功能正在开发中,当前页面保留设计预览。') + '</p>' +
|
|
'</section>';
|
|
}
|
|
|
|
function isPendingPage(body) {
|
|
return Boolean(body && body.getAttribute('data-feature-status') === 'pending');
|
|
}
|
|
|
|
function getMessage(body) {
|
|
return body && body.getAttribute('data-feature-message') || '该功能正在开发中,当前页面保留设计预览。';
|
|
}
|
|
|
|
function showMessage(message) {
|
|
if (root.layui && root.layui.layer) {
|
|
root.layui.layer.msg(message);
|
|
return;
|
|
}
|
|
|
|
if (root.alert) root.alert(message);
|
|
}
|
|
|
|
function containsTarget(container, target) {
|
|
return Boolean(container && target && container.contains(target));
|
|
}
|
|
|
|
function shouldBlockLink(link) {
|
|
var href;
|
|
|
|
if (!link || link.getAttribute('data-feature-link') === 'available') return false;
|
|
href = link.getAttribute('href') || '';
|
|
return Boolean(href) && href.charAt(0) !== '#' && !/^(?:https?:|mailto:|tel:)/i.test(href);
|
|
}
|
|
|
|
function init() {
|
|
var body;
|
|
var main;
|
|
var target;
|
|
var message;
|
|
|
|
if (!documentRef) return;
|
|
body = documentRef.body;
|
|
if (!isPendingPage(body)) return;
|
|
|
|
main = documentRef.querySelector('main');
|
|
target = main && (main.querySelector('.module-main') || main);
|
|
message = getMessage(body);
|
|
|
|
if (target && !target.querySelector('.feature-status-banner')) {
|
|
target.insertAdjacentHTML('afterbegin', buildBanner(message));
|
|
}
|
|
|
|
documentRef.addEventListener('submit', function (event) {
|
|
if (!containsTarget(main, event.target)) return;
|
|
event.preventDefault();
|
|
showMessage(message);
|
|
});
|
|
|
|
documentRef.addEventListener('click', function (event) {
|
|
var source = event.target;
|
|
var button = source && source.closest && source.closest('button');
|
|
var link = source && source.closest && source.closest('a');
|
|
|
|
if (button && containsTarget(main, button) && button.type !== 'reset') {
|
|
event.preventDefault();
|
|
showMessage(message);
|
|
return;
|
|
}
|
|
|
|
// 侧栏用于浏览已设计页面,主内容区的业务入口必须显式声明可用后才能跳转。
|
|
if (!link || !containsTarget(main, link) || link.closest('.module-nav') || !shouldBlockLink(link)) return;
|
|
event.preventDefault();
|
|
showMessage(message);
|
|
});
|
|
}
|
|
|
|
return {
|
|
buildBanner: buildBanner,
|
|
isPendingPage: isPendingPage,
|
|
shouldBlockLink: shouldBlockLink,
|
|
init: init
|
|
};
|
|
});
|