mpwxs.js 2.3 KB

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