index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import baseComponent from '../helpers/baseComponent'
  2. baseComponent({
  3. useExport: true,
  4. properties: {
  5. prefixCls: {
  6. type: String,
  7. value: 'wux-backdrop',
  8. },
  9. transparent: {
  10. type: Boolean,
  11. value: false,
  12. },
  13. zIndex: {
  14. type: Number,
  15. value: 1000,
  16. },
  17. classNames: {
  18. type: null,
  19. value: 'wux-animate--fadeIn',
  20. },
  21. mountOnEnter: {
  22. type: Boolean,
  23. value: true,
  24. },
  25. unmountOnExit: {
  26. type: Boolean,
  27. value: true,
  28. },
  29. visible: {
  30. type: Boolean,
  31. value: false,
  32. observer(newVal) {
  33. this.setActive(newVal)
  34. if (!newVal) {
  35. this._backdropHolds = 0
  36. }
  37. },
  38. },
  39. },
  40. data: {
  41. active: false,
  42. },
  43. computed: {
  44. classes: ['prefixCls, transparent', function(prefixCls, transparent) {
  45. const wrap = transparent ? `${prefixCls}--transparent` : prefixCls
  46. const bd = `${prefixCls}__bd`
  47. const btn = `${prefixCls}__aria-btn`
  48. return {
  49. wrap,
  50. bd,
  51. btn,
  52. }
  53. }],
  54. },
  55. methods: {
  56. /**
  57. * 设置组件是否可见
  58. */
  59. setActive(active) {
  60. if (this.data.active !== active) {
  61. this.setData({
  62. active,
  63. })
  64. }
  65. },
  66. /**
  67. * 完全展示后触发
  68. */
  69. onEntered() {
  70. this.triggerEvent('showed')
  71. },
  72. /**
  73. * 完全关闭后触发
  74. */
  75. onExited() {
  76. this.triggerEvent('closed')
  77. },
  78. /**
  79. * 点击事件
  80. */
  81. onClick() {
  82. this.triggerEvent('click')
  83. },
  84. /**
  85. * 阻止冒泡
  86. */
  87. noop() {},
  88. ['export']() {
  89. const self = this
  90. /**
  91. * 保持锁定
  92. */
  93. const retain = () => {
  94. if (typeof this._backdropHolds !== 'number' || !this._backdropHolds) {
  95. this._backdropHolds = 0
  96. }
  97. this._backdropHolds = this._backdropHolds + 1
  98. if (this._backdropHolds === 1) {
  99. this.setActive(true)
  100. }
  101. }
  102. /**
  103. * 释放锁定
  104. */
  105. const release = () => {
  106. if (this._backdropHolds === 1) {
  107. this.setActive(false)
  108. }
  109. this._backdropHolds = Math.max(0, this._backdropHolds - 1)
  110. }
  111. return {
  112. retain,
  113. release,
  114. get backdropHolds() {
  115. return self._backdropHolds || 0
  116. },
  117. }
  118. },
  119. },
  120. attached() {
  121. const { visible: active } = this.data
  122. this.setActive(active)
  123. },
  124. })