index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Dialog from './Dialog'
  2. import Window from './Window'
  3. import Toast from './Toast'
  4. import Alert from './Alert'
  5. import Confirm from './Confirm'
  6. import DialogContent from './Dialog-content'
  7. import { mount } from '../../utils/componentHelper'
  8. Dialog.use = function use(app) {
  9. Dialog.toast = function (options) {
  10. if (typeof options == 'string') {
  11. options = {
  12. content: options,
  13. }
  14. }
  15. const { destroy, vNode, el } = mount(Toast, {
  16. app,
  17. props: {
  18. ...options,
  19. destroy,
  20. },
  21. })
  22. if (!Dialog.toast._destroys) {
  23. Dialog.toast._destroys = []
  24. }
  25. Dialog.toast._destroys.push(destroy)
  26. return {
  27. hide: function () {
  28. let destroy = null
  29. while ((destroy = Dialog.toast._destroys.shift()) && destroy) {
  30. destroy()
  31. }
  32. }.bind(this),
  33. }
  34. }
  35. Dialog.toast.hide = function () {
  36. if (Dialog.toast._destroys && Dialog.toast._destroys.length) {
  37. let destroy = Dialog.toast._destroys.pop()
  38. destroy && destroy()
  39. }
  40. }
  41. Dialog.alert = function (options) {
  42. if (typeof options == 'string') {
  43. options = {
  44. content: options,
  45. }
  46. }
  47. const { destroy } = mount(Alert, {
  48. app,
  49. props: { ...options, destroy: () => destroy() },
  50. })
  51. this.alert.hide = function () {
  52. destroy()
  53. }
  54. return this.alert
  55. }
  56. Dialog.confirm = function (options) {
  57. if (typeof options == 'string') {
  58. options = {
  59. content: options,
  60. }
  61. }
  62. let promise
  63. if (!options.func) {
  64. promise = new Promise(resolve => {
  65. options.func = result => resolve(result === 'ok')
  66. })
  67. }
  68. const { destroy } = mount(Confirm, {
  69. app,
  70. props: { ...options, destroy: () => destroy() },
  71. })
  72. this.confirm.hide = function () {
  73. destroy()
  74. }
  75. return promise || this.confirm
  76. }
  77. }
  78. export { Window, Toast, Alert, Confirm, DialogContent }
  79. export default Dialog