accessibility_tools.js 7.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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
/*

============================================
License for Application
============================================

This license is governed by United States copyright law, and with respect to matters
of tort, contract, and other causes of action it is governed by North Carolina law,
without regard to North Carolina choice of law provisions.  The forum for any dispute
resolution shall be in Wake County, North Carolina.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list
   of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this
   list of conditions and the following disclaimer in the documentation and/or other
   materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
var focusedElementBeforeModal;

var accessible_modal = function(trigger, closeButtonId, modalId, mainPageId) {
  // Modifies a lean modal to optimize focus management.
  // "trigger" is the selector for the link element that triggers the modal.
  // "closeButtonId" is the selector for the button that closes out the modal.
  // "modalId" is the selector for the modal being managed
  // "mainPageId" is the selector for the main part of the page
44
  //
45
  // based on http://accessibility.oit.ncsu.edu/training/aria/modal-window/modal-window.js
46
  //
47 48
  // see http://accessibility.oit.ncsu.edu/blog/2013/09/13/the-incredible-accessible-modal-dialog/
  // for more information on managing modals
49 50
  //
    var focusableElementsString = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]';
51

52 53
    $(trigger).click(function() {
        focusedElementBeforeModal = $(trigger);
54 55

    // when modal is opened, adjust tabindexes and aria-hidden attributes
56 57 58 59 60 61 62 63
        $(mainPageId).attr('aria-hidden', 'true');
        $(modalId).attr('aria-hidden', 'false');

        var focusableItems = $(modalId).find('*').filter(focusableElementsString).filter(':visible');

        focusableItems.attr('tabindex', '2');
        $(closeButtonId).attr('tabindex', '1');
        $(closeButtonId).focus();
64 65

    // define the last tabbable element to complete tab cycle
66 67 68 69 70 71
        var last;
        if (focusableItems.length !== 0) {
            last = focusableItems.last();
        } else {
            last = $(closeButtonId);
        }
72 73

    // tab on last element in modal returns to the first one
74 75
        last.on('keydown', function(e) {
            var keyCode = e.keyCode || e.which;
76
      // 9 is the js keycode for tab
77 78 79 80 81
            if (!e.shiftKey && keyCode === 9) {
                e.preventDefault();
                $(closeButtonId).focus();
            }
        });
82 83

    // shift+tab on first element in modal returns to the last one
84 85
        $(closeButtonId).on('keydown', function(e) {
            var keyCode = e.keyCode || e.which;
86
      // 9 is the js keycode for tab
87 88 89 90 91
            if (e.shiftKey && keyCode == 9) {
                e.preventDefault();
                last.focus();
            }
        });
92 93

    // manage aria-hidden attrs, return focus to trigger on close
94 95 96 97 98
        $('#lean_overlay, ' + closeButtonId).click(function() {
            $(mainPageId).attr('aria-hidden', 'false');
            $(modalId).attr('aria-hidden', 'true');
            focusedElementBeforeModal.focus();
        });
99 100

    // get modal to exit on escape key
101 102
        $('.modal').on('keydown', function(e) {
            var keyCode = e.keyCode || e.which;
103
      // 27 is the javascript keycode for the ESC key
104 105 106 107 108
            if (keyCode === 27) {
                e.preventDefault();
                $(closeButtonId).click();
            }
        });
109 110 111 112 113

    // In IE, focus shifts to iframes when they load.
    // These lines ensure that focus is shifted back to the close button
    // in the case that a modal that contains an iframe is opened in IE.
    // see http://stackoverflow.com/questions/15792620/how-to-get-focus-back-for-parent-window-from-an-iframe-programmatically-in-javas
114 115 116 117 118 119 120
        var initialFocus = true;
        $(modalId).find('iframe').on('focus', function() {
            if (initialFocus) {
                $(closeButtonId).focus();
                initialFocus = false;
            }
        });
121
    });
122
};
123

124 125 126
// NOTE: This is a gross hack to make the skip links work for Webkit browsers
// see http://stackoverflow.com/questions/6280399/skip-links-not-working-in-chrome/12720183#12720183

127
// handle things properly for clicks
128 129
$('.nav-skip').click(function() {
    var href = $(this).attr('href');
130
    if (href) {
131
        $(href).attr('tabIndex', -1).focus();
132 133 134
    }
});
// and for the enter key
135
$('.nav-skip').keypress(function(e) {
136
    if (e.which == 13) {
137
        var href = $(this).attr('href');
138
        if (href) {
139
            $(href).attr('tabIndex', -1).focus();
140 141 142
        }
    }
});
143 144

// Creates a window level SR object that can be used for giving audible feedback to screen readers.
145
$(function() {
146 147 148
    var SRAlert;

    SRAlert = (function() {
149
        function SRAlert() {
150 151 152 153 154 155 156 157 158 159 160 161 162 163
            // This initialization sometimes gets done twice, so take to only create a single reader-feedback div.
            var readerFeedbackID = 'reader-feedback',
                $readerFeedbackSelector = $('#' + readerFeedbackID);

            if ($readerFeedbackSelector.length === 0) {
                edx.HtmlUtils.append(
                    $('body'),
                    edx.HtmlUtils.interpolateHtml(
                        edx.HtmlUtils.HTML('<div id="{readerFeedbackID}" class="sr" aria-live="polite"></div>'),
                        {readerFeedbackID: readerFeedbackID}
                    )
                );
            }
            this.el = $('#' + readerFeedbackID);
164
        }
165

166
        SRAlert.prototype.clear = function() {
167
            edx.HtmlUtils.setHtml(this.el, '');
168
        };
169

170
        SRAlert.prototype.readElts = function(elts) {
171
            var texts = [];
172
            $.each(elts, function(idx, value) {
173
                texts.push($(value).html());
174
            });
175
            return this.readTexts(texts);
176
        };
177

178
        SRAlert.prototype.readText = function(text) {
179 180 181 182 183 184 185 186 187 188 189 190 191
            return this.readTexts([text]);
        };

        SRAlert.prototype.readTexts = function(texts) {
            var htmlFeedback = edx.HtmlUtils.HTML('');
            $.each(texts, function(idx, value) {
                htmlFeedback = edx.HtmlUtils.interpolateHtml(
                    edx.HtmlUtils.HTML('{previous_feedback}<p>{value}</p>\n'),
                    // "value" may be HTML, if an element is being passed
                    {previous_feedback: htmlFeedback, value: edx.HtmlUtils.HTML(value)}
                );
            });
            edx.HtmlUtils.setHtml(this.el, htmlFeedback);
192
        };
193

194
        return SRAlert;
195 196
    })();

197
    window.SR = new SRAlert();
198
});