From e7059f97b737d22c7e7132a6e51042cf5d1f0670 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 5 Mar 2024 12:25:29 +0100 Subject: [PATCH] user profile pf5 (#27503) Signed-off-by: Erik Jan de Wit --- .../theme/keycloak.v2/login/login.ftl | 4 +- .../theme/keycloak.v2/login/register.ftl | 12 +- .../login/resources/js/userProfile.js | 72 +++++++++++ .../login/user-profile-commons.ftl | 115 +++++++++++------- 4 files changed, 150 insertions(+), 53 deletions(-) create mode 100644 themes/src/main/resources/theme/keycloak.v2/login/resources/js/userProfile.js diff --git a/themes/src/main/resources/theme/keycloak.v2/login/login.ftl b/themes/src/main/resources/theme/keycloak.v2/login/login.ftl index ec46c9252e..447103aed1 100755 --- a/themes/src/main/resources/theme/keycloak.v2/login/login.ftl +++ b/themes/src/main/resources/theme/keycloak.v2/login/login.ftl @@ -99,8 +99,8 @@ <#elseif section = "info" > <#if realm.password && realm.registrationAllowed && !registrationDisabled??> -
-
+ -
-
+
+ +
+ + diff --git a/themes/src/main/resources/theme/keycloak.v2/login/resources/js/userProfile.js b/themes/src/main/resources/theme/keycloak.v2/login/resources/js/userProfile.js new file mode 100644 index 0000000000..bfde5ae8e4 --- /dev/null +++ b/themes/src/main/resources/theme/keycloak.v2/login/resources/js/userProfile.js @@ -0,0 +1,72 @@ +// @ts-check +/** + * @typedef {Object} AnnotationDescriptor + * @property {string} name - The name of the field to register (e.g. `numberFormat`). + * @property {(element: HTMLElement) => (() => void) | void} onAdd - The function to call when a new element is added to the DOM. + */ + +const observer = new MutationObserver(onMutate); +observer.observe(document.body, { childList: true, subtree: true }); + +/** @type {AnnotationDescriptor[]} */ +const descriptors = []; + +/** @type {WeakMap void>} */ +const cleanupFunctions = new WeakMap(); + +/** + * @param {AnnotationDescriptor} descriptor + */ +export function registerElementAnnotatedBy(descriptor) { + descriptors.push(descriptor); + + document.querySelectorAll(`[data-${descriptor.name}]`).forEach((element) => { + if (element instanceof HTMLElement) { + handleNewElement(element, descriptor); + } + }); +} + +/** + * @type {MutationCallback} + */ +function onMutate(mutations) { + const removedNodes = mutations.flatMap((mutation) => Array.from(mutation.removedNodes)); + + for (const node of removedNodes) { + if (!(node instanceof HTMLElement)) { + continue; + } + + const handleRemovedElement = cleanupFunctions.get(node); + + if (handleRemovedElement) { + handleRemovedElement(); + } + + cleanupFunctions.delete(node); + } + + const addedNodes = mutations.flatMap((mutation) => Array.from(mutation.addedNodes)); + + for (const descriptor of descriptors) { + for (const node of addedNodes) { + const input = node.querySelector('input'); + if (input.hasAttribute(`data-${descriptor.name}`)) { + handleNewElement(input, descriptor); + } + } + } +} + +/** + * @param {HTMLElement} element + * @param {AnnotationDescriptor} descriptor + */ +function handleNewElement(element, descriptor) { + const cleanup = descriptor.onAdd(element); + + if (cleanup) { + cleanupFunctions.set(element, cleanup); + } +} diff --git a/themes/src/main/resources/theme/keycloak.v2/login/user-profile-commons.ftl b/themes/src/main/resources/theme/keycloak.v2/login/user-profile-commons.ftl index bbbd3fd436..c6748dd9fb 100644 --- a/themes/src/main/resources/theme/keycloak.v2/login/user-profile-commons.ftl +++ b/themes/src/main/resources/theme/keycloak.v2/login/user-profile-commons.ftl @@ -29,36 +29,61 @@ <#nested "beforeField" attribute> -
-