product-details.js 5.29 KB
$(function() {
    var Goods = {
        initialize: function() {
            this.initGoodsNum();
            this.bindEvents();
        },
        bindEvents: function() {
            var _this = this;
            $('.j_goodsInfo').on('click', '.reduce', function() {
                if ($(this).hasClass('disabled')) {
                    return;
                }
                _this.reduceNum($(this));
            }).on('click', '.add', function() {
                if ($(this).hasClass('disabled')) {
                    return;
                }
                _this.addNum($(this));
            }).on('change', '.m-input-number input', function() {
                _this.verInputVal($(this));
            });

            $('.props').on('click', '.btn', function() {
                $(this).addClass('selected').siblings().removeClass('selected');
            });

            $('.submitBtn').on('click', function(e){
            	console.log($(this).closest('.goods-data').find('.j_goodsInfo'));
            	$(this).closest('.goods-data').find('.j_goodsInfo').find('.props').find('.error-box').toggle(!_this.verAttrSelected($(this).closest('.goods-data').find('.j_goodsInfo')));
            	if(!_this.verAttrSelected($(this).closest('.goods-data').find('.j_goodsInfo'))){
            		e.preventDefault();
            	}
            });
        },
        initGoodsNum: function() {
            var _this = this;
            $('.j_goodsInfo').each(function() {
                var min = _this.getMinNum($('.j_goodsInfo').children());
                var max = _this.getMaxNum($('.j_goodsInfo').children());
                $(this).find('.m-input-number input').val(min).siblings('.reduce').addClass('disabled');
            });
        },
        getGoodsNum: function(input) {
            var num = +$.trim(input.val());
            var min = +this.getMinNum(input);
            var max = +this.getMaxNum(input);

            if (!/^\d+$/.test(num)) {
                num = 0;
            }

            num = num <= min ? min : num;
            num = num >= max ? max : num;
            return num;
        },
        getMinNum: function(input) {
            if (input.closest('.j_goodsInfo').find('.small-sales').length <= 0) {
                return input.closest('.j_goodsInfo').find('.j_minNum').text();
            }
            return input.closest('.j_goodsInfo').find('em[data-range]').eq(0).attr('data-range').split(',')[0];
        },
        getMaxNum: function(input) {
            return input.closest('.j_goodsInfo').find('.j_maxNum').text();
        },
        addNum: function(target) {
            var input = target.siblings('input');
            var num = this.getGoodsNum(input);
            var min = this.getMinNum(input);
            var max = this.getMaxNum(input);

            num += 1;
            num = num <= min ? min : num;
            num = num >= max ? max : num;
            input.val(num);
            this.updateBtnStyle(target, num);
        },
        reduceNum: function(target) {
            var input = target.siblings('input');
            var num = this.getGoodsNum(input);
            var min = this.getMinNum(input);
            var max = this.getMaxNum(input);

            num -= 1;
            num = num <= min ? min : num;
            num = num >= max ? max : num;
            input.val(num);
            this.updateBtnStyle(target, num);
        },
        verInputVal: function(target) {
            var num = this.getGoodsNum(target);
            target.val(num);
            this.updateBtnStyle(target, num);
        },
        updateBtnStyle: function(target, num) {
            var min = this.getMinNum(target);
            var max = this.getMaxNum(target);
            target.closest('.j_goodsInfo').find('.add').toggleClass('disabled', num == max);
            target.closest('.j_goodsInfo').find('.reduce').toggleClass('disabled', num == min);
            this.tabPriceStyle(target, num);
        },
        tabPriceStyle: function(target, num) {
            var wrap = target.closest('.j_goodsInfo');
            if (target.closest('.j_goodsInfo').find('.small-sales').length <= 0) {
                return;
            }
            wrap.find('em[data-range]').each(function() {
                var prices = $(this).attr('data-range').split(',');
                if (prices.length == 2) {
                    if (num >= +prices[0] && num <= +prices[1]) {
                        $(this).closest('.cell').addClass('current').siblings().removeClass('current');
                        return false;
                    }
                } else {
                    if (num >= prices[0]) {
                        $(this).closest('.cell').addClass('current').siblings().removeClass('current');
                        return false;
                    }
                }
            })
        },
        verAttrSelected:function(wrap){
        	var isOk = true;
        	wrap.find('.props .info-attr').each(function(){
        		var ok = $(this).find('.selected').length > 0;
        		$(this).find('.title').toggleClass('font-red', !ok);
        		$(this).closest('.props').find('.error-box').toggle(!ok);
        		isOk = isOk ? ok : isOk;
        		if(!ok){
        			return false;
        		}
        	});
        	if(!isOk){
        		return;
        	}
        	return isOk;        	
        }
    };
    Goods.initialize();
});