index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { defaults } from './utils'
  4. let _notification = null
  5. baseComponent({
  6. useFunc: true,
  7. data: defaults,
  8. computed: {
  9. classes: ['prefixCls', function(prefixCls) {
  10. const wrap = classNames(prefixCls)
  11. const content = `${prefixCls}__content`
  12. const hd = `${prefixCls}__hd`
  13. const image = `${prefixCls}__image`
  14. const bd = `${prefixCls}__bd`
  15. const title = `${prefixCls}__title`
  16. const text = `${prefixCls}__text`
  17. const ft = `${prefixCls}__ft`
  18. return {
  19. wrap,
  20. content,
  21. hd,
  22. image,
  23. bd,
  24. title,
  25. text,
  26. ft,
  27. }
  28. }],
  29. },
  30. methods: {
  31. /**
  32. * 隐藏
  33. */
  34. hide() {
  35. this.$$setData({ in: false })
  36. if (typeof this.fns.onClose === 'function') {
  37. this.fns.onClose(this.data.data)
  38. }
  39. },
  40. /**
  41. * 显示
  42. */
  43. show(opts = {}) {
  44. const closePromise = new Promise((resolve) => {
  45. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  46. const callback = () => {
  47. this.hide()
  48. return resolve(true)
  49. }
  50. this.$$setData({ in: true, ...options })
  51. if (_notification) {
  52. clearTimeout(_notification.timeout)
  53. _notification = null
  54. }
  55. _notification = {
  56. hide: this.hide,
  57. }
  58. _notification.timeout = setTimeout(callback, options.duration)
  59. })
  60. const result = () => {
  61. if (_notification) {
  62. _notification.hide.call(this)
  63. }
  64. }
  65. result.then = (resolve, reject) => closePromise.then(resolve, reject)
  66. result.promise = closePromise
  67. return result
  68. },
  69. /**
  70. * 点击事件
  71. */
  72. onClick() {
  73. if (typeof this.fns.onClick === 'function') {
  74. this.fns.onClick(this.data.data)
  75. }
  76. },
  77. },
  78. })