Authored by 梁志锋

update

@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 */ 5 */
6 6
7 var $ = require('jquery'), 7 var $ = require('jquery'),
8 - Hammer = require('hammer'), 8 + Hammer = require('yoho.hammer'),
9 Swiper = require('yoho.iswiper'), 9 Swiper = require('yoho.iswiper'),
10 lazyLoad = require('yoho.lazyload'); 10 lazyLoad = require('yoho.lazyload');
11 11
@@ -16,7 +16,13 @@ var searchH = $('.newbrand-search').outerHeight(), @@ -16,7 +16,13 @@ var searchH = $('.newbrand-search').outerHeight(),
16 headerH = $('.yoho-header').outerHeight(), 16 headerH = $('.yoho-header').outerHeight(),
17 brandSwipe = parseInt(searchH) + parseInt(headerH) - 1; 17 brandSwipe = parseInt(searchH) + parseInt(headerH) - 1;
18 18
19 -var myHammer; 19 +var myHammer,
  20 + brandsData,
  21 + $keyword,
  22 + searchBtnHammer,
  23 + clearTextHammer;
  24 +
  25 +var $rightBarCon = $('#right-bar .con').find('b').unbind();
20 26
21 swiper = new Swiper('.swiper-container', { 27 swiper = new Swiper('.swiper-container', {
22 lazyLoading: true, 28 lazyLoading: true,
@@ -32,31 +38,151 @@ $('.yoho-header').css({ @@ -32,31 +38,151 @@ $('.yoho-header').css({
32 }); 38 });
33 $('.banner-top').css('padding-top', '90px'); 39 $('.banner-top').css('padding-top', '90px');
34 40
35 -if ($brandList.length > 0) {  
36 - $(window).scroll(function() {  
37 - var scrTop = $(window).scrollTop();  
38 -  
39 - $('.brand-list').each(function() {  
40 - var offTop = $(this).offset().top - brandSwipe;  
41 -  
42 - if (scrTop >= offTop) {  
43 - $brandList.find('.title-bar').css('position', 'static');  
44 - $(this).find('.title-bar').css({  
45 - position: 'fixed',  
46 - top: brandSwipe  
47 - });  
48 - } else {  
49 - $(this).find('.title-bar').css('position', 'static'); 41 +$(window).scroll(function() {
  42 + var scrTop = $(window).scrollTop();
  43 +
  44 + $('.brand-list').each(function() {
  45 + var offTop = $(this).offset().top - brandSwipe;
  46 +
  47 + if (scrTop >= offTop) {
  48 + $brandList.find('.title-bar').css('position', 'static');
  49 + $(this).find('.title-bar').css({
  50 + position: 'fixed',
  51 + top: brandSwipe
  52 + });
  53 + } else {
  54 + $(this).find('.title-bar').css('position', 'static');
  55 + }
  56 + });
  57 +});
  58 +
  59 +
  60 +function rightBarBindClick() {
  61 + $rightBarCon = $('#right-bar .con').find('b').unbind();
  62 + myHammer = new Hammer($rightBarCon[0]);
  63 + myHammer.on('tap', function(e) {
  64 + var index = $rightBarCon.index();
  65 +
  66 + if ($('.bar-' + index).size() > 0) {
  67 + document.body.scrollTop = parseInt($('.bar-' + index)[0].offsetTop) - parseInt(brandSwipe - 1);
  68 + }
  69 + e.srcEvent.stopPropagation();
  70 + });
  71 +}
  72 +
  73 +if ($rightBarCon[0]) {
  74 + rightBarBindClick();
  75 +}
  76 +
  77 +/**
  78 + * 品牌搜索
  79 + */
  80 +
  81 +function searchResult() {
  82 + var keyword = ($keyword.val() + '').toLowerCase();
  83 + var result = {},
  84 + i = 0,
  85 + rightBarHtml,
  86 + html = '';
  87 +
  88 + // 遍历首字母搜索
  89 + $.each(brandsData, function(k, v) {
  90 + if ($.isArray(v)) {
  91 +
  92 + // 遍历品牌,进行匹配
  93 + $.each(v, function(i, brand) {
  94 + if (brand.name.toLowerCase().indexOf(keyword) > -1) {
  95 + result[k] = result[k] || [];
  96 + result[k].push(brand);
  97 + }
  98 + });
  99 + }
  100 + });
  101 +
  102 + // 根据搜索结果生成 HTML
  103 + rightBarHtml = '<div id="right-bar" class="right-bar"><div class="con" id="con">';
  104 + $.each(result, function(k, v) {
  105 + var brandHtml = ['<div class="brand-list bar-', i, '">'];
  106 +
  107 + i++;
  108 + brandHtml.push('<div class="title-bar"><h2>');
  109 + brandHtml.push(k);
  110 + brandHtml.push('</h2></div>');
  111 + $.each(v, function(i, brand) {
  112 + brandHtml.push('<p><a href="' + brand.url + '">' + brand.name);
  113 + if (brand.isNew) {
  114 + brandHtml.push('<i class="icon-hot">Hot</i>');
  115 + }
  116 + if (brand.isHot) {
  117 + brandHtml.push('<i class="icon-new">New</i>');
50 } 118 }
  119 + brandHtml.push('</a></p>');
51 }); 120 });
  121 + brandHtml.push('</div>');
  122 + html += brandHtml.join('');
  123 +
  124 + if (k.indexOf(0) > -1) {
  125 + k = '#';
  126 + }
  127 +
  128 + rightBarHtml = rightBarHtml + '<b>' + k + '</b>';
52 }); 129 });
53 -}  
54 130
55 -myHammer = new Hammer($('#right-bar .con').find('b').unbind()[0]);  
56 -myHammer.on('tap', function(e) {  
57 - var index = $(this).index(); 131 + rightBarHtml += '</div>';
58 132
59 - if ($('.bar-' + index).size() > 0) {  
60 - document.body.scrollTop = parseInt($('.bar-' + index)[0].offsetTop) - parseInt(brandSwipe - 1); 133 + if (Object.keys(result).length > 0) {
  134 + html += rightBarHtml;
61 } 135 }
62 -}); 136 +
  137 + // 插入 dom,绑定事件
  138 + $('.search-result').html(html);
  139 +
  140 + if (Object.keys(result).length > 0) {
  141 + rightBarBindClick();
  142 + }
  143 +}
  144 +
  145 +if ($('.brand-search-page').length) {
  146 + brandsData = $.parseJSON($('#brands-data').html());
  147 + $keyword = $('#keyword');
  148 +
  149 + $keyword.on('input', function() {
  150 + if ($keyword.val().length) {
  151 + searchResult();
  152 + $(this).closest('.search-box').css('width', '11.25rem');
  153 + $('.search-action').show();
  154 + } else {
  155 + $(this).closest('.search-box').css('width', '12.5rem');
  156 + $('.search-action').hide();
  157 + }
  158 + });
  159 +
  160 +
  161 + searchBtnHammer = new Hammer($('.search-btn')[0]);
  162 + searchBtnHammer.on('tap', function(e) {
  163 + searchResult();
  164 + e.srcEvent.stopPropagation();
  165 + });
  166 +
  167 + clearTextHammer = new Hammer($('.clear-text')[0]);
  168 + clearTextHammer.on('tap', function(e) {
  169 + $('#keyword').val('').trigger('input');
  170 + e.srcEvent.stopPropagation();
  171 + });
  172 +
  173 + $('form.search-box').on('submit', function() {
  174 + return false;
  175 + });
  176 +
  177 + // clearHistoryHammer = new Hammer($('#clear-text')[0]);
  178 + // clearHistoryHammer.on('tap', function() {
  179 +
  180 + // // setcookie('h_brands', '', {
  181 + // // expire: -1,
  182 + // // path: '/',
  183 + // // domain: '.m.yohobuy.com'
  184 + // // });
  185 + // $('#history-keyword').remove();
  186 + // $(this).hide();
  187 + // });
  188 +}