mpwxs.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { isPC } from "./isPC"
  2. export default {
  3. data() {
  4. return {
  5. position: [],
  6. button: {},
  7. btn: "[]"
  8. }
  9. },
  10. // computed: {
  11. // pos() {
  12. // return JSON.stringify(this.position)
  13. // },
  14. // btn() {
  15. // return JSON.stringify(this.button)
  16. // }
  17. // },
  18. watch: {
  19. button: {
  20. handler(newVal) {
  21. this.btn = JSON.stringify(newVal)
  22. },
  23. deep: true
  24. },
  25. show(newVal) {
  26. if (this.autoClose) return
  27. if (!this.button) {
  28. this.init()
  29. return
  30. }
  31. this.button.show = newVal
  32. },
  33. leftOptions() {
  34. this.init()
  35. },
  36. rightOptions() {
  37. this.init()
  38. }
  39. },
  40. created() {
  41. if (this.swipeaction.children !== undefined) {
  42. this.swipeaction.children.push(this)
  43. }
  44. },
  45. mounted() {
  46. this.init()
  47. },
  48. beforeDestroy() {
  49. this.swipeaction.children.forEach((item, index) => {
  50. if (item === this) {
  51. this.swipeaction.children.splice(index, 1)
  52. }
  53. })
  54. },
  55. methods: {
  56. init() {
  57. clearTimeout(this.swipetimer)
  58. this.swipetimer = setTimeout(() => {
  59. this.getButtonSize()
  60. }, 50)
  61. },
  62. closeSwipe(e) {
  63. if (!this.autoClose) return
  64. this.swipeaction.closeOther(this)
  65. },
  66. change(e) {
  67. this.$emit('change', e.open)
  68. let show = this.button.show
  69. if (show !== e.open) {
  70. this.button.show = e.open
  71. }
  72. },
  73. appTouchStart(e) {
  74. // #ifdef H5
  75. if(isPC()) return
  76. // #endif
  77. const {
  78. clientX
  79. } = e.changedTouches[0]
  80. this.clientX = clientX
  81. this.timestamp = new Date().getTime()
  82. },
  83. appTouchEnd(e, index, item, position) {
  84. // #ifdef H5
  85. if(isPC()) return
  86. // #endif
  87. const {
  88. clientX
  89. } = e.changedTouches[0]
  90. // fixed by xxxx 模拟点击事件,解决 ios 13 点击区域错位的问题
  91. let diff = Math.abs(this.clientX - clientX)
  92. let time = (new Date().getTime()) - this.timestamp
  93. if (diff < 40 && time < 300) {
  94. this.$emit('click', {
  95. content: item,
  96. index,
  97. position
  98. })
  99. }
  100. },
  101. onClickForPC(index, item, position) {
  102. // #ifdef H5
  103. if(!isPC()) return
  104. // #endif
  105. this.$emit('click', {
  106. content: item,
  107. index,
  108. position
  109. })
  110. },
  111. getButtonSize() {
  112. const views = uni.createSelectorQuery().in(this)
  113. views
  114. .selectAll('.uni-swipe_button-group')
  115. .boundingClientRect(data => {
  116. let show = 'none'
  117. if (this.autoClose) {
  118. show = 'none'
  119. } else {
  120. show = this.show
  121. }
  122. this.button = {
  123. data,
  124. show
  125. }
  126. })
  127. .exec()
  128. }
  129. }
  130. }