chat-qa.page.js 2.3 KB

'use strict';
let tip = require('plugin/tip');

let $hotGroup = $('.hot-group'),
    $searchGroup = $('.search-group'),
    $noResult = $('.no-result');

require('common');

$('.list-group div').on('touchstart',
    function() {
        $(this).addClass('highlight');
    }).on('touchend touchcancel',
    function() {
        $(this).removeClass('highlight');
    }
);

$('.open-down').click(
    function() {
        $(this).next('.down-item').toggle().siblings('.down-item').hide();
    }
);

$('.cancel-btn').click(
    function() {
        window.history.go(-1);
    }
);

$(
    function() {
        $('.get-qa').focus();
    }
);

$('.get-qa').on('keyup focus',
    function() {
        let searchText = $(this).val();

        if (searchText === '') {
            $hotGroup.show();
            $searchGroup.hide();
            $noResult.hide();
        } else {
            $.ajax({
                url: '/service/keySearch',
                data: {
                    keyword: searchText
                },
                success: function(result) {
                    $searchGroup.show();
                    $noResult.hide();
                    $hotGroup.hide();
                    let keyList = result.data.helper_list;

                    if (keyList === '') {
                        $searchGroup.empty();
                        $noResult.show();
                        $noResult.find('.noKey').html(searchText);
                    } else {
                        let keyItem = '';

                        for (let i = 0; i < keyList.length; i++) {
                            let redText = keyList[i].caption.replace(searchText, '<span>$&</span>');
                            let id = keyList[i].id;

                            keyItem += '<a class="common-item" href="./qaDetail?keyword=' + searchText +
                                '&sonId=' + id + '">' +
                                            '<p>' + redText + '</p>' +
                                            '<i class="arr-ico iconfont">&#xe604;</i>' +
                                        '</a>';
                        }
                        $searchGroup.html(keyItem);
                    }
                },
                error: function() {
                    tip.show('网络异常!');
                }
            });
        }
    }
);