LM.js 38 KB

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