Blame view

public/js/product/break-code.page.js 13.8 KB
王水玲 authored
1 2 3 4 5 6
/**
 * 断码区
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/5/20
 */
陈峰 authored
7
require('scss/product/sale/break-code-index.page.scss');
郝肖肖 authored
8
陈峰 authored
9
let $ = require('yoho-jquery'),
10
    Swiper = require('yoho-swiper2'),
王水玲 authored
11
    lazyLoad = require('yoho-jquery-lazyload'),
陈峰 authored
12 13 14
    tip = require('js/plugin/tip'),
    filter = require('js/plugin/filter'),
    loading = require('js/plugin/loading');
王水玲 authored
15
陈峰 authored
16
let $goodsContainer = $('#goods-container'),
王水玲 authored
17 18 19 20 21 22
    $goodsChildren = $goodsContainer.children(),
    $coatGc = $($goodsChildren.get(0)),
    $trouserGc = $($goodsChildren.get(1)),
    $shoesgc = $($goodsChildren.get(2)),
    $othergc = $($goodsChildren.get(3));
陈峰 authored
23
let $filterMask;
王水玲 authored
24
陈峰 authored
25
let winH = $(window).height(),
王水玲 authored
26 27 28
    noResult = '<p class="no-result">未找到相关搜索结果</p>';

// 默认筛选条件
陈峰 authored
29
let defaultOpt = require('js/common/query-param');
王水玲 authored
30
陈峰 authored
31
let storeOpt = {};
王水玲 authored
32
陈峰 authored
33
let $listNav = $('#list-nav'),
34 35
    $sizeArea = $('.size-area'),
    $subSize = $('.sub-size'),
王水玲 authored
36 37 38 39 40 41

    // 导航数据信息
    navInfo = {
        coat: {
            reload: true,
            page: 0,
王水玲 authored
42
            end: false
王水玲 authored
43 44 45 46
        },
        trouser: {
            reload: true,
            page: 0,
王水玲 authored
47
            end: false
王水玲 authored
48 49 50 51
        },
        shoes: {
            reload: true,
            page: 0,
王水玲 authored
52
            end: false
王水玲 authored
53 54 55 56
        },
        other: {
            reload: true,
            page: 0,
王水玲 authored
57
            end: false
王水玲 authored
58 59 60 61 62
        }
    },
    $pre = $listNav.find('.active'), // 纪录进入筛选前的active项,初始为选中项
    searching;
陈峰 authored
63
let goodsType = ['coat', 'trouser', 'shoes', 'other'];
王水玲 authored
64
陈峰 authored
65 66
require('js/common');
require('js/common/suspend-cart'); // 悬浮购物车
王水玲 authored
67
王水玲 authored
68 69 70
// 获取url里面的参数的值
function getUrlParam(name) {
    // 构造一个含有目标参数的正则表达式对象
lijing authored
71
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
王水玲 authored
72 73

    // 匹配目标参数
lijing authored
74
    let r = window.location.search.substr(1).match(reg);
王水玲 authored
75 76 77 78 79 80 81

    if (r !== null) {
        return r[2];
    }
    return null; // 返回参数值
}
王水玲 authored
82
defaultOpt = $.extend({
ccbikai(👎🏻🍜) authored
83
    yh_channel: getUrlParam('channel') || defaultOpt.channel || 'boys', // TODO  GO ON!
郭成尧 authored
84 85
    saleType: 1,
    currentUrl: location.pathname
王水玲 authored
86 87 88 89
}, defaultOpt);

storeOpt = $.extend({}, defaultOpt);
王水玲 authored
90 91 92 93
$.each($listNav.find('li'), function(key, item) {
    $(item).addClass(goodsType[key]);
});
94
$.each($sizeArea, function(key, item) {
95 96 97
    $(item).addClass(goodsType[key]);
});
王水玲 authored
98 99
// 判断导航类型
function judgeType(dom) {
lijing authored
100
    let navType;
王水玲 authored
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    if (dom.hasClass('coat')) {
        navType = 'coat';
    } else if (dom.hasClass('trouser')) {
        navType = 'trouser';
    } else if (dom.hasClass('shoes')) {
        navType = 'shoes';
    } else {
        navType = 'other';
    }

    return navType;
}

/**
 * 筛选注册的回调,筛选子项点击后逻辑
 * 需要执行search的场景:1.点选筛选项;2.relaod为true时切换导航;3.下拉加载
 * @param opt {type, id}
 */
