| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 1 | import { h } from 'vue'; |
| SurenNeware | c2862dc | 2020-09-11 18:02:42 +0530 | [diff] [blame] | 2 | import StatusIcon from '../Global/StatusIcon'; |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 3 | import i18n from '@/i18n'; |
| Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 4 | const BVToastMixin = { |
| SurenNeware | c2862dc | 2020-09-11 18:02:42 +0530 | [diff] [blame] | 5 | components: { |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 6 | StatusIcon, |
| SurenNeware | c2862dc | 2020-09-11 18:02:42 +0530 | [diff] [blame] | 7 | }, |
| Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 8 | methods: { |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 9 | $_BVToastMixin_createTitle(title, status) { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 10 | const statusIcon = h(StatusIcon, { status }); |
| 11 | return h('strong', { class: 'toast-icon' }, [statusIcon, title]); |
| SurenNeware | c2862dc | 2020-09-11 18:02:42 +0530 | [diff] [blame] | 12 | }, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 13 | $_BVToastMixin_createBody(messageBody) { |
| 14 | if (Array.isArray(messageBody)) { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 15 | return messageBody.map((message) => h('p', { class: 'mb-0' }, message)); |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 16 | } else { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 17 | return [h('p', { class: 'mb-0' }, messageBody)]; |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 18 | } |
| 19 | }, |
| 20 | $_BVToastMixin_createTimestamp() { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 21 | const timestamp = this.$filters.formatTime(new Date()); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 22 | return h('p', { class: 'mt-3 mb-0' }, timestamp); |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 23 | }, |
| 24 | $_BVToastMixin_createRefreshAction() { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 25 | return h( |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 26 | 'BLink', |
| 27 | { |
| 28 | class: 'd-inline-block mt-3', |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 29 | onClick: () => { |
| 30 | require('@/eventBus').default.$emit('refresh-application'); |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 31 | }, |
| 32 | }, |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 33 | i18n.global.t('global.action.refresh'), |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 34 | ); |
| 35 | }, |
| 36 | $_BVToastMixin_initToast(body, title, variant) { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 37 | // Use global toast plugin (works with Options API) |
| 38 | // Extract text content from VNodes for display |
| 39 | |
| 40 | // Extract title text from VNode |
| 41 | const titleText = |
| 42 | typeof title === 'string' |
| 43 | ? title |
| 44 | : title?.children?.[1] || title?.children || ''; |
| 45 | |
| 46 | // Extract body text from VNode array |
| 47 | // Each VNode (paragraph) should be on its own line |
| 48 | const bodyLines = Array.isArray(body) |
| 49 | ? body.map((node) => { |
| 50 | if (typeof node === 'string') return node; |
| 51 | // Extract text from VNode children |
| 52 | const text = node?.children || node?.props?.children || ''; |
| 53 | // Ensure timestamps and other paragraphs are on separate lines |
| 54 | return text; |
| 55 | }) |
| 56 | : [typeof body === 'string' ? body : body?.children || '']; |
| 57 | |
| 58 | // Join with newlines to ensure timestamps appear on their own line |
| 59 | const bodyText = bodyLines.filter(Boolean).join('\n'); |
| 60 | |
| 61 | // Show toast via global plugin |
| 62 | if (this.$toast) { |
| 63 | this.$toast.show({ |
| 64 | body: bodyText, |
| 65 | props: { |
| 66 | title: titleText, |
| 67 | variant, |
| 68 | isStatus: true, |
| 69 | solid: false, // Use light backgrounds with dark text (not solid colors) |
| 70 | // Success toasts auto-dismiss after 10s, others stay until closed |
| 71 | interval: variant === 'success' ? 10000 : 0, |
| 72 | // Note: Progress bar hidden via CSS in _toasts.scss (JS props to hide progress bar don't work as documented in Bootstrap Vue Next 0.40.8) |
| 73 | }, |
| 74 | }); |
| 75 | } else { |
| 76 | // Fallback: log to console |
| 77 | /* eslint-disable no-console */ |
| 78 | console[variant === 'danger' ? 'error' : 'log']( |
| 79 | `[toast:${variant}]`, |
| 80 | bodyText, |
| 81 | ); |
| 82 | /* eslint-enable no-console */ |
| 83 | } |
| Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 84 | }, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 85 | successToast( |
| 86 | message, |
| 87 | { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 88 | title: t = i18n.global.t('global.status.success'), |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 89 | timestamp, |
| 90 | refreshAction, |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 91 | } = {}, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 92 | ) { |
| 93 | const body = this.$_BVToastMixin_createBody(message); |
| 94 | const title = this.$_BVToastMixin_createTitle(t, 'success'); |
| 95 | if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction()); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 96 | if (timestamp) { |
| 97 | body.push(' '); // Extra newline for spacing above timestamp |
| 98 | body.push(this.$_BVToastMixin_createTimestamp()); |
| 99 | } |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 100 | this.$_BVToastMixin_initToast(body, title, 'success'); |
| Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 101 | }, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 102 | errorToast( |
| 103 | message, |
| 104 | { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 105 | title: t = i18n.global.t('global.status.error'), |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 106 | timestamp, |
| 107 | refreshAction, |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 108 | } = {}, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 109 | ) { |
| 110 | const body = this.$_BVToastMixin_createBody(message); |
| 111 | const title = this.$_BVToastMixin_createTitle(t, 'danger'); |
| 112 | if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction()); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 113 | if (timestamp) { |
| 114 | body.push(' '); // Extra newline for spacing above timestamp |
| 115 | body.push(this.$_BVToastMixin_createTimestamp()); |
| 116 | } |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 117 | this.$_BVToastMixin_initToast(body, title, 'danger'); |
| Derick Montague | 4e90eed | 2020-03-03 18:11:44 -0600 | [diff] [blame] | 118 | }, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 119 | warningToast( |
| 120 | message, |
| 121 | { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 122 | title: t = i18n.global.t('global.status.warning'), |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 123 | timestamp, |
| 124 | refreshAction, |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 125 | } = {}, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 126 | ) { |
| 127 | const body = this.$_BVToastMixin_createBody(message); |
| 128 | const title = this.$_BVToastMixin_createTitle(t, 'warning'); |
| 129 | if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction()); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 130 | if (timestamp) { |
| 131 | body.push(' '); // Extra newline for spacing above timestamp |
| 132 | body.push(this.$_BVToastMixin_createTimestamp()); |
| 133 | } |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 134 | this.$_BVToastMixin_initToast(body, title, 'warning'); |
| 135 | }, |
| 136 | infoToast( |
| 137 | message, |
| 138 | { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 139 | title: t = i18n.global.t('global.status.informational'), |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 140 | timestamp, |
| 141 | refreshAction, |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 142 | } = {}, |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 143 | ) { |
| 144 | const body = this.$_BVToastMixin_createBody(message); |
| 145 | const title = this.$_BVToastMixin_createTitle(t, 'info'); |
| 146 | if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction()); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 147 | if (timestamp) { |
| 148 | body.push(' '); // Extra newline for spacing above timestamp |
| 149 | body.push(this.$_BVToastMixin_createTimestamp()); |
| 150 | } |
| Yoshie Muranaka | f92e296 | 2021-02-09 12:41:53 -0800 | [diff] [blame] | 151 | this.$_BVToastMixin_initToast(body, title, 'info'); |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 152 | }, |
| 153 | }, |
| Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | export default BVToastMixin; |