uni-refresh.wxs 2.14 KB
Newer Older
zhanxin liu committed
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
var pullDown = {
    threshold: 95,
    maxHeight: 200,
    callRefresh: 'onrefresh',
    callPullingDown: 'onpullingdown',
    refreshSelector: '.uni-refresh'
};

function ready(newValue, oldValue, ownerInstance, instance) {
    var state = instance.getState()
    state.canPullDown = newValue;
    console.log(newValue);
}

function touchStart(e, instance) {
    var state = instance.getState();
    state.refreshInstance = instance.selectComponent(pullDown.refreshSelector);
    state.canPullDown = (state.refreshInstance != null && state.refreshInstance != undefined);
    if (!state.canPullDown) {
        return
    }

    console.log("touchStart");

    state.height = 0;
    state.touchStartY = e.touches[0].pageY || e.changedTouches[0].pageY;
    state.refreshInstance.setStyle({
        'height': 0
    });
    state.refreshInstance.callMethod("onchange", true);
}

function touchMove(e, ownerInstance) {
    var instance = e.instance;
    var state = instance.getState();
    if (!state.canPullDown) {
        return
    }

    var oldHeight = state.height;
    var endY = e.touches[0].pageY || e.changedTouches[0].pageY;
    var height = endY - state.touchStartY;
    if (height > pullDown.maxHeight) {
        return;
    }

    var refreshInstance = state.refreshInstance;
    refreshInstance.setStyle({
        'height': height + 'px'
    });

    height = height < pullDown.maxHeight ? height : pullDown.maxHeight;
    state.height = height;
    refreshInstance.callMethod(pullDown.callPullingDown, {
        height: height
    });
}

function touchEnd(e, ownerInstance) {
    var state = e.instance.getState();
    if (!state.canPullDown) {
        return
    }

    state.refreshInstance.callMethod("onchange", false);

    var refreshInstance = state.refreshInstance;
    if (state.height > pullDown.threshold) {
        refreshInstance.callMethod(pullDown.callRefresh);
        return;
    }

    refreshInstance.setStyle({
        'height': 0
    });
}

function propObserver(newValue, oldValue, instance) {
    pullDown = newValue;
}

module.exports = {
    touchmove: touchMove,
    touchstart: touchStart,
    touchend: touchEnd,
    propObserver: propObserver
}