/
flash-message.js
185 lines (138 loc) · 4.26 KB
/
flash-message.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
import Ember from 'ember';
import FlashMessage from 'ember-flash-messages/models/flash-message';
const {
Component,
RSVP,
computed,
run,
on
} = Ember;
export default Component.extend({
/* Options */
action: null,
className: 'flash-message',
content: null,
duration: null,
iconClassFormat: 'icon-{{type}}',
type: null,
message: computed({
get() {
return FlashMessage.create({
action: this.get('action'),
content: this.get('content'),
createdAt: this.get('createdAt'),
duration: this.get('duration'),
type: this.get('type'),
});
},
set(key, value) {
this.setProperties(value);
}
}),
/* Properties */
animationDuration: computed.oneWay('flashMessageQueue.animationDuration'),
attributeBindings: ['role'],
classNameBindings: ['className', 'typeClass', 'visible'],
createdAt: null,
dataTest: 'flash-message',
inQueue: computed.bool('parentView.isMessageQueueComponent'),
removeMessageAction: null,
role: 'alert',
tagName: 'dl',
visible: false,
iconClass: computed('iconClassFormat', 'type', function() {
const format = this.get('iconClassFormat');
return format.replace('{{type}}', this.get('type'));
}),
typeClass: computed('className', 'type', function() {
const { className, type } = this.getProperties(
[ 'className', 'type' ]
);
const affix = type ? '-' + type : '';
return `${className}${affix}`;
}),
/* Event handling */
click() {
/* Remove message visually... */
this.handleClick().then(() => {
const message = this.get('message') || this.get('attrs.message.value');
/* this.sendAction('action') is automatically run here */
/* ... Then remove message from queue(s) */
this.sendAction('removeMessageAction', message);
});
},
handleClick() {
const { inQueue, parentView } = this.getProperties(
[ 'inQueue', 'parentView' ]
);
return new RSVP.Promise((resolve /*, reject */) => {
/* If message is in the queue, see if the queue should remain visible... */
if (inQueue && parentView.getQueueLength() > 1) {
resolve();
} else {
this.setVisibility(false);
Ember.run.later(this, function() {
if (!inQueue) {
this.removeFromParent();
}
resolve();
}, this.get('animationDuration'));
}
});
},
/* Animation methods */
hide() {
this.$().slideUp(this.get('animationDuration'));
},
show() {
this.$().slideDown(this.get('animationDuration'));
},
setVisibility(shouldShow) {
var method = shouldShow ? 'show' : 'hide';
if (!this.get('isDestroying')) {
/* Enough time to invoke CSS transitions */
run.later(this, function() {
if (!this.get('isDestroying')) {
this.set('visible', shouldShow);
}
}, 100);
this[method]();
}
},
/* Private methods */
_hideEndingQueue: Ember.on('willInsertElement', function() {
const queue = this.get('flashMessageQueue');
/* If this message is in the timed queue we might
need to hide the message before it's removed from
the queue, but only if there are no other messages
in the queue. */
if (queue && this.get('message.duration') !== 0) {
queue.on('willHideQueue', this, function() {
const queueLength = queue.get('timedMessages.length');
/* If there is not another message queued, start
hiding the queue */
if (queueLength === 1) {
this.setVisibility(false);
}
/* However, check to see if another message has been
added in the interim and, if so, cancel the hiding of
the queue */
/* TODO - Remove 0.9, which allows for small margin for error */
run.later(this, function() {
if (queueLength > 1) {
this.setVisibility(true);
}
}, this.get('animationDuration') * 0.9);
});
}
}),
_showOnRender: on('didInsertElement', function() {
/* Assert the required properties are passed. Don't check
for the content property because this could be used as a
block helper */
this.setVisibility(true);
}),
_hideOnDestroy: on('willDestroyElement', function() {
this.setVisibility(false);
}),
});