index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { $wuxBackdrop } from '../index'
  4. import { defaults, iconTypes } from './utils'
  5. let _toast = null
  6. baseComponent({
  7. useFunc: true,
  8. data: defaults,
  9. computed: {
  10. classes: ['prefixCls, icon', function(prefixCls, hasIcon) {
  11. const wrap = classNames(prefixCls)
  12. const content = classNames(`${prefixCls}__content`, {
  13. [`${prefixCls}__content--has-icon`]: hasIcon,
  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 (_toast) {
  33. clearTimeout(_toast.timeout)
  34. _toast = null
  35. }
  36. this.$$setData({ in: false })
  37. this.$wuxBackdrop && this.$wuxBackdrop.release()
  38. if (typeof this.fns.success === 'function') {
  39. this.fns.success()
  40. }
  41. },
  42. /**
  43. * 显示
  44. */
  45. show(opts) {
  46. if (typeof opts === 'string') {
  47. opts = Object.assign({}, {
  48. text: arguments[0],
  49. }, arguments[1])
  50. }
  51. const closePromise = new Promise((resolve) => {
  52. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  53. const iconType = iconTypes[options.type] || options.icon
  54. const callback = () => {
  55. this.hide()
  56. return resolve(true)
  57. }
  58. options.icon = iconType
  59. this.removed = false
  60. this.$$setData({ in: true, ...options })
  61. this.$wuxBackdrop && this.$wuxBackdrop.retain()
  62. if (_toast) {
  63. clearTimeout(_toast.timeout)
  64. _toast = null
  65. }
  66. _toast = {
  67. hide: this.hide,
  68. }
  69. _toast.timeout = setTimeout(callback, Math.max(0, options.duration))
  70. })
  71. const result = () => {
  72. if (_toast) {
  73. _toast.hide.call(this)
  74. }
  75. }
  76. result.then = (resolve, reject) => closePromise.then(resolve, reject)
  77. result.promise = closePromise
  78. return result
  79. },
  80. /**
  81. * 成功提示
  82. */
  83. success(opts) {
  84. if (typeof opts === 'string') {
  85. opts = Object.assign({}, {
  86. text: arguments[0],
  87. }, arguments[1])
  88. }
  89. return this.show(Object.assign({
  90. ...opts,
  91. type: 'success',
  92. }))
  93. },
  94. /**
  95. * 警告提示
  96. */
  97. warning(opts) {
  98. if (typeof opts === 'string') {
  99. opts = Object.assign({}, {
  100. text: arguments[0],
  101. }, arguments[1])
  102. }
  103. return this.show(Object.assign({
  104. ...opts,
  105. type: 'forbidden',
  106. }))
  107. },
  108. /**
  109. * 错误提示
  110. */
  111. error(opts) {
  112. if (typeof opts === 'string') {
  113. opts = Object.assign({}, {
  114. text: arguments[0],
  115. }, arguments[1])
  116. }
  117. return this.show(Object.assign({
  118. ...opts,
  119. type: 'cancel',
  120. }))
  121. },
  122. /**
  123. * 文本提示
  124. */
  125. info(opts) {
  126. if (typeof opts === 'string') {
  127. opts = Object.assign({}, {
  128. text: arguments[0],
  129. }, arguments[1])
  130. }
  131. return this.show(Object.assign({
  132. ...opts,
  133. type: 'text',
  134. }))
  135. },
  136. },
  137. created() {
  138. if (this.data.mask) {
  139. this.$wuxBackdrop = $wuxBackdrop('#wux-backdrop', this)
  140. }
  141. },
  142. })