代码功能更新
This commit is contained in:
@@ -0,0 +1,522 @@
|
||||
layui.define([
|
||||
'message',
|
||||
'table',
|
||||
'jquery',
|
||||
'element',
|
||||
'yaml',
|
||||
'form',
|
||||
'laytpTab',
|
||||
'apimenu',
|
||||
'frame',
|
||||
'theme',
|
||||
'convert'
|
||||
],
|
||||
function(exports) {
|
||||
"use strict";
|
||||
|
||||
var $ = layui.jquery,
|
||||
form = layui.form,
|
||||
element = layui.element,
|
||||
yaml = layui.yaml,
|
||||
laytpTab = layui.laytpTab,
|
||||
convert = layui.convert,
|
||||
laytpApiMenu = layui.apimenu,
|
||||
laytpFrame = layui.frame,
|
||||
laytpTheme = layui.theme,
|
||||
message = layui.message;
|
||||
|
||||
var bodyFrame;
|
||||
var sideMenu;
|
||||
var bodyTab;
|
||||
var config;
|
||||
var logout = function() {};
|
||||
var msgInstance;
|
||||
|
||||
var body = layui.$('body');
|
||||
|
||||
var laytpApidoc = new function() {
|
||||
|
||||
// 默认配置
|
||||
var configType = 'yml';
|
||||
var configPath = 'api.config.yml';
|
||||
|
||||
this.setConfigPath = function(path) {
|
||||
configPath = path;
|
||||
};
|
||||
|
||||
this.setConfigType = function(type) {
|
||||
configType = type;
|
||||
};
|
||||
|
||||
this.setAvatar = function(url, username) {
|
||||
var image = new Image();
|
||||
image.src = url || "/static/admin/images/avatar.jpg";
|
||||
image.onload = function() {
|
||||
layui.$(".layui-nav-img").attr("src", convert.imageToBase64(image));
|
||||
};
|
||||
layui.$(".layui-nav-img").parent().append(username);
|
||||
};
|
||||
|
||||
this.render = function(initConfig) {
|
||||
if (initConfig !== undefined) {
|
||||
applyConfig(initConfig);
|
||||
} else {
|
||||
applyConfig(laytpApidoc.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.$(".layui-logo .logo").attr("src", param.logo.image);
|
||||
layui.$(".layui-logo .title").html(param.logo.title);
|
||||
};
|
||||
|
||||
this.menuRender = function(param) {
|
||||
sideMenu = laytpApiMenu.render({
|
||||
elem: 'sideMenu',
|
||||
async: param.menu.async !== undefined ? param.menu.async : true,
|
||||
theme: "dark-theme",
|
||||
// height: '100%',
|
||||
method: param.menu.method,
|
||||
control: param.menu.control ? 'control' : false, // control
|
||||
defaultMenu: 0,
|
||||
accordion: param.menu.accordion,
|
||||
url: param.menu.data,
|
||||
data: param.menu.data, //async为false时,传入菜单数组
|
||||
parseData: function(res){
|
||||
if(param.isSearch){
|
||||
var result = {
|
||||
"id" : -1,
|
||||
"is_menu" : 1,
|
||||
"is_show" : 1,
|
||||
"title" : "搜索结果",
|
||||
"type" : 0,
|
||||
"children" : res,
|
||||
"href" : "",
|
||||
"icon" : ""
|
||||
};
|
||||
var resArr = [];
|
||||
resArr.push(result);
|
||||
return resArr;
|
||||
}else{
|
||||
window.menuData = laytpApidoc.parseData(res.data);
|
||||
return window.menuData;
|
||||
}
|
||||
},
|
||||
change: function() {
|
||||
compatible();
|
||||
},
|
||||
done: function() {
|
||||
let firstMenuObj = layui.$("#sideMenu a[menu-id='" + param.menu.select + "']").parent();
|
||||
let menuType = $(firstMenuObj.html()).attr("doc-type");
|
||||
let id = $(firstMenuObj.html()).attr("menu-id");
|
||||
$('.api-page-tab-content').hide();
|
||||
$('#' + menuType + '_' + id).show();
|
||||
sideMenu.selectItem(param.menu.select);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.parseData = function(data){
|
||||
$.each(data, function(i, item) {
|
||||
var tempItem = item;
|
||||
// tempItem.id = item.id;
|
||||
// tempItem.href = item.href;
|
||||
// tempItem.icon = item.icon;
|
||||
tempItem.type = 0;
|
||||
// tempItem.title = item.name;
|
||||
if(typeof item.children != null && typeof item.children !== "undefined" && item.children.length > 0){
|
||||
tempItem.children = laytpApidoc.parseData(item.children);
|
||||
}else{
|
||||
tempItem.type = 1;
|
||||
}
|
||||
data[i] = tempItem;
|
||||
});
|
||||
return data;
|
||||
};
|
||||
|
||||
this.bodyRender = function(param) {
|
||||
if (param.tab.muiltTab) {
|
||||
sideMenu.click(function(dom, data) {
|
||||
let menuType = data.docType;
|
||||
let id = data.menuId;
|
||||
$('.api-page-tab-content').hide();
|
||||
$('#' + menuType + '_' + id).show();
|
||||
compatible();
|
||||
});
|
||||
} else {
|
||||
sideMenu.click(function(dom, data) {
|
||||
let menuType = data.docType;
|
||||
let id = data.menuId;
|
||||
$('.api-page-tab-content').hide();
|
||||
$('#' + menuType + '_' + id).show();
|
||||
compatible();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.keepLoad = function(param) {
|
||||
compatible();
|
||||
setTimeout(function() {
|
||||
layui.$(".loader-main").fadeOut(200);
|
||||
}, param.other.keepLoad)
|
||||
};
|
||||
|
||||
this.themeRender = function(option) {
|
||||
if (option.theme.allowCustom === false) {
|
||||
layui.$(".setting").remove();
|
||||
}
|
||||
var colorId = localStorage.getItem("theme-color");
|
||||
var currentColor = getColorById(colorId);
|
||||
localStorage.setItem("theme-color", currentColor.id);
|
||||
localStorage.setItem("theme-color-context", currentColor.color);
|
||||
laytpTheme.changeTheme(window, option.other.autoHead);
|
||||
var menu = localStorage.getItem("theme-menu");
|
||||
if (menu == null) {
|
||||
menu = option.theme.defaultMenu;
|
||||
} else {
|
||||
if (option.theme.allowCustom === false) {
|
||||
menu = option.theme.defaultMenu;
|
||||
}
|
||||
}
|
||||
localStorage.setItem("theme-menu", menu);
|
||||
this.menuSkin(menu);
|
||||
}
|
||||
|
||||
this.menuSkin = function(theme) {
|
||||
var laytpApidoc = layui.$(".laytp-admin");
|
||||
laytpApidoc.removeClass("light-theme");
|
||||
laytpApidoc.removeClass("dark-theme");
|
||||
laytpApidoc.addClass(theme);
|
||||
}
|
||||
|
||||
this.logout = function(callback) {
|
||||
logout = callback;
|
||||
}
|
||||
|
||||
this.message = function(callback) {
|
||||
if (callback != null) {
|
||||
msgInstance.click(callback);
|
||||
} else {
|
||||
msgInstance.click(messageTip);
|
||||
}
|
||||
}
|
||||
|
||||
this.jump = function(id, title, url) {
|
||||
if (config.tab.muiltTab) {
|
||||
bodyTab.addTabOnly({
|
||||
id: id,
|
||||
title: title,
|
||||
url: url,
|
||||
icon: null,
|
||||
close: true
|
||||
}, 300);
|
||||
} else {
|
||||
sideMenu.selectItem(id);
|
||||
bodyFrame.changePage(url, title, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var messageTip = function(id, title, context, form) {
|
||||
layer.open({
|
||||
type: 1,
|
||||
title: '消息', //标题
|
||||
area: ['390px', '330px'], //宽高
|
||||
shade: 0.4, //遮罩透明度
|
||||
content: "<div style='background-color:whitesmoke;'><div class='layui-card'><div class='layui-card-body'>来源 : " +
|
||||
form + "</div><div class='layui-card-header' >标题 : " + title +
|
||||
"</div><div class='layui-card-body' >内容 : " + context + "</div></div></div>", //支持获取DOM元素
|
||||
btn: ['确认'], //按钮组
|
||||
scrollbar: false, //屏蔽浏览器滚动条
|
||||
yes: function(index) { //layer.msg('yes'); //点击确定回调
|
||||
layer.close(index);
|
||||
showToast();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function collaspe() {
|
||||
sideMenu.collaspe();
|
||||
var admin = layui.$(".laytp-admin");
|
||||
var left = layui.$(".layui-icon-spread-left")
|
||||
var right = layui.$(".layui-icon-shrink-right")
|
||||
if (admin.is(".laytp-mini")) {
|
||||
$(".search").show();
|
||||
left.addClass("layui-icon-shrink-right")
|
||||
left.removeClass("layui-icon-spread-left")
|
||||
admin.removeClass("laytp-mini");
|
||||
} else {
|
||||
$(".search").hide();
|
||||
right.addClass("layui-icon-spread-left")
|
||||
right.removeClass("layui-icon-shrink-right")
|
||||
admin.addClass("laytp-mini");
|
||||
}
|
||||
}
|
||||
|
||||
body.on("click", ".logout", function() {
|
||||
// 回调
|
||||
var result = logout();
|
||||
|
||||
if (result) {
|
||||
// 清空缓存
|
||||
bodyTab.clear();
|
||||
}
|
||||
})
|
||||
|
||||
body.on("click", ".collaspe,.laytp-cover", function() {
|
||||
collaspe();
|
||||
});
|
||||
|
||||
body.on("click", ".fullScreen", function() {
|
||||
if (layui.$(this).hasClass("layui-icon-screen-restore")) {
|
||||
screenFun(2).then(function() {
|
||||
layui.$(".fullScreen").eq(0).removeClass("layui-icon-screen-restore");
|
||||
});
|
||||
} else {
|
||||
screenFun(1).then(function() {
|
||||
layui.$(".fullScreen").eq(0).addClass("layui-icon-screen-restore");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
body.on("click", '[user-menu-id]', function() {
|
||||
if (config.tab.muiltTab) {
|
||||
bodyTab.addTabOnly({
|
||||
id: layui.$(this).attr("user-menu-id"),
|
||||
title: layui.$(this).attr("user-menu-title"),
|
||||
url: layui.$(this).attr("user-menu-url"),
|
||||
icon: "",
|
||||
close: true
|
||||
}, 300);
|
||||
} else {
|
||||
bodyFrame.changePage(layui.$(this).attr("user-menu-url"), "", true);
|
||||
}
|
||||
});
|
||||
|
||||
body.on("click", ".setting", function() {
|
||||
|
||||
var bgColorHtml =
|
||||
'<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>';
|
||||
|
||||
var html =
|
||||
'<div class="laytpone-color">\n' +
|
||||
'<div class="color-title">整体风格</div>\n' +
|
||||
'<div class="color-content">\n' +
|
||||
'<ul>\n' + bgColorHtml + '</ul>\n' +
|
||||
'</div>\n' +
|
||||
'</div>';
|
||||
|
||||
layer.open({
|
||||
type: 1,
|
||||
offset: 'r',
|
||||
area: ['320px', '100%'],
|
||||
title: false,
|
||||
shade: 0.1,
|
||||
closeBtn: 0,
|
||||
shadeClose: false,
|
||||
anim: -1,
|
||||
skin: 'layer-anim-right',
|
||||
move: false,
|
||||
content: html + buildColorHtml() + buildLinkHtml() + bottomTool(),
|
||||
success: function(layero, index) {
|
||||
|
||||
var color = localStorage.getItem("theme-color");
|
||||
var menu = localStorage.getItem("theme-menu");
|
||||
|
||||
if (color !== "null") {
|
||||
layui.$(".select-color-item").removeClass("layui-icon").removeClass("layui-icon-ok");
|
||||
layui.$("*[color-id='" + color + "']").addClass("layui-icon").addClass("layui-icon-ok");
|
||||
}
|
||||
if (menu !== "null") {
|
||||
layui.$("*[data-select-bgcolor]").removeClass("layui-this");
|
||||
layui.$("[data-select-bgcolor='" + menu + "']").addClass("layui-this");
|
||||
}
|
||||
layui.$('#layui-layer-shade' + index).click(function() {
|
||||
var $layero = layui.$('#layui-layer' + index);
|
||||
$layero.animate({
|
||||
left: $layero.offset().left + $layero.width()
|
||||
}, 200, function() {
|
||||
layer.close(index);
|
||||
});
|
||||
})
|
||||
|
||||
layui.$('#closeTheme').click(function() {
|
||||
var $layero = layui.$('#layui-layer' + index);
|
||||
$layero.animate({
|
||||
left: $layero.offset().left + $layero.width()
|
||||
}, 200, function() {
|
||||
layer.close(index);
|
||||
});
|
||||
})
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function bottomTool() {
|
||||
return "<button id='closeTheme' style='position: absolute;bottom: 20px;left: 20px;' class='laytp-btn'>关闭</button>"
|
||||
}
|
||||
|
||||
body.on('click', '[data-select-bgcolor]', function() {
|
||||
var theme = layui.$(this).attr('data-select-bgcolor');
|
||||
layui.$('[data-select-bgcolor]').removeClass("layui-this");
|
||||
layui.$(this).addClass("layui-this");
|
||||
localStorage.setItem("theme-menu", theme);
|
||||
laytpApidoc.menuSkin(theme);
|
||||
});
|
||||
|
||||
body.on('click', '.select-color-item', function() {
|
||||
layui.$(".select-color-item").removeClass("layui-icon").removeClass("layui-icon-ok");
|
||||
layui.$(this).addClass("layui-icon").addClass("layui-icon-ok");
|
||||
var colorId = layui.$(".select-color-item.layui-icon-ok").attr("color-id");
|
||||
var currentColor = getColorById(colorId);
|
||||
localStorage.setItem("theme-color", currentColor.id);
|
||||
localStorage.setItem("theme-color-context", currentColor.color);
|
||||
laytpTheme.changeTheme(window, config.other.autoHead);
|
||||
});
|
||||
|
||||
function applyConfig(param) {
|
||||
config = param;
|
||||
laytpApidoc.logoRender(param);
|
||||
laytpApidoc.menuRender(param);
|
||||
laytpApidoc.bodyRender(param);
|
||||
laytpApidoc.themeRender(param);
|
||||
laytpApidoc.keepLoad(param);
|
||||
if (param.header.message !== false) {
|
||||
laytpApidoc.messageRender(param);
|
||||
}
|
||||
}
|
||||
|
||||
function getColorById(id) {
|
||||
var color;
|
||||
var flag = false;
|
||||
$.each(config.colors, function(i, value) {
|
||||
if (value.id === id) {
|
||||
color = value;
|
||||
flag = true;
|
||||
}
|
||||
})
|
||||
if (flag === false || config.theme.allowCustom === false) {
|
||||
$.each(config.colors, function(i, value) {
|
||||
if (value.id === config.theme.defaultColor) {
|
||||
color = value;
|
||||
}
|
||||
})
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
function buildLinkHtml() {
|
||||
var links = "";
|
||||
$.each(config.links, function(i, value) {
|
||||
links += '<a class="more-menu-item" href="' + value.href + '">' +
|
||||
'<i class="' + value.icon + '" style="font-size: 19px;"></i> ' + value.title +
|
||||
'</a>'
|
||||
})
|
||||
return '<div class="more-menu-list">' + links + '</div>';
|
||||
}
|
||||
|
||||
function buildColorHtml() {
|
||||
return "";
|
||||
var colors = "";
|
||||
$.each(config.colors, function(i, value) {
|
||||
colors += "<span class='select-color-item' color-id='" + value.id + "' style='background-color:" + value.color +
|
||||
";'></span>";
|
||||
})
|
||||
return "<div class='select-color'><div class='select-color-title'>主题配色</div><div class='select-color-content'>" +
|
||||
colors + "</div></div>"
|
||||
}
|
||||
|
||||
function compatible() {
|
||||
if (layui.$(window).width() <= 768) {
|
||||
collaspe()
|
||||
}
|
||||
}
|
||||
|
||||
function screenFun(num) {
|
||||
num = num || 1;
|
||||
num = num * 1;
|
||||
var 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;
|
||||
}
|
||||
return new Promise(function(res, rej) {
|
||||
res("返回值");
|
||||
});
|
||||
}
|
||||
|
||||
function isFullscreen() {
|
||||
return document.fullscreenElement ||
|
||||
document.msFullscreenElement ||
|
||||
document.mozFullScreenElement ||
|
||||
document.webkitFullscreenElement || false;
|
||||
}
|
||||
|
||||
window.onresize = function() {
|
||||
if (!isFullscreen()) {
|
||||
layui.$(".fullScreen").eq(0).removeClass("layui-icon-screen-restore");
|
||||
}
|
||||
}
|
||||
|
||||
exports('apidoc', laytpApidoc);
|
||||
})
|
||||
@@ -0,0 +1,491 @@
|
||||
layui.define(['table', 'jquery', 'element'], function(exports) {
|
||||
"use strict";
|
||||
|
||||
var MOD_NAME = 'apimenu',
|
||||
$ = layui.jquery,
|
||||
element = layui.element;
|
||||
|
||||
var laytpApiMenu = function(opt) {
|
||||
this.option = opt;
|
||||
};
|
||||
|
||||
// 供外部调用的,渲染菜单方法
|
||||
laytpApiMenu.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() {}
|
||||
};
|
||||
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 {
|
||||
//renderMenu中需要调用done事件,done事件中需要menu对象,但是此时还未返回menu对象,做个延时提前返回对象
|
||||
window.setTimeout(function() { renderMenu(option);}, 500);
|
||||
}
|
||||
|
||||
// 处理高度
|
||||
layui.$("#"+opt.elem).height(option.height);
|
||||
return new laytpApiMenu(opt);
|
||||
};
|
||||
|
||||
// 任意左侧菜单绑定点击事件
|
||||
laytpApiMenu.prototype.click = function(clickEvent) {
|
||||
var _this = this;
|
||||
layui.$(document).off("click", "#" + _this.option.elem + " .site-demo-active").on("click", "#" + _this.option.elem + " .site-demo-active", function() {
|
||||
// layui.$("body").on("click", "#" + _this.option.elem + " .site-demo-active", function() {
|
||||
var dom = layui.$(this);
|
||||
var data = {
|
||||
menuId: dom.attr("menu-id"),
|
||||
docType: dom.attr("doc-type"),
|
||||
menuTitle: dom.attr("menu-title"),
|
||||
};
|
||||
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 (layui.$("#" + _this.option.elem).is(".laytp-nav-mini")) {
|
||||
if (_this.option.accordion) {
|
||||
activeMenus = layui.$(this).parent().parent().parent().children("a");
|
||||
} else {
|
||||
activeMenus.push(layui.$(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;
|
||||
}
|
||||
|
||||
// 样式选择
|
||||
laytpApiMenu.prototype.skin = function(skin) {
|
||||
var menu = layui.$(".laytp-nav-tree[lay-filter='" + this.option.elem + "']").parent();
|
||||
menu.removeClass("dark-theme");
|
||||
menu.removeClass("light-theme");
|
||||
menu.addClass(skin);
|
||||
};
|
||||
|
||||
// 选择没有子级的菜单节点
|
||||
laytpApiMenu.prototype.selectItem = function(laytpId) {
|
||||
if (this.option.control != false) {
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-side-scroll ").find("ul").css({
|
||||
display: "none"
|
||||
});
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass(
|
||||
"layui-this");
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("ul").css({
|
||||
display: "block"
|
||||
});
|
||||
var controlId = layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("ul").attr("menu-id");
|
||||
if (controlId != undefined) {
|
||||
layui.$("#" + this.option.control).find(".layui-this").removeClass("layui-this");
|
||||
layui.$("#" + this.option.control).find("[menu-id='" + controlId + "']").addClass("layui-this");
|
||||
}
|
||||
}
|
||||
if (this.option.accordion === true) {
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".laytp-nav-tree").find(".layui-nav-itemed").removeClass(
|
||||
"layui-nav-itemed");
|
||||
}
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".laytp-nav-tree").find(".layui-this").removeClass(
|
||||
"layui-this");
|
||||
if (!layui.$("#" + this.option.elem).is(".laytp-nav-mini")) {
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-nav-item").addClass("layui-nav-itemed");
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("dd").addClass("layui-nav-itemed");
|
||||
}
|
||||
layui.$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parent().addClass("layui-this");
|
||||
}
|
||||
|
||||
var activeMenus;
|
||||
// 手机模式下,右下角按钮点击展开收缩左侧菜单事件
|
||||
laytpApiMenu.prototype.collaspe = function(time) {
|
||||
var elem = this.option.elem;
|
||||
var config = this.option;
|
||||
if (layui.$("#" + this.option.elem).is(".laytp-nav-mini")) {
|
||||
$.each(activeMenus, function(i, item) {
|
||||
layui.$("#" + elem + " a[menu-id='" + layui.$(this).attr("menu-id") + "']").parent().addClass("layui-nav-itemed");
|
||||
});
|
||||
layui.$("#" + this.option.elem).removeClass("laytp-nav-mini");
|
||||
layui.$("#" + this.option.elem).animate({
|
||||
width: "220px"
|
||||
}, 150);
|
||||
isHoverMenu(false, config);
|
||||
} else {
|
||||
activeMenus = layui.$("#" + this.option.elem).find(".layui-nav-itemed>a");
|
||||
layui.$("#" + this.option.elem).find(".layui-nav-itemed").removeClass("layui-nav-itemed");
|
||||
layui.$("#" + this.option.elem).addClass("laytp-nav-mini");
|
||||
layui.$("#" + this.option.elem).animate({
|
||||
width: "60px"
|
||||
}, 400);
|
||||
isHoverMenu(true, config);
|
||||
}
|
||||
};
|
||||
|
||||
function getData(url) {
|
||||
var defer = $.Deferred();
|
||||
var contact = (url.indexOf('?') > -1) ? "&" : "?";
|
||||
$.get(url + contact + "fresh=" + Math.random(), function(result) {
|
||||
defer.resolve(result)
|
||||
});
|
||||
return defer.promise();
|
||||
}
|
||||
|
||||
function postData(url) {
|
||||
var defer = $.Deferred();
|
||||
$.post(url + "?fresh=" + Math.random(), function(result) {
|
||||
defer.resolve(result)
|
||||
});
|
||||
return defer.promise();
|
||||
}
|
||||
|
||||
// 内部使用,渲染菜单函数
|
||||
function renderMenu(option) {
|
||||
if (option.parseData != false) {
|
||||
option.data = 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 = '<div style="height:100%!important;" class="laytp-side-scroll layui-side-scroll ' + option.theme + '"><ul lay-filter="' + option.elem +
|
||||
'" class="layui-nav arrow laytp-menu layui-nav-tree laytp-nav-tree">';
|
||||
$.each(option.data, function(i, item) {
|
||||
var content = '<li class="layui-nav-item">';
|
||||
if (i === option.defaultOpen) {
|
||||
content = '<li class="layui-nav-item layui-nav-itemed">';
|
||||
}
|
||||
var href = "javascript:void(0);";
|
||||
var className = "site-demo-active";
|
||||
if (item.openType === "_blank" && item.type === 1) {
|
||||
className = "";
|
||||
}
|
||||
if (item.type === 0) {
|
||||
// 创 建 目 录 结 构
|
||||
content += '<a href="javascript:;" menu-type="' + item.type + '" doc-type="' + item.docType + '" menu-id="' + item.id + '" href="' + href + '">' +
|
||||
'<span>' + item.title + '</span></a>';
|
||||
} else if (item.type === 1) {
|
||||
content += '<a class="' + className + '" menu-type="' + item.type + '" doc-type="' + item.docType + '" menu-id="' +
|
||||
item.id +
|
||||
'" menu-title="' + item.title + '" href="' + href + '"><span>' + item.title + '</span></a>';
|
||||
}
|
||||
// 调 用 递 归 方 法 加 载 无 限 层 级 的 子 菜 单
|
||||
content += loadchild(item);
|
||||
// 结 束 一 个 根 菜 单 项
|
||||
content += '</li>';
|
||||
menuHtml += content;
|
||||
});
|
||||
// 结 束 菜 单 结 构 的 初 始 化
|
||||
menuHtml += "</ul></div>";
|
||||
// 将 菜 单 拼 接 到 初 始 化 容 器 中
|
||||
layui.$("#" + option.elem).html(menuHtml);
|
||||
}
|
||||
|
||||
// 创建多系统菜单, 包括渲染静态html和绑定顶部菜单点击事件
|
||||
function createMenuAndControl(option) {
|
||||
var control = '<ul class="layui-nav laytp-nav-control pc layui-hide-xs">';
|
||||
var controlPe = '<ul class="layui-nav laytp-nav-control layui-hide-sm">';
|
||||
// 声 明 头 部
|
||||
var menu = '<div class="layui-side-scroll ' + option.theme + '">';
|
||||
// 开 启 同 步 操 作
|
||||
var index = 0;
|
||||
var controlItemPe = '<dl class="layui-nav-child">';
|
||||
var config = layui.apidoc.readConfig();
|
||||
$.each(option.data, function(i, item) {
|
||||
var menuItem = '';
|
||||
var controlItem = '';
|
||||
if(index < config.menu['maxTopMenuNum']){
|
||||
if (i === option.defaultMenu) {
|
||||
controlItem = '<li menu-title="' + item.title + '" doc-type="' + item.docType + '" menu-id="' + item.id +
|
||||
'" class="layui-this layui-nav-item"><a href="javascript:void(0);">' + item.title + '</a></li>';
|
||||
menuItem = '<ul menu-id="' + item.id + '" lay-filter="' + option.elem +
|
||||
'" class="layui-nav arrow laytp-menu layui-nav-tree laytp-nav-tree">';
|
||||
// 兼容移动端
|
||||
controlPe += '<li class="layui-nav-item"><a class="pe-title" href="javascript:void(0);" >' + item.title + '</a>';
|
||||
controlItemPe += '<dd menu-title="' + item.title + '" menu-id="' + item.id +
|
||||
'"><a href="javascript:void(0);">' + item.title + '</a></dd>';
|
||||
} else {
|
||||
controlItem = '<li menu-title="' + item.title + '" doc-type="' + item.docType + '" menu-id="' + item.id +
|
||||
'" class="layui-nav-item"><a href="javascript:void(0);">' + item.title + '</a></li>';
|
||||
menuItem = '<ul style="display:none" menu-id="' + item.id + '" lay-filter="' + option.elem +
|
||||
'" class="layui-nav arrow layui-nav-tree laytp-nav-tree">';
|
||||
controlItemPe += '<dd menu-title="' + item.title + '" doc-type="' + item.docType + '" menu-id="' + item.id +
|
||||
'"><a href="javascript:void(0);">' + item.title + '</a></dd>';
|
||||
}
|
||||
index++;
|
||||
}else{
|
||||
menuItem = '<ul style="display:none" menu-id="' + item.id + '" lay-filter="' + option.elem +
|
||||
'" class="layui-nav arrow layui-nav-tree laytp-nav-tree">';
|
||||
controlItemPe += '<dd menu-title="' + item.title + '" doc-type="' + item.docType + '" menu-id="' + item.id +
|
||||
'"><a href="javascript:void(0);">' + item.title + '</a></dd>';
|
||||
index++;
|
||||
}
|
||||
|
||||
$.each(item.children, function(i, note) {
|
||||
// 创 建 每 一 个 菜 单 项
|
||||
var content = '<li class="layui-nav-item" >';
|
||||
var href = "javascript:void(0);";
|
||||
var target = "";
|
||||
var className = "site-demo-active";
|
||||
if (note.openType == "_blank" && note.type == 1) {
|
||||
href = note.href;
|
||||
target = "target='_blank'";
|
||||
className = "";
|
||||
}
|
||||
// 判 断 菜 单 类 型 0 是 不可跳转的目录 1 是 可 点 击 跳 转 的 菜 单
|
||||
if (note.type == 0) {
|
||||
// 创 建 目 录 结 构
|
||||
content += '<a href="javascript:void(0);" menu-type="' + item.type + '" doc-type="' + note.docType + '" menu-id="' + note.id +
|
||||
'" ><span>' + note.title +
|
||||
'</span></a>';
|
||||
} else if (note.type == 1) {
|
||||
// 创 建 菜 单 结 构
|
||||
content += '<a class="' + className + '" menu-type="' + item.type + '" doc-type="' + note.docType + '" menu-id="' + note.id +
|
||||
'" menu-title="' + note.title + '" href="' + href + '"><span>' + note.title + '</span></a>';
|
||||
}
|
||||
content += loadchild(note);
|
||||
content += '</li>';
|
||||
menuItem += content;
|
||||
})
|
||||
menu += menuItem + '</ul>';
|
||||
control += controlItem;
|
||||
})
|
||||
|
||||
if(option.data.length > config.menu['maxTopMenuNum']){
|
||||
control += '<li class="layui-nav-item">\n' +
|
||||
'<a href="javascript:void(0);" class="laytp-icon laytp-icon-elipsis"></a>' +
|
||||
'<!-- 功 能 菜 单 -->\n' +
|
||||
'<dl class="layui-nav-child">';
|
||||
var moreTopMenuIndex = 0;
|
||||
$.each(option.data, function(i, item) {
|
||||
var moreTopMenuItem = '';
|
||||
if(moreTopMenuIndex >= config.menu['maxTopMenuNum']){
|
||||
moreTopMenuItem = '<dd><a menu-title="' + item.title + '" menu-id="' + item.id +'">' + item.title + '</a></dd>';
|
||||
}
|
||||
moreTopMenuIndex++;
|
||||
control += moreTopMenuItem;
|
||||
});
|
||||
control += '</dl></li></ul>';
|
||||
}
|
||||
|
||||
controlItemPe += "</li></dl></ul>";
|
||||
controlPe += controlItemPe;
|
||||
layui.$("#" + option.control).html(control);
|
||||
layui.$("#" + option.control).append(controlPe);
|
||||
layui.$("#" + option.elem).html(menu);
|
||||
// 绑定顶部菜单点击事件
|
||||
layui.$(document).off("click", "#" + option.control + " .laytp-nav-control [menu-id]").on("click", "#" + option.control + " .laytp-nav-control [menu-id]", function() {
|
||||
// layui.$("#" + option.control + " .laytp-nav-control").on("click", "[menu-id]", function() {
|
||||
layui.$("#" + option.elem).find(".laytp-nav-tree").css({
|
||||
display: 'none'
|
||||
});
|
||||
layui.$("#" + option.elem).find(".laytp-nav-tree[menu-id='" + layui.$(this).attr("menu-id") + "']").css({
|
||||
display: 'block'
|
||||
});
|
||||
layui.$("#" + option.control).find(".pe-title").html(layui.$(this).attr("menu-title"));
|
||||
layui.$("#" + option.control).find("");
|
||||
option.change(layui.$(this).attr("menu-id"), layui.$(this).attr("menu-title"), layui.$(this).attr("menu-href"));
|
||||
// 自动点击左侧的第一个菜单
|
||||
var s = layui.$("a:first",layui.$("#" + option.elem).find("ul[style='display: block;']")).parent();
|
||||
if(s.hasClass("layui-nav-itemed")){
|
||||
recursionFindA(s);
|
||||
}else{
|
||||
layui.$("a:first",layui.$("#" + option.elem).find("ul[style='display: block;']")).click();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 点击顶部菜单,自动点击左侧菜单,当菜单已经是展开状态时,要递归找到最低级别的A标签进行点击
|
||||
function recursionFindA(obj){
|
||||
var dlObj = layui.$("dl:first", obj);
|
||||
if(dlObj.length === 0){
|
||||
layui.$("a:first", obj).click();
|
||||
}else{
|
||||
recursionFindA(layui.$("dd:first",dlObj));
|
||||
}
|
||||
}
|
||||
|
||||
/** 加载子菜单 (递归)*/
|
||||
function loadchild(obj) {
|
||||
// 判 单 是 否 是 菜 单, 如 果 是 菜 单 直 接 返 回
|
||||
if (obj.type == 1) {
|
||||
return "";
|
||||
}
|
||||
// 创 建 子 菜 单 结 构
|
||||
var content = '<dl class="layui-nav-child">';
|
||||
// 如 果 嵌 套 不 等 于 空
|
||||
if (obj.children != null && obj.children.length > 0) {
|
||||
// 遍 历 子 项 目
|
||||
$.each(obj.children, function(i, note) {
|
||||
// 创 建 子 项 结 构
|
||||
content += '<dd>';
|
||||
var href = "javascript:;";
|
||||
var target = "";
|
||||
var className = "site-demo-active";
|
||||
if (note.openType == "_blank" && note.type == 1) {
|
||||
href = note.href;
|
||||
target = "target='_blank'";
|
||||
className = "";
|
||||
}
|
||||
// 判 断 子 项 类 型
|
||||
if (note.type == 0) {
|
||||
// 创 建 目 录 结 构
|
||||
content += '<a href="' + href + '" doc-type="' + note.docType + '" menu-id="' + note.id +
|
||||
'"><i class="' + note.icon + '"></i><span>' + note.title + '</span></a>';
|
||||
} else if (note.type == 1) {
|
||||
// 创 建 菜 单 结 构
|
||||
content += '<a class="' + className + '" doc-type="' + note.docType + '" menu-id="' + note.id + '" menu-title="' + note.title + '" href="' + href +
|
||||
'" ><i class="' + note.icon + '"></i><span>' + note.title + '</span></a>';
|
||||
}
|
||||
// 加 载 子 项 目 录
|
||||
content += loadchild(note);
|
||||
// 结 束 当 前 子 菜 单
|
||||
content += '</dd>';
|
||||
});
|
||||
// 封 装
|
||||
} else {
|
||||
content += '<div class="toast"> 无 内 容 </div>';
|
||||
}
|
||||
content += '</dl>';
|
||||
return content;
|
||||
}
|
||||
|
||||
// 左侧菜单点击事件
|
||||
function downShow(option) {
|
||||
layui.$(document).off("click", "#" + option.elem + " a[menu-type='0']").on("click", "#" + option.elem + " a[menu-type='0']", function() {
|
||||
// layui.$("body #" + option.elem).on("click", "a[menu-type='0']", function() {
|
||||
if (!layui.$("#" + option.elem).is(".laytp-nav-mini")) {
|
||||
var superEle = layui.$(this).parent();
|
||||
var ele = layui.$(this).next('.layui-nav-child');
|
||||
var heights = ele.children("dd").length * 48;
|
||||
|
||||
if (superEle.is(".layui-nav-itemed")) {
|
||||
if (option.accordion) {
|
||||
superEle.parent().find(".layui-nav-itemed").removeClass("layui-nav-itemed");
|
||||
superEle.addClass("layui-nav-itemed");
|
||||
//自动点击第一个子菜单
|
||||
layui.$("a:first",ele).click();
|
||||
}
|
||||
ele.height(0);
|
||||
ele.animate({
|
||||
height: heights + "px"
|
||||
}, 200, function() {
|
||||
ele.css({
|
||||
height: "auto"
|
||||
});
|
||||
});
|
||||
} else {
|
||||
superEle.addClass("layui-nav-itemed");
|
||||
ele.animate({
|
||||
height: "0px"
|
||||
}, 200, function() {
|
||||
ele.css({
|
||||
height: "auto"
|
||||
});
|
||||
superEle.removeClass("layui-nav-itemed");
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** 二 级 悬 浮 菜 单*/
|
||||
function isHoverMenu(b, option) {
|
||||
if (b) {
|
||||
layui.$("#" + option.elem + ".laytp-nav-mini .layui-nav-item,#" + option.elem + ".laytp-nav-mini dd").hover(function(e) {
|
||||
e.stopPropagation();
|
||||
var _this = layui.$(this);
|
||||
_this.siblings().find(".layui-nav-child")
|
||||
.removeClass("layui-nav-hover").css({
|
||||
left: 0,
|
||||
top: 0
|
||||
});
|
||||
_this.children(".layui-nav-child").addClass("layui-nav-hover");
|
||||
_this.closest('.layui-nav-item').data('time') && clearTimeout(_this.closest('.layui-nav-item').data('time'));
|
||||
var height = layui.$(window).height();
|
||||
var topLength = _this.offset().top;
|
||||
var thisHeight = _this.children(".layui-nav-child").height();
|
||||
if ((thisHeight + topLength) > height) {
|
||||
topLength = height - thisHeight - 10;
|
||||
}
|
||||
var left = _this.offset().left + 60;
|
||||
if (!_this.hasClass("layui-nav-item")) {
|
||||
left = _this.offset().left + _this.width();
|
||||
}
|
||||
_this.children(".layui-nav-child").offset({
|
||||
top: topLength,
|
||||
left: left + 3
|
||||
});
|
||||
}, function(e) {
|
||||
e.stopPropagation();
|
||||
var _this = layui.$(this);
|
||||
_this.closest('.layui-nav-item').data('time', setTimeout(function() {
|
||||
_this.closest('.layui-nav-item')
|
||||
.find(".layui-nav-child")
|
||||
.removeClass("layui-nav-hover")
|
||||
.css({
|
||||
left: 0,
|
||||
top: 0
|
||||
});
|
||||
}, 50));
|
||||
})
|
||||
} else {
|
||||
layui.$("#" + option.elem + " .layui-nav-item").off('mouseenter').unbind('mouseleave');
|
||||
layui.$("#" + option.elem + " dd").off('mouseenter').unbind('mouseleave');
|
||||
}
|
||||
}
|
||||
|
||||
exports(MOD_NAME, new laytpApiMenu());
|
||||
});
|
||||
Reference in New Issue
Block a user