index.js 6.11 KB
/**
 * 搜索页
 * @author:zxr
 * @date: 2016/8/15
 */

var $ = require('yoho-jquery'),
    security = require('../../plugin/security'),
    tip = require('../../plugin/tip'),
    Hammer = require('yoho-hammer'),
    dialog = require('../../plugin/dialog');

var $input = $('#search-input input');

var $clear = $('#search-input .clear-input');

var $form = $('#search-form');

var $history = $('.history');
var $historySearch = $('.history-search');
var $clearHistory = $('#clear-history');

// var $buriedpoint = $('.buriedpoint');
var $search = $('#search');
var searchUrl = $search.closest('form').attr('action');

var writeSearch = require('./write-search');

var ranToken = writeSearch.getRanToken();
var historyval = writeSearch.getHistoryval();

var chHammer, cHammer;

var C_ID, POS_ID, FLR_INDEX;

require('../../common');

chHammer = new Hammer($clearHistory[0]);

chHammer.on('tap', function() {
    dialog.showDialog({
        dialogText: '您确定要删除您的最近搜索吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {

        if (localStorage) {
            localStorage.removeItem(historyval);
        }

        $history.html('');
        $historySearch.hide();
        $clearHistory.hide();

        window.rePosFooter();

        dialog.showDialog({
            dialogText: '删除成功',
            autoHide: true,
            fast: true
        });
    });
});

// 跳到搜索页
function goSearch(query) {
    // 保存搜索的内容
    writeSearch.setHistoryValFun(query);
    document.location.href = searchUrl + '?query=' + query;
}

// 搜索输入联动
function inputAction() {
    var $searchAssociate = $('.search-associate');
    var $icon = $('.search-icon');
    var $searchItems = $('.search-items');

    $input.on('input', function() {
        if ($input.val() === '') {
            $icon.css('color', '#b2b2b2');
            $clear.addClass('hide');
            $searchAssociate.html('');
            $searchItems.show();
            $searchAssociate.hide();
        } else {
            $icon.css('color', '#666');
            $clear.removeClass('hide');
            $searchAssociate.show();
        }

        // 联动搜索
        $.ajax({
            url: '/product/search/fuzzyDatas',
            data: {
                keyword: $input.val()
            },
            dataType: 'json',
            success: function(data) {
                var ajaxHtml = '';
                var i;

                if (data.length > 0) {

                    for (i = 0; i < data.length; i++) {
                        ajaxHtml += '<li><span class="keyword">' + data[i].keyword + '</span><span class="count">' +
                        data[i].count + ' items<i class="iconfont">&#xe614;</i></span></li>';
                    }

                    $searchAssociate.html(ajaxHtml);
                    $searchItems.hide();
                } else {
                    $searchAssociate.html('');
                }

                $searchAssociate.find('li').on('touchend', function() {
                    goSearch($(this).find('.keyword').html());
                });
            },
            error: function() {
                tip.show('网络断开连接了~');
            }
        });
    });
}

C_ID = window._ChannelVary[window.cookie('_Channel')];

// 热门搜索、最近搜索事件
$('.search-items .search-group').on('click', 'li', function(event) {
    var query = '';

    if (event.target.nodeName === 'A') {
        query = $(event.target).html();
    }

    if (event.target.nodeName === 'LI') {
        query = $(event.target).find('a').html();
    }

    if ($(this).parents('.search-group').hasClass('history-search')) {
        POS_ID = 1;
        FLR_INDEX = 1;
    } else {
        POS_ID = 2;
        FLR_INDEX = 2;
    }

    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_KEYWORD_SEARCH_C',
            param: JSON.stringify({
                C_ID: C_ID,
                KEYWORD: query,
                POS_ID: POS_ID,
                FLR_INDEX: FLR_INDEX
            })
        }, true);
    }

    goSearch(query);
});

inputAction();

cHammer = new Hammer($clear[0]);
cHammer.on('tap', function() {
    $input.val('').trigger('input');
});

$search.on('touchend', function() {
    var $buriedpoint = $form.find('.buriedpoint');

    if ($buriedpoint.val() === '') {
        $buriedpoint.val($('#default-terms').val());
    }

    // 保存搜索的内容
    writeSearch.setHistoryValFun($buriedpoint.val());

    if (security.hasDangerInput()) {
        return false;
    }

    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_KEYWORD_SEARCH_C',
            param: JSON.stringify({
                C_ID: C_ID,
                KEYWORD: $buriedpoint.val(),
                POS_ID: 0,
                FLR_INDEX: 0
            })
        }, true);
    }

    $(this).closest('form').submit();
    return false;
});

// 初始化历史搜索的内容
(function() {
    var html = '',
        history,
        historys, i, num = 1;

    try {
        if (localStorage) {
            historys = localStorage.getItem(historyval);

            if (historys && historys.length > 0) {
                historys = historys.split(ranToken);
                for (i = historys.length; i > 0; i--) {
                    history = historys[i - 1];

                    if (history === '') {
                        continue;
                    }

                    if (num++ > 10) {
                        break;
                    }

                    html += '<li><a href="javascript:void(0);">' + history + '</li>';
                }

                $history.html(html);

                if (html !== '') {
                    $clearHistory.removeClass('hide');
                    $historySearch.removeClass('hide');
                }

                window.rePosFooter();
            } else {
                $historySearch.hide();
                $clearHistory.hide();
            }
        }
    } catch (e) {
        $historySearch.hide();
        $clearHistory.hide();
    }
}());