xref: /openbmc/webui-vue/src/plugins/toast.js (revision d36ac8a8be8636ddd0e64ce005d507b21bcdeb00)
1*d36ac8a8Sjason westoverimport { useToast } from 'bootstrap-vue-next';
2*d36ac8a8Sjason westover
3*d36ac8a8Sjason westover// Global toast plugin for Options API components
4*d36ac8a8Sjason westover// Bootstrap Vue Next's useToast is a composable that needs setup() context
5*d36ac8a8Sjason westover// This plugin makes it accessible globally via app.config.globalProperties
6*d36ac8a8Sjason westover
7*d36ac8a8Sjason westoverlet toastController = null;
8*d36ac8a8Sjason westover
9*d36ac8a8Sjason westoverexport const ToastPlugin = {
10*d36ac8a8Sjason westover  install(app) {
11*d36ac8a8Sjason westover    // Initialize toast controller in the app context
12*d36ac8a8Sjason westover    // This will be called once during app setup
13*d36ac8a8Sjason westover    app.mixin({
14*d36ac8a8Sjason westover      beforeCreate() {
15*d36ac8a8Sjason westover        // Only initialize once at the root
16*d36ac8a8Sjason westover        if (!toastController && this === this.$root) {
17*d36ac8a8Sjason westover          try {
18*d36ac8a8Sjason westover            toastController = useToast();
19*d36ac8a8Sjason westover          } catch (e) {
20*d36ac8a8Sjason westover            console.warn('Failed to initialize toast controller:', e);
21*d36ac8a8Sjason westover          }
22*d36ac8a8Sjason westover        }
23*d36ac8a8Sjason westover      },
24*d36ac8a8Sjason westover    });
25*d36ac8a8Sjason westover
26*d36ac8a8Sjason westover    // Provide global toast methods
27*d36ac8a8Sjason westover    app.config.globalProperties.$toast = {
28*d36ac8a8Sjason westover      show(options) {
29*d36ac8a8Sjason westover        if (toastController?.create) {
30*d36ac8a8Sjason westover          toastController.create(options);
31*d36ac8a8Sjason westover        } else {
32*d36ac8a8Sjason westover          console.warn('Toast controller not available:', options);
33*d36ac8a8Sjason westover        }
34*d36ac8a8Sjason westover      },
35*d36ac8a8Sjason westover      info(body, options = {}) {
36*d36ac8a8Sjason westover        this.show({
37*d36ac8a8Sjason westover          ...options,
38*d36ac8a8Sjason westover          body,
39*d36ac8a8Sjason westover          props: {
40*d36ac8a8Sjason westover            variant: 'info',
41*d36ac8a8Sjason westover            isStatus: true,
42*d36ac8a8Sjason westover            ...options.props,
43*d36ac8a8Sjason westover          },
44*d36ac8a8Sjason westover        });
45*d36ac8a8Sjason westover      },
46*d36ac8a8Sjason westover      success(body, options = {}) {
47*d36ac8a8Sjason westover        this.show({
48*d36ac8a8Sjason westover          ...options,
49*d36ac8a8Sjason westover          body,
50*d36ac8a8Sjason westover          props: {
51*d36ac8a8Sjason westover            variant: 'success',
52*d36ac8a8Sjason westover            isStatus: true,
53*d36ac8a8Sjason westover            interval: 10000,
54*d36ac8a8Sjason westover            // Note: Progress bar hidden via CSS in _toasts.scss (JS props don't work as documented in Bootstrap Vue Next 0.40.8)
55*d36ac8a8Sjason westover            ...options.props,
56*d36ac8a8Sjason westover          },
57*d36ac8a8Sjason westover        });
58*d36ac8a8Sjason westover      },
59*d36ac8a8Sjason westover      warning(body, options = {}) {
60*d36ac8a8Sjason westover        this.show({
61*d36ac8a8Sjason westover          ...options,
62*d36ac8a8Sjason westover          body,
63*d36ac8a8Sjason westover          props: {
64*d36ac8a8Sjason westover            variant: 'warning',
65*d36ac8a8Sjason westover            isStatus: true,
66*d36ac8a8Sjason westover            ...options.props,
67*d36ac8a8Sjason westover          },
68*d36ac8a8Sjason westover        });
69*d36ac8a8Sjason westover      },
70*d36ac8a8Sjason westover      danger(body, options = {}) {
71*d36ac8a8Sjason westover        this.show({
72*d36ac8a8Sjason westover          ...options,
73*d36ac8a8Sjason westover          body,
74*d36ac8a8Sjason westover          props: {
75*d36ac8a8Sjason westover            variant: 'danger',
76*d36ac8a8Sjason westover            isStatus: true,
77*d36ac8a8Sjason westover            ...options.props,
78*d36ac8a8Sjason westover          },
79*d36ac8a8Sjason westover        });
80*d36ac8a8Sjason westover      },
81*d36ac8a8Sjason westover    };
82*d36ac8a8Sjason westover  },
83*d36ac8a8Sjason westover};
84