index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Popup, PopupPosition } from 'vant'
  2. import { defineComponent, PropType } from 'vue'
  3. import qs from 'query-string'
  4. export default defineComponent({
  5. name: 'col-popup',
  6. props: {
  7. height: {
  8. type: String,
  9. default: '100%'
  10. },
  11. width: {
  12. type: String,
  13. default: '100%'
  14. },
  15. teleport: {
  16. type: String,
  17. default: ''
  18. },
  19. destroy: {
  20. type: Boolean,
  21. default: false
  22. },
  23. modelValue: {
  24. type: Boolean,
  25. default: false
  26. },
  27. position: {
  28. type: String as PropType<PopupPosition>,
  29. default: 'bottom'
  30. },
  31. zIndex: {
  32. type: Number,
  33. default: 2018
  34. },
  35. onClose: {
  36. type: Function,
  37. default: () => {}
  38. }
  39. },
  40. data() {
  41. return {
  42. popupShow: false,
  43. isDestroy: false
  44. }
  45. },
  46. watch: {
  47. modelValue() {
  48. this.hashState()
  49. }
  50. },
  51. mounted() {
  52. this.destroy && (this.isDestroy = false)
  53. window.addEventListener('hashchange', this.onHash, false)
  54. },
  55. unmounted() {
  56. window.removeEventListener('hashchange', this.onHash, false)
  57. },
  58. methods: {
  59. onHash() {
  60. this.$emit('update:modelValue', false)
  61. this.isDestroy = false
  62. this.onClose && this.onClose()
  63. },
  64. onPopupClose(val: boolean) {
  65. this.$emit('update:modelValue', val)
  66. this.hashState()
  67. },
  68. hashState() {
  69. // 打开弹窗
  70. if (this.modelValue) {
  71. this.isDestroy = false
  72. const splitUrl = window.location.hash.slice(1).split('?')
  73. const query = qs.parse(splitUrl[1])
  74. let times = 0
  75. for (const key in query) {
  76. times++
  77. }
  78. const origin = window.location.href
  79. const url = times > 0 ? '&cPop=' + +new Date() : '?cPop=' + +new Date()
  80. history.pushState('', '', `${origin}${url}`)
  81. } else {
  82. const splitUrl = window.location.hash.slice(1).split('?')
  83. const query = qs.parse(splitUrl[1])
  84. if (query.cPop) {
  85. window.history.go(-1)
  86. }
  87. }
  88. if (this.$refs.protocolPopup) {
  89. ;(this.$refs.protocolPopup as any).scrollTop = 0
  90. }
  91. }
  92. },
  93. render() {
  94. return (
  95. <Popup
  96. ref="protocolPopup"
  97. show={this.modelValue}
  98. transitionAppear={true}
  99. position={this.position}
  100. teleport={this.teleport}
  101. style={{ height: this.height, width: this.width }}
  102. zIndex={this.zIndex}
  103. onClosed={() => {
  104. if (this.destroy) {
  105. this.isDestroy = true
  106. }
  107. }}
  108. >
  109. {this.$slots.default && !this.isDestroy && this.$slots.default()}
  110. </Popup>
  111. )
  112. }
  113. })