layui.define(['table', 'jquery', 'element'], function (exports) { "use strict"; var MOD_NAME = 'menu', $ = layui.jquery, element = layui.element; var pearMenu = function (opt) { this.option = opt; }; pearMenu.prototype.render = function (opt) { var option = { elem: opt.elem, async: opt.async, parseData: opt.parseData, url: opt.url, method: opt.method ? opt.method : "GET", defaultOpen: opt.defaultOpen, defaultSelect: opt.defaultSelect, control: opt.control, defaultMenu: opt.defaultMenu, accordion: opt.accordion, height: opt.height, theme: opt.theme, data: opt.data ? opt.data : [], change: opt.change ? opt.change : function () { }, done: opt.done ? opt.done : function () { } } var tempDone = option.done; option.done = function(){ if (option.control) { rationalizeHeaderControlWidthAuto(option); } tempDone(); } if (option.async) { if (option.method === "GET") { getData(option.url).then(function (data) { option.data = data; renderMenu(option); }); } else { postData(option.url).then(function (data) { option.data = data; renderMenu(option); }); } } else { // 延时返回,和 javascript 执行时序关联 window.setTimeout(function () { renderMenu(option); }, 500); } // 处理高度 $("#" + opt.elem).height(option.height) return new pearMenu(option); } pearMenu.prototype.click = function (clickEvent) { var _this = this; $("body").on("click", "#" + _this.option.elem + " .site-demo-active", function () { var dom = $(this); var data = { menuId: dom.attr("menu-id"), menuTitle: dom.attr("menu-title"), menuPath: dom.attr("menu-title"), menuIcon: dom.attr("menu-icon"), menuUrl: dom.attr("menu-url"), openType: dom.attr("open-type") }; var doms = hash(dom); if (doms != null) { if (doms.text() != '') { data['menuPath'] = doms.find("span").text() + " / " + data['menuPath']; } } if (doms != null) { var domss = hash(doms); if (domss != null) { if (domss.text() != '') { data['menuPath'] = domss.find("span").text() + " / " + data['menuPath']; } } } if (domss != null) { var domsss = hash(domss); if (domsss != null) { if (domsss.text() != '') { data['menuPath'] = domsss.find("span").text() + " / " + data['menuPath']; } } } if ($("#" + _this.option.elem).is(".pear-nav-mini")) { if (_this.option.accordion) { activeMenus = $(this).parent().parent().parent().children("a"); } else { activeMenus.push($(this).parent().parent().parent().children("a")); } } clickEvent(dom, data); }) } function hash(dom) { var d = dom.parent().parent().prev(); if (d.prop("tagName") === "UL") { return null; } return d; } pearMenu.prototype.skin = function (skin) { var menu = $(".pear-nav-tree[lay-filter='" + this.option.elem + "']").parent(); menu.removeClass("dark-theme"); menu.removeClass("light-theme"); menu.addClass(skin); } pearMenu.prototype.selectItem = function (pearId) { if (this.option.control != false) { $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find("ul").css({ display: "none" }); $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass( "layui-this"); $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").css({ display: "block" }); var controlId = $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").attr("pear-id"); if (controlId != undefined) { $("#" + this.option.control).find(".layui-this").removeClass("layui-this"); $("#" + this.option.control).find("[pear-id='" + controlId + "']").addClass("layui-this"); } } $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-this").removeClass( "layui-this"); if (!$("#" + this.option.elem).is(".pear-nav-mini")) { var openEle = null; var openEleHeight = 0; $($("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents('.layui-nav-child').get().reverse()).each(function () { if (!$(this).parent().is('.layui-nav-itemed')) { if (openEleHeight == 0) { openEle = $(this); } else { $(this).parent().addClass('layui-nav-itemed'); $(this).css({ height: 'auto', }); } openEleHeight += $(this).children("dd").length * 48; } }); if (this.option.accordion) { if (openEleHeight > 0) { var currentDom = openEle.parent().siblings('.layui-nav-itemed').children(".layui-nav-child"); currentDom.animate({ height: "0px" }, 240, function () { currentDom.css({ height: "auto" }); $(this).parent().removeClass("layui-nav-itemed"); $(this).find('.layui-nav-itemed').removeClass("layui-nav-itemed"); }); } } if (openEleHeight > 0) { openEle.parent().addClass("layui-nav-itemed"); openEle.height(0); openEle.animate({ height: openEleHeight + "px" }, 240, function () { $(this).css({ height: 'auto' }); }); } } $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parent().addClass("layui-this"); } var activeMenus; pearMenu.prototype.collapse = function (time) { var elem = this.option.elem; var config = this.option; if ($("#" + this.option.elem).is(".pear-nav-mini")) { $.each(activeMenus, function (i, item) { $("#" + elem + " a[menu-id='" + $(this).attr("menu-id") + "']").parent().addClass("layui-nav-itemed"); }) $("#" + this.option.elem).removeClass("pear-nav-mini"); $("#" + this.option.elem).animate({ width: "220px" }, 180); isHoverMenu(false, config); var that = this; $("#" + this.option.elem) .promise() .done(function () { if (that.option.control) { rationalizeHeaderControlWidth(that.option); } }) } else { activeMenus = $("#" + this.option.elem).find(".layui-nav-itemed>a"); $("#" + this.option.elem).find(".layui-nav-itemed").removeClass("layui-nav-itemed"); $("#" + this.option.elem).addClass("pear-nav-mini"); $("#" + this.option.elem).animate({ width: "60px" }, 400); var that = this; $("#" + this.option.elem) .promise() .done(function () { isHoverMenu(true, config); if (that.option.control) { rationalizeHeaderControlWidth(that.option); } }) } } function getData(url) { var defer = $.Deferred(); var symbol = url.indexOf('?') !== -1 ? '&' : '?'; $.get(url + symbol + "fresh=" + Math.random(), function (result) { defer.resolve(result) }); return defer.promise(); } function postData(url) { var defer = $.Deferred(); var symbol = url.indexOf('?') !== -1 ? '&' : '?'; $.post(url + symbol + "fresh=" + Math.random(), function (result) { defer.resolve(result) }, "json"); return defer.promise(); } function renderMenu(option) { if (option.parseData != false) { option.parseData(option.data); } if (option.data.length > 0) { if (option.control != false) { createMenuAndControl(option); } else { createMenu(option); } } element.init(); downShow(option); option.done(); } function createMenu(option) { var menuHtml = '