1 | Materialize.toast = function (message, displayLength, className, completeCallback) {
|
2 | className = className || "";
|
3 |
|
4 | var container = document.getElementById('toast-container');
|
5 |
|
6 |
|
7 | if (container === null) {
|
8 |
|
9 | container = document.createElement('div');
|
10 | container.id = 'toast-container';
|
11 | document.body.appendChild(container);
|
12 | }
|
13 |
|
14 |
|
15 | var newToast = createToast(message);
|
16 |
|
17 |
|
18 | if(message){
|
19 | container.appendChild(newToast);
|
20 | }
|
21 |
|
22 | newToast.style.opacity = 0;
|
23 |
|
24 |
|
25 | Vel(newToast, {translateY: '-35px', opacity: 1 }, {duration: 300,
|
26 | easing: 'easeOutCubic',
|
27 | queue: false});
|
28 |
|
29 |
|
30 | var timeLeft = displayLength;
|
31 | var counterInterval;
|
32 | if (timeLeft != null) {
|
33 | counterInterval = setInterval (function(){
|
34 | if (newToast.parentNode === null)
|
35 | window.clearInterval(counterInterval);
|
36 |
|
37 |
|
38 | if (!newToast.classList.contains('panning')) {
|
39 | timeLeft -= 20;
|
40 | }
|
41 |
|
42 | if (timeLeft <= 0) {
|
43 |
|
44 | Vel(newToast, {"opacity": 0, marginTop: '-40px'}, { duration: 375,
|
45 | easing: 'easeOutExpo',
|
46 | queue: false,
|
47 | complete: function(){
|
48 |
|
49 | if(typeof(completeCallback) === "function")
|
50 | completeCallback();
|
51 |
|
52 | this[0].parentNode.removeChild(this[0]);
|
53 | }
|
54 | });
|
55 | window.clearInterval(counterInterval);
|
56 | }
|
57 | }, 20);
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | function createToast(html) {
|
63 |
|
64 |
|
65 | var toast = document.createElement('div');
|
66 | toast.classList.add('toast');
|
67 | if (className) {
|
68 | var classes = className.split(' ');
|
69 |
|
70 | for (var i = 0, count = classes.length; i < count; i++) {
|
71 | toast.classList.add(classes[i]);
|
72 | }
|
73 | }
|
74 |
|
75 | if ( typeof HTMLElement === "object" ? html instanceof HTMLElement : html && typeof html === "object" && html !== null && html.nodeType === 1 && typeof html.nodeName==="string"
|
76 | ) {
|
77 | toast.appendChild(html);
|
78 | }
|
79 | else if (html instanceof jQuery) {
|
80 |
|
81 | toast.appendChild(html[0]);
|
82 | }
|
83 | else {
|
84 |
|
85 | toast.innerHTML = html;
|
86 | }
|
87 |
|
88 | var hammerHandler = new Hammer(toast, {prevent_default: false});
|
89 | hammerHandler.on('pan', function(e) {
|
90 | var deltaX = e.deltaX;
|
91 | var activationDistance = 80;
|
92 |
|
93 |
|
94 | if (!toast.classList.contains('panning')){
|
95 | toast.classList.add('panning');
|
96 | }
|
97 |
|
98 | var opacityPercent = 1-Math.abs(deltaX / activationDistance);
|
99 | if (opacityPercent < 0)
|
100 | opacityPercent = 0;
|
101 |
|
102 | Vel(toast, {left: deltaX, opacity: opacityPercent }, {duration: 50, queue: false, easing: 'easeOutQuad'});
|
103 |
|
104 | });
|
105 |
|
106 | hammerHandler.on('panend', function(e) {
|
107 | var deltaX = e.deltaX;
|
108 | var activationDistance = 80;
|
109 |
|
110 |
|
111 | if (Math.abs(deltaX) > activationDistance) {
|
112 | Vel(toast, {marginTop: '-40px'}, { duration: 375,
|
113 | easing: 'easeOutExpo',
|
114 | queue: false,
|
115 | complete: function(){
|
116 | if(typeof(completeCallback) === "function") {
|
117 | completeCallback();
|
118 | }
|
119 | toast.parentNode.removeChild(toast);
|
120 | }
|
121 | });
|
122 |
|
123 | } else {
|
124 | toast.classList.remove('panning');
|
125 |
|
126 | Vel(toast, { left: 0, opacity: 1 }, { duration: 300,
|
127 | easing: 'easeOutExpo',
|
128 | queue: false
|
129 | });
|
130 |
|
131 | }
|
132 | });
|
133 |
|
134 | return toast;
|
135 | }
|
136 | };
|