]>
Commit | Line | Data |
---|---|---|
b9c5fcf0 | 1 | <template> |
7ef65940 | 2 | <article v-if="show" class="message" :class="message.style"> |
0e045b4c GC |
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> | |
b9c5fcf0 | 8 | </div> |
8ae1fe8a BW |
9 | <div |
10 | v-if="message.content" | |
11 | class="message-body" | |
12 | v-html="message.content" | |
13 | ></div> | |
b9c5fcf0 BW |
14 | </article> |
15 | </template> | |
16 | ||
17 | <script> | |
18 | export default { | |
19 | name: "Message", | |
20 | props: { | |
21 | item: Object, | |
22 | }, | |
7ef65940 BW |
23 | data: function () { |
24 | return { | |
7ef65940 BW |
25 | message: {}, |
26 | }; | |
27 | }, | |
28 | created: async function () { | |
b9c5fcf0 | 29 | // Look for a new message if an endpoint is provided. |
7ef65940 | 30 | this.message = Object.assign({}, this.item); |
1ddf3941 | 31 | await this.getMessage(); |
b9c5fcf0 | 32 | }, |
ba07da6b BW |
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 | }, | |
b9c5fcf0 | 43 | methods: { |
ba07da6b | 44 | getMessage: async function () { |
7596bc52 BW |
45 | if (!this.item) { |
46 | return; | |
47 | } | |
48 | if (this.item.url) { | |
1ddf3941 | 49 | let fetchedMessage = await this.downloadMessage(this.item.url); |
7596bc52 | 50 | if (this.item.mapping) { |
ba07da6b | 51 | fetchedMessage = this.mapRemoteMessage(fetchedMessage); |
7596bc52 BW |
52 | } |
53 | ||
1ddf3941 | 54 | // keep the original config value if no value is provided by the endpoint |
7596bc52 | 55 | const message = this.message; |
742ae4eb | 56 | for (const prop of ["title", "style", "content", "icon"]) { |
1ddf3941 | 57 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { |
7596bc52 | 58 | message[prop] = fetchedMessage[prop]; |
1ddf3941 | 59 | } |
60 | } | |
7596bc52 | 61 | this.message = { ...message }; // Force computed property to re-evaluate |
1ddf3941 | 62 | } |
7596bc52 BW |
63 | |
64 | if (this.item.refreshInterval) { | |
ba07da6b | 65 | setTimeout(this.getMessage, this.item.refreshInterval); |
7596bc52 | 66 | } |
1ddf3941 | 67 | }, |
68 | ||
69 | downloadMessage: function (url) { | |
b9c5fcf0 BW |
70 | return fetch(url).then(function (response) { |
71 | if (response.status != 200) { | |
72 | return; | |
73 | } | |
74 | return response.json(); | |
75 | }); | |
76 | }, | |
6d29bc27 | 77 | |
78 | mapRemoteMessage: function (message) { | |
79 | let mapped = {}; | |
80 | // map property from message into mapped according to mapping config (only if field has a value): | |
ba07da6b BW |
81 | for (const prop in this.item.mapping) |
82 | if (message[this.item.mapping[prop]]) | |
83 | mapped[prop] = message[this.item.mapping[prop]]; | |
6d29bc27 | 84 | return mapped; |
85 | }, | |
b9c5fcf0 BW |
86 | }, |
87 | }; | |
88 | </script> |