layui.define(['jquery', 'tools'], function (exports) { "use strict"; /** * @since Pear Admin 4.0 * * Button component * */ var MOD_NAME = 'menuSearch', tools = layui.tools, $ = layui.jquery; var menuSearch = function (opt) { this.option = opt; }; /** * @since Pear Admin 4.0 * * Button start loading * */ menuSearch.prototype.render = function (opt) { var options = { select: opt.select, elem: opt.elem, dataProvider: opt.dataProvider, } $('body').on("click", options.elem, function () { var _html = [ `
` ].join(''); layer.open({ type: 1, offset: "10%", area: ['600px'], title: false, closeBtn: 0, shadeClose: true, anim: 0, move: false, content: _html, success: function (layero, layeridx) { var $layer = layero; var $content = $(layero).children('.layui-layer-content'); var $input = $(".menu-search-input-wrapper input"); var $noData = $(".menu-search-no-data"); var $list = $(".menu-search-list"); var menuData = options.dataProvider(); $layer.css("border-radius", "6px"); $input.off("focus").focus(); // 搜索输入事件 $input.off("input").on("input", tools.debounce(function () { var keywords = $input.val().trim(); var filteredMenus = filterHandle(menuData, keywords); if (filteredMenus.length) { var tiledMenus = tiledHandle(filteredMenus); var listHtml = createList(tiledMenus); $noData.css("display", "none"); $list.html("").append(listHtml).children(":first").addClass("this") } else { $list.html(""); $noData.css("display", "flex"); } var currentHeight = $(".menu-search-content").outerHeight() $layer.css("height", currentHeight); $content.css("height", currentHeight); }, 500) ) // 列表点击事件 $list.off("click").on("click", "li", function () { var id = $(this).attr("smenu-id"); var title = $(this).attr("smenu-title"); var url = $(this).attr("smenu-url"); var type = $(this).attr("smenu-type"); var openType = $(this).attr("smenu-open-type"); options.select({ id, title, url, type, openType }); layer.close(layeridx); }) $list.off('mouseenter').on("mouseenter", "li", function () { $(".menu-search-list li.this").removeClass("this"); $(this).addClass("this"); }).off("mouseleave").on("mouseleave", "li", function () { $(this).removeClass("this"); }) // 监听键盘事件 $('.menu-search-content').off("keydown").keydown(function (e) { if (e.keyCode === 13 || e.keyCode === 32) { e.preventDefault(); var that = $(".menu-search-list li.this"); var id = that.attr("smenu-id"); var title = that.attr("smenu-title"); var url = that.attr("smenu-url"); var type = that.attr("smenu-type"); var openType = that.attr("smenu-open-type"); options.select({ id, title, url, type, openType }); layer.close(layeridx); } else if (e.keyCode === 38) { e.preventDefault(); var prevEl = $(".menu-search-list li.this").prev(); $(".menu-search-list li.this").removeClass("this"); if (prevEl.length !== 0) { prevEl.addClass("this"); } else { $list.children().last().addClass("this"); } } else if (e.keyCode === 40) { e.preventDefault(); var nextEl = $(".menu-search-list li.this").next(); $(".menu-search-list li.this").removeClass("this"); if (nextEl.length !== 0) { nextEl.addClass("this"); } else { $list.children().first().addClass("this"); } } else if (e.keyCode === 27) { e.preventDefault(); layer.close(layeridx); } }) } }) }); return new menuSearch(options); } /** * @since Pear Admin 4.0 * * 创建结果列表 */ var createList = function (data) { var listHtml = ''; $.each(data, function (index, item) { listHtml += `