Blame view

public/js/header.js 30.1 KB
杨延青 authored
1 2 3 4 5
/**
 * 公共头部
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/5/9
 */
yyq authored
6 7
var $ = require('yoho-jquery'),
    handlebars = require('yoho-handlebars');
杨延青 authored
8 9 10 11 12 13 14

var $tool = $('.tool-wrapper'),
    $yohoGroup = $tool.find('.yoho-group'),
    $loginBox = $('#loginBox');

var $head = $('.head-wrapper'),
    $searchForm = $('#search-form'),
yyq authored
15
    $searchKey = $searchForm.find('.search-key'),
yyq authored
16
    $logotrans = $head.find('.main-logo'),
杨延青 authored
17
    $searchSug = $head.find('.search-suggest'),
OF1706 authored
18
    $searchHistory = $head.find('.search-suggest-history'),
OF1706 authored
19
    $searchHistoryHbs = $('#search-suggest-history'),
杨延青 authored
20
    $goCart = $head.find('.go-cart'),
htoooth authored
21
    $myYohoBox = $('#myYohoBox'),
杨延青 authored
22
    $goodsNum = $goCart.find('.goods-num-tip'),
23
    $miniCart = $head.find('.mini-cart-wrapper'),
24
    $dropDown = $tool.find('.nav-drop-down-container');
杨延青 authored
25
26
var $subNav = $('.sub-nav-list.cure .contain-third');
杨延青 authored
27
htoooth authored
28 29
var fetchUserInfoEvent = $.Callbacks('once'); // eslint-disable-line
yyq authored
30 31
var defaultSearch = $('#defaultsearch').val() || '';
yyq authored
32
var thirdLineNum = 9,
杨延青 authored
33 34 35
    delayer,
    centerFn,
    loginFn,
yyq authored
36 37 38 39
    cartFn,
    requestFrame;

var logoAngle = 0,
40 41
    loopTime = 500,
    reg = /^[\^\!\+\-\(\)\:\[\]\\\{\}\~\*\?\|\&\;\/]{0,}$/g;
杨延青 authored
42
biao authored
43 44
var dataLayer = [];
yyq authored
45 46
var cartTimer;
yyq authored
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
// banner和地址的映射
var bannerMap = {
        listboys: '4f78b0f418fc42314d8b6e791cfb7fa8',
        listgirls: '00c1f025a51b6b597dc37925951ea27d',
        listkids: 'b02df11184727701ade1b6de9737d08c',
        listlifestyle: 'fd35c52dced0c880976ba858346d1fc5',
        searchboys: '9fb8986ea700cc27a8057361c3924394',
        searchgirls: 'e3e207a1443ca60c8037fe52a5560c18',
        searchkids: '620fc77f479da8feaeb06f2324e5d0bb',
        searchlifestyle: 'a3c93301c6ffaf3ed0f36a4a451be36d',
        uniquebrandboys: '2ebb0810c0d1a67e5229149c9c3aba7d',
        uniquebrandgirls: '99e23385f4ba4b65f406b7e2968ac821',
        uniquebrandkids: 'a74ebc9b17840c91b9ea46568111fe6b',
        uniquebrandlifestyle: '0e8c81ead53f56302baa4d0ad967f527',
        brandsboys: '77b352db07129c76a9d532acad149f9f',
        brandsgirls: 'bf047f16e52ebc38be5ce9c7623831e6',
        brandskids: 'e3ae1ce9b5e13c6d271ef3eccb831652',
        brandslifestyle: 'e4ac8029c30f65d7f1af030980d140fe',
        newboys: '869d3c5f3b450fb52101d00a61ce87cb',
        newgirls: 'd953b6dfdac02483d1dcce8d96055954',
        newkids: '0874cb6d75df8e0e78f2d475e53ecc08',
        newlifestyle: '43e8fc8e178115c262bbce2bd0012db7',
        saleboys: 'c846e3165c994769b4201d8c32f3ae9b',
        salegirls: '52b1d389edcbc62d65de71b80c4d6ad0',
        salekids: 'ad8b1703c761ba00973868ab5199cc27',
        salelifestyle: '7acc64905c70ac91846f43fb2cec4bbd',
        homeboys: 'b0856a771ef1b59ab1234c74688fa42d',
        homegirls: '2bd61fa12e4933211518f70fe5ce3c48',
        homekids: '895c59e8c64b40399c9533509507320c',
        homelifestyle: '2e037d4e25d2767352ca3e0a4627f7bd',
        indexboys: '0c911d3000f52e8ca7cffb74f5864c29',
        indexgirls: 'b645b8980c423ab30485e0a9d08c2ef7',
        indexkids: '17f6d5d5d454d2c507bc5fcbc90f7756',
        indexlifestyle: '735cd393e841762af8793c346abbbc36'
    },
    cookieMap = {};
