favorite.js 6.76 KB
/**
 * 我的收藏
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/2/23
 */

var $ = require('yoho.jquery');

var phoneReg = require('../passport/mail-phone-regx').phoneRegx['+86'];

var clockTxt = {
    on: '[ 降价通知 ]',
    off: '[ 取消通知 ]'
};

var favType;

var curSkn;

var $curClock;

//商品收藏
(function() {
    var $defaultSorts,
        $allSorts;

    var $bodyMask,
        $priceNotice,
        $noticeContent,
        $noticeSuccess,
        $noticeSubOrCancel;

    //关闭价格订阅弹窗和蒙层
    function closeNoticeBox() {
        $bodyMask.addClass('hide');
        $priceNotice.addClass('hide');

        //重置头部文字
        $noticeSubOrCancel.text('订阅成功');
    }

    //显示价格订阅弹窗和蒙层
    function showNoticeBox() {
        if (typeof $bodyMask === 'undefined') {
            $('body').append('<div class="body-mask hide"></div>');
            $bodyMask = $('.body-mask');
        }

        $bodyMask.css({
            height: $(document).height(),
            width: $(document).width()
        }).removeClass('hide');

        if ($curClock.hasClass('noticed')) {

            //取消降价通知
            $.ajax({
                type: 'GET',
                url: '/home/favorite/cancelnotice',
                data: {
                    skn: curSkn
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $noticeContent.addClass('hide');
                    $noticeSuccess.removeClass('hide').find('.notice-num').text(data.data.num);
                    $noticeSubOrCancel.text('取消成功');

                    if ($curClock.closest('.reduction-products').length > 0) {
                        $curClock.closest('.fav-good').remove();
                    } else {
                        $curClock.text(clockTxt.on);
                    }
                }
            });
        }

        $priceNotice.removeClass('hide');
    }

    if ($('.fav-products').length === 0) {
        return;
    }

    favType = 'products';

    $defaultSorts = $('.default-sorts');
    $allSorts = $('.all-sorts');

    $priceNotice = $('.price-notice');

    $noticeContent = $('#price-notice .content');
    $noticeSuccess = $('#price-notice .success');
    $noticeSubOrCancel = $noticeSuccess.find('.subscribe-or-cancel');

    //展开分类
    $('#spread-sort').click(function() {
        $defaultSorts.slideUp(function() {
            $allSorts.slideDown();
        });
    });

    //收起分类
    $('#retract-sort').click(function() {
        $allSorts.slideUp(function() {
            $defaultSorts.slideDown();
        });
    });

    //降价通知
    $('.price-down-clock').click(function() {
        $curClock = $(this),

        showNoticeBox();

        curSkn = $curClock.closest('.fav-good').data('skn');
    });

    //降价通知确定按钮
    $('#price-notice').on('click', '.close', function() {
        closeNoticeBox();
    }).on('click', '.price-notice-sure', function() {
        var $phone = $('#notice-phone-num'),
            phone,
            errTxt;

        if ($noticeSuccess.hasClass('hide')) {

            //订阅到手机号
            phone = $.trim($phone.val());

            if (phone === '' || !phoneReg.test(phone)) {
                errTxt = phone === '' ? '请输入手机号码' : '手机号码格式不正确';

                $phone.addClass('error');
                $('#price-notice .err-text').removeClass('hide').find('em').html(errTxt);
                return;
            } else {
                $phone.removeClass('error');
                $('#price-notice .err-text').addClass('hide');
            }

            $.ajax({
                type: 'GET',
                url: '/home/favorite/notice',
                data: {
                    phone: phone,
                    skn: curSkn
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $noticeSuccess.removeClass('hide');
                    $noticeContent.addClass('hide');
                    $noticeSuccess.find('.notice-num').html(data.data.num);

                    //切换商品通知文字
                    $curClock.toggleClass('noticed').text(clockTxt.off);
                }
            });
        } else {

            //关闭窗口
            closeNoticeBox();
        }
    });

    //商品可参加活动
    $('.has-activity').click(function() {
        var $this = $(this),
            $activites = $this.next('.activites'),
            $li = $this.closest('li');

        if ($activites.hasClass('hide')) {

            //显示
            $activites.removeClass('hide');
            $li.css('padding-bottom', $activites.outerHeight());
        } else {

            //隐藏
            $activites.addClass('hide');
            $li.css('padding-bottom', '');
        }
    });
}());


//品牌收藏
(function() {

    if ($('.fav-brands').length === 0) {
        return;
    }

    // 新品到着
    $('.na-not-zero').click(function() {
        var $this = $(this),
            $naList = $this.next('.na-list'),
            $li = $this.closest('li');

        if (!$naList.hasClass('hide')) {
            $naList.addClass('hide');
            $li.css('padding-bottom', '');
            return;
        }

        $.ajax({
            type: 'GET',
            url: '/home/newproduct'
        }).then(function(html) {
            $naList.removeClass('hide').find('.na-content').html(html);
            $li.css('padding-bottom', $naList.outerHeight() + 10);
        });
    });

    $('.na-content').on('click', '.na-pre, .na-next', function() {
        var $this = $(this),
            url = $this.data('url');

        $.ajax({
            type: 'GET',
            url: url
        }).then(function(html) {
            $this.closest('.na-content').html(html);
        });
    });
}());

//全选【通用】
$('#me-checkall').click(function() {
    $('.checkbox input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});

//删除多个商品/品牌/文章【通用】
$('#me-del-checked').click(function() {
    var ids = [],
        url;

    $('.checkbox input[type="checkbox"]:checked').each(function() {
        ids.push($(this).closest('.fav-row').data('id'));
    });

    if (ids.length === 0) {
        return;
    }

    switch (favType) {
        case 'products':
            url = '/home/favorite/multidelgoods';
            break;
        case 'brands':
            url = '/home/favorite/multidelbrands';
            break;
        case 'articles':
            url = '/home/favorite/multidelarticles';
            break;
    }

    $.ajax({
        type: 'GET',
        url: url,
        data: {
            ids: ids
        }
    }).then(function(data) {
        if (data.code === 200) {
            window.history.go(0);
        }
    });
});