brand.js 5.33 KB
/**
 *  品牌一览
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/23
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    Swiper = require('yoho.iswiper'),
    lazyLoad = require('yoho.lazyload');

var swiper,
    $fixTitleBar,
    $brandText,
    $brandHref,
    $brandList = $('.brand-list');
var $icon = $('.search-icon');

var searchH = $('.newbrand-search').outerHeight(),
    headerH = $('.yoho-header').outerHeight(),
    brandSwipe = parseInt(searchH) + parseInt(headerH) - 1,
    minBrandListTop,
    searchArray = [],
    clearTextHammer,
    removeHistory,
    searchList,
    yohoHistory,
    minBrandListTop;

var brandsData,
    $keyword,
    clearTextHammer;

swiper = new Swiper('.swiper-container', {
    lazyLoading: true,
    loop: true,
    autoplay: 3000,
    pagination: '.swiper-pagination .pagination-inner'
});

lazyLoad($('img.lazy'));
$('.yoho-header').css({
    'z-index': 2,
    position: 'fixed',
    top: 0
});

$fixTitleBar = $('<div class="title-bar fixed-title-bar"><h2></h2></div>');
$fixTitleBar.css({
    position: 'fixed',
    top: brandSwipe
}).hide();
minBrandListTop = brandSwipe + $('.hot-brand').outerHeight() + $('.banner-top').outerHeight();
$('.brand-list').last().append($fixTitleBar);

$(window).scroll(function() {
    var scrTop = $(window).scrollTop();

    if ($brandList.eq(0).offset().top < minBrandListTop) {
        $fixTitleBar.hide();
    }

    $brandList.each(function() {
        var offTop = $(this).offset().top - brandSwipe;

        if (scrTop >= offTop) {
            $fixTitleBar.css({
                display: 'block'
            }).find('h2').html($(this).find('.title-bar').text());
        }

    });
});

function searchResult() {
    var keyword = ($keyword.val() + '').toLowerCase();
    var result = {},
        i = 0,
        html = '';

    // 遍历首字母搜索
    $.each(brandsData, function(k, v) {
        if ($.isArray(v)) {

            // 遍历品牌,进行匹配
            $.each(v, function(i, brand) {
                if (brand.name.toLowerCase().indexOf(keyword) > -1) {
                    result[k] = result[k] || [];
                    result[k].push(brand);
                }
            });
        }
    });

    // 根据搜索结果生成 HTML
    $.each(result, function(k, v) {
        var brandHtml = ['<div class="brand-list bar-', i, '">'];

        i++;
        brandHtml.push('<div class="title-bar"><h2>');
        brandHtml.push(k);
        brandHtml.push('</h2></div>');
        $.each(v, function(i, brand) {

            brandHtml.push('<p><a href="' + brand.url + '">' + brand.name);
            if (brand.isNew) {
                brandHtml.push('<i class="icon-hot">HOT</i>');
            }
            if (brand.isHot) {
                brandHtml.push('<i class="icon-new">NEW</i>');
            }
            brandHtml.push('</a></p>');
        });
        brandHtml.push('</div>');
        html += brandHtml.join('');

    });

    // 插入 dom,绑定事件
    $('.search-result').html(html);
    $('.search-result .brand-list p').each(function (index) {
        searchList = new Hammer($('.search-result .brand-list p').eq(index)[0]);
        searchList.on('tap', function() {
            $brandText = $('.search-result .brand-list p').eq(index).find('a').html();
            $brandHref = $('.search-result .brand-list p').eq(index).find('a').attr('href');
            if (localStorage.getItem('yohoHistory')) {
                yohoHistory = localStorage.getItem('yohoHistory');
                searchArray.push(yohoHistory);
            }
            if (searchArray.toString().split($brandText).length < 2) {
                searchArray.push('{"searchName":"' + $brandText + '","searchHref":"' + $brandText + '"}');
            }
            localStorage.setItem('yohoHistory', searchArray);
        });
    });
}

if ($('.brand-search-page').length) {
    brandsData = $.parseJSON($('#brands-data').html());
    $keyword = $('#keyword');

    $keyword.on('input', function() {
        $('.history').css('display', 'none');
        if ($keyword.val().length) {
            searchResult();
            $icon.css('color', '#000');
            $(this).closest('.search-box').css('width', '11.25rem');
            $('.search-action').show();
        } else {
            $icon.css('color', '#b2b2b2');
            $(this).closest('.search-box').css('width', '12.5rem');
            $('.search-action').hide();
        }
    }).focus();

    clearTextHammer = new Hammer($('.clear-text')[0]);
    clearTextHammer.on('tap', function(e) {
        e.preventDefault();
        $('.search-result').html('');
        $('#keyword').val('').trigger('input');
        e.srcEvent.stopPropagation();
    });

    $('form.search-box').on('submit', function() {
        return false;
    });
    if (localStorage && localStorage.getItem('yohoHistory')) {
        yohoHistory = $.parseJSON('[' + localStorage.getItem('yohoHistory') + ']');
        if (yohoHistory) {
            $.each(yohoHistory, function(index, content) {
                $('<a href="' + content.searchHref + '">' + content.searchName + '</a>').appendTo('.historyList');
            });
        }
    } else {
        $('.history').css('display', 'none');
    }

    removeHistory = new Hammer($('.removeHistory')[0]);
    removeHistory.on('tap', function(e) {
        $('.historyList').html(' ');
        localStorage.clear();
    });
}