htoooth authored
84 85 86
$('#signin-url').attr('href', window.signinUrl());
$('#reg-url').attr('href', window.registerUrl());
王水玲 authored
87
$('.phoneapp').mouseenter(function() {
郝肖肖 authored
88
    window._html && window._hmt.push(['_trackEvent', '二维码', '头部手机版', '', '']); // eslint-disable-line
王水玲 authored
89
}).click(function() {
郝肖肖 authored
90
    window._html && window._hmt.push(['_trackEvent', '二维码', '头部手机版', '', '']); // eslint-disable-line
王水玲 authored
91
});
htoooth authored
92
OF1706 authored
93
$('.we-chat').mouseenter(function() {
郝肖肖 authored
94
    window._html && window._hmt.push(['_trackEvent', '二维码', '头部手机版', '', '']);
OF1706 authored
95
}).click(function() {
郝肖肖 authored
96
    window._html && window._hmt.push(['_trackEvent', '二维码', '头部手机版', '', '']);
OF1706 authored
97 98
});
杨延青 authored
99
// handlebars模板
biao authored
100 101
centerFn = handlebars.compile($('#simple-account-info-tpl').html() || '');
loginFn = handlebars.compile($('#header-login-info-tpl').html() || '');
m  
刘传洋 authored
102
cartFn = require('hbs/header/mini-cart-tpl.hbs'); // handlebars.compile($('#mini-cart-tpl').html() || '');
杨延青 authored
103 104

