index.js 5.3 KB

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