-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathnotifyView.js
258 lines (209 loc) · 7.45 KB
/
notifyView.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
define([
'core/js/adapt'
], function(Adapt) {
var NotifyView = Backbone.View.extend({
className: function() {
var classes = 'notify ';
classes += (this.model.get('_classes') || '');
return classes;
},
attributes: {
'role': 'dialog',
'aria-labelledby': 'notify-heading',
'aria-modal': 'true'
},
disableAnimation: false,
isOpen: false,
hasOpened: false,
initialize: function() {
_.bindAll(this, 'resetNotifySize');
this.disableAnimation = Adapt.config.has('_disableAnimation') ? Adapt.config.get('_disableAnimation') : false;
this.setupEventListeners();
this.render();
},
setupEventListeners: function() {
this.listenTo(Adapt, {
'remove page:scrollTo': this.closeNotify,
'notify:resize': this.resetNotifySize,
'notify:cancel': this.cancelNotify,
'notify:close': this.closeNotify,
'device:resize': this.resetNotifySize
});
this._onKeyUp = this.onKeyUp.bind(this);
this.setupEscapeKey();
},
setupEscapeKey: function() {
$(window).on('keyup', this._onKeyUp);
},
onKeyUp: function(event) {
if (event.which !== 27) return;
event.preventDefault();
this.cancelNotify();
},
events: {
'click .js-notify-btn-alert': 'onAlertButtonClicked',
'click .js-notify-btn-prompt': 'onPromptButtonClicked',
'click .js-notify-close-btn': 'onCloseButtonClicked',
'click .js-notify-shadow-click': 'onShadowClicked'
},
render: function() {
var data = this.model.toJSON();
var template = Handlebars.templates.notify;
// hide notify container
this.$el.css('visibility', 'hidden');
// attach popup + shadow
this.$el.html(template(data)).prependTo('body');
// hide popup
this.$('.notify__popup').css('visibility', 'hidden');
// show notify container
this.$el.css('visibility', 'visible');
this.showNotify();
return this;
},
onAlertButtonClicked: function(event) {
event.preventDefault();
// tab index preservation, notify must close before subsequent callback is triggered
this.closeNotify();
Adapt.trigger(this.model.get('_callbackEvent'), this);
},
onPromptButtonClicked: function(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: function(event) {
event.preventDefault();
// tab index preservation, notify must close before subsequent callback is triggered
this.cancelNotify();
},
onShadowClicked: function(event) {
event.preventDefault();
if (this.model.get('_closeOnShadowClick') === false) return;
this.cancelNotify();
},
cancelNotify: function() {
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: function() {
if (!this.hasOpened) return;
$('.notify__popup').removeAttr('style').css({ visibility: 'visible', opacity: 1 });
this.resizeNotify();
},
resizeNotify: function() {
var windowHeight = $(window).height();
var notifyHeight = this.$('.notify__popup').outerHeight();
if (notifyHeight >= windowHeight) {
this.$('.notify__popup').css({
'height': '100%',
'top': 0,
'overflow-y': 'scroll',
'-webkit-overflow-scrolling': 'touch'
});
} else {
this.$('.notify__popup').css({
'margin-top': -(notifyHeight / 2)
});
}
},
showNotify: function() {
this.isOpen = true;
this.addSubView();
// Keep focus from previous action
this.$previousActiveElement = $(document.activeElement);
Adapt.trigger('notify:opened', this);
this.$el.imageready(this.onLoaded.bind(this));
},
onLoaded: function() {
if (this.disableAnimation) {
this.$('.notify__shadow').css('display', 'block');
} else {
this.$('.notify__shadow').velocity({ opacity: 0 }, { duration: 0 }).velocity({ opacity: 1 }, { duration: 400,
begin: function() {
this.$('.notify__shadow').css('display', 'block');
}.bind(this) });
}
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: function() {
// Make sure to make the notify visible and then set
// focus, disabled scroll and manage tabs
this.$('.notify__popup').css('visibility', 'visible');
this.onOpened();
}.bind(this) });
}
},
onOpened: function() {
this.hasOpened = true;
// Allows popup manager to control focus
Adapt.a11y.popupOpened(this.$('.notify__popup'));
Adapt.a11y.scrollDisable('body');
$('html').addClass('notify');
// Set focus to first accessible element
Adapt.a11y.focusFirst(this.$('.notify__popup'), { defer: false });
},
addSubView: function() {
this.subView = this.model.get('_view');
if (!this.subView) return;
this.subView.$el.on('resize', this.resetNotifySize);
this.$('.notify__content-inner').append(this.subView.$el);
},
closeNotify: function (event) {
// 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.listenToOnce(Adapt, 'popup:opened', function() {
// Wait for popup:opened to finish processing
_.defer(this.onCloseReady.bind(this));
});
} else {
this.onCloseReady();
}
},
onCloseReady: function() {
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: function() {
this.$('.notify__popup').css('visibility', 'hidden');
}.bind(this) });
this.$('.notify__shadow').velocity({ opacity: 0 }, { duration: 400,
complete: function() {
this.$el.css('visibility', 'hidden');
this.remove();
}.bind(this) });
}
Adapt.a11y.scrollEnable('body');
$('html').removeClass('notify');
// Return focus to previous active element
Adapt.a11y.popupClosed(this.$previousActiveElement);
// Return reference to the notify view
Adapt.trigger('notify:closed', this);
},
remove: function() {
this.removeSubView();
$(window).off('keyup', this._onKeyUp);
Backbone.View.prototype.remove.apply(this, arguments);
},
removeSubView: function() {
if (!this.subView) return;
this.subView.$el.off('resize', this.resetNotifySize);
this.subView.remove();
this.subView = null;
}
});
return NotifyView;
});