// handlebars helper
yyq authored
105 106 107 108 109 110 111
handlebars.registerHelper('notzero', function(v1, options) {
    if (v1 !== '0') {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});
杨延青 authored
112
m  
刘传洋 authored
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
handlebars.registerHelper('gt', function(v1, v2, options) {
    var ret = v1 > v2;

    if (options.fn) {
        if (ret) {
            return options.fn(this);
        } else {
            // 不满足条件执行{{else}}部分
            return options.inverse(this);
        }
    } else {
        return ret;
    }
});
biao authored
128
function getSource(column, postition, event) {
yyq authored
129 130 131 132 133
    dataLayer.push({
        louceng: column,
        weizhi: postition,
        event: event
    });
biao authored
134 135 136 137 138 139 140 141
}

function closeCover() {
    var $cover = $('#cover');

    $cover.remove();
}
yyq authored
142 143 144 145 146 147 148 149 150 151 152
// 设置头部banner
function setTopBanner(data) {
    var topbanner = '';

    if (data && data.url !== '') {
        topbanner = '<a target="_blank" href="' + data.url + '" class="page-top-banner"' +
            'style="height:36px;border:none;background-image:url(' + window.unescape(data.src) + ');' +
            'background-position: center;display:block;">&nbsp;</a>';
    } else {
        topbanner = '<div class="yoho-notice">' +
            '<div class="notice-container center-content">' +
OF1706 authored
153
            '<h4 class="notice-title">关于系统升级的公告</h4>' +
htoooth authored
154 155 156 157 158 159 160
            '<div class="notice-content">' +
            '<p class="tips">尊敬的顾客:</p>' +
            '<p class="detail">您好!为了向您提供更优质的服务,目前系统正在升级,请耐心等待。</p>' +
            '<p class="detail">系统升级期间,部分地区用户体验会有暂时中断,如遇紧急事宜,欢迎垂询客服热线:' +
            '400-889-9646 09:00-22:30(周一至周日)。稍后系统将恢复正常' +
            '使用,欢迎您继续光顾YOHO!BUY有货!带来不便之处深表歉意,请您谅解!</p>' +
            '</div>' +
yyq authored
161
            '</div>' +
htoooth authored
162
            '</div>';
yyq authored
163 164 165 166
    }
    $('body').prepend(topbanner);
}
yyq authored
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
function browserLowVersionPrompt() {
    var prompt = '<div class="yoho-browser-notice">' +
        '<div class="center-content">' +
        '<h4 class="notice-title">YOHO!BUY有货</h4>' +
        '<div class="notice-content">' +
        '<p>尊敬的顾客:您好!您当前浏览器版本过低,存在安全风险,为了向您提供更优质的服务,请尽快升级浏览器,并设置安装的浏览器为默认浏览器!</p>' +
        '<div class="browser-list">' +
        '<a href="http://www.google.cn/intl/zh-CN/chrome/browser/desktop/index.html" target="_blank">' +
        '<span class="chrome-img browser-img"></span>' +
        '<span>谷歌浏览器</span>' +
        '</a>' +
        '<a href="http://www.firefox.com.cn/download/" target="_blank">' +
        '<span class="firefox-img browser-img"></span>' +
        '<span>火狐浏览器</span>' +
        '</a>' +
        '<a href="http://se.360.cn/" target="_blank">' +
        '<span class="qihu360-img browser-img"></span>' +
        '<span>360浏览器</span>' +
        '</a>' +
        '<label class="hide-notice">暂不,继续浏览 >></label>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</div>';

    $('body').prepend(prompt);

    $('.hide-notice').on('click', function() {
        $('.yoho-browser-notice').remove();
    });
}
yyq authored
199 200 201
// cookie集合
function exeCookieMap() {
    var cookies = document.cookie;
OF1706 authored
202
OF1706 authored
203
    // console.log(cookies);
yyq authored
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
    var cookiearr = cookies.split(';');
    var i;
    var temparr;
    var key;

    for (i = 0; i < cookiearr.length; i++) {
        temparr = cookiearr[i].split('=');
        if (typeof temparr[0] !== 'undefined') {
            key = temparr[0].replace(/\s/g, '');
            cookieMap[key] = temparr[1];
        }
    }
}

/**
 * 获得banner & 异常通知
 * @return {[type]} [description]
 */
function getBannerAndNotice() {
    var INDEXKIDS = 'indexkids',
        INDEXLIFESTYLE = 'indexlifestyle',
        INDEXBOYS = 'indexboys',
        INDEXWOMAN = 'indexgirls',
        UNIQUEBRAND = 'uniquebrand';
    var url = window.location.href;
    var host = window.location.host;
    var code = '';
    var firstarea;

    exeCookieMap();

    if (url.indexOf('search') !== -1) {
        code = bannerMap['search' + cookieMap._Channel];
    }
    if (url.indexOf('list') !== -1) {
        code = bannerMap['list' + cookieMap._Channel];
    }
    if (url.indexOf('brands') !== -1) {
        code = bannerMap['brands' + cookieMap._Channel];
    }
    if (url.indexOf('new') !== -1) {
        code = bannerMap['new' + cookieMap._Channel];
    }
    if (url.indexOf('sale') !== -1) {
        code = bannerMap['sale' + cookieMap._Channel];
    }
    if (url.indexOf('home') !== -1) {
        code = bannerMap['home' + cookieMap._Channel];
    }
    if (url.indexOf('kids') !== -1) {
        code = bannerMap[INDEXKIDS];
    }
    if (url.indexOf('woman') !== -1 || url.indexOf('girls') !== -1) {
        code = bannerMap[INDEXWOMAN];
    }
    if (url.indexOf('lifestyle') !== -1) {
        code = bannerMap[INDEXLIFESTYLE];
    }
    if (url.indexOf('www.yohobuy.com') !== -1 && window.location.pathname === '/') {
        code = bannerMap[INDEXBOYS];
    }
    firstarea = host.split('.')[0];
    if (firstarea !== 'list' && firstarea !== 'search' && firstarea !== 'www' &&
        firstarea !== 'new' && firstarea !== 'item' && firstarea !== 'guang') {
        code = bannerMap[UNIQUEBRAND + cookieMap._Channel];
    }

    $.getJSON('//new.yohobuy.com/common/getbanner?callback=?', {
        client_type: 'web',
        content_code: code
    }, function(JsonData) {
        if (+JsonData.code === 200) {
            if (typeof JsonData.data === 'object') {
                if (JsonData.data.url !== '') {
                    setTopBanner(JsonData.data);
                } else {
                    setTopBanner();
                }
            }
        }
    });
}
杨延青 authored
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
// 格式化三级菜单
function formatThirdMenu() {
    $subNav.each(function() {
        var $thirdList = $(this).find('.hide-list'),
            list = [],
            i = 0;

        if ($thirdList.length) {
            $thirdList.children().each(function() {
                if (i % thirdLineNum === 0) {
                    list.push('');
                }
                list[list.length - 1] += this.outerHTML + '';
                i++;
            });
            for (i = 0; i < 3; i++) {
303 304 305
                if (!list[i]) {
                    return;
                }
杨延青 authored
306 307 308 309 310 311 312 313 314
                $thirdList.before('<dl class="category-list">' + list[i] + '</dl>');
            }
            $thirdList.remove();
        }
    });
}

// 更新头部登陆信息
function updateLoginInfo(data) {
周少峰 authored
315
    if (data.curLevel * 1 === 3) {
杨延青 authored
316 317 318 319 320 321 322 323 324 325 326 327 328
        data.vip3 = true;
    }

    $tool.find('.simple-user-center').html(centerFn(data));
}

// 同步sso登录状态
function syncLoginInfo() {
    var param = {
        return_type: 'jsonp',
        method: 'open.passport.get'
    };
htoooth authored
329
    return $.getJSON('//www.yohobuy.com/common/passport/?callback=?', param, function(jsonData) {
杨延青 authored
330
        if (jsonData && jsonData.data && jsonData.data.result !== -1) {
周少峰 authored
331
            updateLoginInfo(jsonData.data);
杨延青 authored
332 333 334 335 336 337 338 339 340 341 342 343 344
        } else {
            window.setCookie('_UID', '', {
                domain: '.yohobuy.com',
                expires: -1
            });
        }
    });
}

// 关键词搜索联想
function searchSuggest(key) {
    var param = {
        return_type: 'jsonp',
周少峰 authored
345
        keyword: key
杨延青 authored
346 347
    };
刘传洋 authored
348
    $.getJSON('//search.yohobuy.com/product/search/suggest?callback=?', param, function(jsonData) {
周少峰 authored
349 350
        var searchSuggestHtml;
杨延青 authored
351 352
        if (jsonData.code === 200) {
            if (jsonData.data && jsonData.data.length) {
周少峰 authored
353 354
                searchSuggestHtml = handlebars.compile($('#search-suggest-tml').html() || '');
                $searchSug.html(searchSuggestHtml(jsonData)).show();
杨延青 authored
355 356 357 358 359 360 361
            } else {
                $searchSug.hide();
            }
        }
    });
}
yyq authored
362 363 364 365 366 367
function submitSearch() {
    var searchKey = $searchKey.val();

    searchKey = $.trim(searchKey.toLowerCase());

    if (reg.test(searchKey) && searchKey !== '') {
周少峰 authored
368
        location.href = '//search.yohobuy.com/error?query=' + searchKey;
yyq authored
369 370 371 372
    } else {
        if (searchKey === '') {
            $('#query-key').val(defaultSearch);
        }
OF1706 authored
373
        $searchKey.val(searchKey);
yyq authored
374 375
        $searchForm.submit();
    }
OF1706 authored
376 377 378

    // 触发历史搜索保存
    searchSuggestHistory();  //eslint-disable-line
yyq authored
379 380
}
杨延青 authored
381 382
// 同步mini购物车数据
function syncCratInfo(strG) {
yyq authored
383
    var info, total = 0;
yyq authored
384 385 386 387 388 389 390

    if (strG) {
        window.setCookie('_g', strG, {
            path: '/',
            domain: '.yohobuy.com'
        });
    }
yyq authored
391 392 393 394 395

    if (!$goodsNum || !$goodsNum.length) {
        return cartTimer ? clearInterval(cartTimer) : false;
    }
yyq authored
396 397 398 399 400 401 402 403 404 405
    if (window.cookie('_g')) {
        info = $.parseJSON(window.cookie('_g'));
        total = parseInt(info._nac, 10) + parseInt(info._ac, 10);
        total = total > 0 ? total : 0;
        $goCart.data({
            key: info._k,
            num: total
        });
        $goodsNum.text(total);
    }
yyq authored
406 407

    total ? $goodsNum.removeClass('hide') : $goodsNum.addClass('hide');
杨延青 authored
408 409
}
m  
刘传洋 authored
410 411 412 413
function refreshCartNum() {
    var $totalInfo = $miniCart.find('[data-role=totalinfo]');
    var total = 0;
m  
刘传洋 authored
414
    $miniCart.find('.goods-item.hide').each(function() {
m  
刘传洋 authored
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

        var $t = $(this);
        var val = $t.data('num') || 0;

        total += Number(val);
    });

    if (total > 0) {
        $totalInfo.show();
        $totalInfo.find('[data-role=last-num]').text(total);
    } else {
        $totalInfo.hide();
    }
}

function loadCartDetail() {
yyq authored
431
m  
刘传洋 authored
432
    $.getJSON('//www.yohobuy.com/cart/cart/minicart?callback=?', {}, function(jsonData) {
yyq authored
433 434 435 436 437 438 439 440 441
        var totalGoods = [],
            infoArr = [
                'main_goods',
                'advance_goods',
                'outlet_goods',
                'gift_goods',
                'need_pay_gifts'
            ],
            data;
yyq authored
442 443 444

        if (jsonData.code === 200) {
            data = jsonData.data;
yyq authored
445 446 447 448 449
            $.each(infoArr, function(k, v) {
                if (data[v]) {
                    totalGoods = $.merge(totalGoods, data[v]);
                }
            });
yyq authored
450
yyq authored
451 452 453 454 455
            if (totalGoods && totalGoods.length) {
                data.totalGoods = totalGoods;
                $miniCart.html(cartFn({
                    carData: data
                }));
m  
刘传洋 authored
456
                refreshCartNum();
yyq authored
457
            } else {
yyq authored
458
                syncCratInfo('{"_k":"' + $goCart.data().key + '","_nac":0,"_ac":0,"_r":0}');
yyq authored
459 460 461 462
                $miniCart.html('<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>');
            }
        }
    });
杨延青 authored
463 464 465
}

function delCartGoods(data, callback) {
yyq authored
466 467
    var param = {
        return_type: 'jsonp',
yyq authored
468 469 470
        product_sku: data.sku,
        promotion_id: data.proid,
        product_num: data.num
yyq authored
471 472
    };
OF1706 authored
473
    $.getJSON('//www.yohobuy.com/cart/cart/del?callback=?', param, function(jsonData) {
yyq authored
474
        var strG = '';
yyq authored
475 476

        if (jsonData.code === 200) {
yyq authored
477 478
            if (jsonData.data &&
                typeof jsonData.data.total_goods_num !== 'undefined') {
yyq authored
479 480
                strG = '{"_k":"' + data.key + '","_nac":' +
                    jsonData.data.total_goods_num + ',"_ac":0,"_r":0}';
yyq authored
481
                syncCratInfo(strG);
yyq authored
482 483 484 485

                if (jsonData.data.total_goods_num * 1 === 0) {
                    $miniCart.html('<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>');
                }
yyq authored
486
            }
yyq authored
487 488 489
            return callback();
        }
    });
杨延青 authored
490 491
}
yyq authored
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

/**
 * css3动画
 * @return {[type]}   [description]
 */
function requestFrameAct() {
    var prefixList = ['webkit', 'moz', 'ms'];
    var func1 = prefixList[0] + 'RequestAnimationFrame';
    var func2 = prefixList[1] + 'RequestAnimationFrame';
    var func3 = prefixList[2] + 'RequestAnimationFrame';

    if (window[func1]) {
        return function(callback) {
            requestAnimationFrame(callback);
        };
    }
    if (window[func2]) {
        return function(callback) {
            window[func2](callback);
        };
    }
    if (window[func3]) {
        return function(callback) {
            window[func3](callback);
        };
    }
    return function(callback) {
        window.setTimeout(callback, 67);
    };
}


/**
 * css3动画
 * @return {[type]} [description]
 */

function tsAnimate() {
    logoAngle += 10;
    $logotrans.css({
        transform: 'rotateX(' + logoAngle + 'deg)',
        '-webkit-transform': 'rotateX(' + logoAngle + 'deg)',
        '-moz-transform': 'rotateX(' + logoAngle + 'deg)'
    });
    if (logoAngle / 90 % 2 === 1) {
        $logotrans.toggleClass('logo-cn');
    }
    if (logoAngle / 90 % 2 === 0 && logoAngle % 360 !== 0) {
        window.setTimeout(tsAnimate, 3000);
    } else {
        if (logoAngle % 360 === 0) {
            window.setTimeout(tsAnimate, 1 * 60 * 1000);
        } else {
            requestFrame(function() {
                tsAnimate();
            });
        }
    }
}

/**
 * 淡出
 * @return {[type]} [description]
 */
function fadeAnimate() {
    var cycle = 3000;
王水玲 authored
559
    if ($logotrans.hasClass('logo-cn-ie')) {
yyq authored
560 561 562 563
        cycle = 1 * 60 * 1000;
    }

    $logotrans.fadeOut(loopTime, function() {
王水玲 authored
564
        $logotrans.toggleClass('logo-cn-ie');
yyq authored
565
        $logotrans.fadeIn(loopTime, function() {
王水玲 authored
566 567 568
            window.setTimeout(function() {
                fadeAnimate();
            }, cycle);
yyq authored
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        });
    });
}


/**
 * 检测是否支持css3的动画
 * @return {Boolean} [description]
 */
function isSupportCss3Animation() {
    var thisFunc,
        prefixList = ['webkit', 'moz', 'ms'],
        i;

    for (i = 0; i < prefixList.length; i++) {
        thisFunc = prefixList[i] + 'RequestAnimationFrame';
        if (window[thisFunc]) {
            return true;
        } else {
            return false;
        }
    }
591 592 593 594 595 596
}

// 处理pageCache频道显示异常问题
function syncPageChannel() {
    var $header = $('#yoho-header'),
        $navs;
yyq authored
597 598
    var channel = window.homePage || window.cookie('_Channel') || 'boys',
        qs = window.queryString();
599
yyq authored
600
    channel = qs.channel ? qs.channel : channel;
yyq authored
601
602 603
    if ($header && $header.length) {
        $navs = $header.find('.' + channel);
yyq authored
604
yyq authored
605 606 607
        if (!$navs.length) {
            channel = 'boys';
            $navs = $header.find('.' + channel);
608 609 610 611 612 613 614
        }

        // 更新频道菜单选中状态
        $navs.siblings('.cure').removeClass('cure');
        $navs.addClass('cure');

        // 更新频道颜色
yyq authored
615
        $header.addClass(channel).find('.func-area').removeClass('hide');
616 617 618 619

        // 更新三级菜单jq对象
        $subNav = $('.sub-nav-list.cure .contain-third');
    }
yyq authored
620 621 622 623 624 625 626 627
}

if (isSupportCss3Animation()) {
    requestFrame = requestFrameAct();
    window.setTimeout(tsAnimate, 3000);
} else {
    window.setTimeout(fadeAnimate, 3000);
}
628
syncPageChannel();
yyq authored
629
formatThirdMenu(); // 格式化三级菜单
yyq authored
630
yyq authored
631 632 633 634 635 636 637 638
(function() {
    if (document.all && !document.querySelector) {
        browserLowVersionPrompt(); // 低版本升级提示
    } else {
        getBannerAndNotice(); // 获取头部banner
    }
}());
yyq authored
639
cartTimer = setInterval(syncCratInfo, 2000); // 定时同步购物车数量
杨延青 authored
640
htoooth authored
641 642
// 获取头部登陆信息
(function() {
htoooth authored
643 644
    var uid = getUid(),   //eslint-disable-line
        profileName = getProfileName();  // eslint-disable-line
htoooth authored
645 646 647 648 649 650 651

    var info = {
        usercenter: '//www.yohobuy.com/home?t=' + new Date().getTime(),
        nickname: profileName,
        signout: '//www.yohobuy.com/logout.html'
    };
htoooth authored
652 653
    if (uid !== 0) {
        $loginBox.html(loginFn(info));
htoooth authored
654 655 656 657 658
    }

    $loginBox.show();
}());
htoooth authored
659
fetchUserInfoEvent.add(function() {
htoooth authored
660
    if ($('.simple-user-center').length === 0) {
htoooth authored
661 662 663 664
        return;
    }
    syncLoginInfo();
});
htoooth authored
665
OF1706 authored
666 667 668 669 670 671 672 673 674
if ($('[data-role=tool-wrapper]').size() > 0) {
    $dropDown.on('mouseenter', function() {
        $(this).addClass('nav-drop-down-hover');
        $(this).find('.nav-drop-down').fadeIn();
    }).mouseleave(function() {
        $(this).removeClass('nav-drop-down-hover');
        $(this).find('.nav-drop-down').hide();
    });
}
675
htoooth authored
676
$myYohoBox.hover(function() {
htoooth authored
677
    var uid = getUid();  // eslint-disable-line
htoooth authored
678 679 680 681 682 683 684 685 686 687 688 689

    $myYohoBox.addClass('myyoho-hover');

    if (uid === 0) {
        return;
    }

    fetchUserInfoEvent.fire();
}, function() {
    $myYohoBox.removeClass('myyoho-hover');
});
杨延青 authored
690 691 692 693 694 695 696 697 698 699
$yohoGroup.hover(function() {
    var data = $(this).data();

    $(this).text(data.cn);
}, function() {
    var data = $(this).data();

    $(this).text(data.en);
});
yyq authored
700
$searchKey.keyup(function(e) {
杨延青 authored
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    var val = $.trim($(this).val()),
        $child = $searchSug.find('li'),
        $act = $searchSug.find('.action'),
        $focus;

    if (e.which > 36 && e.which < 41) {

        if (e.which === 38) {
            $focus = $act.prev();
            if (!$act.length || !$focus.length) {
                $focus = $child.eq($child.length - 1);
            }
        } else if (e.which === 40) {
            $focus = $act.next();
            if (!$act.length || !$focus.length) {
                $focus = $child.eq(0);
            }
        } else {
            return;
        }

        $child.removeClass('action');
        $focus.addClass('action');
        $(this).val($focus.find('.searchvalue').text());
    } else if (e.which === 13) {
yyq authored
726
        submitSearch();
杨延青 authored
727
    } else {
728 729 730 731
        if ($searchSug && $searchSug.length) {
            val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符
            searchSuggest(val);
        }
杨延青 authored
732
    }
OF1706 authored
733
OF1706 authored
734
    if ($searchKey.val() === '') {
OF1706 authored
735 736 737 738
        // 查询历史记录
        searchSuggestHistory();  //eslint-disable-line
    } else {
        $searchHistory.hide();
OF1706 authored
739
    }
OF1706 authored
740
yyq authored
741 742 743 744 745 746 747 748
}).focus(function() {
    var val = $.trim($(this).val());

    if (val === defaultSearch) {
        $(this).val('');
    }

    $(this).css('color', '#333');
OF1706 authored
749
OF1706 authored
750
OF1706 authored
751 752 753 754
    if ($searchKey.val() === '') {
        // 查询历史记录
        searchSuggestHistory();  //eslint-disable-line
    }
OF1706 authored
755
OF1706 authored
756
yyq authored
757 758 759 760 761 762
}).blur(function() {
    var val = $.trim($(this).val());

    if (val === '') {
        $(this).val(defaultSearch).css('color', '#999');
    }
OF1706 authored
763
yyq authored
764 765
    setTimeout(function() {
        $searchSug.hide();
OF1706 authored
766
        $searchHistory.hide();
yyq authored
767
    }, 200);
杨延青 authored
768 769
});
770 771 772
if ($miniCart && $miniCart.length) {
    $goCart.hover(function() {
        var data, _html = '';
杨延青 authored
773
774 775
        $searchHistory.hide();
776 777 778
        if ($goCart.hasClass('on-hover')) {
            return;
        }
杨延青 authored
779
780 781 782 783 784 785 786 787 788 789
        data = $goCart.data();
        if (data && data.num * 1) {
            _html = '<div class="loading-cart"><h3>加载中,请稍后</h3></div>';
            loadCartDetail(data.key);
        } else {
            _html = '<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>';
        }
        $miniCart.html(_html);
        $goCart.addClass('on-hover');
    }, function() {
m  
刘传洋 authored
790
        $goCart.removeClass('on-hover');
791
    });
杨延青 authored
792
793 794 795 796 797
    $goCart.on('click', '.cart-goods-del', function() {
        var $dom = $(this),
            data = $dom.data(),
            callback;
798 799
        $searchHistory.hide();
800 801 802
        if (data) {
            callback = function() {
                $dom.closest('.goods-item').remove();
m  
刘传洋 authored
803 804
                $miniCart.find('.goods-item:lt(5)').removeClass('hide');
                refreshCartNum();
805 806 807 808 809 810
            };
            data.key = $goCart.data().key;
            delCartGoods(data, callback);
        }
    });
}
杨延青 authored
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

$subNav.on({
    mouseenter: function() {
        var $thirdNav = $(this).children('.third-nav-wrapper'),
            $show = $thirdNav.find('.show-detail'),
            param = {};

        delayer = setTimeout(function() {
            $thirdNav.show();
        }, 200);

        if (!$show.length || $show.hasClass('show')) {
            return;
        }
        param.content_code = $show.data().code;
        param.client_type = 'web';
yyq authored
827 828
        param.width = 337;
        param.height = 250;
杨延青 authored
829
        param._ = new Date();
yyq authored
830
        $.getJSON('//new.yohobuy.com/common/getbanner?callback=?', param, function(JsonData) {
yyq authored
831 832 833 834 835 836 837
            if (JsonData.code === 200) {
                $show.addClass('show');
                $show.find('img').attr('src', JsonData.data.src);
                $show.find('a').attr('href', JsonData.data.url);
                $show.find('.title').text(JsonData.data.title);
            }
        });
杨延青 authored
838 839 840 841 842 843 844 845 846 847
    },
    mouseleave: function() {
        var $thirdNav = $(this).children('.third-nav-wrapper');

        if (delayer) {
            clearTimeout(delayer);
        }
        $thirdNav.hide();
    }
});
runner authored
848 849 850 851 852 853 854 855 856 857 858 859 860

/**
 * 首次进入有弹窗
 * @return {[type]} [description]
 */
function actionCover() {
    var gender = window.cookie('_Gender');
    var newMask = '';
    var windowheight = '';
    var selfheight = '';
    var containertop;
    var length = '';
周少峰 authored
861
    if (window.location.href === document.location.protocol + '//www.yohobuy.com/' &&
862
        (typeof gender === 'undefined' || gender === '' || gender === null)) {
runner authored
863 864 865 866 867 868 869 870 871 872 873 874 875
        $.get('/guide', function(data) {
            newMask = document.createElement('div');
            newMask.id = 'cover';
            newMask.innerHTML = data;
            document.body.appendChild(newMask);
            windowheight = $(window).height();
            selfheight = $('.guide-box').height();
            containertop = windowheight / 2 - selfheight / 2;
            length = $('.guide-box .clear').find('li').length;
            $('.guide-box').css({
                width: (200 * length) + 'px',
                top: containertop + 'px'
            });
yyq authored
876
            $('#cover').bind('click', function() {
runner authored
877 878 879 880 881 882 883 884 885 886 887
                window.setCookie('_Gender', '1,3', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 90
                });
                window.setCookie('_Channel', 'boys', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 7
                });
                closeCover();
biao authored
888
            });
yyq authored
889
            $('#cover .guide-box .close').bind('click', function() {
runner authored
890 891 892 893 894 895 896 897 898 899 900 901 902
                getSource('弹窗', 'CLOSE', 'homepage_man');
                window.setCookie('_Gender', '1,3', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 90
                });
                window.setCookie('_Channel', 'boys', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 7
                });
                closeCover();
            });
yyq authored
903
            $('.boys img , .boys .go').bind('click', function() {
runner authored
904 905 906 907 908 909 910 911 912 913 914 915 916
                getSource('弹窗', 'BOYS', 'homepage_man');
                window.setCookie('_Gender', '1,3', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 90
                });
                window.setCookie('_Channel', 'boys', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 7
                });
                closeCover();
            });
