cuiqian2004
8 天以前 f57571f987f3d25730123f488fccdfa3158ec5b1
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
 
 
var scrollTop = {}; //滚动条位置
// 排序列表
var sortList={};
var isMove = false; //是否可拖动 长按事件控制切换这个状态
var touchTimer = false; //长按事件定时器
// 当页面有多个当前组件时,guid用来识别当前的列表的。因为一个页面内多个组件的wxs作用域相同。
 
 
function setScrollTop(tmpGuid) {
    if (typeof scrollTop[tmpGuid] == "undefined") {
        scrollTop[tmpGuid] = 0;
    }
}
 
function scroll(event, instance) {
    var dataView = instance.selectComponent('#dataView');
    var viewData = dataView.getDataset();
    setScrollTop(viewData.guid)
    scrollTop[viewData.guid] = event.detail.scrollTop;
}
 
function initVar(state, instance) {
    var dataView = instance.selectComponent('#dataView');
    var viewData = dataView.getDataset();
    // 读取配置项
    // 获取scroll-view id
    config = All_Config[viewData.guid];
    
    setScrollTop(config.guid);
    state.initscrollTop = scrollTop[config.guid];
    
}
function getRowSort(findId,instance){
    for (var i = 0; i < sortList[config.guid].length; i++) {
        if(sortList[config.guid][i].id==findId){
            currentRowView = sortList[config.guid][i].rowView;
            return sortList[config.guid][i].lastSort;
        }
    }
}
var shadowRowBoxView=null;
var shadowRowView = null;
var currentRowView=null;
var rowSort=0;
var sorting = false;
function touchstart(event, instance) {
    if(sorting){
        return ;
    }
    sorting = true;
    // 兼容setTimeout
    if(typeof setTimeout ==="undefined" && typeof instance.setTimeout !== 'undefined'){
        setTimeout = instance.setTimeout;
        clearTimeout = instance.clearTimeout;
    }
    
    isMove = false;
    var rowData = event.instance.getDataset();
    var state = instance.getState();
    if (event.touches.length == 1) {
        state.point = event.touches[0];
 
        state.islongTap = true;
        state.rowData = rowData;
        //读取数据
        initVar(state, instance);
    }
    var rowStyle = event.instance.getComputedStyle(['height']);
    config.rowHeight = parseFloat(rowStyle.height); //获取行高
    // 计算shadowRow.style.top
    
    rowSort = getRowSort(rowData.id,instance);
    var shadowRowTop = rowSort * config.rowHeight;
    shadowRowTop = shadowRowTop - scrollTop[config.guid];
    // 加载shadowRow数据
    instance.callMethod("loadShadowRow", {
        rowSort: rowSort
    });
    state.shadowRowTop = shadowRowTop;
    // 设置shadowRow初始位置
    shadowRowBoxView = instance.selectComponent('#shadowRowBox');
    shadowRowBoxView.setStyle({
        'top': shadowRowTop + 'px'
    })
    shadowRowView = instance.selectComponent('#shadowRow')
    //长按事件
    if (config.longTouch) {
        touchTimer && clearTimeout(touchTimer);
        touchTimer = setTimeout(function() {
            longpress(event, instance);
        }, config.longTouchTime)
    }
}
 
function longpress(event, instance) {
    if (config.longTouch) {
        isMove = true;
        moveRow(instance, 0)
    }
}
 
