'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<script>
import {
  deepClone
} from 'uni-shared'
import {
  initScrollBounce,
  disableScrollBounce
} from 'uni-platform/helpers/scroll'
 
function calc (e) {
  return Math.sqrt(e.x * e.x + e.y * e.y)
}
 
export default {
  name: 'MovableArea',
  props: {
    scaleArea: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      width: 0,
      height: 0,
      items: []
    }
  },
  created: function () {
    this.gapV = {
      x: null,
      y: null
    }
    this.pinchStartLen = null
  },
  mounted: function () {
    this._resize()
    initScrollBounce()
  },
  methods: {
    _resize () {
      this._getWH()
      this.items.forEach(function (item, index) {
        item.componentInstance.setParent()
      })
    },
    _find (target, items = this.items) {
      var root = this.$el
 
      function get (node) {
        for (let i = 0; i < items.length; i++) {
          const item = items[i]
          if (node === item.componentInstance.$el) {
            return item
          }
        }
        if (node === root || node === document.body || node === document) {
          return null
        }
        return get(node.parentNode)
      }
      return get(target)
    },
    _touchstart (t) {
      disableScrollBounce({
        disable: true
      })
      var i = t.touches
      if (i) {
        if (i.length > 1) {
          var r = {
            x: i[1].pageX - i[0].pageX,
            y: i[1].pageY - i[0].pageY
          }
          this.pinchStartLen = calc(r)
          this.gapV = r
          if (!this.scaleArea) {
            var touch0 = this._find(i[0].target)
            var touch1 = this._find(i[1].target)
            this._scaleMovableView = touch0 && touch0 === touch1 ? touch0 : null
          }
        }
      }
    },
    _touchmove (t) {
      var n = t.touches
      if (n) {
        if (n.length > 1) {
          t.preventDefault()
          var i = {
            x: n[1].pageX - n[0].pageX,
            y: n[1].pageY - n[0].pageY
          }
          if (this.gapV.x !== null && this.pinchStartLen > 0) {
            var r = calc(i) / this.pinchStartLen
            this._updateScale(r)
          }
          this.gapV = i
        }
      }
    },
    _touchend (e) {
      disableScrollBounce({
        disable: false
      })
      var t = e.touches
      if (!(t && t.length)) {
        if (e.changedTouches) {
          this.gapV.x = 0
          this.gapV.y = 0
          this.pinchStartLen = null
          if (this.scaleArea) {
            this.items.forEach(function (item) {
              item.componentInstance._endScale()
            })
          } else {
            if (this._scaleMovableView) {
              this._scaleMovableView.componentInstance._endScale()
            }
          }
        }
      }
    },
    _updateScale (e) {
      if (e && e !== 1) {
        if (this.scaleArea) {
          this.items.forEach(function (item) {
            item.componentInstance._setScale(e)
          })
        } else {
          if (this._scaleMovableView) {
            this._scaleMovableView.componentInstance._setScale(e)
          }
        }
      }
    },
    _getWH () {
      var style = window.getComputedStyle(this.$el)
      var rect = this.$el.getBoundingClientRect()
      this.width = rect.width - ['Left', 'Right'].reduce(function (all, item) {
        return all + parseFloat(style['border' + item + 'Width']) + parseFloat(style['padding' + item])
      }, 0)
      this.height = rect.height - ['Top', 'Bottom'].reduce(function (all, item) {
        return all + parseFloat(style['border' + item + 'Width']) + parseFloat(style['padding' + item])
      }, 0)
    }
  },
  render (createElement) {
    var items = []
    const $slots = this.$slots.default && deepClone(this.$slots.default, createElement)
    if ($slots) {
      $slots.forEach(vnode => {
        if (vnode.componentOptions && vnode.componentOptions.tag === 'v-uni-movable-view') {
          items.push(vnode)
        }
      })
    }
    this.items = items
    var $listeners = Object.assign({}, this.$listeners)
    var events = ['touchstart', 'touchmove', 'touchend']
    events.forEach(event => {
      var existing = $listeners[event]
      var ours = this[`_${event}`]
      $listeners[event] = existing ? [].concat(existing, ours) : ours
    })
    return createElement('uni-movable-area', {
      on: $listeners
    }, [createElement('v-uni-resize-sensor', {
      on: {
        resize: this._resize
      }
    }), $slots])
  }
}
</script>
<style>
  uni-movable-area {
    display: block;
    position: relative;
    width: 10px;
    height: 10px;
  }
 
  uni-movable-area[hidden] {
    display: none;
  }
</style>