-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtoast.js
99 lines (80 loc) · 2.28 KB
/
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
/**
* A plugin for Framework7 to show black little toasts
*
* @author www.timo-ernst.net
* @license MIT
*/
Framework7.prototype.plugins.toast = function (app, globalPluginParams) {
'use strict';
var Toast = function (text, iconhtml, options) {
var self = this,
$$ = Dom7,
$box;
function hideBox($curbox) {
if ($curbox) {
$curbox.removeClass('fadein').transitionEnd(function () {
$curbox.remove();
if (options && typeof options.onHide == 'function') options.onHide();
});
}
}
function isString(obj) {
return toString.call(obj) === '[object String]';
}
this.show = function (message) {
var clientLeft,
$curbox,
html = [];
// Remove old toasts first if there are still any
$$('.toast-container').off('click').off('transitionEnd').remove();
$box = $$('<div class="toast-container show">');
// Add content
if (isString(iconhtml) && iconhtml.length > 0) {
html.push(
'<div class="toast-icon">' +
iconhtml +
'</div>'
);
}
if (isString(message) && message.length > 0) {
text = message;
}
if (isString(text) && text.length > 0) {
html.push(
'<div class="toast-msg">' +
text +
'</div>'
);
}
$box.html(
html.join('')
);
// Add to DOM
clientLeft = $box[0].clientLeft;
$$('body').append($box);
$box.css('margin-top', $box.outerHeight() / -2 + 'px')
.css('margin-left', $box.outerWidth() / -2 + 'px');
// Hide box on click
$box.click(function () {
hideBox($box);
});
// Dirty hack to cause relayout xD
clientLeft = $box[0].clientLeft;
// Fade in toast
$box.addClass('fadein');
var duration = options && options.duration ? options.duration : 1500;
// Automatically hide box after few seconds
$curbox = $box;
setTimeout(function () {
hideBox($curbox);
}, duration);
};
this.hide = function() {
hideBox($box);
};
return this;
};
app.toast = function (text, iconhtml, options) {
return new Toast(text, iconhtml, options);
};
};