function touchmove(event, instance) {
    var state = instance.getState();
    var rowData = event.instance.getDataset();
    var movePoint = event.touches[0];
    var initPoint = state.point;
    var moveY = movePoint.pageY - initPoint.pageY;
    if (config.longTouch) {
        if (Math.abs(moveY) > 10) {
            clearTimeout(touchTimer);
        }
        if (!isMove) {
            return;
        }
    } 
    moveRow(instance, moveY);
    //阻止滚动页面
    if (event.preventDefault) {
        event.preventDefault();
    }
    return false;
}
function touchend(event, instance) {
    if (config.longTouch) {
        clearTimeout(touchTimer);
    }
    if (lastCommand != "stop") {
        lastCommand = "stop";
        config.autoScroll && instance.callMethod("pageScroll", {
            'guid': config.guid,
            'command': "stop"
        });
    }
    var state = instance.getState();
    // 把隐藏的行重新显示
    resetRowStyle(instance,state.rowData.id)
    // 隐藏ShadowRow
    resetShadowRowStyle(instance,state.offset)
    if (typeof state.offset !== "undefined" && rowSort != state.offset && state.offset != null) {
        var sortArray=[];
        for (var i = 0; i < sortList[config.guid].length; i++) {
            sortList[config.guid][i].lastSort = sortList[config.guid][i].newSort;
            sortArray.push(sortList[config.guid][i].newSort);
            sortList[config.guid][i].rowView.removeClass('ani');
        }
        instance.callMethod("sort", {
            index: rowSort,
            offset: state.offset,
            sortArray:sortArray
        });
    } else {
        sorting = false;
        triggerFeedbackGenerator(instance); //震动反馈
        return false;
    }
    state.offset = null;
    oldOffset = null;
    sorting = false;
    triggerFeedbackGenerator(instance); //震动反馈
    return false;
}
// 重置列表行
function resetRowStyle(instance,id) {
    currentRowView.removeClass('hide');
}
// 重置拖拽行
function resetShadowRowStyle(instance,offset) {
    shadowRowBoxView.removeClass('show');
    shadowRowBoxView.addClass('hide');
    shadowRowBoxView.setStyle({});    
}
var lastCommand = '';
 
// move Row
function moveRow(instance, moveY) {
    var state = instance.getState();
 
    // 显示shadowRowBox
    shadowRowBoxView.removeClass('hide');
    shadowRowBoxView.hasClass('show') || shadowRowBoxView.addClass('show');
    // 移动shadowRowBox里面的shadowRow
    shadowRowView.setStyle({
        'transform': 'translate3d(0,' + moveY + 'px,10px)',
        '-webkit-transform': 'translate3d(0,' + moveY + 'px,10px)'
    });
    // 隐藏列表对应行
    currentRowView.hasClass('hide') || currentRowView.addClass('hide');
    currentRowView.removeClass('ani')
    var listClientY = state.shadowRowTop + moveY + config.rowHeight/2;
    var tmpscrollListTop = scrollTop[config.guid];
    
    // 拖拽至边缘滚动视图 距离顶部距离1.5行高触发上滚动 下滚动同理
    var callMethodData = {
        guid: config.guid,
        command: listClientY < config.ListHeight * 0.2 ? "up" : listClientY > config.ListHeight - (config.ListHeight * 0.2) ? "down" :
            "stop",
        scrollTop: tmpscrollListTop,
    }
    // 把滚动指令发给逻辑层
    if (lastCommand != callMethodData.command) {
        lastCommand = callMethodData.command;
        config.autoScroll && instance.callMethod("pageScroll", callMethodData);
    }
 
    var moveOffset = moveY + scrollTop[config.guid] - state.initscrollTop;
    var offset = calcOffset(rowSort, moveOffset);
    if (offset <= 2 || offset >= config.listLength - 2) {
        callMethodData.command = 'stop';
    }
    // 为减少卡顿,微信小程序端,在滚动视图期间不进行列表位置交换
    if (config.autoScroll && (!config.isAppH5) && callMethodData.command != 'stop') {
        return;
    }
    oldOffset = oldOffset == null ? rowSort : oldOffset;
    if (offset < 0 || offset >= config.listLength) {
        return;
    }
    if (offset == oldOffset) {
        return;
    }
 
    oldOffset = offset;
    state.offset = offset;
    //触发change事件 并交换列表位置
    instance.callMethod("change", {
        index: rowSort,
        moveTo: state.offset
    });
    for (var i = 0; i < sortList[config.guid].length; i++) {
        var sort = sortList[config.guid][i].lastSort;
        var newSort = sortList[config.guid][i].newSort;
        if ((sort >= offset && sort <= rowSort) || (sort <= offset && sort >= rowSort)) {
            if(sort == rowSort) {
                newSort = offset;
            }else{
                newSort = sort < rowSort ? sort+1 : sort-1;
            }
        }else{
            newSort = sort;
        }
        if(sortList[config.guid][i].newSort == newSort){
            continue;
        }
        sortList[config.guid][i].newSort = newSort;
        var translateY = (sortList[config.guid][i].newSort-sortList[config.guid][i].sort) * 100;
        sortList[config.guid][i].rowView.hasClass('ani') || sortList[config.guid][i].rowView.addClass('ani');
        sortList[config.guid][i].rowView.setStyle({
            'transform': 'translate3d(0,' + translateY + '%,0)',
            '-webkit-transform': 'translate3d(0,' + translateY + '%,0)'
        });
    }
    triggerFeedbackGenerator(instance); //震动反馈
}
//计算偏移index
var oldOffset = null;
function calcOffset(initSort, moveY) {
    var offset = initSort + parseInt(moveY / config.rowHeight); //偏移 行高的倍数
    var rest = moveY % config.rowHeight;
    if (rest > 0) {
        offset = offset + (rest / config.rowHeight >= 0.6 ? 1 : 0);
        if (offset < oldOffset) {
            offset = rest / config.rowHeight <= 0.4 ? offset : oldOffset;
        }
    } else 
    {
        offset = offset + (rest / config.rowHeight <= -0.6 ? -1 : 0);
        if (offset > oldOffset) {
            offset = rest / config.rowHeight >= -0.4 ? offset : oldOffset;
        }
    }
    return offset;
}
 
