blob: 0d9fff5f3f050296491fb710f0b6133c41add156 [file] [log] [blame]
jason westoverd36ac8a2025-11-03 20:58:59 -06001import { h } from 'vue';
SurenNewarec2862dc2020-09-11 18:02:42 +05302import StatusIcon from '../Global/StatusIcon';
Surya Vde23ea22024-07-11 15:19:46 +05303import i18n from '@/i18n';
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -08004const BVToastMixin = {
SurenNewarec2862dc2020-09-11 18:02:42 +05305 components: {
Derick Montague602e98a2020-10-21 16:20:00 -05006 StatusIcon,
SurenNewarec2862dc2020-09-11 18:02:42 +05307 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -08008 methods: {
Yoshie Muranakaf92e2962021-02-09 12:41:53 -08009 $_BVToastMixin_createTitle(title, status) {
jason westoverd36ac8a2025-11-03 20:58:59 -060010 const statusIcon = h(StatusIcon, { status });
11 return h('strong', { class: 'toast-icon' }, [statusIcon, title]);
SurenNewarec2862dc2020-09-11 18:02:42 +053012 },
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080013 $_BVToastMixin_createBody(messageBody) {
14 if (Array.isArray(messageBody)) {
jason westoverd36ac8a2025-11-03 20:58:59 -060015 return messageBody.map((message) => h('p', { class: 'mb-0' }, message));
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080016 } else {
jason westoverd36ac8a2025-11-03 20:58:59 -060017 return [h('p', { class: 'mb-0' }, messageBody)];
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080018 }
19 },
20 $_BVToastMixin_createTimestamp() {
Surya Vde23ea22024-07-11 15:19:46 +053021 const timestamp = this.$filters.formatTime(new Date());
jason westoverd36ac8a2025-11-03 20:58:59 -060022 return h('p', { class: 'mt-3 mb-0' }, timestamp);
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080023 },
24 $_BVToastMixin_createRefreshAction() {
jason westoverd36ac8a2025-11-03 20:58:59 -060025 return h(
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080026 'BLink',
27 {
28 class: 'd-inline-block mt-3',
jason westoverd36ac8a2025-11-03 20:58:59 -060029 onClick: () => {
30 require('@/eventBus').default.$emit('refresh-application');
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080031 },
32 },
Surya Vde23ea22024-07-11 15:19:46 +053033 i18n.global.t('global.action.refresh'),
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080034 );
35 },
36 $_BVToastMixin_initToast(body, title, variant) {
jason westoverd36ac8a2025-11-03 20:58:59 -060037 // 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 Muranaka0fc91e72020-02-05 11:23:06 -080084 },
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080085 successToast(
86 message,
87 {
Surya Vde23ea22024-07-11 15:19:46 +053088 title: t = i18n.global.t('global.status.success'),
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080089 timestamp,
90 refreshAction,
Ed Tanous81323992024-02-27 11:26:24 -080091 } = {},
Yoshie Muranakaf92e2962021-02-09 12:41:53 -080092 ) {
93 const body = this.$_BVToastMixin_createBody(message);
94 const title = this.$_BVToastMixin_createTitle(t, 'success');
95 if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
jason westoverd36ac8a2025-11-03 20:58:59 -060096 if (timestamp) {
97 body.push(' '); // Extra newline for spacing above timestamp
98 body.push(this.$_BVToastMixin_createTimestamp());
99 }
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800100 this.$_BVToastMixin_initToast(body, title, 'success');
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800101 },
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800102 errorToast(
103 message,
104 {
Surya Vde23ea22024-07-11 15:19:46 +0530105 title: t = i18n.global.t('global.status.error'),
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800106 timestamp,
107 refreshAction,
Ed Tanous81323992024-02-27 11:26:24 -0800108 } = {},
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800109 ) {
110 const body = this.$_BVToastMixin_createBody(message);
111 const title = this.$_BVToastMixin_createTitle(t, 'danger');
112 if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
jason westoverd36ac8a2025-11-03 20:58:59 -0600113 if (timestamp) {
114 body.push(' '); // Extra newline for spacing above timestamp
115 body.push(this.$_BVToastMixin_createTimestamp());
116 }
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800117 this.$_BVToastMixin_initToast(body, title, 'danger');
Derick Montague4e90eed2020-03-03 18:11:44 -0600118 },
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800119 warningToast(
120 message,
121 {
Surya Vde23ea22024-07-11 15:19:46 +0530122 title: t = i18n.global.t('global.status.warning'),
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800123 timestamp,
124 refreshAction,
Ed Tanous81323992024-02-27 11:26:24 -0800125 } = {},
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800126 ) {
127 const body = this.$_BVToastMixin_createBody(message);
128 const title = this.$_BVToastMixin_createTitle(t, 'warning');
129 if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
jason westoverd36ac8a2025-11-03 20:58:59 -0600130 if (timestamp) {
131 body.push(' '); // Extra newline for spacing above timestamp
132 body.push(this.$_BVToastMixin_createTimestamp());
133 }
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800134 this.$_BVToastMixin_initToast(body, title, 'warning');
135 },
136 infoToast(
137 message,
138 {
Surya Vde23ea22024-07-11 15:19:46 +0530139 title: t = i18n.global.t('global.status.informational'),
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800140 timestamp,
141 refreshAction,
Ed Tanous81323992024-02-27 11:26:24 -0800142 } = {},
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800143 ) {
144 const body = this.$_BVToastMixin_createBody(message);
145 const title = this.$_BVToastMixin_createTitle(t, 'info');
146 if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
jason westoverd36ac8a2025-11-03 20:58:59 -0600147 if (timestamp) {
148 body.push(' '); // Extra newline for spacing above timestamp
149 body.push(this.$_BVToastMixin_createTimestamp());
150 }
Yoshie Muranakaf92e2962021-02-09 12:41:53 -0800151 this.$_BVToastMixin_initToast(body, title, 'info');
Derick Montague602e98a2020-10-21 16:20:00 -0500152 },
153 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800154};
155
156export default BVToastMixin;