diff options
Diffstat (limited to 'src/components/Message.vue')
-rw-r--r-- | src/components/Message.vue | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/src/components/Message.vue b/src/components/Message.vue index 5a1e0ea..00ce158 100644 --- a/src/components/Message.vue +++ b/src/components/Message.vue | |||
@@ -22,26 +22,52 @@ export default { | |||
22 | }, | 22 | }, |
23 | data: function () { | 23 | data: function () { |
24 | return { | 24 | return { |
25 | show: false, | ||
26 | message: {}, | 25 | message: {}, |
27 | }; | 26 | }; |
28 | }, | 27 | }, |
29 | created: async function () { | 28 | created: async function () { |
30 | // Look for a new message if an endpoint is provided. | 29 | // Look for a new message if an endpoint is provided. |
31 | this.message = Object.assign({}, this.item); | 30 | this.message = Object.assign({}, this.item); |
32 | if (this.item && this.item.url) { | 31 | await this.getMessage(); |
33 | const fetchedMessage = await this.getMessage(this.item.url); | 32 | }, |
34 | // keep the original config value if no value is provided by the endpoint | 33 | computed: { |
35 | for (const prop of ["title", "style", "content"]) { | 34 | show: function () { |
36 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | 35 | return this.message.title || this.message.content; |
37 | this.message[prop] = fetchedMessage[prop]; | 36 | }, |
38 | } | 37 | }, |
39 | } | 38 | watch: { |
40 | } | 39 | item: function (item) { |
41 | this.show = this.message.title || this.message.content; | 40 | this.message = Object.assign({}, item); |
41 | }, | ||
42 | }, | 42 | }, |
43 | methods: { | 43 | methods: { |
44 | getMessage: function (url) { | 44 | getMessage: async function () { |
45 | if (!this.item) { | ||
46 | return; | ||
47 | } | ||
48 | if (this.item.url) { | ||
49 | let fetchedMessage = await this.downloadMessage(this.item.url); | ||
50 | console.log("done"); | ||
51 | if (this.item.mapping) { | ||
52 | fetchedMessage = this.mapRemoteMessage(fetchedMessage); | ||
53 | } | ||
54 | |||
55 | // keep the original config value if no value is provided by the endpoint | ||
56 | const message = this.message; | ||
57 | for (const prop of ["title", "style", "content", "icon"]) { | ||
58 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | ||
59 | message[prop] = fetchedMessage[prop]; | ||
60 | } | ||
61 | } | ||
62 | this.message = { ...message }; // Force computed property to re-evaluate | ||
63 | } | ||
64 | |||
65 | if (this.item.refreshInterval) { | ||
66 | setTimeout(this.getMessage, this.item.refreshInterval); | ||
67 | } | ||
68 | }, | ||
69 | |||
70 | downloadMessage: function (url) { | ||
45 | return fetch(url).then(function (response) { | 71 | return fetch(url).then(function (response) { |
46 | if (response.status != 200) { | 72 | if (response.status != 200) { |
47 | return; | 73 | return; |
@@ -49,6 +75,15 @@ export default { | |||
49 | return response.json(); | 75 | return response.json(); |
50 | }); | 76 | }); |
51 | }, | 77 | }, |
78 | |||
79 | mapRemoteMessage: function (message) { | ||
80 | let mapped = {}; | ||
81 | // map property from message into mapped according to mapping config (only if field has a value): | ||
82 | for (const prop in this.item.mapping) | ||
83 | if (message[this.item.mapping[prop]]) | ||
84 | mapped[prop] = message[this.item.mapping[prop]]; | ||
85 | return mapped; | ||
86 | }, | ||
52 | }, | 87 | }, |
53 | }; | 88 | }; |
54 | </script> | 89 | </script> |