CommonRoot.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { App } from "vue";
  2. import type { ICommonRoot } from "./CommonRoot.vue";
  3. import type { DialogAlertOptions } from "./DialogRoot.vue";
  4. import type { ToastShowProps } from "../feedback/Toast.vue";
  5. import type { ActionSheetOptions } from "./ActionSheetRoot.vue";
  6. const currentRootStack : ICommonRoot[] = [];
  7. export function setCurrentRoot(root : ICommonRoot) {
  8. currentRootStack.push(root);
  9. }
  10. export function destroyCurrentRoot(root : ICommonRoot) {
  11. const index = currentRootStack.indexOf(root);
  12. if (index !== -1)
  13. currentRootStack.splice(index, 1);
  14. }
  15. export function NaDialogRoot() : ICommonRoot {
  16. if (!currentRootStack.length)
  17. throw new Error("No dialog root found.");
  18. return currentRootStack[currentRootStack.length - 1];
  19. }
  20. export function alert(options: DialogAlertOptions) {
  21. return NaDialogRoot().alert(options);
  22. }
  23. export function confirm(options: DialogAlertOptions) {
  24. return NaDialogRoot().confirm(options);
  25. }
  26. export function toast(options: ToastShowProps|string) {
  27. return NaDialogRoot().toast(options);
  28. }
  29. export function closeToast() {
  30. return NaDialogRoot().closeToast();
  31. }
  32. export function actionSheet(options: ActionSheetOptions) {
  33. return NaDialogRoot().actionSheet(options);
  34. }
  35. export function notify(options: ToastShowProps) {
  36. return NaDialogRoot().notify(options);
  37. }
  38. let startZIndex = 1010;
  39. export function getCurrentZIndex() {
  40. startZIndex += 3;
  41. return startZIndex;
  42. }
  43. export default {
  44. install(app: App) {
  45. const na : ICommonRoot = {
  46. alert,
  47. confirm,
  48. toast,
  49. closeToast,
  50. actionSheet,
  51. notify,
  52. }
  53. app.config.globalProperties.$na = na;
  54. }
  55. }