|
|
layui.define(["element", "jquery"], function (exports) {
|
|
|
// 获取 layui 的 element 模块
|
|
|
var element = layui.element,
|
|
|
// 获取 layui 的 jQuery 模块
|
|
|
$ = layui.$,
|
|
|
// miniAdmin = layui.miniAdmin, // 注释掉的代码,可能是用于获取 miniAdmin 模块
|
|
|
// 获取 layui 的 layer 模块
|
|
|
layer = layui.layer;
|
|
|
|
|
|
|
|
|
var miniPage = {
|
|
|
|
|
|
/**
|
|
|
* 初始化tab
|
|
|
* @param options 配置选项对象,包含homeInfo、menuList、multiModule等属性
|
|
|
*/
|
|
|
render: function (options) {
|
|
|
// 如果options.homeInfo未定义,则设置为空对象
|
|
|
options.homeInfo = options.homeInfo || {};
|
|
|
// 如果options.menuList未定义,则设置为空数组
|
|
|
options.menuList = options.menuList || [];
|
|
|
// 如果options.multiModule未定义,则设置为false
|
|
|
options.multiModule = options.multiModule || false;
|
|
|
// 如果options.renderPageVersion未定义,则设置为false
|
|
|
options.renderPageVersion = options.renderPageVersion || false;
|
|
|
// 如果options.listenSwichCallback未定义,则设置为空函数
|
|
|
options.listenSwichCallback = options.listenSwichCallback || function () {};
|
|
|
|
|
|
// 获取当前URL的hash部分并去掉前缀'#/'
|
|
|
var href = location.hash.replace(/^#\//, '');
|
|
|
|
|
|
// 如果href为空或未定义,则渲染主页
|
|
|
if (href === null || href === undefined || href === '') {
|
|
|
miniPage.renderHome(options);
|
|
|
} else {
|
|
|
// 否则渲染指定页面
|
|
|
miniPage.renderPage(href, options);
|
|
|
// 根据是否多模块进行监听切换
|
|
|
if (options.multiModule) {
|
|
|
miniPage.listenSwitchMultiModule(href);
|
|
|
} else {
|
|
|
miniPage.listenSwitchSingleModule(href);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 监听事件
|
|
|
miniPage.listen(options);
|
|
|
// 监听hash变化
|
|
|
miniPage.listenHash(options);
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 初始化主页
|
|
|
* @param options
|
|
|
*/
|
|
|
/**
|
|
|
* 渲染主页函数
|
|
|
* @param {Object} options - 包含主页信息和其他选项的对象
|
|
|
*/
|
|
|
renderHome: function (options) {
|
|
|
// 如果options.homeInfo不存在,则初始化为空对象
|
|
|
options.homeInfo = options.homeInfo || {};
|
|
|
// 如果options.homeInfo.href不存在,则初始化为空字符串
|
|
|
options.homeInfo.href = options.homeInfo.href || '';
|
|
|
// 如果options.renderPageVersion不存在,则初始化为false
|
|
|
options.renderPageVersion = options.renderPageVersion || false;
|
|
|
// 隐藏页面头部元素
|
|
|
$('.layuimini-page-header').addClass('layui-hide');
|
|
|
// 渲染页面内容,传入主页链接和选项参数
|
|
|
miniPage.renderPageContent(options.homeInfo.href, options);
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 初始化页面
|
|
|
* @param href
|
|
|
* @param options
|
|
|
*/
|
|
|
/**
|
|
|
* 渲染页面的函数
|
|
|
* @param {string} href - 页面的URL地址
|
|
|
* @param {Object} options - 渲染选项
|
|
|
*/
|
|
|
renderPage: function (href, options) {
|
|
|
// 调用miniPage对象的renderPageTitle方法,渲染页面标题
|
|
|
miniPage.renderPageTitle(href, options);
|
|
|
// 调用miniPage对象的renderPageContent方法,渲染页面内容
|
|
|
miniPage.renderPageContent(href, options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化页面标题
|
|
|
* @param href
|
|
|
* @param options
|
|
|
*/
|
|
|
renderPageTitle: function (href, options) {
|
|
|
// 初始化homeInfo对象,如果不存在则设置为空对象
|
|
|
options.homeInfo = options.homeInfo || {};
|
|
|
// 设置homeInfo的title属性,如果不存在则默认为'主页'
|
|
|
options.homeInfo.title = options.homeInfo.title || '主页';
|
|
|
// 初始化menuList数组,如果不存在则设置为空数组
|
|
|
options.menuList = options.menuList || [];
|
|
|
// 显示页面头部元素
|
|
|
$('.layuimini-page-header').removeClass('layui-hide');
|
|
|
// 构建初始的页面标题HTML,包含返回主页链接和分隔符
|
|
|
var pageTitleHtml = '<a lay-href="" href="javascript:;" class="layuimini-back-home">' + options.homeInfo.title + '</a><span lay-separator="">/</span>\n';
|
|
|
// 根据href和menuList生成页面标题数组
|
|
|
var pageTitleArray = miniPage.buildPageTitleArray(href, options.menuList);
|
|
|
// 如果页面标题数组不为空
|
|
|
if (pageTitleArray.length > 0) {
|
|
|
// 遍历页面标题数组
|
|
|
for (var key in pageTitleArray) {
|
|
|
key = parseInt(key); // 将key转换为整数类型
|
|
|
// 如果不是最后一个元素,添加链接和分隔符
|
|
|
if (key !== pageTitleArray.length - 1) {
|
|
|
pageTitleHtml += '<a><cite>' + pageTitleArray[key] + '</cite></a><span lay-separator="">/</span>\n';
|
|
|
} else {
|
|
|
// 如果是最后一个元素,只添加链接
|
|
|
pageTitleHtml += '<a><cite>' + pageTitleArray[key] + '</cite></a>\n';
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
// 如果页面标题数组为空,从sessionStorage中获取标题
|
|
|
var title = sessionStorage.getItem('layuimini_page_title');
|
|
|
// 如果标题为空或未定义,隐藏页面头部元素
|
|
|
if (title === null || title === undefined || title === '') {
|
|
|
$('.layuimini-page-header').addClass('layui-hide');
|
|
|
} else {
|
|
|
// 否则,添加标题到页面标题HTML中
|
|
|
pageTitleHtml += '<a><cite>' + title + '</cite></a>\n';
|
|
|
}
|
|
|
}
|
|
|
// 清空并更新页面头部的标题内容
|
|
|
$('.layuimini-page-header .layuimini-page-title').empty().html(pageTitleHtml);
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 初始化页面内容
|
|
|
* @param options
|
|
|
* @param href
|
|
|
*/
|
|
|
renderPageContent: function (href, options) {
|
|
|
// 设置默认的 renderPageVersion 选项为 false,如果未提供该选项
|
|
|
options.renderPageVersion = options.renderPageVersion || false;
|
|
|
|
|
|
// 定义页面内容的容器选择器
|
|
|
var container = '.layuimini-content-page';
|
|
|
|
|
|
// 如果需要渲染页面版本,则在 URL 中添加时间戳参数
|
|
|
if (options.renderPageVersion) {
|
|
|
var v = new Date().getTime();
|
|
|
href = href.indexOf("?") > -1 ? href + '&v=' + v : href + '?v=' + v;
|
|
|
}
|
|
|
|
|
|
// 根据页面头部是否隐藏来调整内容容器的高度样式
|
|
|
if ($(".layuimini-page-header").hasClass("layui-hide")) {
|
|
|
$(container).removeAttr("style");
|
|
|
} else {
|
|
|
$(container).attr("style", "height: calc(100% - 36px)");
|
|
|
}
|
|
|
|
|
|
// 清空内容容器的 HTML 内容
|
|
|
$(container).html('');
|
|
|
|
|
|
// 发起 AJAX 请求获取页面内容
|
|
|
$.ajax({
|
|
|
url: href,
|
|
|
type: 'get',
|
|
|
dataType: 'html',
|
|
|
success: function (data) {
|
|
|
// 将获取到的内容插入到内容容器中,并初始化元素
|
|
|
$(container).html(data);
|
|
|
element.init();
|
|
|
},
|
|
|
error: function (xhr, textstatus, thrown) {
|
|
|
// 处理请求失败的情况,显示错误信息
|
|
|
return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 刷新页面内容
|
|
|
* @param options
|
|
|
*/
|
|
|
refresh: function (options) {
|
|
|
// 获取当前URL的hash部分,并去掉开头的'#/'
|
|
|
var href = location.hash.replace(/^#\//, '');
|
|
|
|
|
|
// 如果href为空或未定义,则渲染主页内容
|
|
|
if (href === null || href === undefined || href === '') {
|
|
|
miniPage.renderHome(options);
|
|
|
} else {
|
|
|
// 否则根据href渲染相应的页面内容
|
|
|
miniPage.renderPageContent(href, options);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 构建页面标题数组
|
|
|
* @param href
|
|
|
* @param menuList
|
|
|
*/
|
|
|
/**
|
|
|
* 构建页面标题数组
|
|
|
* @param {string} href - 目标链接
|
|
|
* @param {Array} menuList - 菜单列表
|
|
|
* @returns {Array} - 包含页面标题的数组
|
|
|
*/
|
|
|
buildPageTitleArray: function (href, menuList) {
|
|
|
// 初始化空数组,用于存储最终结果
|
|
|
var array = [],
|
|
|
newArray = [];
|
|
|
// 遍历菜单列表中的每一项
|
|
|
for (key in menuList) {
|
|
|
// 获取当前项
|
|
|
var item = menuList[key];
|
|
|
// 如果当前项的链接与目标链接匹配
|
|
|
if (item.href === href) {
|
|
|
// 将当前项的标题添加到结果数组中
|
|
|
array.push(item.title);
|
|
|
// 终止循环
|
|
|
break;
|
|
|
}
|
|
|
// 如果当前项有子菜单
|
|
|
if (item.child) {
|
|
|
// 递归调用自身,处理子菜单
|
|
|
newArray = miniPage.buildPageTitleArray(href, item.child);
|
|
|
// 如果子菜单中有匹配项
|
|
|
if (newArray.length > 0) {
|
|
|
// 将当前项的标题添加到子菜单结果数组的开头
|
|
|
newArray.unshift(item.title);
|
|
|
// 合并当前项的标题和子菜单结果数组到最终结果数组中
|
|
|
array = array.concat(newArray);
|
|
|
// 终止循环
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 返回最终结果数组
|
|
|
return array;
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 获取指定链接内容
|
|
|
* @param href
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
getHrefContent: function (href) {
|
|
|
// 初始化一个空字符串变量,用于存储获取的内容
|
|
|
var content = '';
|
|
|
// 获取当前时间的时间戳,用于防止缓存
|
|
|
var v = new Date().getTime();
|
|
|
// 发送AJAX请求以获取指定URL的内容
|
|
|
$.ajax({
|
|
|
// 根据URL是否包含查询参数来决定如何添加时间戳
|
|
|
url: href.indexOf("?") > -1 ? href + '&v=' + v : href + '?v=' + v,
|
|
|
// 设置请求类型为GET
|
|
|
type: 'get',
|
|
|
// 期望的响应数据类型为HTML
|
|
|
dataType: 'html',
|
|
|
// 设置请求为同步,等待响应后再继续执行后续代码
|
|
|
async: false,
|
|
|
// 请求成功时的回调函数
|
|
|
success: function (data) {
|
|
|
// 将获取到的数据赋值给content变量
|
|
|
content = data;
|
|
|
},
|
|
|
// 请求失败时的回调函数
|
|
|
error: function (xhr, textstatus, thrown) {
|
|
|
// 显示错误信息并返回提示消息
|
|
|
return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
|
|
|
}
|
|
|
});
|
|
|
// 返回获取到的内容
|
|
|
return content;
|
|
|
},
|
|
|
/**
|
|
|
* 获取弹出层的宽高
|
|
|
* @returns {jQuery[]}
|
|
|
*/
|
|
|
getOpenWidthHeight: function () {
|
|
|
// 获取页面内容区域的宽度
|
|
|
var clienWidth = $(".layuimini-content-page").width();
|
|
|
// 获取页面内容区域的高度
|
|
|
var clientHeight = $(".layuimini-content-page").height();
|
|
|
// 获取页面内容区域相对于文档的左偏移量
|
|
|
var offsetLeft = $(".layuimini-content-page").offset().left;
|
|
|
// 获取页面内容区域相对于文档的上偏移量
|
|
|
var offsetTop = $(".layuimini-content-page").offset().top;
|
|
|
// 返回包含宽度、高度、上偏移量和左偏移量的数组
|
|
|
return [clienWidth, clientHeight, offsetTop, offsetLeft];
|
|
|
},
|
|
|
/**
|
|
|
* 单模块切换
|
|
|
* @param tabId
|
|
|
*/
|
|
|
listenSwitchSingleModule: function (tabId) {
|
|
|
// 遍历所有具有layuimini-href属性的元素
|
|
|
$("[layuimini-href]").each(function () {
|
|
|
// 如果元素的layuimini-href属性值等于传入的tabId
|
|
|
if ($(this).attr("layuimini-href") === tabId) {
|
|
|
// 定义一个函数用于自动展开菜单栏
|
|
|
var addMenuClass = function ($element, type) {
|
|
|
if (type === 1) {
|
|
|
// 为当前元素添加layui-this类
|
|
|
$element.addClass('layui-this');
|
|
|
// 如果当前元素同时拥有layui-nav-item和layui-this类
|
|
|
if ($element.hasClass('layui-nav-item') && $element.hasClass('layui-this')) {
|
|
|
// 将所有菜单项的类名设置为layui-nav-item
|
|
|
$(".layuimini-header-menu li").attr('class', 'layui-nav-item');
|
|
|
} else {
|
|
|
// 递归调用addMenuClass,处理父级元素
|
|
|
addMenuClass($element.parent().parent(), 2);
|
|
|
}
|
|
|
} else {
|
|
|
// 为当前元素添加layui-nav-itemed类
|
|
|
$element.addClass('layui-nav-itemed');
|
|
|
// 如果当前元素同时拥有layui-nav-item和layui-nav-itemed类
|
|
|
if ($element.hasClass('layui-nav-item') && $element.hasClass('layui-nav-itemed')) {
|
|
|
// 将所有菜单项的类名设置为layui-nav-item
|
|
|
$(".layuimini-header-menu li").attr('class', 'layui-nav-item');
|
|
|
} else {
|
|
|
// 递归调用addMenuClass,处理父级元素
|
|
|
addMenuClass($element.parent().parent(), 2);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
// 调用addMenuClass函数,展开当前元素的菜单栏
|
|
|
addMenuClass($(this).parent(), 1);
|
|
|
// 终止each循环
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 多模块切换
|
|
|
* @param tabId
|
|
|
*/
|
|
|
listenSwitchMultiModule: function (tabId) {
|
|
|
// 遍历所有具有 layuimini-href 属性的元素
|
|
|
$("[layuimini-href]").each(function () {
|
|
|
// 如果元素的 layuimini-href 属性值等于传入的 tabId
|
|
|
if ($(this).attr("layuimini-href") === tabId) {
|
|
|
|
|
|
// 定义一个函数,用于自动展开菜单栏
|
|
|
var addMenuClass = function ($element, type) {
|
|
|
if (type === 1) {
|
|
|
// 为元素添加 'layui-this' 类
|
|
|
$element.addClass('layui-this');
|
|
|
// 如果元素同时具有 'layui-nav-item' 和 'layui-this' 类
|
|
|
if ($element.hasClass('layui-nav-item') && $element.hasClass('layui-this')) {
|
|
|
// 获取父元素的 id
|
|
|
var moduleId = $element.parent().attr('id');
|
|
|
// 设置头部菜单项的类名为 'layui-nav-item'
|
|
|
$(".layuimini-header-menu li").attr('class', 'layui-nav-item');
|
|
|
// 为对应的头部菜单项添加 'layui-this' 类
|
|
|
$("#" + moduleId + "HeaderId").addClass("layui-this");
|
|
|
// 隐藏左侧菜单树
|
|
|
$(".layuimini-menu-left .layui-nav.layui-nav-tree").attr('class', 'layui-nav layui-nav-tree layui-hide');
|
|
|
// 显示当前模块的菜单树并添加 'layui-this' 类
|
|
|
$("#" + moduleId).attr('class', 'layui-nav layui-nav-tree layui-this');
|
|
|
} else {
|
|
|
// 递归调用 addMenuClass 函数,处理父级元素
|
|
|
addMenuClass($element.parent().parent(), 2);
|
|
|
}
|
|
|
} else {
|
|
|
// 为元素添加 'layui-nav-itemed' 类
|
|
|
$element.addClass('layui-nav-itemed');
|
|
|
// 如果元素同时具有 'layui-nav-item' 和 'layui-nav-itemed' 类
|
|
|
if ($element.hasClass('layui-nav-item') && $element.hasClass('layui-nav-itemed')) {
|
|
|
// 获取父元素的 id
|
|
|
var moduleId = $element.parent().attr('id');
|
|
|
// 设置头部菜单项的类名为 'layui-nav-item'
|
|
|
$(".layuimini-header-menu li").attr('class', 'layui-nav-item');
|
|
|
// 为对应的头部菜单项添加 'layui-this' 类
|
|
|
$("#" + moduleId + "HeaderId").addClass("layui-this");
|
|
|
// 隐藏左侧菜单树
|
|
|
$(".layuimini-menu-left .layui-nav.layui-nav-tree").attr('class', 'layui-nav layui-nav-tree layui-hide');
|
|
|
// 显示当前模块的菜单树并添加 'layui-this' 类
|
|
|
$("#" + moduleId).attr('class', 'layui-nav layui-nav-tree layui-this');
|
|
|
} else {
|
|
|
// 递归调用 addMenuClass 函数,处理父级元素
|
|
|
addMenuClass($element.parent().parent(), 2);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
// 调用 addMenuClass 函数,处理当前元素的父级元素
|
|
|
addMenuClass($(this).parent(), 1);
|
|
|
return false; // 终止 each 循环
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
/**
|
|
|
* 修改hash地址定位
|
|
|
* @param href
|
|
|
*/
|
|
|
hashChange: function (href) {
|
|
|
// 将传入的 href 参数拼接到当前 URL 的哈希部分,并更新浏览器地址栏
|
|
|
window.location.hash = "/" + href;
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 修改hash地址为主页
|
|
|
*/
|
|
|
hashHome: function () {
|
|
|
// 将当前窗口的哈希值设置为根路径 "/"
|
|
|
window.location.hash = "/";
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 监听
|
|
|
* @param options
|
|
|
*/
|
|
|
/**
|
|
|
* 监听函数,用于处理传入的选项参数。
|
|
|
* @param {Object} options - 配置选项对象。
|
|
|
*/
|
|
|
listen: function (options) {
|
|
|
// 方法体内容待补充
|
|
|
/**
|
|
|
* 打开新窗口
|
|
|
*/
|
|
|
$('body').on('click', '[layuimini-href]', function () {
|
|
|
// 显示加载动画,设置2秒后自动关闭
|
|
|
var loading = layer.load(0, {shade: false, time: 2 * 1000});
|
|
|
|
|
|
// 获取当前点击元素的layuimini-href属性值和target属性值
|
|
|
var href = $(this).attr('layuimini-href'),
|
|
|
target = $(this).attr('target');
|
|
|
|
|
|
// 如果href为空,则直接返回
|
|
|
if(!href) return;
|
|
|
|
|
|
// 保存当前点击的元素引用
|
|
|
var me = this;
|
|
|
|
|
|
// 查找左侧菜单中与当前点击元素href相同的元素
|
|
|
var el = $("[layuimini-href='"+href+"']",".layuimini-menu-left");
|
|
|
|
|
|
// 关闭之前打开的提示框
|
|
|
layer.close(window.openTips);
|
|
|
|
|
|
// 如果找到匹配的元素
|
|
|
if(el.length){
|
|
|
// 移除所有layui-this类名
|
|
|
$(el).closest(".layui-nav-tree").find(".layui-this").removeClass("layui-this");
|
|
|
// 给父元素添加layui-this类名
|
|
|
$(el).parent().addClass("layui-this");
|
|
|
}
|
|
|
|
|
|
// 如果target属性值为_blank,则在新窗口中打开链接
|
|
|
if (target === '_blank') {
|
|
|
// 关闭加载动画
|
|
|
layer.close(loading);
|
|
|
// 在新窗口中打开链接
|
|
|
window.open(href, "_blank");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 调用miniPage对象的hashChange方法,改变页面URL的哈希值
|
|
|
miniPage.hashChange(href);
|
|
|
|
|
|
// 设置左侧菜单的layuimini-page-add属性为yes
|
|
|
$('.layuimini-menu-left').attr('layuimini-page-add', 'yes');
|
|
|
|
|
|
// 关闭加载动画
|
|
|
layer.close(loading);
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
* 在子页面上打开新窗口
|
|
|
*/
|
|
|
$('body').on('click', '[layuimini-content-href]', function () {
|
|
|
// 显示加载动画,设置2秒后自动关闭
|
|
|
var loading = parent.layer.load(0, {shade: false, time: 2 * 1000});
|
|
|
|
|
|
// 获取点击元素的href、title和target属性值
|
|
|
var href = $(this).attr('layuimini-content-href'),
|
|
|
title = $(this).attr('data-title'),
|
|
|
target = $(this).attr('target');
|
|
|
|
|
|
// 如果href为空,则直接返回
|
|
|
if(!href) return;
|
|
|
|
|
|
// 保存当前点击的元素引用
|
|
|
var me = this;
|
|
|
|
|
|
// 查找左侧菜单中匹配的href元素
|
|
|
var el = $("[layuimini-href='"+href+"']",".layuimini-menu-left");
|
|
|
|
|
|
// 关闭之前打开的提示框
|
|
|
layer.close(window.openTips);
|
|
|
|
|
|
// 如果找到匹配的元素
|
|
|
if(el.length){
|
|
|
// 移除所有同级元素的'layui-this'类
|
|
|
$(el).closest(".layui-nav-tree").find(".layui-this").removeClass("layui-this");
|
|
|
// 给父元素添加'layui-this'类
|
|
|
$(el).parent().addClass("layui-this");
|
|
|
}
|
|
|
|
|
|
// 如果target属性为'_blank',则在新窗口中打开链接
|
|
|
if (target === '_blank') {
|
|
|
// 关闭加载动画
|
|
|
parent.layer.close(loading);
|
|
|
// 在新窗口中打开链接
|
|
|
window.open(href, "_blank");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 将页面标题存储到sessionStorage中
|
|
|
sessionStorage.setItem('layuimini_page_title', title);
|
|
|
|
|
|
// 调用hashChange方法更新页面内容
|
|
|
miniPage.hashChange(href);
|
|
|
|
|
|
// 关闭加载动画
|
|
|
parent.layer.close(loading);
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
* 返回主页
|
|
|
*/
|
|
|
$('body').on('click', '.layuimini-back-home', function () {
|
|
|
// 当点击事件触发时,调用 miniPage 对象的 hashHome 方法
|
|
|
miniPage.hashHome();
|
|
|
});
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 监听hash变化
|
|
|
* @returns {boolean}
|
|
|
*/
|
|
|
listenHash: function (options) {
|
|
|
// 初始化homeInfo选项,如果未定义则设置为空对象
|
|
|
options.homeInfo = options.homeInfo || {};
|
|
|
// 初始化multiModule选项,如果未定义则设置为false
|
|
|
options.multiModule = options.multiModule || false;
|
|
|
// 初始化listenSwichCallback选项,如果未定义则设置为空函数
|
|
|
options.listenSwichCallback = options.listenSwichCallback || function () {};
|
|
|
|
|
|
// 监听hash变化事件
|
|
|
window.onhashchange = function () {
|
|
|
// 获取当前hash值并去掉前缀'#/'
|
|
|
var href = location.hash.replace(/^#\//, '');
|
|
|
|
|
|
// 如果listenSwichCallback是函数,则调用它
|
|
|
if (typeof options.listenSwichCallback === 'function') {
|
|
|
options.listenSwichCallback();
|
|
|
}
|
|
|
|
|
|
// 根据hash值判断要渲染的页面
|
|
|
if (href === null || href === undefined || href === '') {
|
|
|
// 如果hash值为空,移除layui-this类并渲染首页
|
|
|
$("[layuimini-href]").parent().removeClass('layui-this');
|
|
|
miniPage.renderHome(options);
|
|
|
} else {
|
|
|
// 否则渲染指定页面
|
|
|
miniPage.renderPage(href, options);
|
|
|
}
|
|
|
|
|
|
// 检查菜单是否添加了新页面
|
|
|
if ($('.layuimini-menu-left').attr('layuimini-page-add') === 'yes') {
|
|
|
// 如果是,则重置属性为'no'
|
|
|
$('.layuimini-menu-left').attr('layuimini-page-add', 'no');
|
|
|
} else {
|
|
|
// 如果不是,重新定位菜单焦点
|
|
|
$("[layuimini-href]").parent().removeClass('layui-this');
|
|
|
if (options.multiModule) {
|
|
|
// 多模块情况下处理菜单切换
|
|
|
miniPage.listenSwitchMultiModule(href);
|
|
|
} else {
|
|
|
// 单模块情况下处理菜单切换
|
|
|
miniPage.listenSwitchSingleModule(href);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
}, |