]>
Commit | Line | Data |
---|---|---|
1 | <template> | |
2 | <article v-if="show" class="message" :class="message.style"> | |
3 | <div v-if="message.title || message.icon" class="message-header"> | |
4 | <p> | |
5 | <i v-if="message.icon" :class="`fa-fw ${message.icon}`"></i> | |
6 | {{ message.title }} | |
7 | </p> | |
8 | </div> | |
9 | <div | |
10 | v-if="message.content" | |
11 | class="message-body" | |
12 | v-html="message.content" | |
13 | ></div> | |
14 | </article> | |
15 | </template> | |
16 | ||
17 | <script> | |
18 | export default { | |
19 | name: "Message", | |
20 | props: { | |
21 | item: Object, | |
22 | }, | |
23 | data: function () { | |
24 | return { | |
25 | message: {}, | |
26 | }; | |
27 | }, | |
28 | created: async function () { | |
29 | // Look for a new message if an endpoint is provided. | |
30 | this.message = Object.assign({}, this.item); | |
31 | await this.getMessage(); | |
32 | }, | |
33 | computed: { | |
34 | show: function () { | |
35 | return this.message.title || this.message.content; | |
36 | }, | |
37 | }, | |
38 | watch: { | |
39 | item: function (item) { | |
40 | this.message = Object.assign({}, item); | |
41 | }, | |
42 | }, | |
43 | methods: { | |
44 | getMessage: async function () { | |
45 | if (this.item && this.item.url) { | |
46 | let fetchedMessage = await this.downloadMessage(this.item.url); | |
47 | if (this.item.mapping) | |
48 | fetchedMessage = this.mapRemoteMessage(fetchedMessage); | |
49 | // keep the original config value if no value is provided by the endpoint | |
50 | for (const prop of ["title", "style", "content"]) { | |
51 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | |
52 | this.message[prop] = fetchedMessage[prop]; | |
53 | } | |
54 | } | |
55 | } | |
56 | if (this.item.refreshInterval) | |
57 | setTimeout(this.getMessage, this.item.refreshInterval); | |
58 | }, | |
59 | ||
60 | downloadMessage: function (url) { | |
61 | return fetch(url).then(function (response) { | |
62 | if (response.status != 200) { | |
63 | return; | |
64 | } | |
65 | return response.json(); | |
66 | }); | |
67 | }, | |
68 | ||
69 | mapRemoteMessage: function (message) { | |
70 | let mapped = {}; | |
71 | // map property from message into mapped according to mapping config (only if field has a value): | |
72 | for (const prop in this.item.mapping) | |
73 | if (message[this.item.mapping[prop]]) | |
74 | mapped[prop] = message[this.item.mapping[prop]]; | |
75 | return mapped; | |
76 | }, | |
77 | }, | |
78 | }; | |
79 | </script> |