Line | |
---|
1 | <script>
|
---|
2 | export default {
|
---|
3 | name: "Snackbar",
|
---|
4 | data() {
|
---|
5 | return {
|
---|
6 | content: null
|
---|
7 | }
|
---|
8 | },
|
---|
9 | computed: {
|
---|
10 | hasContent() {
|
---|
11 | return this.content !== null && this.content !== undefined && this.content.length > 0;
|
---|
12 | }
|
---|
13 | },
|
---|
14 | methods: {
|
---|
15 | setContent(content, timeout = 3000) {
|
---|
16 | this.content = content;
|
---|
17 | const that = this;
|
---|
18 |
|
---|
19 | setTimeout(function() {
|
---|
20 | that.content = null;
|
---|
21 | }, timeout);
|
---|
22 | }
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | export class SnackController {
|
---|
27 | /**
|
---|
28 | * The internal snack queue. Do not manipulate externally.
|
---|
29 | * @type {Array<{c: String, t: Number}>}
|
---|
30 | */
|
---|
31 | static queue = [];
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * The Snackbar Vue component used by this controller.
|
---|
35 | */
|
---|
36 | static bar = null;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * A value defining whether or not a snackbar is active. Do not manipulate externally.
|
---|
40 | */
|
---|
41 | static isActive = false;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Sets the Snackbar Vue component that this controller should use.
|
---|
45 | */
|
---|
46 | static setBar(snackBar) {
|
---|
47 | this.bar = snackBar;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Adds a snack to the queue.
|
---|
52 | * @param {String} content The message of the snackbar.
|
---|
53 | * @param {Number} timeoutMS The time in milliseconds for which the snack will be displayed.
|
---|
54 | */
|
---|
55 | static addSnack(content, timeoutMS = 3000) {
|
---|
56 | this.queue.push({
|
---|
57 | c: content,
|
---|
58 | t: timeoutMS
|
---|
59 | });
|
---|
60 |
|
---|
61 | if (!this.isActive) {
|
---|
62 | this.eatSnacks();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Do not use this method externally.
|
---|
68 | */
|
---|
69 | static eatSnacks() {
|
---|
70 | if (this.isActive || this.queue.length === 0) {
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | this.isActive = true;
|
---|
75 | const { c, t } = this.queue.shift();
|
---|
76 | this.bar.setContent(c, t);
|
---|
77 |
|
---|
78 | setTimeout(function() {
|
---|
79 | SnackController.isActive = false;
|
---|
80 | SnackController.eatSnacks();
|
---|
81 | }, t + 500);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | </script>
|
---|
85 |
|
---|
86 | <template>
|
---|
87 | <div class="paper bar" :class="{ 'bar-show': hasContent }">
|
---|
88 | {{ content }}
|
---|
89 | </div>
|
---|
90 | </template>
|
---|
91 |
|
---|
92 | <style lang="scss" scoped>
|
---|
93 | .bar {
|
---|
94 | position: absolute;
|
---|
95 | bottom: 1em;
|
---|
96 | left: 35%;
|
---|
97 |
|
---|
98 | opacity: 0;
|
---|
99 | width: 30%;
|
---|
100 |
|
---|
101 | transition-duration: 250ms;
|
---|
102 | --paper-color: #2E2E2E;
|
---|
103 | color: white;
|
---|
104 | }
|
---|
105 |
|
---|
106 | .bar-show {
|
---|
107 | opacity: 1;
|
---|
108 | }
|
---|
109 | </style>
|
---|
Note:
See
TracBrowser
for help on using the repository browser.