chose-panel.js 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/**
 * 购物车选择尺寸、颜色和数量面板
 * 显示时构造当前商品信息的HTML插入yoho-page;消失则是直接清除HTML
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/21
 */

// bikai
// 增加init函数,异步请求的接口需要重新初始化一下选择列表
// 异步渲染的模板统一插入 #chose-panel ,兼容页面多个选择框

var $ = require('jquery'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var $chosePanel = $('#chose-panel'),
    $num,
    $chosed,

    // panelTmpl,
    // re = /\d+/,
    leftNum,
    confirming,
    curColorIndex,
    curSizeIndex,
    $curSizeRow,
    hasChooseColor,
    hasChooseSize,
    $curSizeBlock,
    $sizeRowList;

// 读取模板
// $.get('/cart/index/giftinfoTpl', function(html) {
//     if (!html) {
//         tip.show('网络错误');
//         return;
//     }
//     panelTmpl = Handlebars.compile(html);
// }).fail(function() {
//     tip.show('网络错误');
// });

//初始化购物车面板显示
function init() {
    var $firstRow;

    hasChooseColor = false;
    hasChooseSize = false;
    $curSizeBlock = null;
    $sizeRowList = $('.size-list ul');
    $firstRow = $sizeRowList.eq(0);

    //$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');
    //    }
    //});

    $firstRow.toggleClass('hide');
    $curSizeRow = $firstRow;
}
init();

function checkColorSizeNum() {
    if (!hasChooseColor && !hasChooseSize) {
        tip.show('请选择颜色和尺码~');
        return false;
    } else if (!hasChooseColor) {
        tip.show('请选择颜色~');
        return false;
    } else if (!hasChooseSize) {
        tip.show('请选择尺码~');
        return false;
    }
    return true;
}

function show(html) {
    if (html) {
        $chosePanel.html(html);
        init();
    }
    $('.chose-panel').show();

    $('body').css('overflow', 'hidden');
    $num = $('#good-num');
}

////显示当前Panel
//function show() {
//    $('.chose-panel').show();
//
//    $('body').css('overflow', 'hidden');
//    $num = $('#good-num');
//}

//隐藏当前Panel
function hide() {
    $('.chose-panel').hide();
    $('body').css('overflow', 'auto');
}

$('.yoho-page').on('touchstart', '.chose-panel', function(e) {
    var $cur = $(e.target);

    if ($cur.closest('.main').length > 0) {
        return;
    }

    //点击蒙版消失
    hide();
});

$('.color-list').on('touchstart', '.block', function(e) {
    var $this = $(this),
        $that = $(e.target).closest('.chose-items'),
        index,
        curSelectedSizeBlock,
        $sizeChosed,
        scindex,
        curGoodNum;

    var $siblingBlock = $this.closest('.block-list').siblings(':first');

    $this.siblings('.chosed').removeClass('chosed');
    index = $this.index();

    $sizeChosed = $siblingBlock.find('.chosed');
    scindex = $sizeChosed.index();
    $curSizeRow = $sizeRowList.eq(index);

    // 当前颜色已经是选中状态,再点击时
    if ($this.hasClass('chosed')) {

        //颜色原来已经是勾选时,要清空剩余件数的提示
        $that.find('.num .left-num').html('');
        $('#left-num').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 (curSelectedSizeBlock) {
            curGoodNum = $(curSelectedSizeBlock).data('num');
            $(curSelectedSizeBlock).addClass('chosed');

            //如果当前有尺码被选中,且数量等于0,则颜色块添加数量为0的样式
            if (curGoodNum > 0) {
                $that.find('.num .left-num').html('剩余' + curGoodNum + '件');
                $('#left-num').val(curGoodNum);
            } else {
                $(curSelectedSizeBlock).removeClass('zero-stock').addClass('zero-stock');
                $that.find('.num .left-num').html('');
                $('#left-num').val(0);
            }
        }
    }

    // 当前颜色块 切换勾选样式
    $this.toggleClass('chosed');
    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('确定');
        }
    }**/

});

$('.size-list').on('touchstart', '.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('');
        $('#left-num').val(0);
        hasChooseSize = false;

        // 去掉已经选中颜色的 数量为0的样式
        if ($colorChosed.length > 0) {
            $colorChosed.removeClass('zero-stock').addClass('zero-stock');
        }

        // 当前尺码不是选中状态,选中时
    } else {
        hasChooseSize = true;

        curGoodNum = $this.data('num');

        // 之前选中的尺码去掉勾选样式
        if ($curSizeBlock) {
            $curSizeBlock.removeClass('chosed');
        }

        // 如果当前有尺码被选中,且数量等于0,则颜色块添加数量为0的样式, 否则显示剩余件数
        if (curGoodNum > 0 && hasChooseColor) {
            $that.find('.num .left-num').html('剩余' + curGoodNum + '件');
            $('#left-num').val(curGoodNum);
        } else {
            $colorChosed.removeClass('zero-stock').addClass('zero-stock');
            $that.find('.num .left-num').html('');
            $('#left-num').val(0);
        }
    }

    $this.toggleClass('chosed');
    curSizeIndex = index;
    $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')) {

            $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('确定');
        }
    }**/

});

$('.btn-minus').on('touchstart', function() {
    var num = $num.val();

    leftNum = $('#left-num').val();

    if (!checkColorSizeNum()) {
        return;
    }

    if (num === 1 || 0 === leftNum - 0) {
        return;
    }

    $num.val(num - 1);
});

$('.btn-plus').on('touchstart', function() {
    var num = $num.val();

    leftNum = $('#left-num').val();

    if (!checkColorSizeNum()) {
        return;
    }


    if (num - 0 === leftNum || 0 === leftNum) {
        return;
    }

    //TODO:库存数验证
    $num.val(num + 1);
});

$('#chose-btn-sure').on('touchstart', function() {

    var productSku,
        buyNumber = $('#good-num').val() - 0,

        promotionId,
        isEdit = 0,
        numInCart = $('.num-tag').html() - 0,
        num = $num.val();

    if (!checkColorSizeNum()) {
        return;
    }

    if ($('#chose-btn-sure').html() === '已售罄') {
        return;
    }

    leftNum = $('#left-num').val();

    if (num > leftNum) {
        tip.show('您选择的数量超过了最大库存量~');
        return;
    }

    $chosed = $('.block-list>ul>li.chosed');

    if (2 === $chosed.length && 0 === $chosed.closest('.zero-stock').length) {
        productSku = $curSizeBlock.data('skuid');
        promotionId = $('[data-id="' + productSku + '"]').closest('.advance-block').data('promotion-id');
        if (confirming) {
            return false;
        }

        confirming = true;
        loading.showLoadingMask();
        $.ajax({
            method: 'POST',
            url: '/cart/index/add',
            data: {
                productSku: productSku,
                buyNumber: buyNumber,
                promotionId: promotionId,
                isEdit: isEdit
            }
        }).done(function(res) {
            loading.hideLoadingMask();
            if (res.code === 200) {
                $('.num-tag').html(numInCart + buyNumber);
                $('.num-tag').removeClass('hide');
                confirming = false;
                hide();
            }
            if (res.message) {
                tip.show(res.message);
            }
        }).fail(function() {
            tip.show('网络出了点问题~');
        }).always(function() {
            confirming = false;
        });
    }

});

exports.init = init;
exports.show = show;