Files
2026-09-15 10:35:56 +08:00

297 lines
9.9 KiB
JavaScript

define([
'jquery'
], function ($) {
'use strict';
return function (config, element) {
var $root = $(element),
$activeTrigger = $(),
$activeTooltip = $(),
$drawers = $root.find('[data-wiki-drawer]'),
$drawerBackdrop = $root.find('.esun-wiki-drawer-backdrop'),
$activeDrawer = $(),
$activeDrawerTrigger = $(),
drawerMedia = window.matchMedia('(max-width: 860px)'),
hideTimer = null;
function syncSearchClear($input) {
var hasValue = $.trim($input.val()).length > 0;
$input.closest('form').find('.esun-wiki-search-clear').toggleClass('is-visible', hasValue);
}
function cancelTooltipHide() {
if (hideTimer) {
window.clearTimeout(hideTimer);
hideTimer = null;
}
}
function positionTooltip($trigger, $tooltip) {
var triggerRect = $trigger[0].getBoundingClientRect(),
viewportWidth = window.innerWidth || document.documentElement.clientWidth,
viewportHeight = window.innerHeight || document.documentElement.clientHeight,
gutter = 14,
gap = 18,
tooltipWidth,
tooltipHeight,
left,
top,
arrowLeft,
showBelow;
$tooltip
.addClass('is-active')
.css({left: 0, top: 0, visibility: 'hidden'});
tooltipWidth = $tooltip.outerWidth();
tooltipHeight = $tooltip.outerHeight();
left = triggerRect.left + (triggerRect.width / 2) - (tooltipWidth / 2);
left = Math.max(gutter, Math.min(left, viewportWidth - tooltipWidth - gutter));
top = triggerRect.top - tooltipHeight - gap;
showBelow = top < gutter;
if (showBelow) {
top = triggerRect.bottom + gap;
}
top = Math.max(gutter, Math.min(top, viewportHeight - tooltipHeight - gutter));
arrowLeft = triggerRect.left + (triggerRect.width / 2) - left;
arrowLeft = Math.max(26, Math.min(arrowLeft, tooltipWidth - 26));
$tooltip
.toggleClass('is-below', showBelow)
.css({
left: Math.round(left),
top: Math.round(top),
visibility: 'visible',
'--esun-wiki-tip-arrow-left': Math.round(arrowLeft) + 'px'
});
}
function closeTooltip(restoreFocus) {
cancelTooltipHide();
if ($activeTooltip.length) {
$activeTooltip
.removeClass('is-active is-below')
.attr('aria-hidden', 'true')
.removeAttr('style');
}
if ($activeTrigger.length) {
$activeTrigger.attr('aria-expanded', 'false');
if (restoreFocus) {
$activeTrigger.trigger('focus');
}
}
$activeTrigger = $();
$activeTooltip = $();
}
function showTooltip($trigger) {
var tooltipId = $trigger.attr('data-wiki-tip'),
$tooltip = $root.find('[data-wiki-tooltip-id="' + tooltipId + '"]');
if (!$tooltip.length) {
return;
}
cancelTooltipHide();
if ($activeTooltip.length && $activeTooltip[0] !== $tooltip[0]) {
closeTooltip(false);
}
$activeTrigger = $trigger;
$activeTooltip = $tooltip;
$trigger.attr('aria-expanded', 'true');
$tooltip.attr('aria-hidden', 'false');
positionTooltip($trigger, $tooltip);
}
function scheduleTooltipHide() {
cancelTooltipHide();
hideTimer = window.setTimeout(function () {
closeTooltip(false);
}, 140);
}
function closeDrawer(restoreFocus) {
var $trigger = $activeDrawerTrigger;
$drawers.removeClass('is-open');
$root.find('[data-wiki-drawer-open]').attr('aria-expanded', 'false');
$drawerBackdrop.removeClass('is-visible').attr('aria-hidden', 'true');
$('html, body').removeClass('esun-wiki-drawer-lock');
if (restoreFocus && $trigger.length) {
$trigger.trigger('focus');
}
if (drawerMedia.matches) {
$drawers.attr('aria-hidden', 'true');
} else {
$drawers.removeAttr('aria-hidden');
}
$activeDrawer = $();
$activeDrawerTrigger = $();
}
function openDrawer(name, $trigger) {
var $drawer;
if (!drawerMedia.matches) {
return;
}
$drawer = $drawers.filter('[data-wiki-drawer="' + name + '"]');
if (!$drawer.length) {
return;
}
closeTooltip(false);
closeDrawer(false);
$activeDrawer = $drawer;
$activeDrawerTrigger = $trigger;
$drawer.addClass('is-open').attr('aria-hidden', 'false');
$trigger.attr('aria-expanded', 'true');
$drawerBackdrop.addClass('is-visible').attr('aria-hidden', 'false');
$('html, body').addClass('esun-wiki-drawer-lock');
window.setTimeout(function () {
if ($drawer.hasClass('is-open')) {
$drawer.find('.esun-wiki-drawer-close').first().trigger('focus');
}
}, 220);
}
function syncDrawerMode() {
closeDrawer(false);
}
function trapDrawerFocus(event) {
var $focusable,
first,
last;
if (event.key !== 'Tab' || !$activeDrawer.length) {
return;
}
$focusable = $activeDrawer
.find('a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])')
.filter(':visible');
if (!$focusable.length) {
event.preventDefault();
return;
}
first = $focusable[0];
last = $focusable[$focusable.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
$(last).trigger('focus');
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
$(first).trigger('focus');
}
}
$root.find('.esun-wiki-search input[type="search"]').each(function () {
syncSearchClear($(this));
});
$root.on('click', '.esun-wiki-tree-toggle', function () {
var $button = $(this),
$item = $button.closest('.esun-wiki-tree-item'),
isOpen = !$item.hasClass('is-open');
$item.toggleClass('is-open', isOpen);
$button.attr('aria-expanded', isOpen ? 'true' : 'false');
});
$root.on('input change', '.esun-wiki-search input[type="search"]', function () {
syncSearchClear($(this));
});
$root.on('click', '.esun-wiki-search-clear', function () {
var $form = $(this).closest('form'),
$input = $form.find('input[type="search"]');
$input.val('').trigger('focus');
syncSearchClear($input);
if (window.location.search.indexOf('q=') !== -1) {
window.location.href = $form.attr('action');
}
});
$root.on('click', '[data-wiki-drawer-open]', function () {
openDrawer($(this).attr('data-wiki-drawer-open'), $(this));
});
$root.on('click', '[data-wiki-drawer-close]', function () {
closeDrawer(true);
});
$root.on('click', '.esun-wiki-toc a, .esun-wiki-sidebar a.esun-wiki-tree-link', function () {
if (drawerMedia.matches) {
closeDrawer(false);
}
});
$root.on('mouseenter focusin', '.esun-wiki-tip-trigger', function () {
showTooltip($(this));
});
$root.on('mouseleave focusout', '.esun-wiki-tip-trigger', scheduleTooltipHide);
$root.on('click', '.esun-wiki-tip-trigger', function (event) {
event.preventDefault();
showTooltip($(this));
});
$root.on('mouseenter focusin', '.esun-wiki-tooltip', cancelTooltipHide);
$root.on('mouseleave focusout', '.esun-wiki-tooltip', scheduleTooltipHide);
$(window).on('resize.esunWikiTooltip', function () {
if ($activeTrigger.length && $activeTooltip.length) {
positionTooltip($activeTrigger, $activeTooltip);
}
});
$(window).on('scroll.esunWikiTooltip', function () {
if ($activeTrigger.length && $activeTooltip.length) {
positionTooltip($activeTrigger, $activeTooltip);
}
});
$(document).on('mousedown.esunWikiTooltip touchstart.esunWikiTooltip', function (event) {
if (!$(event.target).closest('.esun-wiki-tip-trigger, .esun-wiki-tooltip').length) {
closeTooltip(false);
}
});
$(document).on('keydown.esunWikiTooltip', function (event) {
if (event.key === 'Escape' && $activeDrawer.length) {
closeDrawer(true);
} else if (event.key === 'Escape' && $activeTooltip.length) {
closeTooltip(true);
}
trapDrawerFocus(event);
});
if (drawerMedia.addEventListener) {
drawerMedia.addEventListener('change', syncDrawerMode);
} else {
drawerMedia.addListener(syncDrawerMode);
}
syncDrawerMode();
};
});