yyq authored
917
            $('.girls img, .girls .go').bind('click', function() {
runner authored
918 919 920 921 922 923 924 925 926 927 928 929
                getSource('弹窗', 'GIRLS', 'homepage_woman');
                window.setCookie('_Gender', '2,3', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 90
                });
                window.setCookie('_Channel', 'girls', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 7
                });
            });
yyq authored
930
            $('.lifestyle img, .lifestyle .go').bind('click', function() {
runner authored
931 932 933 934 935 936 937 938 939 940
                window.setCookie('_Channel', 'lifestyle', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 7
                });
                getSource('弹窗', 'LIEFSTYLE', 'homepage_lifestyle');
            });
            $('#cover .guide-box').bind('click', function(event) {
                event.stopPropagation();
            });
runner authored
941
        });
htoooth authored
942 943 944
        $('#cover .guide-box').bind('click', function(e) {
            e.stopPropagation();
        });
runner authored
945
    }
biao authored
946
}
runner authored
947
yyq authored
948
window.submitSearch = submitSearch;
949
OF1706 authored
950 951
function emailUserCertTip() {
OF1706 authored
952
    return $.getJSON('//www.yohobuy.com/passport/cert/headerTip?callback=?', function(jsonData) {
OF1706 authored
953
        if (jsonData && jsonData.data === 'Y') {
954
            $('#yoho-header .swindle-info').addClass('hide');
OF1706 authored
955 956 957 958 959 960 961
            $('#yoho-header .yoho-cert-tip').html(
                '<i class="iconfont left">&#xe63f;</i> ' +
                '您的账号安全等级较低,建议您立即<a href="//www.yohobuy.com/passport/cert/index">绑定手机号</a>');
        }
    });
}
刘传洋 authored
962 963 964 965 966
/**
 * 查询跳转后保留关键字
 * @return {[type]} [description]
 */
