Files
breadcrumb-the-shire/web/js/components/app-password-toggle.js
fs c7b8fd516a feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.

New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering

New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
  module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
  FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00

84 lines
2.9 KiB
JavaScript

/**
* Show/hide password toggle button injected next to password inputs.
*/
import { resolveHost } from '../core/app-dom.js';
export function initPasswordToggles(root = document, options = {}) {
const selector = String(options.selector || 'input[type="password"]').trim() || 'input[type="password"]';
const host = resolveHost(root);
const inputs = Array.from(host.querySelectorAll(selector));
if (!inputs.length) {
return { destroy: () => {} };
}
const showLabel = (document.documentElement.dataset.passwordToggleShowLabel || '').trim();
const hideLabel = (document.documentElement.dataset.passwordToggleHideLabel || '').trim();
if (!showLabel || !hideLabel) {
return { destroy: () => {} };
}
const cleanupFns = [];
inputs.forEach((input) => {
if (!(input instanceof HTMLInputElement)) {return;}
if (input.dataset.passwordToggleBound === '1') {return;}
const label = input.closest('label');
if (!label) {return;}
const parent = input.parentElement;
if (parent instanceof HTMLElement && parent.classList.contains('login-password-wrapper')) {
const existingToggle = parent.querySelector('.login-password-toggle');
if (existingToggle instanceof HTMLButtonElement) {
input.dataset.passwordToggleBound = '1';
existingToggle.dataset.passwordToggleBound = '1';
existingToggle.setAttribute('aria-label', input.type === 'password' ? showLabel : hideLabel);
cleanupFns.push(() => {
delete input.dataset.passwordToggleBound;
delete existingToggle.dataset.passwordToggleBound;
});
return;
}
}
const wrapper = document.createElement('div');
wrapper.className = 'login-password-wrapper';
const toggle = document.createElement('button');
toggle.type = 'button';
toggle.className = 'login-password-toggle';
toggle.setAttribute('aria-label', showLabel);
const icon = document.createElement('i');
icon.className = 'bi bi-eye';
icon.setAttribute('aria-hidden', 'true');
toggle.appendChild(icon);
input.replaceWith(wrapper);
wrapper.appendChild(input);
wrapper.appendChild(toggle);
input.dataset.passwordToggleBound = '1';
toggle.dataset.passwordToggleBound = '1';
const onToggleClick = () => {
const isPassword = input.type === 'password';
input.type = isPassword ? 'text' : 'password';
icon.className = isPassword ? 'bi bi-eye-slash' : 'bi bi-eye';
toggle.setAttribute('aria-label', isPassword ? hideLabel : showLabel);
input.focus();
};
toggle.addEventListener('click', onToggleClick);
cleanupFns.push(() => {
toggle.removeEventListener('click', onToggleClick);
delete input.dataset.passwordToggleBound;
delete toggle.dataset.passwordToggleBound;
});
});
const destroy = () => {
cleanupFns.forEach((cleanup) => cleanup());
};
return { destroy };
}