|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * GwtMaterial |
| 4 | + * %% |
| 5 | + * Copyright (C) 2015 - 2020 GwtMaterialDesign |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package gwt.material.design.client.accessibility; |
| 21 | + |
| 22 | +import com.google.gwt.core.client.JavaScriptObject; |
| 23 | +import com.google.gwt.core.client.ScriptInjector; |
| 24 | +import com.google.gwt.event.dom.client.KeyCodes; |
| 25 | +import com.google.gwt.event.shared.HandlerRegistration; |
| 26 | +import com.google.gwt.resources.client.TextResource; |
| 27 | +import com.google.gwt.user.client.ui.Widget; |
| 28 | +import gwt.material.design.client.base.MaterialWidget; |
| 29 | +import gwt.material.design.client.js.JsMaterialElement; |
| 30 | +import gwt.material.design.client.resources.MaterialDebugResources; |
| 31 | +import gwt.material.design.client.resources.MaterialResources; |
| 32 | +import gwt.material.design.client.ui.MaterialToast; |
| 33 | +import gwt.material.design.jquery.client.api.JQuery; |
| 34 | +import gwt.material.design.jscore.client.api.core.Element; |
| 35 | + |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import static gwt.material.design.client.js.JsMaterialElement.$; |
| 40 | + |
| 41 | +public class AccessibilityControl { |
| 42 | + |
| 43 | + protected static final String FOCUS_VISIBILITY_CLASSNAME = "js-focus-visible"; |
| 44 | + protected static final String FOCUS_VISIBILITY_ATTRIBUTE = "data-js-focus-visible"; |
| 45 | + protected static final String FOCUS_VISIBLE_WIDGET = "focus-visible"; |
| 46 | + protected static final String DATA_FOCUS_ADDED_ATTRIBUTE = "data-focus-visible-added"; |
| 47 | + public static AccessibilityControl instance; |
| 48 | + protected JavaScriptObject resourceUrlObject; |
| 49 | + protected Map<HandlerRegistration, Widget> accessibilityHandlerRegistration; |
| 50 | + protected boolean enabled = true; |
| 51 | + protected boolean loaded; |
| 52 | + |
| 53 | + public AccessibilityControl() { |
| 54 | + accessibilityHandlerRegistration = new HashMap<>(); |
| 55 | + } |
| 56 | + |
| 57 | + public void load(boolean debug) { |
| 58 | + TextResource resource; |
| 59 | + if (!loaded) { |
| 60 | + if (debug) { |
| 61 | + resource = MaterialDebugResources.INSTANCE.focusVisibleJsDebug(); |
| 62 | + } else { |
| 63 | + resource = MaterialResources.INSTANCE.focusVisibleJs(); |
| 64 | + } |
| 65 | + injectJs(resource); |
| 66 | + loaded = true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected void injectJs(TextResource resource) { |
| 71 | + String text = resource.getText() + ("//# sourceURL=" + resource.getName() + ".js"); |
| 72 | + |
| 73 | + // Inject the script resource |
| 74 | + resourceUrlObject = ScriptInjector.fromString(text) |
| 75 | + .setWindow(ScriptInjector.TOP_WINDOW) |
| 76 | + .inject(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This will register the accessibility control for a widget provided with keycodes |
| 81 | + */ |
| 82 | + public void registerWidget(MaterialWidget widget, int key) { |
| 83 | + registerWidget(widget, key, null); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * This will register the accessibility control for a widget provided with default KeyCodes.ENTER |
| 88 | + */ |
| 89 | + public void registerWidget(MaterialWidget widget) { |
| 90 | + registerWidget(widget, KeyCodes.KEY_ENTER); |
| 91 | + } |
| 92 | + |
| 93 | + public void registerWidget(MaterialWidget widget, TriggerCallback callback) { |
| 94 | + registerWidget(widget, KeyCodes.KEY_ENTER, callback); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * This will register the accessibility control for a widget provided with keycodes and |
| 99 | + * TriggerCallback |
| 100 | + */ |
| 101 | + public void registerWidget(MaterialWidget widget, int key, TriggerCallback callback) { |
| 102 | + if (widget != null) { |
| 103 | + HandlerRegistration handlerRegistration = widget.registerHandler(widget.addKeyUpHandler(event -> { |
| 104 | + if (isEnabled() && event.getNativeKeyCode() == key) { |
| 105 | + if (callback != null) { |
| 106 | + callback.call(event); |
| 107 | + } else { |
| 108 | + triggerClick(widget); |
| 109 | + } |
| 110 | + } |
| 111 | + })); |
| 112 | + if (enabled) { |
| 113 | + widget.setTabIndex(0); |
| 114 | + } else { |
| 115 | + widget.getElement().removeAttribute("tabIndex"); |
| 116 | + } |
| 117 | + accessibilityHandlerRegistration.put(handlerRegistration, widget); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * This will unregister any accessibility control to a widget |
| 123 | + * @param widget |
| 124 | + */ |
| 125 | + public void unregisterWidget(Widget widget) { |
| 126 | + if (widget != null) { |
| 127 | + if (widget.getElement().hasClassName(FOCUS_VISIBLE_WIDGET)) { |
| 128 | + widget.removeStyleName(FOCUS_VISIBLE_WIDGET); |
| 129 | + } |
| 130 | + |
| 131 | + if (widget.getElement().hasAttribute(DATA_FOCUS_ADDED_ATTRIBUTE)) { |
| 132 | + widget.getElement().removeAttribute(DATA_FOCUS_ADDED_ATTRIBUTE); |
| 133 | + } |
| 134 | + accessibilityHandlerRegistration.forEach((handlerRegistration, currentWidget) -> { |
| 135 | + if (widget == currentWidget) { |
| 136 | + handlerRegistration.removeHandler(); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void unload() { |
| 143 | + JsMaterialElement html = $("html"); |
| 144 | + if (html != null) { |
| 145 | + if (html.hasClass(FOCUS_VISIBILITY_CLASSNAME)) { |
| 146 | + html.removeClass(FOCUS_VISIBILITY_CLASSNAME); |
| 147 | + } |
| 148 | + html.removeAttr(FOCUS_VISIBILITY_ATTRIBUTE); |
| 149 | + } |
| 150 | + |
| 151 | + if (resourceUrlObject != null) { |
| 152 | + JsMaterialElement scriptTag = $(resourceUrlObject.cast()); |
| 153 | + scriptTag.remove(); |
| 154 | + } |
| 155 | + loaded = false; |
| 156 | + } |
| 157 | + |
| 158 | + public void triggerClick(Widget widget) { |
| 159 | + if (widget != null) { |
| 160 | + JQuery.$(widget.getElement()).click(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + public boolean isEnabled() { |
| 165 | + return enabled; |
| 166 | + } |
| 167 | + |
| 168 | + public void setEnabled(boolean enabled) { |
| 169 | + this.enabled = enabled; |
| 170 | + } |
| 171 | + |
| 172 | + public static AccessibilityControl get() { |
| 173 | + if (instance == null) { |
| 174 | + instance = new AccessibilityControl(); |
| 175 | + } |
| 176 | + return instance; |
| 177 | + } |
| 178 | +} |
0 commit comments