shop.page.js 2.54 KB
var lazyload = require('yoho-jquery-lazyload');

var tplFn = require('../../tpl/product/shop-goods.hbs');

// require('../common/header');
// require('./list/favorite');



lazyload();

$(function() {
    var shopId = $('.yoho-shop-index').data('id');
    var $order = $('.yoho-shop-index .order-page');
    var $nextPage = $('.page.page-next', $order);
    var $prePage = $('.page.page-pre', $order);
    var total = parseInt($('.total-page', $order).text(), 10);
    var page = 1;
    var orderBy;

    function refreshProducts(data) {
        $('.cur-page', $order).text(page);
        if (page === 1) {
            $($prePage).addClass('disable');
            $($nextPage).removeClass('disable');
        } else if (page === total) {
            $($nextPage).addClass('disable');
            $($prePage).removeClass('disable');
        } else {
            $($nextPage).removeClass('disable');
            $($prePage).removeClass('disable');
        }

        $('.yoho-shop-index .all-product-warp .goods-wrap').html(tplFn(data));
    }

    $($nextPage).click(function() {
        if (!$(this).hasClass('disable') && page < total) {
            $.get('/product/shop/query/all', {
                page: page++,
                shopId: shopId
            }, function(data) {
                refreshProducts(data);
            });
        }
    });

    $($prePage).click(function() {
        if (!$(this).hasClass('disable') && page > 1) {
            $.get('/product/shop/query/all', {
                page: page--,
                shopId: shopId
            }, function(data) {
                refreshProducts(data);
            });
        }
    });

    $('.order-area .order').click(function() {
        var order = $(this).data('order');
        var target = $(this).data('target');
        var orders = order.split(',');
        var newOrder = '';

        if (orderBy === orders[0]) {
            newOrder = orders[1] || '';
        } else {
            newOrder = orders[0];
        }

        $('.order-area .order').removeClass('selected');
        $(this).addClass('selected');
        $('.' + target).find('.iconfont').each(function() {
            if ($(this).hasClass(newOrder)) {
                $(this).addClass('selected');
            } else {
                $(this).removeClass('selected');
            }
        });

        orderBy = newOrder;
        page = 1;

        $.get('/product/shop/query/all', {
            page: page,
            shopId: shopId,
            order: newOrder
        }, function(data) {
            refreshProducts(data);
        });
    });
});