-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnotifyPopupView.js
271 lines (246 loc) · 8.08 KB
/
notifyPopupView.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import Adapt from 'core/js/adapt';
import components from 'core/js/components';
import data from 'core/js/data';
import a11y from 'core/js/a11y';
import AdaptView from 'core/js/views/adaptView';
import Backbone from 'backbone';
export default class NotifyPopupView extends Backbone.View {
className() {
return `notify ${this.model.get('_classes') || ''}`;
}
attributes() {
return Object.assign({
role: 'dialog',
'aria-labelledby': 'notify-heading',
'aria-modal': 'true'
}, this.model.get('_attributes'));
}
events() {
return {
'click .js-notify-btn-alert': 'onAlertButtonClicked',
'click .js-notify-btn-prompt': 'onPromptButtonClicked',
'click .js-notify-close-btn': 'onCloseButtonClicked',
'click .js-notify-shadow-click': 'onShadowClicked'
};
}
initialize({ notify }) {
this.notify = notify;
_.bindAll(this, 'resetNotifySize', 'onKeyUp');
this.disableAnimation = Adapt.config.get('_disableAnimation') || false;
this.isOpen = false;
this.hasOpened = false;
this.setupEventListeners();
this.render();
}
setupEventListeners() {
this.listenTo(Adapt, {
remove: this.closeNotify,
'notify:resize': this.resetNotifySize,
'notify:cancel': this.cancelNotify,
'notify:close': this.closeNotify,
'device:resize': this.resetNotifySize
});
this.setupEscapeKey();
}
setupEscapeKey() {
$(window).on('keyup', this.onKeyUp);
}
onKeyUp(event) {
if (event.which !== 27) return;
event.preventDefault();
this.cancelNotify();
}
render() {
const data = this.model.toJSON();
const template = Handlebars.templates.notifyPopup;
// hide notify container
this.$el.css('visibility', 'hidden');
// attach popup + shadow
this.$el.html(template(data)).appendTo('.notify__popup-container');
// hide popup
this.$('.notify__popup').css('visibility', 'hidden');
// show notify container
this.$el.css('visibility', 'visible');
this.showNotify();
return this;
}
onAlertButtonClicked(event) {
event.preventDefault();
// tab index preservation, notify must close before subsequent callback is triggered
this.closeNotify();
Adapt.trigger(this.model.get('_callbackEvent'), this);
}
onPromptButtonClicked(event) {
event.preventDefault();
// tab index preservation, notify must close before subsequent callback is triggered
this.closeNotify();
Adapt.trigger($(event.currentTarget).attr('data-event'), this);
}
onCloseButtonClicked(event) {
event.preventDefault();
// tab index preservation, notify must close before subsequent callback is triggered
this.cancelNotify();
}
onShadowClicked(event) {
event.preventDefault();
if (this.model.get('_closeOnShadowClick') === false) return;
this.cancelNotify();
}
cancelNotify() {
if (this.model.get('_isCancellable') === false) return;
// tab index preservation, notify must close before subsequent callback is triggered
this.closeNotify();
Adapt.trigger('notify:cancelled', this);
}
resetNotifySize() {
if (!this.hasOpened) return;
this.resizeNotify();
}
resizeNotify() {
const windowHeight = $(window).height();
const notifyHeight = this.$('.notify__popup-inner').outerHeight();
const isFullWindow = (notifyHeight >= windowHeight);
this.$('.notify__popup').css({
height: isFullWindow ? '100%' : 'auto',
top: isFullWindow ? 0 : '',
'margin-top': isFullWindow ? '' : -(notifyHeight / 2),
'overflow-y': isFullWindow ? 'scroll' : '',
'-webkit-overflow-scrolling': isFullWindow ? 'touch' : ''
});
}
async showNotify() {
this.isOpen = true;
await this.addSubView();
// Add to the list of open popups
this.notify.stack.push(this);
// Keep focus from previous action
this.$previousActiveElement = $(document.activeElement);
Adapt.trigger('notify:opened', this);
this.$el.imageready(this.onLoaded.bind(this));
}
onLoaded() {
if (this.disableAnimation) {
this.$('.notify__shadow').css('display', 'block');
} else {
this.$('.notify__shadow').velocity({ opacity: 0 }, { duration: 0 }).velocity({ opacity: 1 }, {
duration: 400,
begin: () => {
this.$('.notify__shadow').css('display', 'block');
}
});
}
this.resizeNotify();
if (this.disableAnimation) {
this.$('.notify__popup').css('visibility', 'visible');
this.onOpened();
} else {
this.$('.notify__popup').velocity({ opacity: 0 }, { duration: 0 }).velocity({ opacity: 1 }, {
duration: 400,
begin: () => {
// Make sure to make the notify visible and then set
// focus, disabled scroll and manage tabs
this.$('.notify__popup').css('visibility', 'visible');
this.onOpened();
}
});
}
}
onOpened() {
$.inview();
this.hasOpened = true;
// Allows popup manager to control focus
a11y.popupOpened(this.$el);
a11y.scrollDisable('body');
$('html').addClass('notify');
// Set focus to first accessible element
a11y.focusFirst(this.$('.notify__popup'), { defer: false });
}
async addSubView() {
this.subView = this.model.get('_view');
if (this.model.get('_shouldRenderId') && this.model.get('_id')) {
// Automatically render the specified id
const model = data.findById(this.model.get('_id'));
const View = components.getViewClass(model);
this.subView = new View({ model });
}
if (!this.subView) return;
this.subView.$el.on('resize', this.resetNotifySize);
this.$('.notify__content-inner').append(this.subView.$el);
if (!(this.subView instanceof AdaptView) || this.subView.model.get('_isReady')) return;
// Wait for the AdaptView subview to be ready
return new Promise(resolve => {
const check = (model, value) => {
if (!value) return;
this.subView.model.off('change:_isReady', check);
resolve();
};
this.subView.model.on('change:_isReady', check);
});
}
closeNotify() {
// Make sure that only the top most notify is closed
const stackItem = this.notify.stack[this.notify.stack.length - 1];
if (this !== stackItem) return;
this.notify.stack.pop();
// Prevent from being invoked multiple times - see https://github.com/adaptlearning/adapt_framework/issues/1659
if (!this.isOpen) return;
this.isOpen = false;
// If closeNotify is called before showNotify has finished then wait
// until it's open.
if (this.hasOpened) {
this.onCloseReady();
return;
}
this.listenToOnce(Adapt, 'popup:opened', () => {
// Wait for popup:opened to finish processing
_.defer(this.onCloseReady.bind(this));
});
}
onCloseReady() {
if (this.disableAnimation) {
this.$('.notify__popup').css('visibility', 'hidden');
this.$el.css('visibility', 'hidden');
this.remove();
} else {
this.$('.notify__popup').velocity({ opacity: 0 }, {
duration: 400,
complete: () => {
this.$('.notify__popup').css('visibility', 'hidden');
}
});
this.$('.notify__shadow').velocity({ opacity: 0 }, {
duration: 400,
complete: () => {
this.$el.css('visibility', 'hidden');
this.remove();
}
});
}
a11y.scrollEnable('body');
$('html').removeClass('notify');
// Return focus to previous active element
a11y.popupClosed(this.$previousActiveElement);
// Return reference to the notify view
Adapt.trigger('notify:closed', this);
}
remove(...args) {
this.removeSubView();
$(window).off('keyup', this.onKeyUp);
super.remove(...args);
}
removeSubView() {
if (!this.subView) return;
this.subView.$el.off('resize', this.resetNotifySize);
if (this.subView instanceof AdaptView) {
// Clear up nested views and models
const views = [...this.subView.findDescendantViews(), this.subView];
views.forEach(view => {
view.model.set('_isReady', false);
view.remove();
});
} else {
this.subView.remove();
}
this.subView = null;
}
}