| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- /** EasyWeb spa v3.1.6 date:2020-03-11 License By http://easyweb.vip */
- layui.define(['layer', 'element', 'setter', 'layRouter', 'admin'], function (exports) {
- var $ = layui.jquery;
- var layer = layui.layer;
- var element = layui.element;
- var setter = layui.setter;
- var layRouter = layui.layRouter;
- var admin = layui.admin;
- var headerDOM = '.layui-layout-admin>.layui-header';
- var sideDOM = '.layui-layout-admin>.layui-side>.layui-side-scroll';
- var bodyDOM = '.layui-layout-admin>.layui-body';
- var tabDOM = bodyDOM + '>.layui-tab';
- var titleDOM = bodyDOM + '>.layui-body-header';
- var tabFilter = 'admin-pagetabs';
- var navFilter = 'admin-side-nav';
- var index = {mTabList: []};
- /* 注册路由 */
- index.regRouter = function (menus) {
- $.each(menus, function (i, data) {
- if (data.url && data.url.indexOf('#') === 0) {
- layRouter.reg(data.url, function (info) {
- info.name = data.name;
- info.iframe = data.iframe;
- index.changeView(info);
- });
- }
- if (data.subMenus) index.regRouter(data.subMenus);
- });
- };
- /* 处理路由改变 */
- index.changeView = function (info) {
- var hashPath = index.getHashPath(info);
- var contentDom = bodyDOM + '>div[lay-id]';
- if (setter.pageTabs) { // 多标签模式
- var flag; // 选项卡是否已经添加
- $(tabDOM + '>.layui-tab-title>li').each(function (i) {
- if ($(this).attr('lay-id') === hashPath) {
- flag = true;
- return false;
- }
- });
- if (!flag) { // 添加选项卡
- if ((index.mTabList.length + 1) >= setter.maxTabNum) {
- layer.msg('最多打开' + setter.maxTabNum + '个选项卡', {icon: 2, anim: 6});
- history.back();
- return;
- }
- element.tabAdd(tabFilter, {
- id: hashPath,
- title: '<span class="title">' + (info.name || '') + '</span>',
- content: '<div lay-id="' + hashPath + '" lay-url="' + info.href + '"></div>'
- });
- if (hashPath !== layRouter.index) index.mTabList.push(info); // 记录tab
- if (setter.cacheTab) admin.putTempData('indexTabs', index.mTabList); // 缓存tab
- }
- contentDom = tabDOM + '>.layui-tab-content>.layui-tab-item>div[lay-id="' + hashPath + '"]';
- var oldHref = $(contentDom).attr('lay-url');
- if (info.href !== oldHref) { // 同一个hash参数不同
- // 页面卸载事件回调
- layui.event.call(this, 'admin', 'destroy(' + hashPath + ')');
- //
- $(contentDom).attr('lay-url', info.href);
- flag = false; // 同一个hash参数不同刷新内容
- for (var i = 0; i < index.mTabList.length; i++) {
- if (index.mTabList[i].href === oldHref) index.mTabList[i] = info;
- }
- if (setter.cacheTab) admin.putTempData('indexTabs', index.mTabList); // 缓存tab
- }
- if (!flag || info.refresh) {
- if (info.refresh) layui.event.call(this, 'admin', 'destroy(' + hashPath + ')'); // 页面卸载事件回调
- index.renderView(info, contentDom); // 渲染内容页面
- }
- if (!info.noChange) element.tabChange(tabFilter, hashPath); // 切换到此tab
- } else { // 单标签模式
- admin.activeNav(info.href);
- if ($(contentDom).length <= 0) { // 第一次补充标题栏
- var contentHtml = '<div class="layui-body-header">';
- contentHtml += ' <span class="layui-body-header-title"></span>';
- contentHtml += ' <span class="layui-breadcrumb pull-right" lay-filter="admin-body-breadcrumb" style="visibility: visible;"></span>';
- contentHtml += ' </div>';
- contentHtml += ' <div lay-id="' + hashPath + '" lay-url="' + info.href + '"></div>';
- $(bodyDOM).html(contentHtml);
- } else {
- // 页面卸载事件回调
- layui.event.call(this, 'admin', 'destroy(' + $(contentDom).attr('lay-id') + ')');
- $(contentDom).attr('lay-id', hashPath);
- $(contentDom).attr('lay-url', info.href);
- }
- $('[lay-filter="admin-body-breadcrumb"]').html(index.getBreadcrumbHtml(hashPath));
- var title = info.name;
- if (hashPath === layRouter.index) {
- title = $(info.name).text() || $(sideDOM + ' [href="#/' + layRouter.index + '"]').text() || '主页';
- }
- index.setTabTitle(title);
- index.mTabList.splice(0, index.mTabList.length);
- if (layRouter.index !== hashPath) index.mTabList.push(info); // 记录tab
- if (setter.cacheTab) admin.putTempData('indexTabs', index.mTabList); // 缓存tab
- index.renderView(info, contentDom); // 渲染内容页面
- }
- if (admin.getPageWidth() <= 768) admin.flexible(true); // 移动端自动收起侧导航
- $('.layui-table-tips-c').trigger('click'); // 切换tab关闭表格内浮窗
- };
- /* 渲染组件 */
- index.renderView = function (info, contentDom, loadingDOM) {
- var $contentDom = $(contentDom);
- if (!loadingDOM) loadingDOM = $contentDom.parent();
- if (!info.iframe) {
- admin.showLoading({elem: loadingDOM, size: ''});
- admin.ajax({
- url: setter.viewPath + '/' + info.path.join('/') + setter.viewSuffix,
- data: info.search,
- dataType: 'html',
- success: function (res) {
- admin.removeLoading(loadingDOM);
- if (typeof res !== 'string') res = JSON.stringify(res);
- if (res.indexOf('<tpl') === 0) {
- res = admin.util.tpl(res, info, setter.tplOpen, setter.tplClose);
- }
- $contentDom.html(res);
- admin.renderPerm(); // 移除没有权限的元素
- }
- });
- } else {
- $contentDom.html('<div class="admin-iframe" style="-webkit-overflow-scrolling: touch;"><iframe src="' + info.iframe + '" class="admin-iframe" frameborder="0"></iframe></div>');
- }
- };
- /* 加载主页 */
- index.loadHome = function (data) {
- var cacheTabs = admin.getTempData('indexTabs'); // 获取缓存tab
- index.regRouter([data]);
- if (setter.pageTabs) {
- var info = layRouter.routerInfo(data.url);
- layRouter.index = index.getHashPath(info);
- info.name = data.name;
- info.iframe = data.iframe;
- info.noChange = true;
- index.changeView(info);
- // 恢复缓存tab
- if (data.loadSetting !== false && setter.cacheTab && cacheTabs) {
- for (var i = 0; i < cacheTabs.length; i++) {
- cacheTabs[i].noChange = true;
- index.changeView(cacheTabs[i]);
- }
- }
- }
- // 初始化路由
- layRouter.init({index: data.url, notFound: setter.routerNotFound});
- };
- /* 打开tab */
- index.openNewTab = function (param) {
- index.regRouter([param]);
- layRouter.go(param.url);
- };
- /* 关闭tab */
- index.closeTab = function (hash) {
- element.tabDelete(tabFilter, index.getHashPath(hash));
- };
- /* 获取hash路径 */
- index.getHashPath = function (hash) {
- if (!hash || typeof hash === 'string') {
- return layRouter.routerInfo(hash).path.join('/');
- }
- return hash.path.join('/');
- };
- /* 跳转tab */
- index.go = function (hash) {
- layRouter.go(hash);
- };
- /* 修改Tab标题 */
- index.setTabTitle = function (title, hash) {
- if (setter.pageTabs) {
- if (!title) title = '';
- if (!hash) hash = index.getHashPath();
- if (hash) $(tabDOM + '>.layui-tab-title>li[lay-id="' + hash + '"] .title').html(title);
- } else {
- if (title) {
- $(titleDOM + '>.layui-body-header-title').html(title);
- $(titleDOM).addClass('show');
- $(headerDOM).css('box-shadow', '0 1px 0 0 rgba(0, 0, 0, .03)');
- } else {
- $(titleDOM).removeClass('show');
- admin.util.removeStyle(headerDOM, 'box-shadow');
- }
- }
- };
- /* 自定义Tab标题 */
- index.setTabTitleHtml = function (html) {
- if (setter.pageTabs) return;
- if (html) {
- $(titleDOM).addClass('show');
- $(titleDOM).html(html);
- } else {
- $(titleDOM).removeClass('show');
- }
- };
- /* 获取面包屑 */
- index.getBreadcrumb = function (hash) {
- if (!hash) hash = $(bodyDOM + '>div[lay-id]').attr('lay-id');
- var breadcrumb = [];
- var $href = $(sideDOM).find('[href="#/' + hash + '"]');
- breadcrumb.push($href.text().replace(/(^\s*)|(\s*$)/g, ''));
- while (true) {
- $href = $href.parent('dd').parent('dl').prev('a');
- if ($href.length === 0) break;
- breadcrumb.unshift($href.text().replace(/(^\s*)|(\s*$)/g, ''));
- }
- return breadcrumb;
- };
- /* 获取面包屑结构 */
- index.getBreadcrumbHtml = function (hash) {
- var breadcrumb = index.getBreadcrumb(hash);
- var htmlStr = (hash === layRouter.index ? '' : ('<a href="#/' + layRouter.index + '">首页</a>'));
- for (var i = 0; i < breadcrumb.length - 1; i++) {
- if (htmlStr) htmlStr += '<span lay-separator="">/</span>';
- htmlStr += '<a><cite>' + breadcrumb[i] + '</cite></a>';
- }
- return htmlStr;
- };
- /* 渲染侧边栏 */
- index.renderSide = function (data, tpl, callback, tplOpen, tplClose) {
- if (typeof tpl === 'function') {
- tplClose = tplOpen;
- tplOpen = callback;
- callback = tpl;
- tpl = undefined;
- }
- function removeHide(menus) {
- for (var i = menus.length - 1; i >= 0; i--) {
- if (menus[i].subMenus) {
- removeHide(menus[i].subMenus);
- }
- if (menus[i].type === 1) {
- menus.splice(i, 1);
- } else {
- if (!menus[i].target) menus[i].target = '_self';
- if (!menus[i].url) menus[i].url = 'javascript:;';
- }
- }
- }
- removeHide(data); // 处理数据
- var html = admin.util.tpl(tpl || $('#sideNav').html() || '', data, tplOpen || setter.tplOpen, tplClose || setter.tplClose);
- if (callback) {
- callback(html, {
- data: data,
- side: sideDOM,
- render: function () {
- element.render('nav', 'admin-side-nav');
- }
- });
- } else {
- $(sideDOM + '>.layui-nav').html(html);
- element.render('nav', 'admin-side-nav');
- }
- };
- /* 移动设备遮罩层 */
- var siteShadeDom = '.layui-layout-admin .site-mobile-shade';
- if ($(siteShadeDom).length === 0) {
- $('.layui-layout-admin').append('<div class="site-mobile-shade"></div>');
- }
- $(siteShadeDom).click(function () {
- admin.flexible(true);
- });
- /* 补充tab的dom */
- if (setter.pageTabs && $(tabDOM).length === 0) {
- var tabDomHtml = '<div class="layui-tab" lay-allowClose="true" lay-filter="' + tabFilter + '">';
- tabDomHtml += ' <ul class="layui-tab-title"></ul>';
- tabDomHtml += ' <div class="layui-tab-content"></div>';
- tabDomHtml += ' </div>';
- tabDomHtml += ' <div class="layui-icon admin-tabs-control layui-icon-prev" ew-event="leftPage"></div>';
- tabDomHtml += ' <div class="layui-icon admin-tabs-control layui-icon-next" ew-event="rightPage"></div>';
- tabDomHtml += ' <div class="layui-icon admin-tabs-control layui-icon-down">';
- tabDomHtml += ' <ul class="layui-nav" lay-filter="admin-pagetabs-nav">';
- tabDomHtml += ' <li class="layui-nav-item" lay-unselect>';
- tabDomHtml += ' <dl class="layui-nav-child layui-anim-fadein">';
- tabDomHtml += ' <dd ew-event="closeThisTabs" lay-unselect><a>关闭当前标签页</a></dd>';
- tabDomHtml += ' <dd ew-event="closeOtherTabs" lay-unselect><a>关闭其它标签页</a></dd>';
- tabDomHtml += ' <dd ew-event="closeAllTabs" lay-unselect><a>关闭全部标签页</a></dd>';
- tabDomHtml += ' </dl>';
- tabDomHtml += ' </li>';
- tabDomHtml += ' </ul>';
- tabDomHtml += ' </div>';
- $(bodyDOM).html(tabDomHtml);
- element.render('nav', 'admin-pagetabs-nav');
- }
- /* tab切换监听 */
- element.on('tab(' + tabFilter + ')', function () {
- var hash = $(this).attr('lay-id');
- var url = $(tabDOM + '>.layui-tab-content>.layui-tab-item>div[lay-id="' + hash + '"]').attr('lay-url');
- admin.activeNav(url); // 选中侧边栏
- layRouter.go(url); // 改变hash地址
- admin.resizeTable(0);
- admin.rollPage('auto');
- layui.event.call(this, 'admin', 'show(' + hash + ')');
- });
- /* tab删除监听 */
- element.on('tabDelete(' + tabFilter + ')', function (data) {
- if (data.index > 0 && data.index <= index.mTabList.length) {
- // 页面卸载事件回调
- var hashPath = index.getHashPath(index.mTabList[data.index - 1].href);
- layui.event.call(this, 'admin', 'destroy(' + hashPath + ')');
- //
- index.mTabList.splice(data.index - 1, 1);
- if (setter.cacheTab) admin.putTempData('indexTabs', index.mTabList);
- }
- // 解决偶尔出现关闭后没有选中任何Tab的bug
- if ($(tabDOM + '>.layui-tab-title>li.layui-this').length <= 0) {
- $(tabDOM + '>.layui-tab-title>li:last').trigger('click');
- }
- });
- /* 多系统切换事件 */
- $(document).off('click.navMore').on('click.navMore', '[nav-bind]', function () {
- var navId = $(this).attr('nav-bind');
- $('ul[lay-filter="' + navFilter + '"]').addClass('layui-hide');
- $('ul[nav-id="' + navId + '"]').removeClass('layui-hide');
- $(headerDOM + '>.layui-nav .layui-nav-item').removeClass('layui-this');
- $(this).parent('.layui-nav-item').addClass('layui-this');
- if (admin.getPageWidth() <= 768) admin.flexible(false); // 展开侧边栏
- });
- /* 开启Tab右键菜单 */
- if (setter.openTabCtxMenu && setter.pageTabs) {
- layui.use('contextMenu', function () {
- var contextMenu = layui.contextMenu;
- if (!contextMenu) return; // contextMenu模块可能会不存在
- $(tabDOM + '>.layui-tab-title').off('contextmenu.tab').on('contextmenu.tab', 'li', function (e) {
- var layId = $(this).attr('lay-id');
- contextMenu.show([{
- icon: 'layui-icon layui-icon-refresh',
- name: '刷新当前',
- click: function () {
- element.tabChange(tabFilter, layId);
- var url = $(tabDOM + '>.layui-tab-content>.layui-tab-item>div[lay-id="' + layId + '"]').attr('lay-url');
- admin.refresh(url);
- }
- }, {
- icon: 'layui-icon layui-icon-close-fill ctx-ic-lg',
- name: '关闭当前',
- click: function () {
- admin.closeThisTabs(layId);
- }
- }, {
- icon: 'layui-icon layui-icon-unlink',
- name: '关闭其他',
- click: function () {
- admin.closeOtherTabs(layId);
- }
- }, {
- icon: 'layui-icon layui-icon-close ctx-ic-lg',
- name: '关闭全部',
- click: function () {
- admin.closeAllTabs();
- }
- }], e.clientX, e.clientY);
- return false;
- });
- });
- }
- exports('index', index);
- });
|