Skip to content

Commit

Permalink
Merge pull request #853 from City-of-Helsinki/UHF-10570
Browse files Browse the repository at this point in the history
UHF-10570: Aria label and role for the IBM chats with JavaScript
  • Loading branch information
teroelonen authored Nov 26, 2024
2 parents a09268b + 05cfc29 commit e6cde28
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
46 changes: 46 additions & 0 deletions assets/js/chat_enhancer.js
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 0 additions & 12 deletions assets/js/user_consent_functions.js

This file was deleted.

6 changes: 3 additions & 3 deletions helfi_platform_config.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Block/IbmChatApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit e6cde28

Please sign in to comment.