installment.order.page.js 2.98 KB
var $ = require('yoho-jquery');
var debounce = require('lodash/debounce');
var loading = require('../plugin/loading');
var bp = require('./burying-point');
var hasEnd = false;
var search = {
    pageIndex: 1,
    type: null
};


/**
 * 加载订单明细
 *
 * @param type
 */
var loadOrderList = function(pageIndex, type) {
    if (type && type !== search.type) {
        hasEnd = false;
    }

    if (hasEnd) {
        // 没有更多页了
        return;
    }

    if (type && type !== search.type || pageIndex !== search.pageIndex) {
        $.get('/home/installment/order.html', {
            page: pageIndex,
            type: type
        }).then(function(result) {
            if (result && result.replace(/\s/g, '')) {
                search.pageIndex = pageIndex;
            } else {
                hasEnd = true;
            }

            if (type !== search.type) {
                // 切换TAB时清空列表
                $('#order-list').empty();
                search.type = type;
            }

            $('#order-list').append(result);


            // 订单状态样式
            $('.status').each(function() {
                var text = $(this).text();

                if (text.indexOf(/已还清|已取消|已退款|已结清|订单取消/)) {
                    $(this).addClass('faded');
                }
            });

            if ($('#order-list li').length === 0) {
                $('#no-result').show();
                $('#order-list').hide();
            } else {
                $('#order-list').show();
                $('#no-result').hide();
            }
        });
    }
};

var lastScrollTop = 0;
var windowHeight = $(window).height();
var scrollFn = debounce(function() {
    var scrollTop = $(window).scrollTop();

    // 当scroll到1/4列表高度后继续请求下一页数据
    if (scrollTop > lastScrollTop &&
        (scrollTop + windowHeight >
        $(document).height() - 0.25 * $('#order-list').height() - 50)) {
        loadOrderList(search.pageIndex + 1, search.type);
    }

    lastScrollTop = scrollTop;
}, 100);

/**
 * 顶部TAB
 */
$('.header-tab a').click(function() {
    $(this).parent().addClass('active');
    $(this).parent().siblings().removeClass('active');

    loadOrderList(1, $(this).data('type'));
    return false;
});

// 默认加载第一页
loadOrderList(search.pageIndex, 1);


// 滚屏分页
$(window).scroll(function() {
    scrollFn();
});


((function() {
    var tick = 0;

    // 显示加载进度条
    $(document).ajaxStart(function() {
        tick = new Date().getTime();
        setTimeout(function() {
            if (tick > 0) { // 超过100ms请求仍未结束则显示
                loading.showLoadingMask();
            }
        }, 100);
    });

    $(document).ajaxStop(function() {
        tick = 0;
        loading.hideLoadingMask();
    });
})());

setTimeout(function() {
    // 统计:各模块的落地页进入时
    bp.setContYas({
        POS_ID: $('.installment-order-page').data('posId')
    }, true);
}, 3000);