From 1aa80d4d62eb7d688478313d4e165242da53c076 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 22 Nov 2024 16:49:20 +0200 Subject: [PATCH 1/2] UHF-10570: Merge user consent functionality with chat accessibility enhancements and create one new library, attach it to IbmChatApp --- assets/js/chat_enhancer.js | 46 +++++++++++++++++++++++++++++ helfi_platform_config.libraries.yml | 6 ++-- src/Plugin/Block/IbmChatApp.php | 2 +- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 assets/js/chat_enhancer.js diff --git a/assets/js/chat_enhancer.js b/assets/js/chat_enhancer.js new file mode 100644 index 000000000..a05133d09 --- /dev/null +++ b/assets/js/chat_enhancer.js @@ -0,0 +1,46 @@ +(function (Drupal) { + 'use strict'; + + Drupal.behaviors.chat_enhancer = { + attach: function (context, settings) { + // Add the chat_user_consent functionality + window.chat_user_consent = { + retrieveUserConsent: () => (Drupal.cookieConsent.getConsentStatus(['chat'])), + confirmUserConsent: () => { + if (Drupal.cookieConsent.getConsentStatus(['chat'])) return; + Drupal.cookieConsent.setAcceptedCategories(['chat']); + } + }; + + // Chat accessibility enhancements + let attemptCount = 0; + const maxAttempts = 10; // Maximum number of attempts + + // Function to check for the element and set attributes + function checkAndSetAttributes() { + const element = document.getElementById('aca-wbc-chat-app-button'); + + if (element) { + // Check and set the 'role' attribute if not set + if (!element.hasAttribute('role')) { + element.setAttribute('role', 'region'); + } + + // Check and set the 'aria-labelledby' attribute if not set + if (!element.hasAttribute('aria-labelledby')) { + element.setAttribute('aria-labelledby', 'chat-app-button'); + } + } else if (attemptCount < maxAttempts) { + // If element is not found, wait and try again after a delay + attemptCount++; + setTimeout(checkAndSetAttributes, 1000); + } + } + + // Start the initial check + checkAndSetAttributes(); + } + + } + +})(Drupal); diff --git a/helfi_platform_config.libraries.yml b/helfi_platform_config.libraries.yml index 6ad780073..ac8b24338 100644 --- a/helfi_platform_config.libraries.yml +++ b/helfi_platform_config.libraries.yml @@ -67,11 +67,11 @@ chat_leijuke: - core/drupal - core/drupalSettings -user_consent_functions: - version: 1.0.x +chat_enhancer: + version: 1.0.0 header: true js: - assets/js/user_consent_functions.js: {} + assets/js/chat_enhancer.js: {} dependencies: - core/drupal diff --git a/src/Plugin/Block/IbmChatApp.php b/src/Plugin/Block/IbmChatApp.php index 32e5101c6..f7b260b11 100644 --- a/src/Plugin/Block/IbmChatApp.php +++ b/src/Plugin/Block/IbmChatApp.php @@ -68,7 +68,7 @@ public function blockSubmit($form, FormStateInterface $formState) { */ public function build() { - $library = ['helfi_platform_config/user_consent_functions']; + $library = ['helfi_platform_config/chat_enhancer']; $build = []; From 05cfc29db3988d248f722c93f04ba797ca395d91 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Tue, 26 Nov 2024 13:22:51 +0200 Subject: [PATCH 2/2] UHF-10570: Removed user_consent_functions javascript file --- assets/js/user_consent_functions.js | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 assets/js/user_consent_functions.js diff --git a/assets/js/user_consent_functions.js b/assets/js/user_consent_functions.js deleted file mode 100644 index 5391fef8f..000000000 --- a/assets/js/user_consent_functions.js +++ /dev/null @@ -1,12 +0,0 @@ -(function (Drupal) { - 'use strict'; - - window.chat_user_consent = { - retrieveUserConsent: () => (Drupal.cookieConsent.getConsentStatus(['chat'])), - confirmUserConsent: () => { - if (Drupal.cookieConsent.getConsentStatus(['chat'])) return; - Drupal.cookieConsent.setAcceptedCategories(['chat']); - } - }; - -})(Drupal);