dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// 判断是否出界
var isOutRange = function(x1, y1, x2, y2, x3, y3) {
  return x1 < 0 || x1 >= y1 || x2 < 0 || x2 >= y2 || x3 < 0 || x3 >= y3
}
var edit = false
 
function bool(str) {
  return str === 'true' || str === true
}
/**
 * 排序核心
 * @param {Object} startKey 开始时位置
 * @param {Object} endKey 结束时位置
 * @param {Object} instance wxs内的局部变量快照
 */
var sortCore = function(startKey, endKey, state) {
  var basedata = state.basedata
  var excludeFix = function(sortKey, type) {
    // fixed 元素位置不会变化, 这里直接用 sortKey 获取,更加便捷
    if (state.list[sortKey].fixed) {
      var _sortKey = type ? --sortKey : ++sortKey
      return excludeFix(sortKey, type)
    }
    return sortKey
  }
  
  // 先获取到 endKey 对应的 realKey, 防止下面排序过程中该 realKey 被修改
  var endRealKey = -1
  state.list.forEach(function(item) {
    if (item.sortKey === endKey) endRealKey = item.realKey
  })
  
  return state.list.map(function(item) {
    if (item.fixed) return item
    var sortKey = item.sortKey
    var realKey = item.realKey
    
    if (startKey < endKey) {
      // 正序拖动
      if (sortKey > startKey && sortKey <= endKey) {
        --realKey
        sortKey =  excludeFix(--sortKey, true)
      } else if (sortKey === startKey) {
        realKey = endRealKey
        sortKey = endKey
      }
    } else if (startKey > endKey) {
      // 倒序拖动
      if (sortKey >= endKey && sortKey < startKey) {
        ++realKey
        sortKey = excludeFix(++sortKey, false)
      } else if (sortKey === startKey) {
        realKey = endRealKey
        sortKey = endKey
      }
    }
    
    if (item.sortKey != sortKey) {
      item.translateX = (sortKey % basedata.columns) * 100 + '%'
      item.translateY = Math.floor(sortKey / basedata.columns) * 100 + '%'
      item.sortKey = sortKey
      item.realKey = realKey
    }
    return item
  })
}
 
var triggerCustomEvent = function(list, type, instance) {
  if (!instance) return
  var _list = [],
    listData = [];
    
  list.forEach(function(item) {
    _list[item.sortKey] = item
  })
  _list.forEach(function(item) {
    listData.push(item.data)
  })
  
  // 编译到小程序 funcName作为参数传递导致事件不执行
  switch(type) {
    case 'change':
      instance.callMethod('change', {data: listData})
      break
    case 'sortEnd':
      instance.callMethod('sortEnd', {data: listData})
      break
  }
}
 
var listObserver = function(newVal, oldVal, ownerInstance, instance) {
  var state = ownerInstance.getState()
  state.itemsInstance = ownerInstance.selectAllComponents('.tn-drag__item')
  
  state.list = newVal || []
  
  state.list.forEach(function(item, index) {
    var itemInstance = state.itemsInstance[index]
    if (item && itemInstance) {
      itemInstance.setStyle({
        'transform': 'translate3d('+ item.translateX + ',' + item.translateY +', 0)'
      })
      if (item.fixed) itemInstance.addClass('tn-drag__fixed')
    }
  })
}
 
var baseDataObserver = function(newVal, oldVal, ownerInstance, instance) {
  var state = ownerInstance.getState()
  state.basedata = newVal
}
 
var longPress = function(event, ownerInstance) {
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  
  edit = bool(dataset.edit)
  if (!edit) return
  if (!state.basedata || state.basedata === 'undefined') {
    state.basedata = JSON.parse(dataset.basedata)
  }
  var basedata = state.basedata
  var touches = event.changedTouches[0]
  if (!touches) return
  
  state.current = +dataset.index
  
  // 初始项是固定项则返回
  var item = state.list[state.current]
  if (item && item.fixed) return
  
  // 如果已经在 drag 中则返回, 防止多指触发 drag 动作, touchstart 事件中有效果
  if (state.dragging) return
  
  ownerInstance.callMethod("drag", {
    dragging: true
  })
  
  // 计算X, Y轴初始位移,使item中心移动到点击处,单列的时候X轴初始不做位移
  state.translateX = basedata.columns === 1 ? 0 : touches.pageX - (basedata.itemWidth / 2 + basedata.left)
  state.translateY = touches.pageY - (basedata.itemHeight / 2 + basedata.top)
  state.touchId = touches.identifier
  
  instance.setStyle({
    'transform': 'translate3d(' + state.translateX + 'px,' + state.translateY +'px, 0)'
  })
  state.itemsInstance.forEach(function(item, index) {
    item.removeClass("tn-drag__transition").removeClass("tn-drag__current")
    item.addClass(index === state.current ? "tn-drag__current" : "tn-drag__transition")
  })
  
  ownerInstance.callMethod("vibrate")
  state.dragging = true
}
 
