forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrailsFlashNotificationsHelper.js
More file actions
176 lines (150 loc) · 5.19 KB
/
Copy pathrailsFlashNotificationsHelper.js
File metadata and controls
176 lines (150 loc) · 5.19 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import I18n from 'i18n!shared.flash_notices'
import $ from 'jquery'
import _ from 'underscore'
import preventDefault from 'compiled/fn/preventDefault'
import htmlEscape from 'str/htmlEscape'
import 'jqueryui/effects/drop'
import 'vendor/jquery.cookie'
class RailsFlashNotificationsHelper {
constructor() {
this.holder = null;
this.screenreader_holder = null;
}
initHolder() {
const $current_holders = $('#flash_message_holder');
if ($current_holders.length === 0) {
this.holder = null;
} else {
this.holder = $current_holders[0];
$(this.holder).on('click', '.close_link', (event) => {
event.preventDefault();
});
$(this.holder).on('click', 'li', (event) => {
if ($(event.currentTarget).hasClass('no_close')) {
return;
}
if ($(event.currentTarget).hasClass('unsupported_browser')) {
$.cookie('unsupported_browser_dismissed');
}
$(event.currentTarget).stop(true, true).remove();
});
}
}
holderReady() {
return this.holder != null;
}
createNode(type, content, timeout, cssOptions = {}) {
if(this.holderReady()) {
const node = this.generateNodeHTML(type, content);
$(node).appendTo($(this.holder)).
css(_.extend({zIndex: 2}, cssOptions)).
show('fast').
delay(timeout || 7000).
fadeOut('slow', function() { $(this).remove(); });
}
}
generateNodeHTML(type, content) {
const icon = this.getIconType(type);
// See generateScreenreaderNodeHtml for SR features
return `
<li class="ic-flash-${htmlEscape(type)}" aria-hidden="true">
<div class="ic-flash__icon">
<i class="icon-${htmlEscape(icon)}"></i>
</div>
${this.escapeContent(content)}
<button type="button" class="Button Button--icon-action close_link">
<i class="icon-x"></i>
</button>
</li>
`;
}
getIconType(type) {
if (type === 'success') {
return 'check';
} else if (type === 'warning' || type === 'error') {
return 'warning';
} else {
return 'info';
}
}
initScreenreaderHolder() {
const $current_screenreader_holders = $('#flash_screenreader_holder');
if ($current_screenreader_holders.length === 0) {
this.screenreader_holder = null;
} else {
this.screenreader_holder = $current_screenreader_holders[0];
this.setScreenreaderAttributes();
}
}
screenreaderHolderReady() {
return this.screenreader_holder != null;
}
createScreenreaderNode(content, closable = true) {
if (this.screenreaderHolderReady()) {
const node = $(this.generateScreenreaderNodeHTML(content, closable));
node.appendTo($(this.screenreader_holder));
window.setTimeout( () => {
// Accessibility attributes must be removed for the deletion of the node
// and then reapplied because JAWS/IE will not respect the
// "aria-relevant" attribute and read when the node is deleted if
// the attributes are in place
this.resetScreenreaderAttributes();
node.remove();
this.setScreenreaderAttributes();
}, 10000);
}
}
setScreenreaderAttributes() {
if(this.screenreaderHolderReady()) {
// These attributes are added for accessibility. However, adding them
// to the DOM at load causes some screenreaders to read "alert" when
// the page is loaded. That is why these attributes are added here.
$(this.screenreader_holder).attr('role', 'alert');
$(this.screenreader_holder).attr('aria-live', 'assertive');
$(this.screenreader_holder).attr('aria-relevant', 'additions');
$(this.screenreader_holder).attr('class', 'screenreader-only');
$(this.screenreader_holder).attr('aria-atomic', 'false');
}
}
resetScreenreaderAttributes() {
if(this.screenreaderHolderReady()) {
$(this.screenreader_holder).removeAttr('role');
$(this.screenreader_holder).removeAttr('aria-live');
$(this.screenreader_holder).removeAttr('aria-relevant');
$(this.screenreader_holder).removeAttr('class');
$(this.screenreader_holder).removeAttr('aria-atomic');
}
}
createScreenreaderNodeExclusive(content) {
if (this.screenreaderHolderReady()) {
this.screenreader_holder.innerHTML = '';
let node = $(this.generateScreenreaderNodeHTML(content, false));
node.appendTo($(this.screenreader_holder));
}
}
generateScreenreaderNodeHTML(content, closable) {
let closeContent;
if(closable) {
closeContent = I18n.t('Close');
} else {
closeContent = '';
}
return `
<span>
${this.escapeContent(content)}
${htmlEscape(closeContent)}
</span>
`;
}
/*
xsslint safeString.method escapeContent
*/
escapeContent(content) {
if(content.hasOwnProperty('html')) {
return content.html;
} else {
return htmlEscape(content);
}
}
}
export default RailsFlashNotificationsHelper