function search(opt) {
lijing authored
121
    let setting = {},
王水玲 authored
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
        ext = {},
        att,
        nav, navType,
        page,
        i;

    if (searching) {
        return;
    }

    if ($.type(opt) === 'object') {
        opt = [opt];
    }

    if (opt) {

        // 筛选项变更则重置reload为true
        for (att in navInfo) {
            if (navInfo.hasOwnProperty(att)) {
                navInfo[att].reload = true;
            }
        }
        for (i = 0; i < opt.length; i++) {
            switch (opt[i].type) {
                case 'gender':
                    ext = {
                        gender: opt[i].id
                    };
                    break;
                case 'brand':
                    ext = {
                        brand: opt[i].id
                    };
                    break;
                case 'sort':
                    ext = {
                        sort: opt[i].id
                    };
                    break;
                case 'color':
                    ext = {
                        color: opt[i].id
                    };
                    break;
                case 'size':
                    ext = {
                        size: opt[i].id
                    };
                    break;
                case 'price':
                    ext = {
                        price: opt[i].id
                    };
                    break;
                case 'discount':
                    ext = {
                        discount: opt[i].id
                    };
                    break;
                case 'p_d':
                    ext = {
                        p_d: opt[i].id
                    };
                    break;
runner authored
186 187 188 189
                case 'ageLevel':
                    ext = {
                        age_level: opt[i].id
                    };
王水玲 authored
190
                    break;
王水玲 authored
191 192 193 194 195 196 197 198 199 200
                case 'breakSort':
                    ext = {
                        breakSort: opt[i].id
                    };
                    break;
                case 'breakSize':
                    ext = {
                        breakSize: opt[i].id
                    };
                    break;
王水玲 authored
201 202 203 204
                default:
                    break;
            }
王水玲 authored
205
            $.extend(defaultOpt, ext); // 扩展筛选项
王水玲 authored
206
        }
王水玲 authored
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
    }

    // 导航类别
    navType = judgeType($pre);

    nav = navInfo[navType];
    page = nav.page + 1;
    if (nav.reload) {
        page = 1;
    } else if (nav.end) {

        // 不需要重新加载并且数据请求结束
        return;
    }

    $.extend(setting, defaultOpt, {
        type: 'stock',
        order: 0,
        page: page
    });

    searching = true;
    loading.showLoadingMask();

    $.ajax({
        type: 'GET',
王水玲 authored
233
        url: '/product/sale/search',
王水玲 authored
234 235
        data: setting,
        success: function(data) {
lijing authored
236
            let $container,
王水玲 authored
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
                num;

            switch (navType) {
                case 'coat':
                    $container = $coatGc;
                    break;
                case 'trouser':
                    $container = $trouserGc;
                    break;
                case 'shoes':
                    $container = $shoesgc;
                    break;
                default:
                    $container = $othergc;
                    break;
            }

            if ($container.hasClass('hide')) {
                $container.siblings().addClass('hide');
                $container.removeClass('hide');
            }

            if (data === '') {
                nav.end = true;

                if (nav.reload) {
                    $container.html(noResult);
                }
            } else {
                if (nav.reload) {
                    $container.html(data);
                    lazyLoad($container.find('.lazy'));
                } else {
                    num = $container.find('.good-info').length;
                    $container.append(data);

                    // lazy good-infos who append in
                    lazyLoad($container.find('.good-info:gt(' + (num - 1) + ') .lazy'));
                }
276
            }
ccbikai authored
277
王水玲 authored
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            nav.reload = false;
            nav.page = page;

            searching = false;
            loading.hideLoadingMask();

            window.rePosFooter();
        },
        error: function() {
            tip.show('网络断开连接了~');
            searching = false;
            loading.hideLoadingMask();
        }
    });

}
王水玲 authored
295
// 筛选初始化
296
function filterInit(option) {
王水玲 authored
297
王水玲 authored
298 299 300 301 302
    $.ajax({
        type: 'GET',
        url: '/product/sale/filter',
        data: $.extend(defaultOpt, {
            saleType: '1'
303
        }, option),
王水玲 authored
304 305
        success: function(data) {
306
            $filterMask && $filterMask.remove();
王水玲 authored
307 308 309 310 311 312 313 314 315 316 317 318

            $goodsContainer.append(data);

            $filterMask = $('.filter-mask');

            // 初始化filter&注册filter回调
            filter.initFilter({
                fCbFn: search,
                hCbFn: function() {
                    // 切换active状态到$pre上
                    $pre.addClass('active');
                    $pre.siblings('.filter').removeClass('active');
319
                    $subSize.show();
王水玲 authored
320 321 322 323 324 325 326
                },
                missStatus: true
            });
        }
    });
}
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
setTimeout(function() {
    if ($('.banner-swiper .swiper-slide').length > 1) {
        new Swiper('.swiper-container', {
            observer: true,
            observeParents: true,
            lazyLoading: true,
            lazyLoadingInPrevNext: true,
            loop: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: true,
            paginationClickable: true,
            pagination: '.banner-top .pagination-inner'
        });
    }

    lazyLoad($('.lazy'));
}, 50);
王水玲 authored
344 345 346 347 348 349

$listNav.bind('contextmenu', function() {
    return false;
});

