layuimini.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /**
  2. * date:2019/06/10
  3. * author:Mr.Chung
  4. * description:layuimini 框架扩展
  5. */
  6. layui.define(["element", "jquery"], function (exports) {
  7. var element = layui.element,
  8. $ = layui.$,
  9. layer = layui.layer;
  10. // 判断是否在web容器中打开
  11. if (!/http(s*):\/\//.test(location.href)) {
  12. return layer.alert("请先将项目部署至web容器(Apache/Tomcat/Nginx/IIS/等),否则部分数据将无法显示");
  13. }
  14. layuimini = new function () {
  15. /**
  16. * 系统配置
  17. * @param name
  18. * @returns {{BgColorDefault: number, urlSuffixDefault: boolean}|*}
  19. */
  20. this.config = function (name) {
  21. var config = {
  22. urlHashLocation: true, // URL地址hash定位
  23. urlSuffixDefault: true, // URL后缀
  24. BgColorDefault: 1, // 默认皮肤(0开始)
  25. checkUrlDefault: true, // 是否判断URL有效
  26. };
  27. if (name == undefined) {
  28. return config;
  29. } else {
  30. return config[name];
  31. }
  32. };
  33. /**
  34. * 初始化
  35. * @param url
  36. */
  37. this.init = function (url) {
  38. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  39. layuimini.initBgColor();
  40. layuimini.initDevice();
  41. /*$.getJSON(url, function (data, status) {
  42. if (data == null) {
  43. layuimini.msg_error('暂无菜单信息');
  44. } else {
  45. layuimini.initHome(data.homeInfo);
  46. layuimini.initLogo(data.logoInfo);
  47. layuimini.initClear(data.clearInfo);
  48. layuimini.initMenu(data.menuInfo);
  49. layuimini.initTab();
  50. }
  51. }).fail(function () {
  52. layuimini.msg_error('菜单接口有误');
  53. });*/
  54. layer.close(loading);
  55. };
  56. /**
  57. * 初始化设备端
  58. */
  59. this.initDevice = function () {
  60. if (layuimini.checkMobile()) {
  61. $('.layuimini-tool i').attr('data-side-fold', 0);
  62. $('.layuimini-tool i').attr('class', 'fa fa-indent');
  63. $('.layui-layout-body').attr('class', 'layui-layout-body layuimini-mini');
  64. }
  65. };
  66. /**
  67. * 初始化首页信息
  68. * @param data
  69. */
  70. this.initHome = function (data) {
  71. sessionStorage.setItem('layuiminiHomeHref', data.href);
  72. $('#layuiminiHomeTabId').html('<i class="' + data.icon + '"></i> <span>' + data.title + '</span>');
  73. $('#layuiminiHomeTabId').attr('lay-id', data.href);
  74. $('#layuiminiHomeTabIframe').html('<iframe width="100%" height="100%" frameborder="0" src="' + data.href + '"></iframe>');
  75. };
  76. /**
  77. * 初始化logo信息
  78. * @param data
  79. */
  80. this.initLogo = function (data) {
  81. var html = '<a href="' + data.href + '">\n' +
  82. '<img src="' + data.image + '" alt="logo">\n' +
  83. '<h1>' + data.title + '</h1>\n' +
  84. '</a>';
  85. $('.layui-layout-admin .layui-logo').html(html);
  86. };
  87. /**
  88. * 初始化清理缓存
  89. * @param data
  90. */
  91. this.initClear = function (data) {
  92. $('.layuimini-clear').attr('data-href', data.clearUrl);
  93. };
  94. /**
  95. * 初始化背景色
  96. */
  97. this.initBgColor = function () {
  98. var bgcolorId = sessionStorage.getItem('layuiminiBgcolorId');
  99. if (bgcolorId == null || bgcolorId == undefined || bgcolorId == '') {
  100. bgcolorId = layuimini.config('BgColorDefault');
  101. }
  102. var bgcolorData = layuimini.bgColorConfig(bgcolorId);
  103. var styleHtml = '.layui-layout-admin .layui-header{background-color:' + bgcolorData.headerRight + '!important;}\n' +
  104. '.layui-header>ul>.layui-nav-item.layui-this,.layuimini-tool i:hover{background-color:' + bgcolorData.headerRightThis + '!important;}\n' +
  105. '.layui-layout-admin .layui-logo {background-color:' + bgcolorData.headerLogo + '!important;}\n' +
  106. '.layui-side.layui-bg-black,.layui-side.layui-bg-black>.layui-left-menu>ul {background-color:' + bgcolorData.menuLeft + '!important;}\n' +
  107. '.layui-left-menu .layui-nav .layui-nav-child a:hover:not(.layui-this) {background-color:' + bgcolorData.menuLeftHover + ';}\n' +
  108. '.layui-layout-admin .layui-nav-tree .layui-this, .layui-layout-admin .layui-nav-tree .layui-this>a, .layui-layout-admin .layui-nav-tree .layui-nav-child dd.layui-this, .layui-layout-admin .layui-nav-tree .layui-nav-child dd.layui-this a {\n' +
  109. ' background-color: ' + bgcolorData.menuLeftThis + ' !important;\n' +
  110. '}';
  111. $('#layuimini-bg-color').html(styleHtml);
  112. };
  113. /**
  114. * 初始化菜单栏
  115. * @param data
  116. */
  117. this.initMenu = function (data) {
  118. var headerMenuHtml = '',
  119. headerMobileMenuHtml = '',
  120. leftMenuHtml = '',
  121. headerMenuCheckDefault = 'layui-this',
  122. leftMenuCheckDefault = 'layui-this';
  123. window.menuParameId = 1;
  124. $.each(data, function (key, val) {
  125. headerMenuHtml += '<li class="layui-nav-item ' + headerMenuCheckDefault + '" id="' + key + 'HeaderId" data-menu="' + key + '"> <a href="javascript:;"><i class="' + val.icon + '"></i> ' + val.title + '</a> </li>\n';
  126. headerMobileMenuHtml += '<dd><a href="javascript:;" id="' + key + 'HeaderId" data-menu="' + key + '"><i class="' + val.icon + '"></i> ' + val.title + '</a></dd>\n';
  127. leftMenuHtml += '<ul class="layui-nav layui-nav-tree layui-left-nav-tree ' + leftMenuCheckDefault + '" id="' + key + '">\n';
  128. var menuList = val.child;
  129. $.each(menuList, function (index, menu) {
  130. leftMenuHtml += '<li class="layui-nav-item">\n';
  131. if (menu.child != undefined && menu.child != []) {
  132. leftMenuHtml += '<a href="javascript:;" class="layui-menu-tips" ><i class="' + menu.icon + '"></i><span class="layui-left-nav"> ' + menu.title + '</span> </a>';
  133. var buildChildHtml = function (html, child, menuParameId) {
  134. html += '<dl class="layui-nav-child">\n';
  135. $.each(child, function (childIndex, childMenu) {
  136. html += '<dd>\n';
  137. if (childMenu.child != undefined && childMenu.child != []) {
  138. html += '<a href="javascript:;" class="layui-menu-tips" ><i class="' + childMenu.icon + '"></i><span class="layui-left-nav"> ' + childMenu.title + '</span></a>';
  139. html = buildChildHtml(html, childMenu.child, menuParameId);
  140. } else {
  141. html += '<a href="javascript:;" class="layui-menu-tips" data-type="tabAdd" data-tab-mpi="m-p-i-' + menuParameId + '" data-tab="' + childMenu.href + '" target="' + childMenu.target + '"><i class="' + childMenu.icon + '"></i><span class="layui-left-nav"> ' + childMenu.title + '</span></a>\n';
  142. menuParameId++;
  143. window.menuParameId = menuParameId;
  144. }
  145. html += '</dd>\n';
  146. });
  147. html += '</dl>\n';
  148. return html;
  149. };
  150. leftMenuHtml = buildChildHtml(leftMenuHtml, menu.child, menuParameId);
  151. } else {
  152. leftMenuHtml += '<a href="javascript:;" class="layui-menu-tips" data-type="tabAdd" data-tab-mpi="m-p-i-' + menuParameId + '" data-tab="' + menu.href + '" target="' + menu.target + '"><i class="' + menu.icon + '"></i><span class="layui-left-nav"> ' + menu.title + '</span></a>\n';
  153. menuParameId++;
  154. }
  155. leftMenuHtml += '</li>\n';
  156. });
  157. leftMenuHtml += '</ul>\n';
  158. headerMenuCheckDefault = '';
  159. leftMenuCheckDefault = 'layui-hide';
  160. });
  161. $('.layui-header-pc-menu').html(headerMenuHtml); //电脑
  162. $('.layui-header-mini-menu').html(headerMobileMenuHtml); //手机
  163. $('.layui-left-menu').html(leftMenuHtml);
  164. element.init();
  165. };
  166. /**
  167. * 初始化选项卡
  168. */
  169. this.initTab = function () {
  170. var locationHref = window.location.href;
  171. var urlArr = locationHref.split("#");
  172. if (urlArr.length >= 2) {
  173. var href = urlArr.pop();
  174. // 判断链接是否有效
  175. var checkUrl = layuimini.checkUrl(href);
  176. if (checkUrl != true) {
  177. return layuimini.msg_error(checkUrl);
  178. }
  179. // 判断tab是否存在
  180. var checkTab = layuimini.checkTab(href);
  181. if (!checkTab) {
  182. var title = href,
  183. tabId = href;
  184. $("[data-tab]").each(function () {
  185. var checkHref = $(this).attr("data-tab");
  186. // 判断是否带参数了
  187. if (layuimini.config('urlSuffixDefault')) {
  188. if (href.indexOf("mpi=") > -1) {
  189. var menuParameId = $(this).attr('data-tab-mpi');
  190. if (checkHref.indexOf("?") > -1) {
  191. checkHref = checkHref + '&mpi=' + menuParameId;
  192. } else {
  193. checkHref = checkHref + '?mpi=' + menuParameId;
  194. }
  195. }
  196. }
  197. if (checkHref == tabId) {
  198. title = $(this).html();
  199. title = title.replace('style="display: none;"', '');
  200. // 自动展开菜单栏
  201. var addMenuClass = function ($element, type) {
  202. if (type == 1) {
  203. $element.addClass('layui-this');
  204. if ($element.attr('class') != 'layui-nav-item layui-this') {
  205. addMenuClass($element.parent().parent(), 2);
  206. } else {
  207. var moduleId = $element.parent().attr('id');
  208. $(".layui-header-menu li").attr('class', 'layui-nav-item');
  209. $("#" + moduleId + "HeaderId").addClass("layui-this");
  210. $(".layui-left-nav-tree").attr('class', 'layui-nav layui-nav-tree layui-hide');
  211. $("#" + moduleId).attr('class', 'layui-nav layui-nav-tree layui-this');
  212. }
  213. } else {
  214. $element.addClass('layui-nav-itemed');
  215. if ($element.attr('class') != 'layui-nav-item layui-nav-itemed') {
  216. addMenuClass($element.parent().parent(), 2);
  217. } else {
  218. var moduleId = $element.parent().attr('id');
  219. $(".layui-header-menu li").attr('class', 'layui-nav-item');
  220. $("#" + moduleId + "HeaderId").addClass("layui-this");
  221. $(".layui-left-nav-tree").attr('class', 'layui-nav layui-nav-tree layui-hide');
  222. $("#" + moduleId).attr('class', 'layui-nav layui-nav-tree layui-this');
  223. }
  224. }
  225. };
  226. addMenuClass($(this).parent(), 1);
  227. }
  228. });
  229. var layuiminiHomeTab = $('#layuiminiHomeTab').attr('lay-id'),
  230. layuiminiHomeHref = sessionStorage.getItem('layuiminiHomeHref');
  231. // 非菜单打开的tab窗口
  232. if (href == title) {
  233. var layuiminiTabInfo = JSON.parse(sessionStorage.getItem("layuiminiTabInfo"));
  234. if (layuiminiTabInfo != null) {
  235. var check = layuiminiTabInfo[tabId];
  236. if (check != undefined || check != null) {
  237. title = check['title'];
  238. }
  239. }
  240. }
  241. if (layuiminiHomeTab != href && layuiminiHomeHref != href) {
  242. layuimini.addTab(tabId, href, title, true);
  243. layuimini.changeTab(tabId);
  244. }
  245. }
  246. }
  247. if (layuimini.config('urlHashLocation')) {
  248. layuimini.hashTab();
  249. }
  250. };
  251. /**
  252. * 配色方案配置项(默认选中第一个方案)
  253. * @param bgcolorId
  254. */
  255. this.bgColorConfig = function (bgcolorId) {
  256. var bgColorConfig = [
  257. {
  258. headerRight: '#1aa094',
  259. headerRightThis: '#197971',
  260. headerLogo: '#243346',
  261. menuLeft: '#2f4056',
  262. menuLeftThis: '#1aa094',
  263. menuLeftHover: '#3b3f4b',
  264. },
  265. {
  266. headerRight: '#23262e',
  267. headerRightThis: '#0c0c0c',
  268. headerLogo: '#0c0c0c',
  269. menuLeft: '#23262e',
  270. menuLeftThis: '#1aa094',
  271. menuLeftHover: '#3b3f4b',
  272. },
  273. {
  274. headerRight: '#ffa4d1',
  275. headerRightThis: '#bf7b9d',
  276. headerLogo: '#e694bd',
  277. menuLeft: '#1f1f1f',
  278. menuLeftThis: '#ffa4d1',
  279. menuLeftHover: '#1f1f1f',
  280. },
  281. {
  282. headerRight: '#1aa094',
  283. headerRightThis: '#197971',
  284. headerLogo: '#0c0c0c',
  285. menuLeft: '#23262e',
  286. menuLeftThis: '#1aa094',
  287. menuLeftHover: '#3b3f4b',
  288. },
  289. {
  290. headerRight: '#1e9fff',
  291. headerRightThis: '#0069b7',
  292. headerLogo: '#0c0c0c',
  293. menuLeft: '#1f1f1f',
  294. menuLeftThis: '#1aa094',
  295. menuLeftHover: '#3b3f4b',
  296. },
  297. {
  298. headerRight: '#ffb800',
  299. headerRightThis: '#d09600',
  300. headerLogo: '#243346',
  301. menuLeft: '#2f4056',
  302. menuLeftThis: '#1aa094',
  303. menuLeftHover: '#3b3f4b',
  304. },
  305. {
  306. headerRight: '#e82121',
  307. headerRightThis: '#ae1919',
  308. headerLogo: '#0c0c0c',
  309. menuLeft: '#1f1f1f',
  310. menuLeftThis: '#1aa094',
  311. menuLeftHover: '#3b3f4b',
  312. },
  313. {
  314. headerRight: '#963885',
  315. headerRightThis: '#772c6a',
  316. headerLogo: '#243346',
  317. menuLeft: '#2f4056',
  318. menuLeftThis: '#1aa094',
  319. menuLeftHover: '#3b3f4b',
  320. },
  321. {
  322. headerRight: '#1e9fff',
  323. headerRightThis: '#0069b7',
  324. headerLogo: '#0069b7',
  325. menuLeft: '#1f1f1f',
  326. menuLeftThis: '#1aa094',
  327. menuLeftHover: '#3b3f4b',
  328. },
  329. {
  330. headerRight: '#ffb800',
  331. headerRightThis: '#d09600',
  332. headerLogo: '#d09600',
  333. menuLeft: '#2f4056',
  334. menuLeftThis: '#1aa094',
  335. menuLeftHover: '#3b3f4b',
  336. },
  337. {
  338. headerRight: '#e82121',
  339. headerRightThis: '#ae1919',
  340. headerLogo: '#d91f1f',
  341. menuLeft: '#1f1f1f',
  342. menuLeftThis: '#1aa094',
  343. menuLeftHover: '#3b3f4b',
  344. },
  345. {
  346. headerRight: '#963885',
  347. headerRightThis: '#772c6a',
  348. headerLogo: '#772c6a',
  349. menuLeft: '#2f4056',
  350. menuLeftThis: '#1aa094',
  351. menuLeftHover: '#3b3f4b',
  352. }
  353. ];
  354. if (bgcolorId == undefined) {
  355. return bgColorConfig;
  356. } else {
  357. return bgColorConfig[bgcolorId];
  358. }
  359. };
  360. /**
  361. * 构建背景颜色选择
  362. * @returns {string}
  363. */
  364. this.buildBgColorHtml = function () {
  365. var html = '';
  366. var bgcolorId = sessionStorage.getItem('layuiminiBgcolorId');
  367. if (bgcolorId == null || bgcolorId == undefined || bgcolorId == '') {
  368. bgcolorId = 0;
  369. }
  370. var bgColorConfig = layuimini.bgColorConfig();
  371. $.each(bgColorConfig, function (key, val) {
  372. if (key == bgcolorId) {
  373. html += '<li class="layui-this" data-select-bgcolor="' + key + '">\n';
  374. } else {
  375. html += '<li data-select-bgcolor="' + key + '">\n';
  376. }
  377. html += '<a href="javascript:;" data-skin="skin-blue" style="" class="clearfix full-opacity-hover">\n' +
  378. '<div><span style="display:block; width: 20%; float: left; height: 12px; background: ' + val.headerLogo + ';"></span><span style="display:block; width: 80%; float: left; height: 12px; background: ' + val.headerRight + ';"></span></div>\n' +
  379. '<div><span style="display:block; width: 20%; float: left; height: 40px; background: ' + val.menuLeft + ';"></span><span style="display:block; width: 80%; float: left; height: 40px; background: #f4f5f7;"></span></div>\n' +
  380. '</a>\n' +
  381. '</li>';
  382. });
  383. return html;
  384. };
  385. /**
  386. * 判断窗口是否已打开
  387. * @param tabId
  388. **/
  389. this.checkTab = function (tabId, isIframe) {
  390. // 判断选项卡上是否有
  391. var checkTab = false;
  392. if (isIframe == undefined || isIframe == false) {
  393. $(".layui-tab-title li").each(function () {
  394. checkTabId = $(this).attr('lay-id');
  395. if (checkTabId != null && checkTabId == tabId) {
  396. checkTab = true;
  397. }
  398. });
  399. } else {
  400. parent.layui.$(".layui-tab-title li").each(function () {
  401. checkTabId = $(this).attr('lay-id');
  402. if (checkTabId != null && checkTabId == tabId) {
  403. checkTab = true;
  404. }
  405. });
  406. }
  407. if (checkTab == false) {
  408. return false;
  409. }
  410. // 判断sessionStorage是否有
  411. var layuiminiTabInfo = JSON.parse(sessionStorage.getItem("layuiminiTabInfo"));
  412. if (layuiminiTabInfo == null) {
  413. layuiminiTabInfo = {};
  414. }
  415. var check = layuiminiTabInfo[tabId];
  416. if (check == undefined || check == null) {
  417. return false;
  418. }
  419. return true;
  420. };
  421. /**
  422. * 打开新窗口
  423. * @param tabId
  424. * @param href
  425. * @param title
  426. */
  427. this.addTab = function (tabId, href, title, addSession) {
  428. if (addSession == undefined || addSession == true) {
  429. var layuiminiTabInfo = JSON.parse(sessionStorage.getItem("layuiminiTabInfo"));
  430. if (layuiminiTabInfo == null) {
  431. layuiminiTabInfo = {};
  432. }
  433. layuiminiTabInfo[tabId] = {href: href, title: title}
  434. sessionStorage.setItem("layuiminiTabInfo", JSON.stringify(layuiminiTabInfo));
  435. }
  436. element.tabAdd('layuiminiTab', {
  437. title: title + '<i data-tab-close="" class="layui-icon layui-unselect layui-tab-close">ဆ</i>' //用于演示
  438. , content: '<iframe width="100%" height="100%" frameborder="0" src="' + href + '"></iframe>'
  439. , id: tabId
  440. });
  441. };
  442. /**
  443. * 删除窗口
  444. * @param tabId
  445. */
  446. this.delTab = function (tabId) {
  447. var layuiminiTabInfo = JSON.parse(sessionStorage.getItem("layuiminiTabInfo"));
  448. if (layuiminiTabInfo != null) {
  449. delete layuiminiTabInfo[tabId];
  450. sessionStorage.setItem("layuiminiTabInfo", JSON.stringify(layuiminiTabInfo))
  451. }
  452. element.tabDelete('layuiminiTab', tabId);
  453. };
  454. /**
  455. * 切换选项卡
  456. **/
  457. this.changeTab = function (tabId) {
  458. element.tabChange('layuiminiTab', tabId);
  459. };
  460. /**
  461. * Hash地址的定位
  462. */
  463. this.hashTab = function () {
  464. var layId = location.hash.replace(/^#/, '');
  465. element.tabChange('layuiminiTab', layId);
  466. element.on('tab(layuiminiTab)', function (elem) {
  467. location.hash = $(this).attr('lay-id');
  468. });
  469. };
  470. /**
  471. * 判断是否为手机
  472. */
  473. this.checkMobile = function () {
  474. var ua = navigator.userAgent.toLocaleLowerCase();
  475. var pf = navigator.platform.toLocaleLowerCase();
  476. var isAndroid = (/android/i).test(ua) || ((/iPhone|iPod|iPad/i).test(ua) && (/linux/i).test(pf))
  477. || (/ucweb.*linux/i.test(ua));
  478. var isIOS = (/iPhone|iPod|iPad/i).test(ua) && !isAndroid;
  479. var isWinPhone = (/Windows Phone|ZuneWP7/i).test(ua);
  480. var clientWidth = document.documentElement.clientWidth;
  481. if (!isAndroid && !isIOS && !isWinPhone && clientWidth > 768) {
  482. return false;
  483. } else {
  484. return true;
  485. }
  486. };
  487. /**
  488. * 判断链接是否有效
  489. * @param url
  490. * @returns {boolean}
  491. */
  492. this.checkUrl = function (url) {
  493. if (!layuimini.config('checkUrlDefault')) {
  494. return true;
  495. }
  496. var msg = true;
  497. $.ajax({
  498. url: url,
  499. type: 'get',
  500. contentType: "application/x-www-form-urlencoded; charset=UTF-8",
  501. async: false,
  502. error: function (xhr, textstatus, thrown) {
  503. msg = 'Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!';
  504. }
  505. });
  506. return msg;
  507. };
  508. /**
  509. * 成功
  510. * @param title
  511. * @returns {*}
  512. */
  513. this.msg_success = function (title) {
  514. return layer.msg(title, {icon: 1, shade: this.shade, scrollbar: false, time: 2000, shadeClose: true});
  515. };
  516. /**
  517. * 失败
  518. * @param title
  519. * @returns {*}
  520. */
  521. this.msg_error = function (title) {
  522. return layer.msg(title, {icon: 2, shade: this.shade, scrollbar: false, time: 3000, shadeClose: true});
  523. };
  524. /**
  525. * 选项卡滚动
  526. */
  527. this.tabRoll = function () {
  528. $(window).on("resize", function (event) {
  529. var topTabsBox = $("#top_tabs_box"),
  530. topTabsBoxWidth = $("#top_tabs_box").width(),
  531. topTabs = $("#top_tabs"),
  532. topTabsWidth = $("#top_tabs").width(),
  533. tabLi = topTabs.find("li.layui-this"),
  534. top_tabs = document.getElementById("top_tabs"),
  535. event = event || window.event;
  536. if (topTabsWidth > topTabsBoxWidth) {
  537. if (tabLi.position().left > topTabsBoxWidth || tabLi.position().left + topTabsBoxWidth > topTabsWidth) {
  538. topTabs.css("left", topTabsBoxWidth - topTabsWidth);
  539. } else {
  540. topTabs.css("left", -tabLi.position().left);
  541. }
  542. //拖动效果
  543. var flag = false;
  544. var cur = {
  545. x: 0,
  546. y: 0
  547. }
  548. var nx, dx, x;
  549. function down(event) {
  550. flag = true;
  551. var touch;
  552. if (event.touches) {
  553. touch = event.touches[0];
  554. } else {
  555. touch = event;
  556. }
  557. cur.x = touch.clientX;
  558. dx = top_tabs.offsetLeft;
  559. }
  560. function move(event) {
  561. var self = this;
  562. if (flag) {
  563. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  564. var touch;
  565. if (event.touches) {
  566. touch = event.touches[0];
  567. } else {
  568. touch = event;
  569. }
  570. nx = touch.clientX - cur.x;
  571. x = dx + nx;
  572. if (x > 0) {
  573. x = 0;
  574. } else {
  575. if (x < topTabsBoxWidth - topTabsWidth) {
  576. x = topTabsBoxWidth - topTabsWidth;
  577. } else {
  578. x = dx + nx;
  579. }
  580. }
  581. top_tabs.style.left = x + "px";
  582. //阻止页面的滑动默认事件
  583. document.addEventListener("touchmove", function () {
  584. event.preventDefault();
  585. }, false);
  586. }
  587. }
  588. //鼠标释放时候的函数
  589. function end() {
  590. flag = false;
  591. }
  592. //pc端拖动效果
  593. topTabs.on("mousedown", down);
  594. topTabs.on("mousemove", move);
  595. $(document).on("mouseup", end);
  596. //移动端拖动效果
  597. topTabs.on("touchstart", down);
  598. topTabs.on("touchmove", move);
  599. topTabs.on("touchend", end);
  600. } else {
  601. //移除pc端拖动效果
  602. topTabs.off("mousedown", down);
  603. topTabs.off("mousemove", move);
  604. topTabs.off("mouseup", end);
  605. //移除移动端拖动效果
  606. topTabs.off("touchstart", down);
  607. topTabs.off("touchmove", move);
  608. topTabs.off("touchend", end);
  609. topTabs.removeAttr("style");
  610. return false;
  611. }
  612. }).resize();
  613. };
  614. };
  615. /**
  616. * 关闭选项卡
  617. **/
  618. $('body').on('click', '[data-tab-close]', function () {
  619. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  620. $parent = $(this).parent();
  621. tabId = $parent.attr('lay-id');
  622. if (tabId != undefined || tabId != null) {
  623. layuimini.delTab(tabId);
  624. }
  625. layuimini.tabRoll();
  626. layer.close(loading);
  627. });
  628. /**
  629. * 打开新窗口
  630. */
  631. $('body').on('click', '[data-tab]', function () {
  632. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  633. var tabId = $(this).attr('data-tab'),
  634. href = $(this).attr('data-tab'),
  635. title = $(this).html(),
  636. target = $(this).attr('target');
  637. if (target == '_blank') {
  638. layer.close(loading);
  639. window.open(href, "_blank");
  640. return false;
  641. }
  642. title = title.replace('style="display: none;"', '');
  643. // 拼接参数
  644. if (layuimini.config('urlSuffixDefault')) {
  645. var menuParameId = $(this).attr('data-tab-mpi');
  646. if (href.indexOf("?") > -1) {
  647. href = href + '&mpi=' + menuParameId;
  648. tabId = href;
  649. } else {
  650. href = href + '?mpi=' + menuParameId;
  651. tabId = href;
  652. }
  653. }
  654. // 判断链接是否有效
  655. var checkUrl = layuimini.checkUrl(href);
  656. if (checkUrl != true) {
  657. return layuimini.msg_error(checkUrl);
  658. }
  659. if (tabId == null || tabId == undefined) {
  660. tabId = new Date().getTime();
  661. }
  662. // 判断该窗口是否已经打开过
  663. var checkTab = layuimini.checkTab(tabId);
  664. if (!checkTab) {
  665. layuimini.addTab(tabId, href, title, true);
  666. }
  667. element.tabChange('layuiminiTab', tabId);
  668. layuimini.initDevice();
  669. layuimini.tabRoll();
  670. layer.close(loading);
  671. });
  672. /**
  673. * 在iframe子菜单上打开新窗口
  674. */
  675. $('body').on('click', '[data-iframe-tab]', function () {
  676. var loading = parent.layer.load(0, {shade: false, time: 2 * 1000});
  677. var tabId = $(this).attr('data-iframe-tab'),
  678. href = $(this).attr('data-iframe-tab'),
  679. icon = $(this).attr('data-icon'),
  680. title = $(this).attr('data-title'),
  681. target = $(this).attr('target');
  682. if (target == '_blank') {
  683. parent.layer.close(loading);
  684. window.open(href, "_blank");
  685. return false;
  686. }
  687. title = '<i class="' + icon + '"></i><span class="layui-left-nav"> ' + title + '</span>';
  688. if (tabId == null || tabId == undefined) {
  689. tabId = new Date().getTime();
  690. }
  691. // 判断该窗口是否已经打开过
  692. var checkTab = layuimini.checkTab(tabId, true);
  693. if (!checkTab) {
  694. var layuiminiTabInfo = JSON.parse(sessionStorage.getItem("layuiminiTabInfo"));
  695. if (layuiminiTabInfo == null) {
  696. layuiminiTabInfo = {};
  697. }
  698. layuiminiTabInfo[tabId] = {href: href, title: title}
  699. sessionStorage.setItem("layuiminiTabInfo", JSON.stringify(layuiminiTabInfo));
  700. parent.layui.element.tabAdd('layuiminiTab', {
  701. title: title + '<i data-tab-close="" class="layui-icon layui-unselect layui-tab-close">ဆ</i>' //用于演示
  702. , content: '<iframe width="100%" height="100%" frameborder="0" src="' + href + '"></iframe>'
  703. , id: tabId
  704. });
  705. }
  706. parent.layui.element.tabChange('layuiminiTab', tabId);
  707. layuimini.tabRoll();
  708. parent.layer.close(loading);
  709. });
  710. /**
  711. * 左侧菜单的切换
  712. */
  713. $('body').on('click', '[data-menu]', function () {
  714. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  715. $parent = $(this).parent();
  716. menuId = $(this).attr('data-menu');
  717. // header
  718. $(".layui-header-menu .layui-nav-item.layui-this").removeClass('layui-this');
  719. $(this).addClass('layui-this');
  720. // left
  721. $(".layui-left-menu .layui-nav.layui-nav-tree.layui-this").addClass('layui-hide');
  722. $(".layui-left-menu .layui-nav.layui-nav-tree.layui-this.layui-hide").removeClass('layui-this');
  723. $("#" + menuId).removeClass('layui-hide');
  724. $("#" + menuId).addClass('layui-this');
  725. layer.close(loading);
  726. });
  727. /**
  728. * 清理
  729. */
  730. $('body').on('click', '[data-clear]', function () {
  731. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  732. sessionStorage.clear();
  733. // 判断是否清理服务端
  734. var clearUrl = $(this).attr('data-href');
  735. if (clearUrl != undefined && clearUrl != '' && clearUrl != null) {
  736. $.getJSON(clearUrl, function (data, status) {
  737. layer.close(loading);
  738. if (data.code != 1) {
  739. return layuimini.msg_error(data.msg);
  740. } else {
  741. return layuimini.msg_success(data.msg);
  742. }
  743. }).fail(function () {
  744. layer.close(loading);
  745. return layuimini.msg_error('清理缓存接口有误');
  746. });
  747. } else {
  748. layer.close(loading);
  749. return layuimini.msg_success('清除缓存成功');
  750. }
  751. });
  752. /**
  753. * 刷新
  754. */
  755. $('body').on('click', '[data-refresh]', function () {
  756. $(".layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload();
  757. layuimini.msg_success('刷新成功');
  758. });
  759. /**
  760. * 选项卡操作
  761. */
  762. $('body').on('click', '[data-page-close]', function () {
  763. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  764. var closeType = $(this).attr('data-page-close');
  765. $(".layui-tab-title li").each(function () {
  766. tabId = $(this).attr('lay-id');
  767. var id = $(this).attr('id');
  768. if (id != 'layuiminiHomeTabId') {
  769. var tabClass = $(this).attr('class');
  770. if (closeType == 'all') {
  771. layuimini.delTab(tabId);
  772. } else {
  773. if (tabClass != 'layui-this') {
  774. layuimini.delTab(tabId);
  775. }
  776. }
  777. }
  778. });
  779. layuimini.tabRoll();
  780. layer.close(loading);
  781. });
  782. /**
  783. * 菜单栏缩放
  784. */
  785. $('body').on('click', '[data-side-fold]', function () {
  786. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  787. var isShow = $(this).attr('data-side-fold');
  788. if (isShow == 1) { // 缩放
  789. $(this).attr('data-side-fold', 0);
  790. $('.layuimini-tool i').attr('class', 'fa fa-indent');
  791. $('.layui-layout-body').attr('class', 'layui-layout-body layuimini-mini');
  792. } else { // 正常
  793. $(this).attr('data-side-fold', 1);
  794. $('.layuimini-tool i').attr('class', 'fa fa-outdent');
  795. $('.layui-layout-body').attr('class', 'layui-layout-body layuimini-all');
  796. }
  797. layuimini.tabRoll();
  798. element.init();
  799. layer.close(loading);
  800. });
  801. /**
  802. * 监听提示信息
  803. */
  804. $("body").on("mouseenter", ".layui-menu-tips", function () {
  805. var classInfo = $(this).attr('class'),
  806. tips = $(this).children('span').text(),
  807. isShow = $('.layuimini-tool i').attr('data-side-fold');
  808. if (isShow == 0) {
  809. openTips = layer.tips(tips, $(this), {tips: [2, '#2f4056'], time: 30000});
  810. }
  811. });
  812. $("body").on("mouseleave", ".layui-menu-tips", function () {
  813. var isShow = $('.layuimini-tool i').attr('data-side-fold');
  814. if (isShow == 0) {
  815. try {
  816. layer.close(openTips);
  817. } catch (e) {
  818. console.log(e.message);
  819. }
  820. }
  821. });
  822. /**
  823. * 弹出配色方案
  824. */
  825. $('body').on('click', '[data-bgcolor]', function () {
  826. var loading = layer.load(0, {shade: false, time: 2 * 1000});
  827. var clientHeight = (document.documentElement.clientHeight) - 95;
  828. var bgColorHtml = layuimini.buildBgColorHtml();
  829. var html = '<div class="layuimini-color">\n' +
  830. '<div class="color-title">\n' +
  831. '<span>配色方案</span>\n' +
  832. '</div>\n' +
  833. '<div class="color-content">\n' +
  834. '<ul>\n' + bgColorHtml + '</ul>\n' +
  835. '</div>\n' +
  836. '</div>';
  837. layer.open({
  838. type: 1,
  839. title: false,
  840. closeBtn: 0,
  841. shade: 0.2,
  842. anim: 2,
  843. shadeClose: true,
  844. id: 'layuiminiBgColor',
  845. area: ['340px', clientHeight + 'px'],
  846. offset: 'rb',
  847. content: html,
  848. end:function () {
  849. $('.layuimini-select-bgcolor').removeClass('layui-this');
  850. }
  851. });
  852. layer.close(loading);
  853. });
  854. /**
  855. * 选择配色方案
  856. */
  857. $('body').on('click', '[data-select-bgcolor]', function () {
  858. var bgcolorId = $(this).attr('data-select-bgcolor');
  859. $('.layuimini-color .color-content ul .layui-this').attr('class', '');
  860. $(this).attr('class', 'layui-this');
  861. sessionStorage.setItem('layuiminiBgcolorId', bgcolorId);
  862. layuimini.initBgColor();
  863. });
  864. exports("layuimini", layuimini);
  865. });