-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash-message-toast.js
132 lines (126 loc) · 4.96 KB
/
flash-message-toast.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
export const name = 'flash-message-toast';
(function() {
export const FlashMessage = {};
var styleMap = {
topRight: 'topAlign rightAlign',
topLeft: 'topAlign leftAlign',
topCenter: 'topAlign centerAlign',
bottomRight: 'bottomAlign rightAlign',
bottomLeft: 'bottomAlign leftAlign',
bottomCenter: 'bottomAlign centerAlign',
success: 'flashMessageSuccess',
info: 'flashMessageToast',
error: 'flashMessageError',
warning: 'flashMessageWarning'
};
var animationStyleMap = {
topCenter: {
show: 'flashMessageSlideDownToShow',
hide: 'flashMessageSlideUpToHide'
},
topRight: {
show: 'flashMessageSlideLeftToShow',
hide: 'flashMessageSlideRightToHide'
},
topLeft: {
show: 'flashMessageSlideRightToShow',
hide: 'flashMessageSlideLeftToHide'
},
bottomCenter: {
show: 'flashMessageSlideUpToShow',
hide: 'flashMessageSlideDownToHide'
},
bottomRight: {
show: 'flashMessageSlideLeftToShow',
hide: 'flashMessageSlideRightToHide'
},
bottomLeft: {
show: 'flashMessageSlideRightToShow',
hide: 'flashMessageSlideLeftToHide'
}
};
//align: topRight topLeft topCenter bottomRight bottomLeft bottomCenter. default topCenter
//autoHide: boolean
//hideDelay: ms
//text: string
//isHtml: boolean
//type: string[info, error, success, warning]
//beforeShow: function
//afterShow: function
//beforeHide: function
//afterHide: function
//showCloseButton: boolean default false
FlashMessage.show = function(obj) {
var css = getFlashMessageCssClass(obj);
var id = new Date().getTime() + 'flashMessage';
var hideDelay = obj.hideDelay || 2000;
var text = obj.text || 'hello world';
var type = styleMap[obj.type] || styleMap.info;
var autoHide = obj.autoHide == undefined ? true : obj.autoHide;
var html = [];
html.push('<div id="', id, '" class="flashMessageToast flashMessageOverlay ', css, '" data-align-type="', obj.align, '">');
html.push('<div class="flashMessageWrapper">');
html.push('<div class="flashMessage ', type);
if (obj.showCloseButton) {
html.push(' hasCloseButton');
}
html.push('">');
if (obj.showCloseButton) {
html.push('<a class="flashMessageClose" data-id="', id, '" tabindex="0"></a>');
}
if (obj.isHtml) {
html.push(text);
} else {
html.push($('<div/>').text(text).html());
}
html.push('</div>');
html.push('</div>');
html.push('</div>');
if (obj.beforeShow) {
obj.beforeShow.call(this, id, obj);
}
var a = $('body').append(html.join(''));
$('#' + id + ' .flashMessageClose').on('click', function(e) {
e.preventDefault();
FlashMessage.hide(id, obj);
});
var animationDuration = parseFloat($('#' + id + '> .flashMessageWrapper').css('animationDuration').substring(0, $('#' + id + '> .flashMessageWrapper').css('animationDuration').length - 1)) * 1000;
if (obj.afterShow) {
setTimeout(function() {
obj.afterShow.call(this, id, obj);
}, animationDuration);
}
if (autoHide) {
setTimeout(function() {
FlashMessage.hide(id, obj);
}, hideDelay + animationDuration);
}
return id;
};
FlashMessage.hide = function(id, obj) {
if (!$('#' + id).hasClass('lockFlashMessageToast')) {
$('#' + id).addClass('lockFlashMessageToast');
if (obj.beforeHide) {
obj.beforeHide.call(this, id, obj);
}
var animationDuration = parseFloat($('#' + id + '> .flashMessageWrapper').css('animationDuration').substring(0, $('#' + id + '> .flashMessageWrapper').css('animationDuration').length - 1)) * 1000;
var alignType = $('#' + id).data('align-type');
if (alignType) {
var removeClass = animationStyleMap[alignType] ? animationStyleMap[alignType].show : null;
var addClass = animationStyleMap[alignType] ? animationStyleMap[alignType].hide : null;
$('#' + id).removeClass(removeClass).addClass(addClass);
}
setTimeout(function() {
$('#' + id).remove();
if (obj.afterHide) {
obj.afterHide.call(this, id, obj);
}
}, animationDuration);
}
};
function getFlashMessageCssClass(obj) {
var alignCss = styleMap[obj.align] || styleMap['topCenter'];
var animationCss = animationStyleMap[obj.align] ? animationStyleMap[obj.align].show : animationStyleMap['topCenter'].show;
return alignCss + ' ' + animationCss;
}
})();