favorite.brand.page.js 3.42 KB
/**
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 16/7/20
 */
var dialog = require('../plugins/dialog');
var _confirm = dialog.Confirm;

var FavoriteBrand = {
    init: function() {
        var $root = $('.favorite-brands');

        require('./me');
        require('../plugins/check');

        $('.brand-products ul').each(function() {
            var lis = $('li', this);

            $(lis[0]).addClass('show');
        });

        $('.slide-switch a').click(function() {
            FavoriteBrand.moveSlide(this);
        });

        $('.check', $root).check({
            type: 'checkbox',
            onChange: function(ele, checked) {
                if (checked) {
                    $(ele).next('.brand-info').addClass('choose');
                } else {
                    $(ele).next('.brand-info').removeClass('choose');
                }
            }
        });

        $('.check-all', $root).check({
            type: 'checkbox',
            onChange: function(ele, checked) {
                if (checked) {
                    $('.check', $root).check('checkAll');
                } else {
                    $('.check', $root).check('unCheckAll');
                }
            }
        });

        $('.btn.cancel', $root).click(function() {
            var id = $(this).parents('.brand-info').data('id');
            var type = $(this).parents('.brand-info').data('type');

            FavoriteBrand.doCancel(id, type);
        });

        $('.favorite-cancel', $root).click(function() {
            var shopId = [];
            var brandId = [];

            $('.brand-info.choose', $root).each(function() {
                if ($(this).data('type') === 'shop') {
                    shopId.push($(this).data('id'));
                } else if ($(this).data('type') === 'brand') {
                    brandId.push($(this).data('id'));
                }
            });

            FavoriteBrand.doCancelMulti(shopId.join(','), brandId.join(','));
        });
    },
    _showConfirm: function(cb) {
        new _confirm({
            content: '<h1 class="title">确认删除</h1><p>您确定要删除该收藏么?</p>',
            cb: cb
        }).show();
    },
    moveSlide: function(ele) {
        var wrap = $(ele).parent().next('.slide-wrap');
        var currLi = $('ul li.show', wrap);

        if ($(ele).hasClass('next') && $(currLi).next('li').length > 0) {
            $(currLi).next('li').addClass('show');
            $(currLi).removeClass('show');
        } else if ($(ele).hasClass('prev') && $(currLi).prev('li').length > 0) {
            $(currLi).prev('li').addClass('show');
            $(currLi).removeClass('show');
        }
    },
    doCancel: function(ids, type) {
        this._showConfirm(function() {
            $.post('/me/collection/cancel', {
                type: type || 'brand',
                ids: ids
            }, function(data) {
                if (data.code === 200) {
                    location.href = '/me/collection/brand';
                }
            });
        });
    },
    doCancelMulti: function(shops, brands) {
        this._showConfirm(function() {
            $.post('/me/collection/cancel/multi', {
                shops: shops,
                brands: brands
            }, function(data) {
                if (data.code === 200) {
                    location.href = '/me/collection/brand';
                }
            });
        });
    }
};


$(function() {
    FavoriteBrand.init();
});