index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. import { $wuxBackdrop } from '../index'
  5. baseComponent({
  6. externalClasses: ['wux-content-class', 'wux-header-class', 'wux-body-class', 'wux-footer-class', 'wux-close-class'],
  7. properties: {
  8. prefixCls: {
  9. type: String,
  10. value: 'wux-popup',
  11. },
  12. animationPrefixCls: {
  13. type: String,
  14. value: 'wux-animate',
  15. },
  16. title: {
  17. type: String,
  18. value: '',
  19. },
  20. content: {
  21. type: String,
  22. value: '',
  23. },
  24. extra: {
  25. type: String,
  26. value: '',
  27. },
  28. position: {
  29. type: String,
  30. value: 'center',
  31. observer: 'getTransitionName',
  32. },
  33. wrapStyle: {
  34. type: [String, Object],
  35. value: '',
  36. observer(newVal) {
  37. this.setData({
  38. extStyle: styleToCssString(newVal),
  39. })
  40. },
  41. },
  42. closable: {
  43. type: Boolean,
  44. value: false,
  45. },
  46. mask: {
  47. type: Boolean,
  48. value: true,
  49. },
  50. maskClosable: {
  51. type: Boolean,
  52. value: true,
  53. },
  54. visible: {
  55. type: Boolean,
  56. value: false,
  57. observer: 'setPopupVisible',
  58. },
  59. zIndex: {
  60. type: Number,
  61. value: 1000,
  62. },
  63. },
  64. data: {
  65. transitionName: '',
  66. popupVisible: false,
  67. extStyle: '',
  68. },
  69. computed: {
  70. classes() {
  71. const { prefixCls, position } = this.data
  72. const wrap = classNames(`${prefixCls}-position`, {
  73. [`${prefixCls}-position--${position}`]: position,
  74. })
  75. const content = `${prefixCls}__content`
  76. const hd = `${prefixCls}__hd`
  77. const title = `${prefixCls}__title`
  78. const bd = `${prefixCls}__bd`
  79. const ft = `${prefixCls}__ft`
  80. const extra = `${prefixCls}__extra`
  81. const close = `${prefixCls}__close`
  82. const x = `${prefixCls}__close-x`
  83. return {
  84. wrap,
  85. content,
  86. hd,
  87. title,
  88. bd,
  89. ft,
  90. extra,
  91. close,
  92. x,
  93. }
  94. },
  95. },
  96. methods: {
  97. /**
  98. * 点击关闭按钮事件
  99. */
  100. close() {
  101. this.triggerEvent('close')
  102. },
  103. /**
  104. * 点击蒙层事件
  105. */
  106. onMaskClick() {
  107. if (this.data.maskClosable) {
  108. this.close()
  109. }
  110. },
  111. /**
  112. * 组件关闭后的回调函数
  113. */
  114. onExited() {
  115. this.triggerEvent('closed')
  116. },
  117. /**
  118. * 获取过渡的类名
  119. */
  120. getTransitionName(value = this.data.position) {
  121. const { animationPrefixCls } = this.data
  122. let transitionName = ''
  123. switch (value) {
  124. case 'top':
  125. transitionName = `${animationPrefixCls}--slideInDown`
  126. break
  127. case 'right':
  128. transitionName = `${animationPrefixCls}--slideInRight`
  129. break
  130. case 'bottom':
  131. transitionName = `${animationPrefixCls}--slideInUp`
  132. break
  133. case 'left':
  134. transitionName = `${animationPrefixCls}--slideInLeft`
  135. break
  136. default:
  137. transitionName = `${animationPrefixCls}--fadeIn`
  138. break
  139. }
  140. this.setData({ transitionName })
  141. },
  142. /**
  143. * 设置 popup 组件的显示隐藏
  144. */
  145. setPopupVisible(popupVisible) {
  146. if (this.data.popupVisible !== popupVisible) {
  147. this.setData({ popupVisible })
  148. this.setBackdropVisible(popupVisible)
  149. }
  150. },
  151. /**
  152. * 设置 backdrop 组件的显示隐藏
  153. */
  154. setBackdropVisible(visible) {
  155. if (this.data.mask && this.$wuxBackdrop) {
  156. this.$wuxBackdrop[visible ? 'retain' : 'release']()
  157. }
  158. },
  159. },
  160. created() {
  161. if (this.data.mask) {
  162. this.$wuxBackdrop = $wuxBackdrop('#wux-backdrop', this)
  163. }
  164. },
  165. attached() {
  166. this.setPopupVisible(this.data.visible)
  167. this.getTransitionName()
  168. },
  169. })