//触感反馈
//wxs 不支持条件编译,所以用此方法判断
var isiOSAPP = typeof plus != "undefined" && plus.os.name == 'iOS';
var UISelectionFeedbackGenerator;
var impact
 
if (isiOSAPP) {
    UISelectionFeedbackGenerator = plus.ios.importClass("UISelectionFeedbackGenerator");
    impact = new UISelectionFeedbackGenerator();
    impact.init();
}
function triggerFeedbackGenerator(instance) {
    if (!config.feedbackGenerator) {
        //关闭触感反馈
        return;
    }
    if (isiOSAPP) {
        //异步,避免与点击事件冲突
        setTimeout(function(){
            impact.selectionChanged();
        },0)
    } else {
        if (typeof plus != "undefined") {
            plus.device.vibrate(12)
        } else {
            instance.callMethod("vibrate");
        }
    }
}
var All_Config={};
var config = {};
function receiveData(e,state, instance){
    var data = JSON.parse(e);
    var tmp_config = {};
    var hasGuid = false;
    var sortArray=[];
    for(var i=0;i<data.length;i++){
        var arr = data[i];
        switch (arr[0]){
            case "sortArray":
                sortArray = arr[1];
                break;
            default:
                if(arr[0]=='guid'){
                    hasGuid = true;
                }
                tmp_config[arr[0]] = arr[1];
                break;
        }
    }
    if(!hasGuid){
        return;
    }
    var isUpdateList = false;
    if(typeof All_Config[tmp_config.guid] == "undefined" ||typeof All_Config[tmp_config.guid].lastInitTime == "undefined" ||  All_Config[tmp_config.guid].lastInitTime!=tmp_config.lastInitTime){
        isUpdateList = true;
    }
    All_Config[tmp_config.guid] = tmp_config;
    if(isUpdateList){
        updateSortList(tmp_config.guid, instance,sortArray);
    }
}
// 更新guid对应的排序列表
function updateSortList(guid, instance,sortArray) {
    
    sortList[guid]=[];
    var pageSortList = instance.selectAllComponents('.hm-row');
    for (var i = 0; i < pageSortList.length; i++){
        var tmp_row = {id:pageSortList[i].getDataset().id,sort:i,lastSort:parseInt(pageSortList[i].getDataset().sort),newSort:i,rowView:pageSortList[i]};
        if(sortArray.length>0){
            tmp_row.lastSort = sortArray[i];
            tmp_row.newSort = tmp_row.lastSort;
        }
        sortList[guid].push(tmp_row);
        var translateY = (tmp_row.lastSort-tmp_row.sort) * 100;
        tmp_row.rowView.setStyle({
            'transform': 'translate3d(0,' + translateY + '%,0)',
            '-webkit-transform': 'translate3d(0,' + translateY + '%,0)'
        });
    }
 
}
// 输出
module.exports = {
    receiveData:receiveData,
    scroll: scroll,
    longpress: longpress,
    touchstart: touchstart,
    touchmove: touchmove,
    touchend: touchend
}