help-search.js 2.29 KB
/**
 * [帮助中心 搜索]
 * @author: wsl(shuiling.wang@yoho.cn)
 * @date: 2016/12/02
 */
var yas = require('../common/data-yas');

var helpSearch = {
    $search: $('.help-search'),
    init: function() {
        var _this = this;
        var $helpSearchForm = $('#help-search-form');
        var $searchBtn = $('.search-btn');

        _this.searchDefaultVal();

        $searchBtn.click(function() {
            var key = _this.$search.val();

            if (key === '请输入您想知道的帮助信息') {
                _this.$search.val('');
            }

            _this.searchYas(key);
            $helpSearchForm.submit();
        });

        _this.$search.keyup(function(e) {

            var key = $(this).val();

            if (key === '请输入您想知道的帮助信息') {
                $(this).val('');
            }

            if (e.which === 13) {
                if (key) {
                    _this.searchYas(key);
                    $helpSearchForm.submit();
                }
            }
        });

        // 热搜词埋点
        $('.search-box .keyword a').on('click', function() {
            yas.yasEvent('YB_HELP_SEARCH_C', {
                POS_ID: 2,
                POS_NM: '热搜词 ',
                KEYWORD: $(this).html()
            }, false);
        });
    },
    searchYas: function(keyword) { // 搜索框埋点
        yas.yasEvent('YB_HELP_SEARCH_C', {
            POS_ID: 1,
            POS_NM: '搜索框',
            KEYWORD: keyword
        }, false);
    },
    searchDefaultVal: function() {
        var _this = this;
        var defaultVal = '请输入您想知道的帮助信息';
        var startVal = defaultVal;
        var defaultColor = '#989898';

        if (_this.$search.val()) {
            startVal = _this.$search.val();
            defaultColor = '#1b1b1b';
        }

        _this.$search.focus(function() {
            var key = _this.$search.val();

            if (key === defaultVal) {
                _this.$search.val('').css('color', '#1b1b1b');
            }
        }).blur(function() {
            var key = _this.$search.val();

            if ($.trim(key) === '') {
                _this.$search.val(defaultVal).css('color', '#989898');
            }
        }).val(startVal).css('color', defaultColor);
    }
};

module.exports = helpSearch;