accessibility.js 13.7 KB
Newer Older
1 2 3
(function(define, undefined) {
    'use strict';
    define(['jquery', 'underscore', 'annotator_1.2.9'], function($, _, Annotator) {
4 5 6
    /**
     * Adds the Accessibility Plugin
     **/
7 8
        Annotator.Plugin.Accessibility = function() {
            _.bindAll(this,
9 10
            'addAriaAttributes', 'onHighlightKeyDown', 'onViewerKeyDown',
            'onEditorKeyDown', 'addDescriptions', 'removeDescription',
11
            'focusOnGrabber', 'showViewer', 'onClose', 'focusOnHighlightedText'
12 13 14
        );
        // Call the Annotator.Plugin constructor this sets up the element and
        // options properties.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
            Annotator.Plugin.apply(this, arguments);
        };

        $.extend(Annotator.Plugin.Accessibility.prototype, new Annotator.Plugin(), {
            pluginInit: function() {
                this.annotator.subscribe('annotationViewerTextField', this.addAriaAttributes);
                this.annotator.subscribe('annotationsLoaded', this.addDescriptions);
                this.annotator.subscribe('annotationCreated', this.addDescriptions);
                this.annotator.subscribe('annotationCreated', this.focusOnHighlightedText);
                this.annotator.subscribe('annotationDeleted', this.removeDescription);
                this.annotator.element.on('keydown.accessibility.hl', '.annotator-hl', this.onHighlightKeyDown);
                this.annotator.element.on('keydown.accessibility.viewer', '.annotator-viewer', this.onViewerKeyDown);
                this.annotator.element.on('keydown.accessibility.editor', '.annotator-editor', this.onEditorKeyDown);
                this.addFocusGrabber();
                this.addTabIndex();
            },

            destroy: function() {
                this.annotator.unsubscribe('annotationViewerTextField', this.addAriaAttributes);
                this.annotator.unsubscribe('annotationsLoaded', this.addDescriptions);
                this.annotator.unsubscribe('annotationCreated', this.addDescriptions);
                this.annotator.unsubscribe('annotationCreated', this.focusOnHighlightedText);
                this.annotator.unsubscribe('annotationDeleted', this.removeDescription);
                this.annotator.element.off('.accessibility');
                this.removeFocusGrabber();
            },

            addTabIndex: function() {
                this.annotator.element
44 45
                .find('.annotator-edit, .annotator-delete')
                .attr('tabindex', 0);
46
            },
47

48 49 50 51 52 53 54
            addFocusGrabber: function() {
                this.focusGrabber = $('<span />', {
                    'class': 'edx-notes-focus-grabber',
                    'tabindex': '-1'
                });
                this.annotator.wrapper.before(this.focusGrabber);
            },
55

56 57 58 59 60 61
            removeFocusGrabber: function() {
                if (this.focusGrabber) {
                    this.focusGrabber.remove();
                    this.focusGrabber = null;
                }
            },
62

63 64 65
            focusOnGrabber: function() {
                this.annotator.wrapper.siblings('.edx-notes-focus-grabber').focus();
            },
66

67 68 69 70
            addDescriptions: function(annotations) {
                if (!_.isArray(annotations)) {
                    annotations = [annotations];
                }
71

72 73 74 75 76 77 78 79
                _.each(annotations, function(annotation) {
                    var id = annotation.id || _.uniqueId();

                    this.annotator.wrapper.after($('<div />', {
                        'class': 'aria-note-description sr',
                        'id': 'aria-note-description-' + id,
                        'text': Annotator.Util.escape(annotation.text)
                    }));
80

81 82 83 84 85
                    $(annotation.highlights).attr({
                        'aria-describedby': 'aria-note-description-' + id
                    });
                }, this);
            },
86

87 88 89 90 91 92
            removeDescription: function(annotation) {
                var id = $(annotation.highlights).attr('aria-describedby');
                $('#' + id).remove();
            },

            addAriaAttributes: function(field, annotation) {
93
            // Add ARIA attributes to associated note ie <div>My note</div>
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                $(field).attr({
                    'tabindex': -1,
                    'role': 'note',
                    'class': 'annotator-note'
                });
            },

            focusOnHighlightedText: function() {
                var viewer = this.annotator.viewer,
                    editor = this.annotator.editor,
                    highlight;

                try {
                    if (viewer.isShown()) {
                        highlight = viewer.annotations[0].highlights[0];
                    } else if (editor.isShown()) {
                        highlight = editor.annotation.highlights[0];
                    }
                    highlight.focus();
                } catch (err) {}
            },
115

116 117
            getViewerTabControls: function() {
                var viewer, note, viewerControls, editButton, delButton, closeButton, tabControls = [];
118 119

            // Viewer elements
120 121 122 123 124 125
                viewer = this.annotator.element.find('.annotator-viewer');
                note = viewer.find('.annotator-note');
                viewerControls = viewer.find('.annotator-controls');
                editButton = viewerControls.find('.annotator-edit');
                delButton = viewerControls.find('.annotator-delete');
                closeButton = viewerControls.find('.annotator-close');
126

127
                tabControls.push(note, editButton, delButton, closeButton);
128

129 130
                return tabControls;
            },
131

132 133 134
            getEditorTabControls: function() {
                var editor, editorControls, textArea, saveButton, cancelButton, tabControls = [], annotatorItems,
                    tagInput = null;
135 136

            // Editor elements
137 138 139 140 141 142
                editor = this.annotator.element.find('.annotator-editor');
                editorControls = editor.find('.annotator-controls');
                annotatorItems = editor.find('.annotator-listing').find('.annotator-item');
                textArea = annotatorItems.first().children('textarea');
                saveButton = editorControls.find('.annotator-save');
                cancelButton = editorControls.find('.annotator-cancel');
143

cahrens committed
144
            // If the tags plugin is enabled, add the ability to tab into it.
145 146 147
                if (annotatorItems.length > 1) {
                    tagInput = annotatorItems.first().next().children('input');
                }
cahrens committed
148

149 150 151 152 153
                tabControls.push(textArea);
                if (tagInput) {
                    tabControls.push(tagInput);
                }
                tabControls.push(saveButton, cancelButton);
154

155 156
                return tabControls;
            },
157

158 159
            focusOnNextTabControl: function(tabControls, tabControl) {
                var nextIndex;
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                _.each(tabControls, function(element, index) {
                    if (element.is(tabControl)) {
                        nextIndex = index === tabControls.length - 1 ? 0 : index + 1;
                        tabControls[nextIndex].focus();
                    }
                });
            },

            focusOnPreviousTabControl: function(tabControls, tabControl) {
                var previousIndex;
                _.each(tabControls, function(element, index) {
                    if (element.is(tabControl)) {
                        previousIndex = index === 0 ? tabControls.length - 1 : index - 1;
                        tabControls[previousIndex].focus();
                    }
                });
            },

            showViewer: function(position, annotation) {
                annotation = $.makeArray(annotation);
                this.annotator.showViewer(annotation, position);
                this.annotator.element.find('.annotator-listing').focus();
                this.annotator.subscribe('annotationDeleted', this.focusOnGrabber);
            },

            onClose: function() {
                this.focusOnHighlightedText();
                this.annotator.unsubscribe('annotationDeleted', this.focusOnGrabber);
            },

            onHighlightKeyDown: function(event) {
                var KEY = $.ui.keyCode,
                    keyCode = event.keyCode,
                    target = $(event.currentTarget),
                    annotation, position;

                switch (keyCode) {
198 199 200 201
                case KEY.TAB:
                    // This happens only when coming from notes page
                    if (this.annotator.viewer.isShown()) {
                        this.annotator.element.find('.annotator-listing').focus();
202 203
                        event.preventDefault();
                        event.stopPropagation();
204 205 206 207 208 209 210
                    }
                    break;
                case KEY.ENTER:
                case KEY.SPACE:
                    if (!this.annotator.viewer.isShown()) {
                        position = target.position();
                        this.showViewer(position, target.data('annotation'));
211 212
                        event.preventDefault();
                        event.stopPropagation();
213 214 215 216
                    }
                    break;
                case KEY.ESCAPE:
                    this.annotator.viewer.hide();
217 218
                    event.preventDefault();
                    event.stopPropagation();
219
                    break;
220 221
                }
            },
222

223 224 225 226 227 228
            onViewerKeyDown: function(event) {
                var KEY = $.ui.keyCode,
                    keyCode = event.keyCode,
                    target = $(event.target),
                    listing = this.annotator.element.find('.annotator-listing'),
                    tabControls;
229

230
                switch (keyCode) {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                case KEY.TAB:
                    tabControls = this.getViewerTabControls();
                    if (event.shiftKey) { // Tabbing backwards
                        if (target.is(listing)) {
                            _.last(tabControls).focus();
                        }
                        else {
                            this.focusOnPreviousTabControl(tabControls, target);
                        }
                    } else { // Tabbing forward
                        if (target.is(listing)) {
                            _.first(tabControls).focus();
                        }
                        else {
                            this.focusOnNextTabControl(tabControls, target);
                        }
                    }
                    event.preventDefault();
                    event.stopPropagation();
                    break;
                case KEY.ENTER:
                case KEY.SPACE:
                    if (target.hasClass('annotator-close')) {
                        this.onClose();
255
                        this.annotator.viewer.hide();
256 257 258 259 260
                        event.preventDefault();
                    }
                    break;
                case KEY.ESCAPE:
                    this.onClose();
261
                    this.annotator.viewer.hide();
262 263
                    event.preventDefault();
                    break;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
                }
            },

            onEditorKeyDown: function(event) {
                var KEY = $.ui.keyCode,
                    keyCode = event.keyCode,
                    target = $(event.target),
                    editor, form, editorControls, save, cancel,
                    tabControls;

                editor = this.annotator.element.find('.annotator-editor');
                form = editor.find('.annotator-widget');
                editorControls = editor.find('.annotator-controls');
                save = editorControls.find('.annotator-save');
                cancel = editorControls.find('.annotator-cancel');

                switch (keyCode) {
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                case KEY.TAB:
                    tabControls = this.getEditorTabControls();
                    if (event.shiftKey) { // Tabbing backwards
                        if (target.is(form)) {
                            _.last(tabControls).focus();
                        } else {
                            this.focusOnPreviousTabControl(tabControls, target);
                        }
                    } else { // Tabbing forward
                        if (target.is(form)) {
                            _.first(tabControls).focus();
                        } else {
                            this.focusOnNextTabControl(tabControls, target);
                        }
                    }
                    event.preventDefault();
                    event.stopPropagation();
                    break;
                case KEY.ENTER:
                    if (target.is(save) || event.metaKey || event.ctrlKey) {
301
                        this.onClose();
302 303
                        this.annotator.editor.submit();
                    } else if (target.is(cancel)) {
304
                        this.onClose();
305 306 307 308 309 310 311 312
                        this.annotator.editor.hide();
                    } else {
                        break;
                    }
                    event.preventDefault();
                    break;
                case KEY.SPACE:
                    if (target.is(save)) {
313
                        this.onClose();
314 315
                        this.annotator.editor.submit();
                    } else if (target.is(cancel)) {
316
                        this.onClose();
317 318 319 320 321 322 323 324
                        this.annotator.editor.hide();
                    } else {
                        break;
                    }
                    event.preventDefault();
                    break;
                case KEY.ESCAPE:
                    this.onClose();
325
                    this.annotator.editor.hide();
326 327
                    event.preventDefault();
                    break;
328
                }
329
            }
330
        });
331 332
    });
}).call(this, define || RequireJS.define);