forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.js
More file actions
147 lines (129 loc) · 4.55 KB
/
dialog.js
File metadata and controls
147 lines (129 loc) · 4.55 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(function ($) {
var focusableElementsSelector = "a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]";
// Based on the incredible accessible modal dialog.
window.AccessibleDialog = function(modalDiv, dialogRole, title, descDiv, closeButtonLabel, width, fullscreen, escapeHook) {
this.title = title;
this.closeButtonLabel = closeButtonLabel;
this.escapeHook = escapeHook;
this.baseId = $(modalDiv).attr('id') || Math.floor(Math.random() * 1000000000).toString();
var thisObj = this;
var modal = modalDiv;
this.modal = modal;
modal.css({
'width': width || '50%',
'top': (fullscreen ? '0' : '25%')
});
modal.addClass('modalDialog');
if (!fullscreen) {
var closeButton = $('<button>',{
'class': 'modalCloseButton',
'title': thisObj.closeButtonLabel,
'aria-label': thisObj.closeButtonLabel
}).text('X');
closeButton.keydown(function (event) {
// Space key down
if (event.which === 32) {
thisObj.hide();
}
}).click(function () {
thisObj.hide();
});
var titleH1 = $('<h1></h1>');
titleH1.attr('id', 'modalTitle-' + this.baseId);
titleH1.css('text-align', 'center');
titleH1.text(title);
descDiv.attr('id', 'modalDesc-' + this.baseId);
modal.attr({
'aria-labelledby': 'modalTitle-' + this.baseId,
'aria-describedby': 'modalDesc-' + this.baseId
});
modal.prepend(titleH1);
modal.prepend(closeButton);
}
modal.attr({
'aria-hidden': 'true',
'role': dialogRole
});
modal.keydown(function (event) {
// Escape
if (event.which === 27) {
if (thisObj.escapeHook) {
thisObj.escapeHook(event, this);
}
else {
thisObj.hide();
event.preventDefault();
}
}
// Tab
else if (event.which === 9) {
// Manually loop tab navigation inside the modal.
var parts = modal.find('*');
var focusable = parts.filter(focusableElementsSelector).filter(':visible');
if (focusable.length === 0) {
return;
}
var focused = $(':focus');
var currentIndex = focusable.index(focused);
if (event.shiftKey) {
// If backwards from first element, go to last.
if (currentIndex === 0) {
focusable.get(focusable.length - 1).focus();
event.preventDefault();
}
}
else {
if (currentIndex === focusable.length - 1) {
focusable.get(0).focus();
event.preventDefault();
}
}
}
event.stopPropagation();
});
$('body > *').not('.modalOverlay').not('.modalDialog').attr('aria-hidden', 'false');
};
AccessibleDialog.prototype.show = function () {
if (!this.overlay) {
// Generate overlay.
var overlay = $('<div></div>').attr({
'class': 'modalOverlay',
'tabindex': '-1'
});
this.overlay = overlay;
$('body').append(overlay);
// Keep from moving focus out of dialog when clicking outside of it.
overlay.on('mousedown.accessibleModal', function (event) {
event.preventDefault();
});
}
$('body > *').not('.modalOverlay').not('.modalDialog').attr('aria-hidden', 'true');
this.overlay.css('display', 'block');
this.modal.css('display', 'block');
this.modal.attr({
'aria-hidden': 'false',
'tabindex': '-1'
});
this.focusedElementBeforeModal = $(':focus');
var focusable = this.modal.find("*").filter(focusableElementsSelector).filter(':visible');
if (focusable.length === 0) {
this.focusedElementBeforeModal.blur();
}
var thisObj = this;
setTimeout(function () {
// originally set focus on the first focusable element
// thisObj.modal.find('button.modalCloseButton').first().focus();
// but setting focus on dialog seems to provide more reliable access to ALL content within
thisObj.modal.focus();
}, 300);
};
AccessibleDialog.prototype.hide = function () {
if (this.overlay) {
this.overlay.css('display', 'none');
}
this.modal.css('display', 'none');
this.modal.attr('aria-hidden', 'true');
$('body > *').not('.modalOverlay').not('.modalDialog').attr('aria-hidden', 'false');
this.focusedElementBeforeModal.focus();
};
})(jQuery);