Pear-Admin-Layui/component/pear/modules/admin.js

436 lines
12 KiB
JavaScript
Raw Normal View History

2021-01-16 14:48:49 +00:00
layui.define(['table', 'jquery', 'element', 'yaml','form', 'tab', 'menu', 'frame', 'theme'],
2020-07-15 21:22:46 +00:00
function(exports) {
"use strict";
const $ = layui.jquery,
2020-07-15 21:22:46 +00:00
form = layui.form,
element = layui.element,
yaml = layui.yaml,
2020-07-15 21:22:46 +00:00
pearTab = layui.tab,
pearMenu = layui.menu,
2021-01-16 14:48:49 +00:00
pearFrame = layui.frame,
pearTheme = layui.theme;
2020-07-15 21:22:46 +00:00
let bodyFrame;
let sideMenu;
let bodyTab;
let config;
const body = $('body');
2020-11-23 09:25:05 +00:00
const pearAdmin = new function() {
// 默认配置
let configType = 'yml';
let configPath = 'pear.config.yml';
2020-11-23 09:25:05 +00:00
this.setConfigPath = function(path) {
2020-11-08 22:27:38 +00:00
configPath = path;
}
this.setConfigType = function(type) {
configType = type;
}
2020-11-08 22:27:38 +00:00
2020-11-23 09:25:05 +00:00
this.render = function(initConfig) {
if (initConfig !== undefined) {
2020-10-11 03:35:31 +00:00
applyConfig(initConfig);
} else {
applyConfig(pearAdmin.readConfig());
2020-10-11 03:35:31 +00:00
}
2020-07-15 21:22:46 +00:00
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.readConfig = function() {
if(configType === "yml"){
return yaml.load(configPath);
}
else
{
let data;
$.ajax({
url:configPath,
type:'get',
dataType:'json',
async: false,
success:function(result){
data = result;
}
})
return data;
}
2020-11-08 22:27:38 +00:00
}
2020-11-23 09:25:05 +00:00
this.logoRender = function(param) {
2020-08-04 01:09:52 +00:00
$(".layui-logo .logo").attr("src", param.logo.image);
$(".layui-logo .title").html(param.logo.title);
2020-07-15 21:22:46 +00:00
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.menuRender = function(param) {
2020-07-15 21:22:46 +00:00
sideMenu = pearMenu.render({
2020-08-04 01:09:52 +00:00
elem: 'sideMenu',
async: param.menu.async !== undefined ? param.menu.async : true,
2020-07-21 13:42:43 +00:00
theme: "dark-theme",
2020-07-15 21:22:46 +00:00
height: '100%',
control: param.menu.control ? 'control' : false, // control
2020-08-11 11:34:44 +00:00
defaultMenu: 0,
2020-08-04 01:09:52 +00:00
accordion: param.menu.accordion,
2020-09-30 04:57:03 +00:00
url: param.menu.data,
2020-11-08 22:27:38 +00:00
data: param.menu.data, //async为false时传入菜单数组
2020-08-11 11:34:44 +00:00
parseData: false,
2020-11-23 09:25:05 +00:00
change: function() {
2020-08-11 11:34:44 +00:00
compatible();
},
2020-11-23 09:25:05 +00:00
done: function() {
2020-10-13 16:27:55 +00:00
sideMenu.selectItem(param.menu.select);
}
});
2020-07-15 21:22:46 +00:00
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.bodyRender = function(param) {
body.on("click", ".refresh", function() {
const 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");
bodyTab.refresh(400);
2020-11-23 09:25:05 +00:00
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)
})
2020-08-04 12:31:05 +00:00
if (param.tab.muiltTab) {
2020-07-15 21:22:46 +00:00
bodyTab = pearTab.render({
elem: 'content',
roll: true,
tool: true,
width: '100%',
height: '100%',
2021-01-26 14:21:31 +00:00
session: param.tab.session,
2020-07-15 21:22:46 +00:00
index: 0,
2020-08-04 12:31:05 +00:00
tabMax: param.tab.tabMax,
2020-11-23 09:25:05 +00:00
closeEvent: function(id) {
2020-07-15 21:22:46 +00:00
sideMenu.selectItem(id);
},
data: [{
2020-08-04 12:31:05 +00:00
id: param.tab.index.id,
url: param.tab.index.href,
title: param.tab.index.title,
2020-07-15 21:22:46 +00:00
close: false
2021-01-26 14:21:31 +00:00
}],
success: function(id){
2021-01-26 15:17:21 +00:00
if(param.tab.session){
setTimeout(function(){
sideMenu.selectItem(id);
bodyTab.positionTab();
},500)
}
2021-01-26 14:21:31 +00:00
}
2020-07-15 21:22:46 +00:00
});
2020-11-23 09:25:05 +00:00
bodyTab.click(function(id) {
2020-10-28 09:33:54 +00:00
if (!param.tab.keepState) {
bodyTab.refresh(false);
}
2020-07-15 21:22:46 +00:00
bodyTab.positionTab();
sideMenu.selectItem(id);
})
2020-11-23 09:25:05 +00:00
sideMenu.click(function(dom, data) {
2020-12-18 11:36:38 +00:00
2020-07-15 21:22:46 +00:00
bodyTab.addTabOnly({
id: data.menuId,
title: data.menuTitle,
url: data.menuUrl,
icon: data.menuIcon,
close: true
}, 300);
compatible();
})
} else {
bodyFrame = pearFrame.render({
elem: 'content',
2020-12-29 10:05:28 +00:00
title: '首页',
2020-08-11 08:22:32 +00:00
url: param.tab.index.href,
2020-07-15 21:22:46 +00:00
width: '100%',
height: '100%'
});
2020-11-23 09:25:05 +00:00
sideMenu.click(function(dom, data) {
2020-07-15 21:22:46 +00:00
bodyFrame.changePage(data.menuUrl, data.menuPath, true);
compatible()
})
}
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.keepLoad = function(param) {
2020-07-15 21:22:46 +00:00
compatible()
2020-11-23 09:25:05 +00:00
setTimeout(function() {
2020-09-30 07:11:18 +00:00
$(".loader-main").fadeOut(200);
}, param.other.keepLoad)
2020-07-15 21:22:46 +00:00
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.themeRender = function(option) {
if (option.theme.allowCustom === false) {
2020-07-23 05:37:18 +00:00
$(".setting").remove();
}
const colorId = localStorage.getItem("theme-color");
2021-01-16 14:48:49 +00:00
const currentColor = getColorById(colorId);
localStorage.setItem("theme-color", currentColor.id);
localStorage.setItem("theme-color-context", currentColor.color);
2021-01-17 13:43:58 +00:00
pearTheme.changeTheme(window, option.other.autoHead);
let menu = localStorage.getItem("theme-menu");
if (menu === "null") {
2020-07-23 06:03:59 +00:00
menu = option.theme.defaultMenu;
2020-07-25 07:44:22 +00:00
} else {
if (option.theme.allowCustom === false) {
2020-07-23 06:03:59 +00:00
menu = option.theme.defaultMenu;
2020-07-23 05:37:18 +00:00
}
}
2020-07-25 07:44:22 +00:00
localStorage.setItem("theme-menu", menu);
2020-07-22 20:41:52 +00:00
this.menuSkin(menu);
}
2020-10-28 09:33:54 +00:00
2020-11-23 09:25:05 +00:00
this.menuSkin = function(theme) {
const pearAdmin = $(".pear-admin");
pearAdmin.removeClass("light-theme");
pearAdmin.removeClass("dark-theme");
pearAdmin.addClass(theme);
2020-07-22 20:41:52 +00:00
}
2020-07-15 21:22:46 +00:00
};
2020-11-23 09:25:05 +00:00
function collaspe() {
2020-07-15 21:22:46 +00:00
sideMenu.collaspe();
const admin = $(".pear-admin");
const left = $(".layui-icon-spread-left")
const 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");
2020-07-15 21:22:46 +00:00
} else {
right.addClass("layui-icon-spread-left")
right.removeClass("layui-icon-shrink-right")
admin.addClass("pear-mini");
2020-07-15 21:22:46 +00:00
}
}
body.on("click", ".collaspe,.pear-cover", function() {
2020-11-23 09:25:05 +00:00
collaspe()
2020-10-11 03:35:31 +00:00
});
2020-07-15 21:22:46 +00:00
body.on("click", ".fullScreen", function() {
2020-07-15 21:22:46 +00:00
if ($(this).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");
});
}
});
2020-10-28 09:33:54 +00:00
body.on("click", '[user-menu-id]', function() {
2020-10-28 09:33:54 +00:00
if (config.tab.muiltTab) {
bodyTab.addTabOnly({
2020-07-27 16:25:02 +00:00
id: $(this).attr("user-menu-id"),
title: $(this).attr("user-menu-title"),
url: $(this).attr("user-menu-url"),
icon: "",
close: true
}, 300);
2020-10-28 09:33:54 +00:00
} else {
2020-07-27 16:25:02 +00:00
bodyFrame.changePage($(this).attr("user-menu-url"), "", true);
}
2020-10-11 03:35:31 +00:00
});
2020-07-15 21:22:46 +00:00
body.on("click", ".setting", function() {
let bgColorHtml =
2020-07-22 20:41:52 +00:00
'<li class="layui-this" data-select-bgcolor="dark-theme" >' +
'<a href="javascript:;" data-skin="skin-blue" style="" class="clearfix full-opacity-hover">' +
'<div><span style="display:block; width: 20%; float: left; height: 12px; background: #28333E;"></span><span style="display:block; width: 80%; float: left; height: 12px; background: white;"></span></div>' +
'<div><span style="display:block; width: 20%; float: left; height: 40px; background: #28333E;"></span><span style="display:block; width: 80%; float: left; height: 40px; background: #f4f5f7;"></span></div>' +
'</a>' +
'</li>';
bgColorHtml +=
'<li data-select-bgcolor="light-theme" >' +
'<a href="javascript:;" data-skin="skin-blue" style="" class="clearfix full-opacity-hover">' +
'<div><span style="display:block; width: 20%; float: left; height: 12px; background: white;"></span><span style="display:block; width: 80%; float: left; height: 12px; background: white;"></span></div>' +
'<div><span style="display:block; width: 20%; float: left; height: 40px; background: white;"></span><span style="display:block; width: 80%; float: left; height: 40px; background: #f4f5f7;"></span></div>' +
'</a>' +
'</li>';
2020-07-23 05:37:18 +00:00
const html =
2020-07-22 20:41:52 +00:00
'<div class="pearone-color">\n' +
'<div class="color-title">整体风格</div>\n' +
'<div class="color-content">\n' +
'<ul>\n' + bgColorHtml + '</ul>\n' +
'</div>\n' +
'</div>';
2020-10-28 09:33:54 +00:00
2020-07-15 21:22:46 +00:00
layer.open({
type: 1,
offset: 'r',
2020-12-17 16:24:44 +00:00
area: ['340px', '100%'],
2020-07-15 21:22:46 +00:00
title: false,
shade: 0.1,
closeBtn: 0,
shadeClose: false,
anim: -1,
skin: 'layer-anim-right',
move: false,
content: html + buildColorHtml() + buildLinkHtml() + bottomTool(),
2020-07-15 21:22:46 +00:00
success: function(layero, index) {
form.render();
2020-10-28 09:33:54 +00:00
const color = localStorage.getItem("theme-color");
const menu = localStorage.getItem("theme-menu");
2020-07-23 05:37:18 +00:00
if (color !== "null") {
2020-07-22 20:41:52 +00:00
$(".select-color-item").removeClass("layui-icon")
.removeClass("layui-icon-ok");
$("*[color-id='" + color + "']").addClass("layui-icon")
.addClass("layui-icon-ok");
}
if (menu !== "null") {
2020-07-22 20:41:52 +00:00
$("*[data-select-bgcolor]").removeClass("layui-this");
2020-07-23 05:37:18 +00:00
$("[data-select-bgcolor='" + menu + "']").addClass("layui-this");
2020-07-22 20:41:52 +00:00
}
2020-07-15 21:22:46 +00:00
$('#layui-layer-shade' + index).click(function() {
const $layero = $('#layui-layer' + index);
2020-07-15 21:22:46 +00:00
$layero.animate({
left: $layero.offset().left + $layero.width()
}, 200, function() {
layer.close(index);
});
})
$('#closeTheme').click(function() {
const $layero = $('#layui-layer' + index);
$layero.animate({
left: $layero.offset().left + $layero.width()
}, 200, function() {
layer.close(index);
});
})
2020-07-15 21:22:46 +00:00
}
});
2020-10-11 03:35:31 +00:00
});
2020-10-28 09:33:54 +00:00
function bottomTool(){
return "<button id='closeTheme' style='position: absolute;bottom: 20px;left: 20px;' class='pear-btn'>关闭</button>"
}
body.on('click', '[data-select-bgcolor]', function() {
const theme = $(this).attr('data-select-bgcolor');
2020-07-21 17:31:28 +00:00
$('[data-select-bgcolor]').removeClass("layui-this");
$(this).addClass("layui-this");
2020-07-22 20:41:52 +00:00
localStorage.setItem("theme-menu", theme);
pearAdmin.menuSkin(theme);
});
2020-10-28 09:33:54 +00:00
body.on('click', '.select-color-item', function() {
2020-07-22 20:41:52 +00:00
$(".select-color-item").removeClass("layui-icon").removeClass("layui-icon-ok");
$(this).addClass("layui-icon").addClass("layui-icon-ok");
const colorId = $(".select-color-item.layui-icon-ok").attr("color-id");
2021-01-16 14:48:49 +00:00
const currentColor = getColorById(colorId);
localStorage.setItem("theme-color", currentColor.id);
localStorage.setItem("theme-color-context", currentColor.color);
2021-01-17 13:43:58 +00:00
pearTheme.changeTheme(window, config.other.autoHead);
2020-07-21 17:31:28 +00:00
});
2020-10-28 09:33:54 +00:00
function applyConfig(param) {
2020-10-11 03:35:31 +00:00
config = param;
pearAdmin.logoRender(param);
pearAdmin.menuRender(param);
pearAdmin.bodyRender(param);
pearAdmin.themeRender(param);
pearAdmin.keepLoad(param);
}
2020-07-22 20:41:52 +00:00
function getColorById(id) {
let color;
let flag = false;
2020-09-30 04:57:03 +00:00
$.each(config.colors, function(i, value) {
if (value.id === id) {
2020-07-22 20:41:52 +00:00
color = value;
2020-07-23 05:37:18 +00:00
flag = true;
2020-07-22 20:41:52 +00:00
}
})
if (flag === false || config.theme.allowCustom === false) {
2020-09-30 04:57:03 +00:00
$.each(config.colors, function(i, value) {
if (value.id === config.theme.defaultColor) {
2020-07-25 07:44:22 +00:00
color = value;
2020-07-23 05:37:18 +00:00
}
})
}
2020-07-22 20:41:52 +00:00
return color;
2020-07-21 17:31:28 +00:00
}
2020-07-22 20:41:52 +00:00
function buildLinkHtml() {
let links = "";
2020-09-30 04:57:03 +00:00
$.each(config.links, function(i, value) {
2020-07-22 20:41:52 +00:00
links += '<a class="more-menu-item" href="' + value.href + '" target="_blank">' +
'<i class="' + value.icon + '" style="font-size: 19px;"></i> ' + value.title +
'</a>'
2020-07-21 17:31:28 +00:00
})
2020-07-22 20:41:52 +00:00
return '<div class="more-menu-list">' + links + '</div>';
2020-07-21 17:31:28 +00:00
}
2020-07-22 20:41:52 +00:00
function buildColorHtml() {
let colors = "";
2020-09-30 04:57:03 +00:00
$.each(config.colors, function(i, value) {
2020-07-25 07:44:22 +00:00
colors += "<span class='select-color-item' color-id='" + value.id + "' style='background-color:" + value.color +
";'></span>";
2020-07-21 17:31:28 +00:00
})
2020-07-25 07:44:22 +00:00
return "<div class='select-color'><div class='select-color-title'>主题色</div><div class='select-color-content'>" +
colors + "</div></div>"
2020-07-21 17:31:28 +00:00
}
2020-10-28 09:33:54 +00:00
function compatible() {
if ($(window).width() <= 768) {
2020-11-23 09:25:05 +00:00
collaspe()
}
}
2020-10-28 09:33:54 +00:00
function screenFun(num) {
num = num || 1;
num = num * 1;
const docElm = document.documentElement;
switch (num) {
case 1:
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
} else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
} else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
} else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
break;
case 2:
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
break;
}
2020-10-28 09:33:54 +00:00
return new Promise(function(res, rej) {
res("返回值");
});
}
2020-12-17 12:53:46 +00:00
2020-07-15 21:22:46 +00:00
exports('admin', pearAdmin);
2020-09-30 07:11:18 +00:00
})