2 <article v-if="show" class="message" :class="message.style">
3 <div v-if="message.title || message.icon" class="message-header">
5 <i v-if="message.icon" :class="`fa-fw ${message.icon}`"></i>
10 v-if="message.content"
12 v-html="message.content"
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();
35 return this.message.title || this.message.content;
39 item: function (item) {
40 this.message = Object.assign({}, item);
44 getMessage: async function () {
49 let fetchedMessage = await this.downloadMessage(this.item.url);
51 if (this.item.mapping) {
52 fetchedMessage = this.mapRemoteMessage(fetchedMessage);
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];
62 this.message = { ...message }; // Force computed property to re-evaluate
65 if (this.item.refreshInterval) {
66 setTimeout(this.getMessage, this.item.refreshInterval);
70 downloadMessage: function (url) {
71 return fetch(url).then(function (response) {
72 if (response.status != 200) {
75 return response.json();
79 mapRemoteMessage: function (message) {
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]];