| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- 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
- }
|