index.tsx 2.6 KB

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