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

var $ = require('jquery');

var lazyLoad = require('yoho.lazyload');
var load = require('../plugin/loading');

var dialog = require('./dialog');

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

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

var page = 0;

var end = false,
    loading = false;

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

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

    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 === ' ') {
                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) {
                    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() {
    var $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);
                }
            }
        });
    });
});