browse-record.page.js 3.24 KB
/**
 * 浏览记录
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/28
 */

require('scss/home/record.page.scss');
require('js/common');

let $ = require('yoho-jquery');

let lazyLoad = require('yoho-jquery-lazyload'),
    load = require('js/plugin/loading'),
    tip = require('js/plugin/tip');

let dialog = require('js/plugin/dialog');

let $page = $('.records');

let $loadMore = $('.load-more'),
    $more = $loadMore.children('.more'),
    $noMore = $loadMore.children('.no-more');

let page = 1;

let end = false,
    loading = false;

let winH = $(window).height();

load.init();
function moreRecord(cb) {
    let count = $page.children('.browse-record-good').length;

    if (count === 0) {
        return;
    }

    if (loading) {
        return;
    }

    if (page === 1) {
        $loadMore.removeClass('hide');
    }

    loading = true;
    $.ajax({
        type: 'GET',
        url: '/home/recordContent',
        data: {
            page: page + 1
        },
        success: function(data) {
            if (!data || data === '') {
                end = true;
                $more.addClass('hide');
                $noMore.removeClass('hide');
            } else {

                if (count === 0) {
                    $page.html(data);

                    lazyLoad();
                } else {
                    $page.append(data);

                    lazyLoad($page.find('.browse-record-good:gt(' + (count - 1) + ') .lazy'));
                }
                window.rePosFooter();
                page++;

                if (cb) {
                    return cb();
                }
            }
        },
        complete: function() {
            loading = false;
        }
    });
}

load.showLoadingMask();
moreRecord(load.hideLoadingMask);

$(window).scroll(function() {
    if (end) {
        return;
    }

    if (winH + $(window).scrollTop() > $(document).height() - 0.25 * $page.height()) {
        moreRecord();
    }
});

$page.on('touchstart', '.del-icon', function() {
    let $good = $(this).closest('.browse-record-good'),
        skn = $good.data('skn');

    dialog.showDialog({
        dialogText: '确定删除此条浏览记录吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {

        $.ajax({
            type: 'GET',
            url: '/home/delRecord',
            data: {
                skn: skn
            },
            success: function(data) {
                if (data.code === 200) {
                    dialog.showDialog({
                        dialogText: '删除浏览记录成功',
                        autoHide: 2000,
                        fast: true
                    });
                    setTimeout(function() {
                        window.history.go(0);
                    }, 1200);
                }
            }
        });
    });
});

$('.deps').on('touchstart', 'span', function() {
    $(this).css('background', '#eee');
}).on('touchend touchcancel', 'span', function() {
    $(this).css('background', 'transparent');
});

$('.invalidGoods').on('touchstart touchend', function(e) {
    let $this = $(e.target).closest('span');

    if ($this.hasClass('del-icon')) {
        return;
    }
    tip.show('商品已下架');
    return false;
});