-
Notifications
You must be signed in to change notification settings - Fork 2
/
toast-er.html
executable file
·394 lines (352 loc) · 10.4 KB
/
toast-er.html
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!--
@license
Copyright 2015 Mason Louchart
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<link rel="import" href="../lite-signal/lite-signal.html">
<!--
`paper-toast` with some swagg features.
The toaster has a bin, where all signals are stored. This allows for multiple
signals to be received at once, and they will be fulfilled in order of their
receipt. Separate configurations are still maintained for each signal.
There are 4 types of toast (info, success, warning and error) that can be
selected using the `type` property. Each type sets a light at the bottom of the
toast according to the level of danger.
You can also set the blink level (slow, medium or fast) using the `blink` property.
The toast blinks more or less rapidly according to the defined level.
The toaster's position can be set using the `position` property.
Possible values include bottom-left (default), bottom-right, top-left and
top-right.
<br>
### Example
Just put the tag in the index page...
<toast-er></toast-er>
<toast-er duration="5000"></toast-er>
<toast-er type="info"></toast-er>
<toast-er type="warning" blink="medium"></toast-er>
<toast-er position="top-right"></toast-er>
...and fire some events, that's all!
<script>
function someMethod() {
this.dispatchEvent(
new CustomEvent('lite-signal', {
bubbles: true,
compose: true, // to cross Shadow DOM boundaries,
detail: {
name: 'toaster-show',
data: {
text: 'You are not allowed to do this.',
type: 'error',
duration: 5000,
blink: 'fast',
position: 'top-right'
}
}
})
);
}
</script>
There are no required options excepted `text`!
<br><br>
## Audited events
Name | Description
:--------------------------|:----------
`lite-signal-toaster-show` | Shows the toast with received data
@customElement
@polymer
@demo demo/index.html
@blurb `paper-toast` with some swagg features.
@homepage https://masonlouchart.github.io/toast-er/
-->
<dom-module id="toast-er">
<template>
<style>
:host {
display: inline-block;
box-sizing: border-box;
position: fixed;
}
:host.top paper-toast {
top: 0;
transform: translateY(-100px);
}
:host.top paper-toast.paper-toast-open {
top: 12px;
transform: translateY(0px);
}
#button {
position: absolute;
top: 4px; bottom: 4px; right: 0;
z-index: 1;
}
#underline {
position: absolute;
bottom: 0; left: 0; right: 0;
height: 4px;
}
.info {
background-color: rgb(0, 186, 255);
}
.success {
background-color: rgb(11, 219, 0);
}
.warning {
background-color: rgb(255, 168, 0);
}
.error {
background-color: rgb(251, 0, 0);
}
@-webkit-keyframes cooking {
from {
opacity: 1;
}
50% {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes cooking {
from {
opacity: 1;
}
50% {
opacity: 0;
}
to {
opacity: 1;
}
}
.slow {
-webkit-animation: cooking 3s linear 0.2s infinite;
animation: cooking 3s linear 0.2s infinite;
}
.medium {
-webkit-animation: cooking 2s linear 0.2s infinite;
animation: cooking 2s linear 0.2s infinite;
}
.fast {
-webkit-animation: cooking 1s linear 0.2s infinite;
animation: cooking 1s linear 0.2s infinite;
}
</style>
<paper-toast id="toast">
<paper-button id="button" on-tap="_buttonTap" hidden></paper-button>
<span id="underline"></span>
</paper-toast>
<lite-signal on-lite-signal-toaster-show="_putInStack"></lite-signal>
</template>
<script>
class Toaster extends Polymer.Element {
static get is() {
return 'toast-er';
}
static get properties() {
return {
/**
* The text shown by the toast.
*
* @type {String}
*/
text: {
type: String
},
/**
* The text shown on the button. If the text is empty the button is
* hidden.
*
* @type {String}
*/
buttonText: {
type: String,
value: ''
},
/**
* Event to fire when the button is pressed.
*
* @type {String}
* @default button-pressed
*/
buttonEvent: {
type: String,
value: 'button-pressed'
},
/**
* Duration of displaying.
*
* @type {Number}
* @default 3000
*/
duration: {
type: Number,
value: 3000
},
/**
* Defines the color of the toast underline. (success | info | warning | error)
* <ul>
* <li>success = color #0bdb00 (flashy green)</li>
* <li>info = color #00baff (cyan)</li>
* <li>warning = color #ffa800 (orange)</li>
* <li>error = color #fb0000 (red)</li>
* </ul>
*
* @type {String}
*/
type: {
type: String
},
/**
* Defines the speed of blinking of the toast's underline.
* (slow | medium | fast)
* <ul>
* <li>slow = transition in 3 sec</li>
* <li>medium = transition in 2 sec</li>
* <li>fast = transition in 1 sec</li>
* </ul>
*
* @type {String}
*/
blink: {
type: String
},
/**
* Sets the toaster position.
* (top-right | top-left | bottom-right | bottom-left)
*
* @type {String}
* @default bottom-left
*/
position: {
type: String
},
/**
* Stack of messages.
*
* @type {Array}
* @default []
*/
_stack: {
type: Array,
value: function() {
return [];
}
}
}
}
/**
* Shows the toast then `_shift` the stack.
*
* @param {Object} event lite-signal-toaster-show
* @param {Object} message to display can contains `text` {String},
* `type` {String}, `duration` {Number}, `blink` {String} and `position` {String}
*/
_putInStack(event, message) {
let text = message.text || this.text;
if (text && text.trim().length) {
this.push('_stack', message);
this._shift();
}
}
_buttonTap(event) {
this.dispatchEvent(new CustomEvent(
'toaster-' + event.target.buttonEvent, {
bubbles: true,
composed: true
}
));
this.$.toast.close();
this._shift();
}
/**
* Shows message by unstacking of toasts.
*/
_shift() {
// Turn off the toaster if the queue is empty.
if (!this._stack.length) {
setTimeout(() => {
this._hideToast();
}, 1000);
return;
}
// Get configuration of the toast.
let msg = this._stack[0];
let buttonText = msg.buttonText || this.buttonText;
let hasButton = buttonText.length != 0;
let duration = msg.duration || (hasButton && this.duration < 10000 ? 10000 : this.duration);
let blink = msg.blink || this.blink;
let position = msg.position || this.position;
let text = msg.text || this.text;
let type = msg.type || this.type;
let toast = this.$.toast;
// Shows the toast.
if (!toast.opened) {
let button = this.$.button;
this.shift('_stack');
toast.duration = duration;
toast.text = text;
button.hidden = !hasButton;
button.buttonEvent = msg.buttonEvent || this.buttonEvent;
button.textContent = buttonText;
this._displayToast(type, blink, position);
setTimeout(() => {
toast.show();
}, 100);
}
// Queue the next toast.
setTimeout(() => {
this._shift();
}, duration + 500);
}
/**
* Sets the toaster bottom color according to the given type, defines the
* blinking speed and sets the toast position.
*
* @param {String} type (info | success | warning | error) defines the toaster bottom color
* @param {String} blink (slow | medium | fast) defines the blinking speed
* @param {String} position (top-left, top-right, bottom-left, bottom-right) defines the toaster position
*/
_displayToast(type, blink, position) {
let underline = this.$.underline;
let toast = this.$.toast;
// Set the underline type.
type = type && type.toLowerCase() || '';
underline.classList.toggle('info', type === 'info');
underline.classList.toggle('success', type === 'success');
underline.classList.toggle('warning', type === 'warning');
underline.classList.toggle('error', type === 'error');
// Set the blink speed.
blink = blink && blink.toLowerCase() || '';
underline.classList.toggle('slow', blink === 'slow');
underline.classList.toggle('medium', blink === 'medium');
underline.classList.toggle('fast', blink === 'fast');
// Set the toaster position.
position = position && position.toLowerCase().split('-') || [];
toast.horizontalAlign = position[1] || 'left';
toast.verticalAlign = position[0] || 'bottom';
this.classList.toggle('top', toast.verticalAlign === 'top');
}
/**
* Disables toaster bottom color and blinking.
*/
_hideToast() {
let underline = this.$.underline;
underline.classList.remove('slow');
underline.classList.remove('medium');
underline.classList.remove('fast');
}
}
customElements.define(Toaster.is, Toaster);
</script>
</dom-module>