liuxiaolong
2019-05-06 3e0536f508aad49f743e7bfabca34e3980a1b6e2
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<template>
  <div ref="box">
    <slot></slot>
  </div>
</template>
 
<script>
export default {
  props: {
    index: {
      type: Number,
      default: 0
    },
    w: {
      type: Number,
      default: 100
    },
    h: {
      type: Number,
      default: 100
    },
    data: {
      type: Object,
      default: null
    },
    tags: {
      type: Object,
      default: null
    },
    toDOMId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      grabEvents: ['mousedown', 'touchstart'],
      moveEvents: ['mousemove', 'touchmove'],
      releaseEvents: ['mouseup', 'touchend'],
      isAdd: false,
      isOnDowm: false,
      boxContent: {},
      beginx: 0,
      beginy: 0,
      item: '',
      // 移动复制的Dom 节点
      CloneDom: null
    }
  },
  mounted() {
    this.item = this.$refs.box
    this.listenEvents()
  },
  methods: {
    // 封装API
    _addGrabListeners() {
      this.grabEvents.forEach(e => {
        window.addEventListener(e, this._onMouseDown, { passive: false })
      })
      this.moveEvents.forEach(e => {
        window.document.addEventListener(e, this._onMouseMove, {
          passive: false
        })
      })
      this.releaseEvents.forEach(e => {
        window.document.addEventListener(e, this._onMouseUp, { passive: false })
      })
    },
 
    listenEvents() {
      this._addGrabListeners()
    },
 
    _onMouseDown(e) {
      this.boxContent = this._getContainerRect(this.item)
      this.isAdd = this._current(this.boxContent, e)
      if (!this.isOnDowm && this.isAdd) {
        this.isOnDowm = true
        // this.emit('isOnDowm', this.)
        this.$emit('down', {
          index: this.index,
          data: this.data,
          tags: this.tags
        })
        this.beginx = e.clientX || e.changedTouches[0].clientX
        this.beginy = e.clientY || e.changedTouches[0].clientY
        this.CloneDom = this.item.cloneNode(true)
        document.getElementsByTagName('body')[0].append(this.CloneDom)
        this.CloneDom.style.position = 'absolute'
        this.CloneDom.style.width = this.w + 'px'
        this.CloneDom.style.height = this.h + 'px'
        this.CloneDom.style.top = this.beginy - 20 + 'px'
        this.CloneDom.style.left = this.beginx - 20 + 'px'
        this.CloneDom.style.opacity = 0.4
      }
    },
    _onMouseMove(e) {
      if (!this.isOnDowm) return
      let boxContent = this._getContainerRect(this.item)
      this.boxContent = boxContent
      let clientX = e.clientX || e.changedTouches[0].clientX
      let clientY = e.clientY || e.changedTouches[0].clientY
      let eventX = clientX - this.beginx
      let eventY = clientY - this.beginy
      this.beginy = clientY
      this.beginx = clientX
      if (!this.isAdd || !this.CloneDom) return
      let x = eventX + this.CloneDom.offsetLeft
      let y = eventY + this.CloneDom.offsetTop
      if (this.CloneDom) {
        this.CloneDom.style.position = 'absolute'
        this.CloneDom.style.top = y + 'px'
        this.CloneDom.style.left = x + 'px'
      }
    },
    _onMouseUp(e) {
      this.CloneDom &&
        document.getElementsByTagName('body')[0].removeChild(this.CloneDom)
      this.CloneDom = null
      if (this.isOnDowm && this.isAdd) {
        let clientX = e.clientX || e.changedTouches[0].clientX
        let clientY = e.clientY || e.changedTouches[0].clientY
        this.isOnDowm = false
        if (this.toDOMId !== '') {
          let toDOM = document.getElementById(this.toDOMId)
          // 拖放对象 元素位置信息
          let toDOMContent = this._getContainerRect(toDOM)
          if (
            clientY >= toDOMContent.top &&
            clientY <= toDOMContent.bottom &&
            clientX >= toDOMContent.left &&
            clientX <= toDOMContent.right
          ) {
            clientX = clientX - toDOMContent.left
            clientY = clientY - toDOMContent.top
          } else {
            return false
          }
        }
        console.log(clientX, '_onMouseUp clientX')
        this.$emit('_getContainerRect', {
          x: clientX,
          y: clientY,
          index: this.index,
          data: this.data,
          tags: this.tags
        })
 
        // this.reset()
      }
      return false
    },
    // 判断鼠标是否在元素上
    /**
     * @function 判断鼠标是否在元素上
     * @param { object } 元素的位置信息  left right top bottom width height
     * @param { object } event事件信息
     * @returns { boolean } true 是 false 否
     */
    _current(getContainerRect, e) {
      if (
        getContainerRect.left <= (e.clientX || e.changedTouches[0].clientX) &&
        getContainerRect.right >= (e.clientX || e.changedTouches[0].clientX) &&
        getContainerRect.top <= (e.clientY || e.changedTouches[0].clientY) &&
        getContainerRect.bottom >= (e.clientY || e.changedTouches[0].clientY)
      ) {
        return true
      } else {
        return false
      }
    },
    /**
     * @function 获取元素位置信息
     * @param { string } DOM元素
     * @returns { object } left right top bottom width height
     */
    _getContainerRect(element) {
      // console.log(element, 'element')
      const _rect = element.getBoundingClientRect()
      return {
        left: _rect.left,
        right: _rect.right,
        top: _rect.top,
        bottom: _rect.bottom,
        width: _rect.width,
        height: _rect.height
      }
    },
    /**
     * 还原位置信息
     */
    reset() {
      if (this.item.style.position) {
        this.item.style.position = 'static'
      }
    }
  }
}
</script>
 
<style scoped>
img {
  width: 100%;
  height: 100%;
}
</style>