Debian Codegraph @

docs/assets/js/accessibility-manager.js

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 // Create the fheroes2 namespace if it doesn't exist
22 globalThis.fheroes2 = globalThis.fheroes2 || {};
23
24 // Utility class for handling common accessibility features
25 class AccessibilityManager
26 {
27 constructor()
28 {
29 this.liveRegion = this.createLiveRegion();
30 }
31
32 // Creates a live region for screen reader announcements
33 // The live region is a div element that is used to announce messages to
34 // screen readers.
35 // Returns: The live region element
36 createLiveRegion()
37 {
38 const liveRegion = document.createElement( 'div' );
39 liveRegion.setAttribute( 'aria-live', 'assertive' );
40 liveRegion.setAttribute( 'aria-atomic', 'true' );
41 // This doesn't work as a CSS property, so we need to set it manually
42 liveRegion.style.cssText
43 = 'position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;';
44 document.body.appendChild( liveRegion );
45 return liveRegion;
46 }
47
48 // Announces a message to screen readers
49 // Parameters:
50 // message - The message to announce
51 announce( message )
52 {
53 // Clear the live region first
54 this.liveRegion.textContent = '';
55
56 // Force a reflow to ensure the announcement is triggered if the user
57 // triggers multiple times in a row.
58 requestAnimationFrame( () => {
59 // Set the new message
60 this.liveRegion.textContent = message;
61 } );
62
63 // Force another reflow to ensure the announcement is triggered
64 requestAnimationFrame( () => {
65 // No additional content needed, just forcing a reflow
66 } );
67 }
68
69 // Creates an accessible element (button or span)
70 // Parameters:
71 // options - An object containing the following properties:
72 // text - The element text
73 // ariaLabel - The ARIA label
74 // className - Additional CSS classes
75 // elementType - The element type ('button' or 'span')
76 // Returns: The accessible element
77 createAccessibleElement( options )
78 {
79 const { text, ariaLabel, className = '', elementType = 'button' } = options;
80
81 const element = document.createElement( elementType );
82 element.textContent = text;
83 element.setAttribute( 'aria-label', ariaLabel );
84 element.setAttribute( 'role', 'button' );
85 element.setAttribute( 'tabindex', '0' );
86 if ( className ) {
87 element.className = className;
88 }
89 return element;
90 }
91
92 // Creates an accessible button element
93 // Parameters:
94 // options - An object containing the following properties:
95 // text - The button text
96 // ariaLabel - The ARIA label
97 // className - Additional CSS classes
98 // Returns: The button element
99 createAccessibleButton( options )
100 {
101 return this.createAccessibleElement( {...options, elementType : 'button' } );
102 }
103
104 // Creates an accessible span element
105 // Parameters:
106 // options - An object containing the following properties:
107 // text - The span text
108 // ariaLabel - The ARIA label
109 // className - Additional CSS classes
110 // Returns: The span element
111 createAccessibleSpan( options )
112 {
113 return this.createAccessibleElement( {...options, elementType : 'span' } );
114 }
115
116 // Updates element state with visual and screen reader feedback
117 // Parameters:
118 // options - An object containing the following properties:
119 // element - The element to update
120 // success - Whether the action was successful
121 // successMessage - Message for screen reader on success
122 // errorMessage - Message for screen reader on error
123 // successText - Text to show on element for success
124 // errorText - Text to show on element for error
125 // defaultText - Default element text
126 // resetDelay - Delay before resetting element (ms)
127 // isHtml - Whether the text contains HTML
128 updateElementState( options )
129 {
130 const { element, success, successMessage, errorMessage, successText, errorText, defaultText, resetDelay = 2000, isHtml = false } = options;
131
132 // Update visual state first
133 if ( success ) {
134 if ( isHtml ) {
135 element.innerHTML = successText;
136 }
137 else {
138 element.textContent = successText;
139 }
140 element.classList.add( 'success' );
141 }
142 else {
143 if ( isHtml ) {
144 element.innerHTML = errorText;
145 }
146 else {
147 element.textContent = errorText;
148 }
149 element.classList.add( 'error' );
150 }
151
152 // Announce to screen reader after a short delay to ensure visual update is complete
153 setTimeout( () => { this.announce( success ? successMessage : errorMessage ); }, 50 );
154
155 // Reset element after delay
156 setTimeout( () => {
157 if ( isHtml ) {
158 element.innerHTML = defaultText;
159 }
160 else {
161 element.textContent = defaultText;
162 }
163 element.classList.remove( 'success', 'error' );
164 }, resetDelay );
165 }
166
167 // Sets up keyboard event handling for an element
168 // Parameters:
169 // element - The element to add keyboard events to
170 // handler - The click handler function
171 setupKeyboardEvents( element, handler )
172 {
173 element.addEventListener( 'keydown', ( event ) => {
174 if ( event.key === 'Enter' || event.key === ' ' ) {
175 event.preventDefault();
176 handler( event );
177 }
178 } );
179 }
180 }
181
182 // Export the accessibility manager to the fheroes2 namespace
183 globalThis.fheroes2.AccessibilityManager = AccessibilityManager;