...
|
...
|
@@ -10,53 +10,42 @@ |
|
|
// 异步渲染的模板统一插入 #chose-panel ,兼容页面多个选择框
|
|
|
|
|
|
var $ = require('jquery'),
|
|
|
Handlebars = require('yoho.handlebars'),
|
|
|
tip = require('../plugin/tip'),
|
|
|
loading = require('../plugin/loading');
|
|
|
|
|
|
var panelTmpl,
|
|
|
$chosePanel = $('#chose-panel'),
|
|
|
var $chosePanel = $('#chose-panel'),
|
|
|
$num,
|
|
|
$chosed,
|
|
|
$imgsThumb,
|
|
|
|
|
|
// re = /\d+/,
|
|
|
|
|
|
$leftNum,
|
|
|
leftNum,
|
|
|
confirming,
|
|
|
hasChooseColor = false,
|
|
|
hasChooseSize = false,
|
|
|
curColorIndex,
|
|
|
curSizeIndex,
|
|
|
$curSizeRow,
|
|
|
$curSizeBlock = null,
|
|
|
$sizeRowList = $('.size-list ul');
|
|
|
|
|
|
// 读取模板
|
|
|
$.get('/cart/index/giftinfoTpl', function(html) {
|
|
|
if (!html) {
|
|
|
tip.show('网络错误');
|
|
|
return;
|
|
|
}
|
|
|
panelTmpl = Handlebars.compile(html);
|
|
|
}).fail(function() {
|
|
|
tip.show('网络错误');
|
|
|
});
|
|
|
hasChooseColor,
|
|
|
hasChooseSize,
|
|
|
$curSizeBlock,
|
|
|
$sizeRowList,
|
|
|
cbFn,
|
|
|
$allChoseItems,
|
|
|
$yohoPage = $('.yoho-page');
|
|
|
|
|
|
//初始化购物车面板显示
|
|
|
function init() {
|
|
|
var $firstRow;
|
|
|
|
|
|
//$colorList = $('.chose-panel .color-list ul>li');
|
|
|
//$sizeList = $('.chose-panel .size-list ul>li');
|
|
|
//firstColorId = $colorList.eq(0).data('id');
|
|
|
//$sizeList.each(function() {
|
|
|
// colorIndex = $(this).data('colorid');
|
|
|
//
|
|
|
// if (colorIndex === firstColorId) {
|
|
|
// $(this).removeClass('hide');
|
|
|
// }
|
|
|
//});
|
|
|
|
|
|
var $firstRow = $sizeRowList.eq(0);
|
|
|
|
|
|
hasChooseColor = false;
|
|
|
hasChooseSize = false;
|
|
|
$curSizeBlock = null;
|
|
|
$imgsThumb = $('.chose-panel').find('.thumb'),
|
|
|
$allChoseItems = $('.chose-items'),
|
|
|
$sizeRowList = $('.size-list ul'),
|
|
|
$leftNum = $('#left-num'),
|
|
|
$firstRow = $sizeRowList.eq(0);
|
|
|
$firstRow.toggleClass('hide');
|
|
|
$curSizeRow = $firstRow;
|
|
|
}
|
...
|
...
|
@@ -75,34 +64,64 @@ function checkColorSizeNum() { |
|
|
return true;
|
|
|
}
|
|
|
|
|
|
init();
|
|
|
|
|
|
function show(data) {
|
|
|
if (data) {
|
|
|
$chosePanel.html(panelTmpl(data));
|
|
|
function show(html, cb) {
|
|
|
if (html) {
|
|
|
$chosePanel.html(html);
|
|
|
init();
|
|
|
}
|
|
|
$('.chose-panel').show();
|
|
|
|
|
|
$('body').css('overflow', 'hidden');
|
|
|
$num = $('#good-num');
|
|
|
cbFn = cb;
|
|
|
}
|
|
|
|
|
|
////显示当前Panel
|
|
|
//function show() {
|
|
|
// $('.chose-panel').show();
|
|
|
//
|
|
|
// $('body').css('overflow', 'hidden');
|
|
|
// $num = $('#good-num');
|
|
|
//}
|
|
|
|
|
|
//隐藏当前Panel
|
|
|
function hide() {
|
|
|
$('.chose-panel').hide();
|
|
|
$('body').css('overflow', 'auto');
|
|
|
cbFn = null;
|
|
|
}
|
|
|
|
|
|
//修改加入购物车的文字和背景
|
|
|
function updateConformButtonClassAndText() {
|
|
|
$chosed = $allChoseItems.find('.chosed');
|
|
|
if (2 === $chosed.closest('.zero-stock').length) {
|
|
|
$('#chose-btn-sure').css('background-color', '#c0c0c0').html('已售罄');
|
|
|
} else {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313').html('确定');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$('.yoho-page').on('touchstart', '.chose-panel', function(e) {
|
|
|
//重置颜色块的库存为0的样式
|
|
|
function resetColorZeroStock($siblingBlock) {
|
|
|
var numArray = ($curSizeBlock.data('numstr') + '').split('/'),
|
|
|
i;
|
|
|
|
|
|
if (!hasChooseSize) {
|
|
|
$siblingBlock.find('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
for (i = 0; i < numArray.length; i++) {
|
|
|
if ('0' === numArray[i]) {
|
|
|
$siblingBlock.find('.block').eq(i).addClass('zero-stock');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 选择了颜色切换商品图片
|
|
|
function changeGoodImgWhenClickColor() {
|
|
|
if (hasChooseColor && curColorIndex) {
|
|
|
$imgsThumb.addClass('hide').eq(curColorIndex).removeClass('hide');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
init();
|
|
|
|
|
|
$yohoPage.on('touchstart', '.chose-panel', function(e) {
|
|
|
var $cur = $(e.target);
|
|
|
|
|
|
if ($cur.closest('.main').length > 0) {
|
...
|
...
|
@@ -113,12 +132,11 @@ $('.yoho-page').on('touchstart', '.chose-panel', function(e) { |
|
|
hide();
|
|
|
});
|
|
|
|
|
|
$('.color-list').on('touchstart', '.block', function(e) {
|
|
|
$yohoPage.on('touchstart', '.color-list .block', function(e) {
|
|
|
var $this = $(this),
|
|
|
$that = $(e.target).closest('.chose-items'),
|
|
|
index,
|
|
|
curSelectedSizeBlock,
|
|
|
$sizeChosed,
|
|
|
curSizeBlock,
|
|
|
$preSiblingBlock,
|
|
|
scindex,
|
|
|
curGoodNum;
|
|
|
|
...
|
...
|
@@ -127,50 +145,46 @@ $('.color-list').on('touchstart', '.block', function(e) { |
|
|
$this.siblings('.chosed').removeClass('chosed');
|
|
|
index = $this.index();
|
|
|
|
|
|
$sizeChosed = $siblingBlock.find('.chosed');
|
|
|
scindex = $sizeChosed.index();
|
|
|
$preSiblingBlock = $siblingBlock.find('.chosed');
|
|
|
scindex = $preSiblingBlock.index();
|
|
|
$curSizeRow = $sizeRowList.eq(index);
|
|
|
|
|
|
// 当前颜色已经是选中状态,再点击时
|
|
|
if ($this.hasClass('chosed')) {
|
|
|
|
|
|
//颜色原来已经是勾选时,要清空剩余件数的提示
|
|
|
$that.find('.num .left-num').html('');
|
|
|
$('#left-num').val(0);
|
|
|
$allChoseItems.find('.num .left-num').html('');
|
|
|
$leftNum.val(0);
|
|
|
hasChooseColor = false;
|
|
|
|
|
|
// 当前颜色不是选中状态,选中时
|
|
|
} else {
|
|
|
hasChooseColor = true;
|
|
|
|
|
|
//根据颜色切换图片
|
|
|
if ($this.closest('.block-list').hasClass('color-list')) {
|
|
|
$('.chose-panel').find('.thumb').addClass('hide').eq(index).removeClass('hide');
|
|
|
}
|
|
|
|
|
|
//把当前选中颜色对应的尺码那一行显示出来
|
|
|
$sizeRowList.addClass('hide');
|
|
|
$curSizeRow.removeClass('hide').addClass('show');
|
|
|
|
|
|
// 之前选中的尺码去掉勾选样式
|
|
|
if ($sizeChosed.length > 0) {
|
|
|
$sizeChosed.removeClass('chosed');
|
|
|
curSelectedSizeBlock = $curSizeRow.children().get(scindex);
|
|
|
if ($preSiblingBlock.length > 0) {
|
|
|
$preSiblingBlock.removeClass('chosed');
|
|
|
|
|
|
}
|
|
|
|
|
|
curSizeBlock = $curSizeRow.children().get(scindex);
|
|
|
|
|
|
// 当前选中颜色对应的尺码行,其对应的尺码加上勾选样式 (前提是要判断下这个尺码是否存在)
|
|
|
if (curSelectedSizeBlock) {
|
|
|
curGoodNum = $(curSelectedSizeBlock).data('num');
|
|
|
$(curSelectedSizeBlock).addClass('chosed');
|
|
|
if (curSizeBlock) {
|
|
|
curGoodNum = $(curSizeBlock).data('num');
|
|
|
$(curSizeBlock).addClass('chosed');
|
|
|
|
|
|
//如果当前有尺码被选中,且数量等于0,则颜色块添加数量为0的样式
|
|
|
if (curGoodNum > 0) {
|
|
|
$that.find('.num .left-num').html('剩余' + curGoodNum + '件');
|
|
|
$('#left-num').val(curGoodNum);
|
|
|
$allChoseItems.find('.num .left-num').html('剩余' + curGoodNum + '件');
|
|
|
$leftNum.val(curGoodNum);
|
|
|
} else {
|
|
|
$(curSelectedSizeBlock).removeClass('zero-stock').addClass('zero-stock');
|
|
|
$that.find('.num .left-num').html('');
|
|
|
$('#left-num').val(0);
|
|
|
$allChoseItems.find('.num .left-num').html('');
|
|
|
$leftNum.val(0);
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -180,106 +194,31 @@ $('.color-list').on('touchstart', '.block', function(e) { |
|
|
curColorIndex = index;
|
|
|
|
|
|
// 设置按钮的样式和文字
|
|
|
$chosed = $('.chose-items').find('.chosed');
|
|
|
if (2 === $chosed.closest('.zero-stock').length) {
|
|
|
$('#chose-btn-sure').css('background-color', '#c0c0c0');
|
|
|
$('#chose-btn-sure').html('已售罄');
|
|
|
} else {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
}
|
|
|
|
|
|
/**if ($chosed.length === 0) {
|
|
|
$this.closest('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
$siblingBlock.find('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
} else if ($chosed.length === 1 && $this.hasClass('chosed')) {
|
|
|
$sizeList.addClass('hide');
|
|
|
|
|
|
//切换尺码信息
|
|
|
$sizeList.each(function() {
|
|
|
colorIndex = $(this).data('colorid');
|
|
|
if (colorIndex === $this.data('id')) {
|
|
|
$(this).removeClass('hide');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
numArray = ($chosed.data('numstr') + '').split('/');
|
|
|
$siblingBlock.find('.block').removeClass('zero-stock');
|
|
|
for (i = 0; i < numArray.length; i++) {
|
|
|
if ('0' === numArray[i]) {
|
|
|
$siblingBlock.find('.block').eq(i).addClass('zero-stock');
|
|
|
}
|
|
|
}
|
|
|
} else if ($chosed.length === 1 && !$this.hasClass('chosed')) {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
|
|
|
$siblingBlock.find('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
$that.find('.num .left-num').html('');
|
|
|
} else if ($chosed.length === 2) {
|
|
|
$siblingBlock.find('.block').removeClass('zero-stock');
|
|
|
for (i = 0; i < currentNumArray.length; i++) {
|
|
|
if ('0' === currentNumArray[i]) {
|
|
|
$siblingBlock.find('.block').eq(i).addClass('zero-stock');
|
|
|
}
|
|
|
}
|
|
|
numArray = ($siblingBlock.find('.chosed').data('numstr') + '').split('/');
|
|
|
$that.find('.num .left-num').html('剩余' + numArray[index] + '件');
|
|
|
if (2 === $chosed.closest('.zero-stock').length) {
|
|
|
$('#chose-btn-sure').css('background-color', '#c0c0c0');
|
|
|
$('#chose-btn-sure').html('已售罄');
|
|
|
} else {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
}
|
|
|
}**/
|
|
|
updateConformButtonClassAndText();
|
|
|
|
|
|
// 修改颜色时修改商品图片
|
|
|
changeGoodImgWhenClickColor();
|
|
|
});
|
|
|
|
|
|
$('.size-list').on('touchstart', '.block', function(e) {
|
|
|
$yohoPage.on('touchstart', '.size-list .block', function(e) {
|
|
|
var $this = $(this),
|
|
|
$that = $(e.target).closest('.chose-items'),
|
|
|
index,
|
|
|
$colorChosed,
|
|
|
curGoodNum;
|
|
|
|
|
|
var $siblingBlock = $this.closest('.block-list').siblings(':first');
|
|
|
|
|
|
$this.siblings('.chosed').removeClass('chosed');
|
|
|
index = $this.index();
|
|
|
|
|
|
$colorChosed = $siblingBlock.find('.chosed');
|
|
|
$curSizeRow = $sizeRowList.eq(index);
|
|
|
|
|
|
// 当前尺码已经是选中状态,再点击时
|
|
|
if ($this.hasClass('chosed')) {
|
|
|
|
|
|
//尺码原来已经是勾选时,要清空剩余件数的提示
|
|
|
$that.find('.num .left-num').html('');
|
|
|
$allChoseItems.find('.num .left-num').html('');
|
|
|
$('#left-num').val(0);
|
|
|
hasChooseSize = false;
|
|
|
|
|
|
// 去掉已经选中颜色的 数量为0的样式
|
|
|
if ($colorChosed.length > 0) {
|
|
|
$colorChosed.removeClass('zero-stock').addClass('zero-stock');
|
|
|
}
|
|
|
|
|
|
// 当前尺码不是选中状态,选中时
|
|
|
} else {
|
|
|
hasChooseSize = true;
|
...
|
...
|
@@ -291,15 +230,15 @@ $('.size-list').on('touchstart', '.block', function(e) { |
|
|
$curSizeBlock.removeClass('chosed');
|
|
|
}
|
|
|
|
|
|
// 如果当前有尺码被选中,且数量等于0,则颜色块添加数量为0的样式, 否则显示剩余件数
|
|
|
// 如果当前有尺码被选中,且数量等于0, 否则显示剩余件数
|
|
|
if (curGoodNum > 0 && hasChooseColor) {
|
|
|
$that.find('.num .left-num').html('剩余' + curGoodNum + '件');
|
|
|
$allChoseItems.find('.num .left-num').html('剩余' + curGoodNum + '件');
|
|
|
$('#left-num').val(curGoodNum);
|
|
|
} else {
|
|
|
$colorChosed.removeClass('zero-stock').addClass('zero-stock');
|
|
|
$that.find('.num .left-num').html('');
|
|
|
$allChoseItems.find('.num .left-num').html('');
|
|
|
$('#left-num').val(0);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$this.toggleClass('chosed');
|
...
|
...
|
@@ -307,83 +246,15 @@ $('.size-list').on('touchstart', '.block', function(e) { |
|
|
$curSizeBlock = $this;
|
|
|
|
|
|
// 设置按钮的样式和文字
|
|
|
$chosed = $('.chose-items').find('.chosed');
|
|
|
if (2 === $chosed.closest('.zero-stock').length) {
|
|
|
$('#chose-btn-sure').css('background-color', '#c0c0c0');
|
|
|
$('#chose-btn-sure').html('已售罄');
|
|
|
} else {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
}
|
|
|
|
|
|
/**if ($chosed.length === 0) {
|
|
|
$this.closest('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
$siblingBlock.find('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
} else if ($chosed.length === 1 && $this.hasClass('chosed')) {
|
|
|
if ($this.closest('.block-list').hasClass('color-list')) {
|
|
|
updateConformButtonClassAndText();
|
|
|
|
|
|
$sizeList.addClass('hide');
|
|
|
|
|
|
//切换尺码信息
|
|
|
$sizeList.each(function() {
|
|
|
colorIndex = $(this).data('colorid');
|
|
|
if (colorIndex === $this.data('id')) {
|
|
|
$(this).removeClass('hide');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
numArray = ($chosed.data('numstr') + '').split('/');
|
|
|
$siblingBlock.find('.block').removeClass('zero-stock');
|
|
|
for (i = 0; i < numArray.length; i++) {
|
|
|
if ('0' === numArray[i]) {
|
|
|
$siblingBlock.find('.block').eq(i).addClass('zero-stock');
|
|
|
}
|
|
|
}
|
|
|
} else if ($chosed.length === 1 && !$this.hasClass('chosed')) {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
|
|
|
$siblingBlock.find('ul>li').each(function() {
|
|
|
$(this).removeClass('zero-stock');
|
|
|
if ('0' === $(this).data('num')) {
|
|
|
$(this).addClass('zero-stock');
|
|
|
}
|
|
|
});
|
|
|
$that.find('.num .left-num').html('');
|
|
|
} else if ($chosed.length === 2) {
|
|
|
$siblingBlock.find('.block').removeClass('zero-stock');
|
|
|
for (i = 0; i < currentNumArray.length; i++) {
|
|
|
if ('0' === currentNumArray[i]) {
|
|
|
$siblingBlock.find('.block').eq(i).addClass('zero-stock');
|
|
|
}
|
|
|
}
|
|
|
numArray = ($siblingBlock.find('.chosed').data('numstr') + '').split('/');
|
|
|
$that.find('.num .left-num').html('剩余' + numArray[index] + '件');
|
|
|
if (2 === $chosed.closest('.zero-stock').length) {
|
|
|
$('#chose-btn-sure').css('background-color', '#c0c0c0');
|
|
|
$('#chose-btn-sure').html('已售罄');
|
|
|
} else {
|
|
|
$('#chose-btn-sure').css('background-color', '#eb0313');
|
|
|
$('#chose-btn-sure').html('确定');
|
|
|
}
|
|
|
}**/
|
|
|
// 重置颜色块的样式
|
|
|
resetColorZeroStock($siblingBlock);
|
|
|
|
|
|
});
|
|
|
|
|
|
$('.btn-minus').on('touchstart', function() {
|
|
|
var num = $num.val();
|
|
|
$yohoPage.on('touchstart', '.btn-minus', function() {
|
|
|
var num = parseInt($num.val(), 10);
|
|
|
|
|
|
leftNum = $('#left-num').val();
|
|
|
|
...
|
...
|
@@ -396,10 +267,8 @@ $('.btn-minus').on('touchstart', function() { |
|
|
}
|
|
|
|
|
|
$num.val(num - 1);
|
|
|
});
|
|
|
|
|
|
$('.btn-plus').on('touchstart', function() {
|
|
|
var num = $num.val();
|
|
|
}).on('touchstart', '.btn-plus', function() {
|
|
|
var num = parseInt($num.val(), 10);
|
|
|
|
|
|
leftNum = $('#left-num').val();
|
|
|
|
...
|
...
|
@@ -414,9 +283,7 @@ $('.btn-plus').on('touchstart', function() { |
|
|
|
|
|
//TODO:库存数验证
|
|
|
$num.val(num + 1);
|
|
|
});
|
|
|
|
|
|
$('#chose-btn-sure').on('touchstart', function() {
|
|
|
}).on('touchstart', '#chose-btn-sure', function() {
|
|
|
|
|
|
var productSku,
|
|
|
buyNumber = $('#good-num').val() - 0,
|
...
|
...
|
@@ -426,10 +293,12 @@ $('#chose-btn-sure').on('touchstart', function() { |
|
|
numInCart = $('.num-tag').html() - 0,
|
|
|
num = $num.val();
|
|
|
|
|
|
//颜色尺码没有选择
|
|
|
if (!checkColorSizeNum()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//TODO status change
|
|
|
if ($('#chose-btn-sure').html() === '已售罄') {
|
|
|
return;
|
|
|
}
|
...
|
...
|
@@ -464,10 +333,13 @@ $('#chose-btn-sure').on('touchstart', function() { |
|
|
}).done(function(res) {
|
|
|
loading.hideLoadingMask();
|
|
|
if (res.code === 200) {
|
|
|
$('.num-tag').html(numInCart + buyNumber);
|
|
|
$('.num-tag').removeClass('hide');
|
|
|
$('.num-tag').html(numInCart + buyNumber).removeClass('hide');
|
|
|
confirming = false;
|
|
|
hide();
|
|
|
|
|
|
if (cbFn) {
|
|
|
cbFn(res.location);
|
|
|
}
|
|
|
}
|
|
|
if (res.message) {
|
|
|
tip.show(res.message);
|
...
|
...
|
|