$listNav.on('touchend touchcancel', function(e) {
lijing authored
350
    let $this = $(e.target).closest('li'),
王水玲 authored
351
        nav, sortId, allSub, navType, $active;
王水玲 authored
352 353 354 355 356 357

    e.preventDefault();
    if ($this.hasClass('filter')) {

        // 筛选面板切换状态
        if ($this.hasClass('active')) {
358
            $('.filter-mask').addClass('hide');
王水玲 authored
359
王水玲 authored
360
            // 点击筛选钱的active项恢复active
王水玲 authored
361 362
            $pre.addClass('active');
            $this.removeClass('active');
363
            $subSize.show();
王水玲 authored
364 365
        } else {
            $pre = $this.siblings('.active');
366
            $('.sub-size').hide();
王水玲 authored
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            $pre.removeClass('active');
            $this.addClass('active');
            filter.showFilter();
        }
    } else {

        // 导航类别
        navType = judgeType($this);

        nav = navInfo[navType];

        if (!($this.hasClass('active'))) {

            $active = $this.siblings('.active');
王水玲 authored
382 383 384
            if ($this.attr('class') !== $pre.attr('class')) {
                // 重置筛选项
                filter.resetFilter();
王水玲 authored
385
                $('.classify .shower').removeClass('default');
王水玲 authored
386 387 388 389

                defaultOpt.breakSort = $this.data('id');
                defaultOpt.breakSize = $this.data('allsub');
王水玲 authored
390 391
                filterInit();
                defaultOpt = $.extend({}, storeOpt);
王水玲 authored
392 393
            }
王水玲 authored
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
            $pre = $this; // $pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项

            if ($active.hasClass('filter')) {

                // 若之前active项为筛选,则隐藏筛选面板
                filter.hideFilter();
            } else {

                // 切换container显示
                $goodsContainer.children('.container:not(.hide)').addClass('hide');

                switch (navType) {
                    case 'coat':
                        $coatGc.removeClass('hide');
                        break;

                    case 'trouser':
                        $trouserGc.removeClass('hide');
                        break;

                    case 'shoes':
                        $shoesgc.removeClass('hide');
                        break;
                    case 'other':
                        $othergc.removeClass('hide');
                        break;
                    default:
                        break;
                }
            }

            $active.removeClass('active');
            $this.addClass('active');
            sortId = $this.data('id');
王水玲 authored
428
            allSub = $this.data('allsub');
429
            $sizeArea.eq($this.index()).show().siblings().hide();
王水玲 authored
430 431 432 433
        }

        if (nav.reload) {
            search([{
王水玲 authored
434
                type: 'breakSort',
王水玲 authored
435
                id: sortId
王水玲 authored
436 437 438
            }, {
                type: 'breakSize',
                id: allSub
王水玲 authored
439 440 441 442 443 444
            }]);
        }
    }
    e.stopPropagation();
});
王水玲 authored
445
// 尺码选择事件
446
$sizeArea.on('click', function(e) {
lijing authored
447
    let $this = $(e.target).closest('li'),
王水玲 authored
448
        index = $this.parents('.size-area').index(),
王水玲 authored
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
        $parentType = $listNav.find('li').eq(index),
        nav, sortId, sizeId, navType, $active;

    e.preventDefault();

    if (typeof $this === 'undefined' || $this.length === 0) {
        return;
    }

    // 导航类别
    navType = judgeType($parentType);

    nav = navInfo[navType];

    if ($this.hasClass('active')) {
        return;
    } else {
        nav.reload = true;
        $active = $this.siblings('.active');
        $pre = $parentType; // $pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项
        $active.removeClass('active');
        $this.addClass('active');
        sortId = $parentType.data('id');
        sizeId = $this.data('id');
    }

    if (nav.reload) {
        search([{
王水玲 authored
477
            type: 'breakSort',
王水玲 authored
478 479
            id: sortId
        }, {
王水玲 authored
480
            type: 'breakSize',
王水玲 authored
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
            id: sizeId
        }]);
    }
    e.stopPropagation();
});

function scrollHandler() {

    // 当scroll到1/4$goodsContainer高度后继续请求下一页数据
    if ($(window).scrollTop() + winH >
        $(document).height() - 0.25 * $goodsContainer.height() - 50) {
        if (typeof $pre !== 'undefined') {
            search();
        }
    }
}

// srcoll to load more
$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

// 初始请求最新第一页数据
504
$(function() {
lijing authored
505 506 507
    let type = window.queryString.type;
    let selector;
    let $selectNav;
508 509 510 511 512 513 514

    // type 1: 上装, 3:下装, 6: 鞋装
    if (type && type !== '1') {
        selector = type === '3' ? '.trouser' : '.shoes';
    } else {
        selector = '.coat';
    }
515
    $selectNav = $listNav.find(selector);
ccbikai authored
516
    $selectNav.addClass('active').siblings().removeClass('active');
517 518
    $sizeArea.hide();
    $sizeArea.filter(selector).show();
519 520 521 522 523

    filterInit({
        breakSort: $selectNav.data('id'),
        breakSize: $selectNav.data('allsub')
    });
524 525
    search([{
        type: 'breakSort',
ccbikai authored
526
        id: $selectNav.data('id')
527 528
    }, {
        type: 'breakSize',
ccbikai authored
529
        id: $selectNav.data('allsub')
530 531
    }]);
});
王水玲 authored
532 533 534 535 536 537

$listNav.on('touchstart', 'li', function() {
    $(this).addClass('bytouch');
}).on('touchend touchcancel', function() {
    $listNav.find('li').removeClass('bytouch');
});