var touchStart = function(event, ownerInstance) {
  var instance = event.instance
  var dataset = instance.getDataset()
  edit = bool(dataset.edit)
}
 
var touchMove = function(event, ownerInstance) {
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  var basedata = state.basedata
  
  if (!state.dragging || !edit) return
  var touches = event.changedTouches[0]
  if (!touches) return
  
  // 如果不是同一个触发点则返回
  if (state.touchId !== touches.identifier) return
  
  // 计算X,Y轴位移, 单列时候X轴初始不做位移
  var translateX = basedata.columns === 1 ? 0 : touches.pageX - (basedata.itemWidth / 2 + basedata.left)
  var translateY = touches.pageY - (basedata.itemHeight / 2 + basedata.top)
  
  // 到顶到低自动滑动
  if (touches.clientY > basedata.windowHeight - basedata.itemHeight - basedata.realBottomSize) {
    // 当前触摸点pageY + item高度 - (屏幕高度 - 底部固定区域高度)
    ownerInstance.callMethod('pageScroll', {
      scrollTop: touches.pageY + basedata.itemHeight - (basedata.windowHeight - basedata.realBottomSize)
    })
  } else if (touches.clientY < basedata.itemHeight + basedata.realTopSize) {
    // 当前触摸点pageY - item高度 - 顶部固定区域高
    ownerInstance.callMethod('pageScroll', {
      scrollTop: touches.pageY - basedata.itemHeight - basedata.realTopSize
    })
  }
  
  // 设置当前激活元素的偏移量
  instance.setStyle({
    'transform': 'translate3d('+ translateX + 'px,' + translateY + 'px, 0)'
  })
  
  var startKey = state.list[state.current].sortKey
  var currentX = Math.round(translateX / basedata.itemWidth)
  var currentY = Math.round(translateY / basedata.itemHeight)
  var endKey = currentX + basedata.columns * currentY
  
  // 目标项时固定项则返回
  var item = state.list[endKey]
  if (item && item.fixed) return
  
  // X轴或者Y轴超出范围则返回
  if (isOutRange(currentX, basedata.columns, currentY, basedata.rows, endKey, state.list.length)) return
  
  // 防止拖拽过程中发生乱序问题
  if (startKey === endKey || startKey === state.preStartKey) return
  state.preStartKey = startKey
  
  var list = sortCore(startKey, endKey, state)
  state.itemsInstance.forEach(function(itemInstance, index) {
    var item = list[index]
    if (index !== state.current) {
      itemInstance.setStyle({
        'transform': 'translate3d('+ item.translateX + ',' + item.translateY +', 0)'
      })
    }
  })
  
  // ownerInstance.callMethod('vibrate')
  ownerInstance.callMethod('listDataChange', {
    data: list
  })
  triggerCustomEvent(list, "change", ownerInstance)
}
 
var touchEnd = function(event, ownerInstance) {
  var instance = event.instance
  var dataset = instance.getDataset()
  var state = ownerInstance.getState()
  var basedata = state.basedata
  
  if (!state.dragging || !edit) return
  triggerCustomEvent(state.list, "sortEnd", ownerInstance)
  
  instance.addClass('tn-drag__transition')
  instance.setStyle({
    'transform': 'translate3d('+ state.list[state.current].translateX + ',' + state.list[state.current].translateY + ', 0)'
  })
  state.itemsInstance.forEach(function(item, index) {
    item.removeClass('tn-drag__transition')
  })
  
  state.preStartKey = -1
  state.dragging = false
  ownerInstance.callMethod('drag', {
    dragging: false
  })
  state.current = -1
  state.translateX = 0
  state.translateY = 0
}
 
module.exports = {
    longPress: longPress,
    touchStart: touchStart,
    touchMove: touchMove,
    touchEnd: touchEnd,
    baseDataObserver: baseDataObserver,
    listObserver: listObserver
}