layui.define(['message', 'table', 'jquery', 'element', 'yaml', 'form', 'tab', 'menu', 'frame', 'theme', 'convert','fullscreen'], function(exports) { "use strict"; var $ = layui.jquery, form = layui.form, element = layui.element, yaml = layui.yaml, pearTab = layui.tab, convert = layui.convert, pearMenu = layui.menu, pearFrame = layui.frame, pearTheme = layui.theme, message = layui.message, fullscreen=layui.fullscreen; var bodyFrame; var sideMenu; var bodyTab; var config; var logout = function() {}; var msgInstance; var body = $('body'); var pearAdmin = new function() { var configType = 'yml'; var configPath = 'pear.config.yml'; this.setConfigPath = function(path) { configPath = path; } this.setConfigType = function(type) { configType = type; } this.render = function(initConfig) { if (initConfig !== undefined) { applyConfig(initConfig); } else { applyConfig(pearAdmin.readConfig()); } } this.readConfig = function() { if (configType === "yml") { return yaml.load(configPath); } else { var data; $.ajax({ url: configPath, type: 'get', dataType: 'json', async: false, success: function(result) { data = result; } }) return data; } } this.messageRender = function(option) { var option = { elem: '.message', url: option.header.message, height: '250px' }; msgInstance = message.render(option); } this.logoRender = function(param) { $(".layui-logo .logo").attr("src", param.logo.image); $(".layui-logo .title").html(param.logo.title); } this.menuRender = function(param) { sideMenu = pearMenu.render({ elem: 'sideMenu', async: param.menu.async !== undefined ? param.menu.async : true, theme: "dark-theme", height: '100%', method: param.menu.method, control: isControl(param) === 'true' || isControl(param) === true ? 'control' : false, // control controlWidth: param.menu.controlWidth, defaultMenu: 0, accordion: param.menu.accordion, url: param.menu.data, data: param.menu.data, parseData: false, change: function() { compatible(); }, done: function() { sideMenu.isCollapse = param.menu.collapse; sideMenu.selectItem(param.menu.select); pearAdmin.collapse(param); } }); } this.bodyRender = function(param) { body.on("click", ".refresh", function() { refresh(); }) if (isMuiltTab(param) === "true" || isMuiltTab(param) === true) { bodyTab = pearTab.render({ elem: 'content', roll: true, tool: true, width: '100%', height: '100%', session: param.tab.session, index: 0, tabMax: param.tab.max, preload: param.tab.preload, closeEvent: function(id) { sideMenu.selectItem(id); }, data: [{ id: param.tab.index.id, url: param.tab.index.href, title: param.tab.index.title, close: false }], success: function(id) { if (param.tab.session) { setTimeout(function() { sideMenu.selectItem(id); bodyTab.positionTab(); }, 500) } } }); bodyTab.click(function(id) { if (!param.tab.keepState) { bodyTab.refresh(false); } bodyTab.positionTab(); sideMenu.selectItem(id); }) sideMenu.click(function(dom, data) { bodyTab.addTabOnly({ id: data.menuId, title: data.menuTitle, url: data.menuUrl, icon: data.menuIcon, close: true }, 300); compatible(); }) } else { bodyFrame = pearFrame.render({ elem: 'content', title: '首页', url: param.tab.index.href, width: '100%', height: '100%' }); sideMenu.click(function(dom, data) { bodyFrame.changePage(data.menuUrl, true); compatible() }) } } this.keepLoad = function(param) { compatible() setTimeout(function() { $(".loader-main").fadeOut(200); }, param.other.keepLoad) } this.themeRender = function(option) { if (option.theme.allowCustom === false) { $(".setting").remove(); } var colorId = localStorage.getItem("theme-color"); var currentColor = getColorById(colorId); localStorage.setItem("theme-color", currentColor.id); localStorage.setItem("theme-color-color", currentColor.color); localStorage.setItem("theme-color-second", currentColor.second); pearTheme.changeTheme(window, isAutoHead(config)); var menu = localStorage.getItem("theme-menu"); if (menu === null) { menu = option.theme.defaultMenu; } else { if (option.theme.allowCustom === false) { menu = option.theme.defaultMenu; } } var header = localStorage.getItem("theme-header"); if (header === null) { header = option.theme.defaultHeader; } else { if (option.theme.allowCustom === false) { header = option.theme.defaultHeader; } } var banner = localStorage.getItem("theme-banner"); if (banner === null) { banner = option.theme.banner; } else { if (option.theme.allowCustom === false) { banner = option.theme.banner; } } var autoHead = localStorage.getItem("auto-head"); if (autoHead === null) { autoHead = option.other.autoHead; } else { if (option.theme.allowCustom === false) { autoHead = option.other.autoHead; } } var muiltTab = localStorage.getItem("muilt-tab"); if (muiltTab === null) { muiltTab = option.tab.enable; } else { if (option.theme.allowCustom === false) { muiltTab = option.tab.enable; } } var control = localStorage.getItem("control"); if (control === null) { control = option.menu.control; } else { if (option.theme.allowCustom === false) { control = option.menu.control; } } var footer = localStorage.getItem("footer"); if( footer === null) { footer = option.other.footer; }else{ if (option.theme.allowCustom === false) { footer = option.other.footer; } } localStorage.setItem("muilt-tab", muiltTab); localStorage.setItem("theme-banner", banner); localStorage.setItem("theme-menu", menu); localStorage.setItem("theme-header", header); localStorage.setItem("auto-head", autoHead); localStorage.setItem("control", control); localStorage.setItem("footer", footer); this.menuSkin(menu); this.headerSkin(header); this.bannerSkin(banner); this.footer(footer); } this.footer = function(footer){ var bodyDOM = $(".pear-admin .layui-body"); var footerDOM = $(".pear-admin .layui-footer"); if (footer === true || footer === "true") { footerDOM.removeClass("close"); bodyDOM.css("bottom", footerDOM.outerHeight()); } else { footerDOM.addClass("close"); bodyDOM.css("bottom", ""); } } this.bannerSkin = function(theme) { var pearAdmin = $(".pear-admin"); pearAdmin.removeClass("banner-layout"); if (theme === true || theme === "true") { pearAdmin.addClass("banner-layout"); } } this.collapse = function(param) { if (param.menu.collapse) { if ($(window).width() >= 768) { collapse() } } } this.menuSkin = function(theme) { var pearAdmin = $(".pear-admin .layui-side"); pearAdmin.removeClass("light-theme"); pearAdmin.removeClass("dark-theme"); pearAdmin.addClass(theme); } this.headerSkin = function(theme) { var pearAdmin = $(".pear-admin .layui-header"); pearAdmin.removeClass("light-theme"); pearAdmin.removeClass("dark-theme"); pearAdmin.addClass(theme); } this.logout = function(callback) { logout = callback; } this.message = function(callback) { if (callback != null) { msgInstance.click(callback); } } this.collapseSide = function() { collapse() } this.refreshThis = function() { refresh() } this.refresh = function(id) { $("iframe[id='"+ id +"']").attr('src', $("iframe[id='"+ id +"']").attr('src')); } this.addTab = function(id, title, url) { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { bodyTab.addTabOnly({ id: id, title: title, url: url, icon: null, close: true }, 400); } else { return; } } this.closeTab = function(id) { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { pearTab.delTabByElem('content', id, function(currentId){ sideMenu.selectItem(currentId); }); } else { return; } } this.closeCurrentTab = function() { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { pearTab.delCurrentTabByElem('content', function(id){ sideMenu.selectItem(id); }); } else { return; } } this.closeOtherTab = function() { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { pearTab.delOtherTabByElem('content', function(id){ sideMenu.selectItem(id); }); } else { return; } } this.closeAllTab = function() { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { pearTab.delAllTabByElem('content', function(id){ sideMenu.selectItem(id); }); } else { return; } } this.changeTabTitle = function(id, title) { pearTab.changeTabTitleById('content', id ,title); } this.changeIframe = function(id, title, url) { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { return; } else { sideMenu.selectItem(id); bodyFrame.changePage(url, true); } } this.jump = function(id, title, url) { if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) { pearAdmin.addTab(id, title, url) } else { pearAdmin.changeIframe(id, title, url) } } this.fullScreen = function() { if ($(".fullScreen").hasClass("layui-icon-screen-restore")) { screenFun(2).then(function() { $(".fullScreen").eq(0).removeClass("layui-icon-screen-restore"); }); } else { screenFun(1).then(function() { $(".fullScreen").eq(0).addClass("layui-icon-screen-restore"); }); } } }; function refresh() { var refreshA = $(".refresh a"); refreshA.removeClass("layui-icon-refresh-1"); refreshA.addClass("layui-anim"); refreshA.addClass("layui-anim-rotate"); refreshA.addClass("layui-anim-loop"); refreshA.addClass("layui-icon-loading"); if (isMuiltTab(config) === "true" || isMuiltTab(config) === true) bodyTab.refresh(true); else bodyFrame.refresh(true); setTimeout(function() { refreshA.addClass("layui-icon-refresh-1"); refreshA.removeClass("layui-anim"); refreshA.removeClass("layui-anim-rotate"); refreshA.removeClass("layui-anim-loop"); refreshA.removeClass("layui-icon-loading"); }, 600) } function collapse() { sideMenu.collapse(); var admin = $(".pear-admin"); var left = $(".layui-icon-spread-left") var right = $(".layui-icon-shrink-right") if (admin.is(".pear-mini")) { left.addClass("layui-icon-shrink-right") left.removeClass("layui-icon-spread-left") admin.removeClass("pear-mini"); sideMenu.isCollapse = false; } else { right.addClass("layui-icon-spread-left") right.removeClass("layui-icon-shrink-right") admin.addClass("pear-mini"); sideMenu.isCollapse = true; } } body.on("click", ".logout", function() { if (logout() && bodyTab) { bodyTab.clear(); } }) body.on("click", ".collapse,.pear-cover", function() { collapse(); }); body.on("click", ".menuSearch", function () { // 过滤菜单 var filterHandle = function (filterData, val) { if (!val) return []; var filteredMenus = []; filterData = $.extend(true, {}, filterData); $.each(filterData, function (index, item) { if (item.children && item.children.length) { var children = filterHandle(item.children, val) var obj = $.extend({}, item, { children: children }); if (children && children.length) { filteredMenus.push(obj); } else if (item.title.indexOf(val) >= 0) { item.children = []; // 父级匹配但子级不匹配,就去除子级 filteredMenus.push($.extend({}, item)); } } else if (item.title.indexOf(val) >= 0) { filteredMenus.push(item); } }) return filteredMenus; } // 树转路径 var tiledHandle = function (data) { var tiledMenus = []; var treeTiled = function (data, content) { var path = ""; var separator = " / "; // 上级路径 if (!content) content = ""; $.each(data, function (index, item) { if (item.children && item.children.length) { path += content + item.title + separator; var childPath = treeTiled(item.children, path); path += childPath; if (!childPath) path = ""; // 重置路径 } else { path += content + item.title tiledMenus.push({ path: path, info: item }); path = ""; //重置路径 } }) return path; }; treeTiled(data); return tiledMenus; } // 创建搜索列表 var createList = function (data) { var _listHtml = ''; $.each(data, function (index, item) { _listHtml += '