UNPKG

platform-project

Version:

平台项目

108 lines (102 loc) 3.39 kB
var words = ['哼哈哈哈嘿嘿', '哈哈哈哈哈哈哈哈哈哈', '黑恶化IE黑恶黑嘿嘿嘿嘿', '嘿哈哈嘿哈哈嘿哈哈', '哈黑和哈嘿嘿哈哈和嘿嘿']; //辅助搜索 function AssistantList(options) { this.node = options.node; this.input = this.node.find("[type='text']"); this.assistant = this.node.find("[data-node='assistant']"); this.state = false; this.isEnter = false; } AssistantList.prototype = { constructor: AssistantList, trigger: function () { if (!this.node.length || !this.input.length || !this.assistant.length) { return } this.searchUrl = this.node.attr('data-url'); this.bindEvent(); }, //绑定事件 bindEvent: function () { var _this = this; _this.input.on('focus', _this.handleFocus()); _this.input.on('blur', _this.handleBlur()); _this.input.on('keyup', _this.handleKeyup()); _this.assistant.on('click', '.item', _this.onClickItem()); _this.assistant.hover(function () { _this.isEnter = true; }, function () { _this.isEnter = false; }); }, //获得焦点 handleFocus: function () { var _this = this; return function () { var keyword = _this.input.val().trim(); if (!keyword || _this.state) { return } _this.getAssistItems(); } }, //失去焦点 handleBlur: function () { var _this = this; return function (e) { if (_this.isEnter) { return; } _this.assistant.addClass('hidden'); } }, //改变输入 handleKeyup: function () { var _this = this; return function () { _this.getAssistItems(); } }, onClickItem: function(){ var _this = this; return function(e){ e.stopPropagation(); _this.input.val($(this).html()); _this.assistant.addClass('hidden'); } }, getAssistItems: function () { var _this = this; var keyword = _this.input.val().trim(); if (!keyword || _this.state) { return }; // _this.state = true; //keyword = encodeURI(url); // $.ajax({ // url: _this.searchUrl + '?keyword=' + keyword, // type: 'GET', // dataType: 'json', // success: function (data) { // if (data.status != 200) { // } // }, // error: function () { // console.log('网络错误'); // }, // complete: function(){ // _this.state = false; // } // }); //test var liStr = ''; for (var i = 0; i < words.length; i++) { var word = words[i]; if (word.indexOf(keyword) > -1) { liStr += '<li><a class="item" href="javascript:;">' + word + '</a></li>'; } } _this.assistant.empty().html(liStr).removeClass('hidden'); } } $.fn.assistantSearch = function () { if (!$(this).length) { return } $(this).each(function () { new AssistantList({ node: $(this) }).trigger(); }); } module.exports = AssistantList;