index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { defaults } from './utils'
  4. let _toptips = null
  5. baseComponent({
  6. useFunc: true,
  7. data: defaults,
  8. computed: {
  9. classes: ['prefixCls, icon', function(prefixCls, iconType) {
  10. const ico = iconType ? iconType : 'cancel'
  11. const wrap = classNames(prefixCls)
  12. const content = classNames(`${prefixCls}__content`, {
  13. [`${prefixCls}__content--${ico}`]: ico,
  14. })
  15. const icon = `${prefixCls}__icon`
  16. const text = `${prefixCls}__text`
  17. return {
  18. wrap,
  19. content,
  20. icon,
  21. text,
  22. }
  23. }],
  24. },
  25. methods: {
  26. /**
  27. * 隐藏
  28. */
  29. hide() {
  30. if (this.removed) return false
  31. this.removed = true
  32. if (_toptips) {
  33. clearTimeout(_toptips.timeout)
  34. _toptips = null
  35. }
  36. this.$$setData({ in: false })
  37. if (typeof this.fns.success === 'function') {
  38. this.fns.success()
  39. }
  40. },
  41. /**
  42. * 显示
  43. */
  44. show(opts = {}) {
  45. const closePromise = new Promise((resolve) => {
  46. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  47. const callback = () => {
  48. this.hide()
  49. return resolve(true)
  50. }
  51. this.removed = false
  52. this.$$setData({ in: true, ...options })
  53. if (_toptips) {
  54. clearTimeout(_toptips.timeout)
  55. _toptips = null
  56. }
  57. _toptips = {
  58. hide: this.hide,
  59. }
  60. _toptips.timeout = setTimeout(callback, options.duration)
  61. })
  62. const result = () => {
  63. if (_toptips) {
  64. _toptips.hide.call(this)
  65. }
  66. }
  67. result.then = (resolve, reject) => closePromise.then(resolve, reject)
  68. result.promise = closePromise
  69. return result
  70. },
  71. success(opts = {}) {
  72. return this.show(Object.assign({
  73. ...opts,
  74. icon: 'success',
  75. }))
  76. },
  77. info(opts = {}) {
  78. return this.show(Object.assign({
  79. ...opts,
  80. icon: 'info',
  81. }))
  82. },
  83. warn(opts = {}) {
  84. return this.show(Object.assign({
  85. ...opts,
  86. icon: 'warn',
  87. }))
  88. },
  89. error(opts = {}) {
  90. return this.show(Object.assign({
  91. ...opts,
  92. icon: 'cancel',
  93. }))
  94. },
  95. },
  96. })