|
| 1 | +<template> |
| 2 | + <NotificationMessage |
| 3 | + :notification="notification" |
| 4 | + :selections="selections" |
| 5 | + data-el="generic-notification" :to="to" |
| 6 | + > |
| 7 | + <template #icon> |
| 8 | + <component :is="notificationData.iconComponent" /> |
| 9 | + </template> |
| 10 | + <template #title> |
| 11 | + {{ notificationData.title }} |
| 12 | + </template> |
| 13 | + <template #message> |
| 14 | + <!-- eslint-disable-next-line vue/no-v-html --> |
| 15 | + <span v-html="notificationData.message" /> |
| 16 | + </template> |
| 17 | + </NotificationMessage> |
| 18 | +</template> |
| 19 | + |
| 20 | +<script> |
| 21 | +import { defineAsyncComponent } from 'vue' |
| 22 | +
|
| 23 | +import IconDeviceSolid from '../../components/icons/DeviceSolid.js' |
| 24 | +import IconNodeRedSolid from '../../components/icons/NodeRedSolid.js' |
| 25 | +import NotificationMessageMixin from '../../mixins/NotificationMessage.js' |
| 26 | +
|
| 27 | +import NotificationMessage from './Notification.vue' |
| 28 | +
|
| 29 | +export default { |
| 30 | + name: 'GenericNotification', |
| 31 | + components: { NotificationMessage, IconNodeRedSolid }, |
| 32 | + mixins: [NotificationMessageMixin], |
| 33 | + data () { |
| 34 | + return { |
| 35 | + knownEvents: { |
| 36 | + 'instance-crashed': { |
| 37 | + icon: 'instance', |
| 38 | + title: 'Node-RED Instance Crashed', |
| 39 | + message: '"<i>{{instance.name}}</i>" has crashed' |
| 40 | + }, |
| 41 | + 'instance-safe-mode': { |
| 42 | + icon: 'instance', |
| 43 | + title: 'Node-RED Instance Safe Mode', |
| 44 | + message: '"<i>{{instance.name}}</i>" is running in safe mode' |
| 45 | + }, |
| 46 | + 'device-crashed': { |
| 47 | + icon: 'device', |
| 48 | + title: 'Node-RED Device Crashed', |
| 49 | + message: '"<i>{{device.name}}</i>" has crashed' |
| 50 | + }, |
| 51 | + 'device-safe-mode': { |
| 52 | + icon: 'device', |
| 53 | + title: 'Node-RED Device Safe Mode', |
| 54 | + message: '"<i>{{device.name}}</i>" is running in safe mode' |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + computed: { |
| 60 | + to () { |
| 61 | + if (typeof this.notification.data?.to === 'object') { return this.notification.data.to } |
| 62 | + if (typeof this.notification.data?.to === 'string') { return { path: this.notification.data.to } } |
| 63 | + if (typeof this.notification.data?.url === 'string') { return { url: this.notification.data.url } } |
| 64 | + if (this.notification.data?.instance?.id) { |
| 65 | + return { |
| 66 | + name: 'instance-overview', |
| 67 | + params: { id: this.notification.data.instance.id } |
| 68 | + } |
| 69 | + } |
| 70 | + return null // no link |
| 71 | + }, |
| 72 | + notificationData () { |
| 73 | + const event = this.knownEvents[this.notification.type] || {} |
| 74 | + event.createdAt = new Date(this.notification.createdAt).toLocaleString() |
| 75 | + // get title and message |
| 76 | + if (!event.title) { |
| 77 | + event.title = this.notification.data?.title || this.notification.type.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase()) |
| 78 | + } |
| 79 | + if (!event.message) { |
| 80 | + event.message = this.notification.data?.message || `Event occurred at ${event.createdAt}` |
| 81 | + } |
| 82 | +
|
| 83 | + // icon handling |
| 84 | + event.icon = event.icon || this.notification.data?.icon |
| 85 | + if (event.icon === 'instance' || event.icon === 'project') { |
| 86 | + event.iconComponent = IconNodeRedSolid |
| 87 | + } else if (event.icon === 'device') { |
| 88 | + event.iconComponent = IconDeviceSolid |
| 89 | + } else { |
| 90 | + event.iconComponent = defineAsyncComponent(() => import('@heroicons/vue/solid').then(x => x[event.icon] || x.BellIcon)) |
| 91 | + } |
| 92 | +
|
| 93 | + // Perform known substitutions |
| 94 | + event.title = this.substitutions(event.title) |
| 95 | + event.message = this.substitutions(event.message) |
| 96 | + return event |
| 97 | + } |
| 98 | + }, |
| 99 | + methods: { |
| 100 | + substitutions (str) { |
| 101 | + let result = str |
| 102 | + const regex = /{{([^}]+)}}/g // find all {{key}} occurrences |
| 103 | + let match = regex.exec(result) |
| 104 | + while (match) { |
| 105 | + const key = match[1] |
| 106 | + const value = this.getObjectProperty(this.notification.data || {}, key) || key.split('.')[0].replace(/\b\w/g, l => l.toUpperCase()) |
| 107 | + result = result.replace(match[0], value) |
| 108 | + match = regex.exec(result) |
| 109 | + } |
| 110 | + return result |
| 111 | + }, |
| 112 | + getObjectProperty (obj, path) { |
| 113 | + return (path || '').trim().split('.').reduce((acc, part) => acc && acc[part], obj) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +</script> |
| 118 | +
|
| 119 | +<style scoped lang="scss"> |
| 120 | +
|
| 121 | +</style> |
0 commit comments