-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnotifyPushView.js
121 lines (96 loc) · 2.8 KB
/
notifyPushView.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
import Adapt from 'core/js/adapt';
export default class NotifyPushView extends Backbone.View {
className() {
const classes = [
'notify-push',
this.model.get('_classes'),
this.model.get('_type') === 'a11y-push' && 'aria-label'
].filter(Boolean).join(' ');
return classes;
}
attributes() {
return {
role: 'dialog',
'aria-labelledby': 'notify-push-heading',
'aria-modal': 'false'
};
}
initialize() {
this.listenTo(Adapt, {
'notify:pushShown notify:pushRemoved': this.updateIndexPosition,
remove: this.remove
});
this.listenTo(this.model.collection, {
remove: this.updateIndexPosition,
'change:_index': this.updatePushPosition
});
this.preRender();
this.render();
}
events() {
return {
'click .js-notify-push-close-btn': 'closePush',
'click .js-notify-push-inner': 'triggerEvent'
};
}
preRender() {
this.hasBeenRemoved = false;
}
render() {
const data = this.model.toJSON();
const template = Handlebars.templates.notifyPush;
this.$el.html(template(data)).appendTo('.notify__push-container');
_.defer(this.postRender.bind(this));
return this;
}
postRender() {
this.$el.addClass('is-active');
_.delay(this.closePush.bind(this), this.model.get('_timeout'));
Adapt.trigger('notify:pushShown');
}
closePush(event) {
if (event) {
event.preventDefault();
}
// Check whether this view has been removed as the delay can cause it to be fired twice
if (this.hasBeenRemoved === false) {
this.hasBeenRemoved = true;
this.$el.removeClass('is-active');
_.delay(() => {
this.model.collection.remove(this.model);
Adapt.trigger('notify:pushRemoved', this);
this.remove();
}, 600);
}
}
triggerEvent(event) {
Adapt.trigger(this.model.get('_callbackEvent'));
this.closePush();
}
updateIndexPosition() {
if (this.hasBeenRemoved) return;
const models = this.model.collection.models;
models.forEach((model, index) => {
if (!model.get('_isActive')) return;
model.set('_index', index);
this.updatePushPosition();
});
}
updatePushPosition() {
if (this.hasBeenRemoved) {
return;
}
if (typeof this.model.get('_index') !== 'undefined') {
const elementHeight = this.$el.height();
const offset = 20;
const navigationHeight = $('.nav').height();
const currentIndex = this.model.get('_index');
let flippedIndex = (currentIndex === 0) ? 1 : 0;
if (this.model.collection.where({ _isActive: true }).length === 1) {
flippedIndex = 0;
}
const positionLowerPush = (elementHeight + offset) * flippedIndex + navigationHeight + offset;
this.$el.css('top', positionLowerPush);
}
}
}