function actionAddKeyWords() {
yyq authored
967
    var keywords = $('#nav_keyword').text();
刘传洋 authored
968
yyq authored
969 970
    if (keywords !== '') {
        $searchKey.val(keywords).css('color', '#333');
刘传洋 authored
971
    } else {
yyq authored
972
        $searchKey.val(defaultSearch).css('color', '#999');
刘传洋 authored
973 974 975 976 977 978 979 980
    }
    $(document).click(function(e) {
        if (!$(e.target).closest('.searchspan, .search-list').length) {
            $('.search-list').hide();
        }
    });
}
OF1706 authored
981 982 983 984
/**
 * 历史记录 搜索提示  api
 * @return {[type]} [description]
 */
刘传洋 authored
985
OF1706 authored
986
function searchSuggestHistory() {
OF1706 authored
987 988
    var param = {
        return_type: 'jsonp',
OF1706 authored
989
        query: encodeURIComponent((window.queryString().query || '').replace(/\+/ig, ' '))
OF1706 authored
990
    };
OF1706 authored
991
李奇 authored
992
    if ($searchHistory.children().length) {
OF1706 authored
993 994 995
        return $searchHistory.show();
    }
OF1706 authored
996 997 998
    if ($searchKey.attr('alt')) {
        param.query = '';
        $searchKey.attr('alt', false);
OF1706 authored
999 1000
    }
OF1706 authored
1001
    $.getJSON('//search.yohobuy.com/product/search/history?callback=?', param, function(jsonData) {
OF1706 authored
1002
OF1706 authored
1003
        if ($searchKey.val() === '') {
OF1706 authored
1004
OF1706 authored
1005 1006
            var searchSuggestHistoryHtml; //eslint-disable-line
OF1706 authored
1007 1008 1009
            searchSuggestHistoryHtml = handlebars.compile($searchHistoryHbs.html() || '');
            $searchHistory.html(searchSuggestHistoryHtml(jsonData)).show();
OF1706 authored
1010
            if ($searchHistory.find('li').length === 0) {
OF1706 authored
1011
                $searchHistory.hide();
OF1706 authored
1012 1013
            } else {
                $searchHistory.show();
OF1706 authored
1014 1015
            }
OF1706 authored
1016
            // 历史记录清空
yyq authored
1017
            $('.search-suggest-title .search-del').click(function() {
OF1706 authored
1018 1019
                window.setCookie('_History', '', {domain: '.yohobuy.com'});
                $(this).closest('.search-suggest-history').hide();
OF1706 authored
1020
                $searchKey.attr('alt', true);
OF1706 authored
1021 1022
                return false;
            });
刘传洋 authored
1023 1024 1025 1026 1027

        }
    });
}
OF1706 authored
1028
$(function() {
OF1706 authored
1029
    $('.code-down-box').bind('click', '.icon-del', function() {
OF1706 authored
1030
        $('.code-down-box').hide();
OF1706 authored
1031 1032 1033
    });
});
yyq authored
1034
actionCover();
1035
actionAddKeyWords();
m  
刘传洋 authored
1036
setTimeout(emailUserCertTip, 0);
1037