menu.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. layui.define(['table', 'jquery', 'element'], function(exports) {
  2. "use strict";
  3. let authInfo = JSON.parse(localStorage.getItem(window._AUTH_INFO_));
  4. let authInfoDeductionSwitch = 0;
  5. if (authInfo){
  6. authInfoDeductionSwitch = authInfo.detail.deduction_switch;
  7. }
  8. var MOD_NAME = 'menu',
  9. $ = layui.jquery,
  10. element = layui.element;
  11. var pearMenu = function(opt) {
  12. this.option = opt;
  13. };
  14. pearMenu.prototype.render = function(opt) {
  15. if (authInfoDeductionSwitch===0){
  16. // 全局
  17. }else if (authInfoDeductionSwitch===1){
  18. // cps
  19. opt.url = 'admin/data/cps_menu.json';
  20. }else{
  21. // cpa
  22. opt.url = 'admin/data/cpa_menu.json';
  23. }
  24. var option = {
  25. elem: opt.elem,
  26. async: opt.async,
  27. parseData: opt.parseData,
  28. url: opt.url,
  29. method: opt.method ? opt.method : "GET",
  30. defaultOpen: opt.defaultOpen,
  31. defaultSelect: opt.defaultSelect,
  32. control: opt.control,
  33. controlWidth: opt.controlWidth ? opt.controlWidth: 500,
  34. defaultMenu: opt.defaultMenu,
  35. accordion: opt.accordion,
  36. height: opt.height,
  37. theme: opt.theme,
  38. data: opt.data ? opt.data : [],
  39. change: opt.change ? opt.change : function() {},
  40. done: opt.done ? opt.done : function() {}
  41. }
  42. if (option.async) {
  43. if (option.method === "GET") {
  44. getData(option.url).then(function(data) {
  45. option.data = data;
  46. renderMenu(option);
  47. });
  48. } else {
  49. postData(option.url).then(function(data) {
  50. option.data = data;
  51. renderMenu(option);
  52. });
  53. }
  54. } else {
  55. //renderMenu中需要调用done事件,done事件中需要menu对象,但是此时还未返回menu对象,做个延时提前返回对象
  56. window.setTimeout(function() { renderMenu(option);}, 500);
  57. }
  58. // 处理高度
  59. $("#"+opt.elem).height(option.height)
  60. setTimeout(function(){
  61. $("#"+opt.control+" .control").on("mousewheel DOMMouseScroll", function(event) {
  62. var delta = (event.originalEvent.wheelDelta && (event.originalEvent.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
  63. (event.originalEvent.detail && (event.originalEvent.detail > 0 ? -1 : 1)); // firefox
  64. if (delta > 0) {
  65. for (var num = 1; num < 20; num++) {
  66. setTimeout(function() {
  67. if ($("#"+opt.control+" .control ul").css('left').replace("px", "") < 0) {
  68. $("#"+opt.control+" .control ul").css("left", "+=2px");
  69. }
  70. }, 10)
  71. }
  72. } else if (delta < 0) {
  73. if(( (Number)($("#"+opt.control+" .control ul").css("left").replace("px","")) + ($("#"+opt.control+" .control ul").width() - $("#"+opt.control+" .control").width())) > 0){
  74. for (var num = 1; num < 20; num++) {
  75. setTimeout(function() {
  76. $("#"+opt.control+" .control ul").css("left", "-=2px");
  77. }, 10)
  78. }
  79. }
  80. }
  81. });
  82. },1000)
  83. return new pearMenu(opt);
  84. }
  85. pearMenu.prototype.click = function(clickEvent) {
  86. var _this = this;
  87. $("body").on("click", "#" + _this.option.elem + " .site-demo-active", function() {
  88. var dom = $(this);
  89. var data = {
  90. menuId: dom.attr("menu-id"),
  91. menuTitle: dom.attr("menu-title"),
  92. menuPath: dom.attr("menu-title"),
  93. menuIcon: dom.attr("menu-icon"),
  94. menuUrl: dom.attr("menu-url"),
  95. openType: dom.attr("open-type")
  96. };
  97. var doms = hash(dom);
  98. if (doms != null) {
  99. if (doms.text() != '') {
  100. data['menuPath'] = doms.find("span").text() + " / " + data['menuPath'];
  101. }
  102. }
  103. if (doms != null) {
  104. var domss = hash(doms);
  105. if(domss!=null){
  106. if (domss.text() != '') {
  107. data['menuPath'] = domss.find("span").text() + " / " + data['menuPath'];
  108. }}
  109. }
  110. if (domss != null) {
  111. var domsss = hash(domss);
  112. if(domsss!=null){
  113. if (domsss.text() != '') {
  114. data['menuPath'] = domsss.find("span").text() + " / " + data['menuPath'];
  115. }}
  116. }
  117. if ($("#" + _this.option.elem).is(".pear-nav-mini")) {
  118. if (_this.option.accordion) {
  119. activeMenus = $(this).parent().parent().parent().children("a");
  120. } else {
  121. activeMenus.push($(this).parent().parent().parent().children("a"));
  122. }
  123. }
  124. clickEvent(dom, data);
  125. })
  126. }
  127. function hash(dom) {
  128. var d = dom.parent().parent().prev();
  129. if (d.prop("tagName") === "UL") {
  130. return null;
  131. }
  132. return d;
  133. }
  134. pearMenu.prototype.skin = function(skin) {
  135. var menu = $(".pear-nav-tree[lay-filter='" + this.option.elem + "']").parent();
  136. menu.removeClass("dark-theme");
  137. menu.removeClass("light-theme");
  138. menu.addClass(skin);
  139. }
  140. pearMenu.prototype.selectItem = function(pearId) {
  141. if (this.option.control != false) {
  142. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find("ul").css({
  143. display: "none"
  144. });
  145. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass(
  146. "layui-this");
  147. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").css({
  148. display: "block"
  149. });
  150. var controlId = $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").attr("pear-id");
  151. if (controlId != undefined) {
  152. $("#" + this.option.control).find(".layui-this").removeClass("layui-this");
  153. $("#" + this.option.control).find("[pear-id='" + controlId + "']").addClass("layui-this");
  154. }
  155. }
  156. if (this.option.accordion == true) {
  157. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-nav-itemed").removeClass(
  158. "layui-nav-itemed");
  159. }
  160. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-this").removeClass(
  161. "layui-this");
  162. if (!$("#" + this.option.elem).is(".pear-nav-mini")) {
  163. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-nav-item").addClass("layui-nav-itemed");
  164. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("dd").addClass("layui-nav-itemed");
  165. }
  166. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parent().addClass("layui-this");
  167. }
  168. var activeMenus;
  169. pearMenu.prototype.collaspe = function(time) {
  170. var elem = this.option.elem;
  171. var config = this.option;
  172. if ($("#" + this.option.elem).is(".pear-nav-mini")) {
  173. $.each(activeMenus, function(i, item) {
  174. $("#" + elem + " a[menu-id='" + $(this).attr("menu-id") + "']").parent().addClass("layui-nav-itemed");
  175. })
  176. $("#" + this.option.elem).removeClass("pear-nav-mini");
  177. $("#" + this.option.elem).animate({
  178. width: "220px"
  179. }, 150);
  180. isHoverMenu(false, config);
  181. } else {
  182. activeMenus = $("#" + this.option.elem).find(".layui-nav-itemed>a");
  183. $("#" + this.option.elem).find(".layui-nav-itemed").removeClass("layui-nav-itemed");
  184. $("#" + this.option.elem).addClass("pear-nav-mini");
  185. $("#" + this.option.elem).animate({
  186. width: "60px"
  187. }, 400);
  188. isHoverMenu(true, config);
  189. }
  190. }
  191. function getData(url) {
  192. var defer = $.Deferred();
  193. $.get(url + "?fresh=" + Math.random(), function(result) {
  194. defer.resolve(result)
  195. });
  196. return defer.promise();
  197. }
  198. function postData(url) {
  199. var defer = $.Deferred();
  200. $.post(url + "?fresh=" + Math.random(), function(result) {
  201. defer.resolve(result)
  202. });
  203. return defer.promise();
  204. }
  205. function renderMenu(option) {
  206. if (option.parseData != false) {
  207. option.parseData(option.data);
  208. }
  209. if (option.data.length > 0) {
  210. if (option.control != false) {
  211. createMenuAndControl(option);
  212. } else {
  213. createMenu(option);
  214. }
  215. }
  216. element.init();
  217. downShow(option);
  218. option.done();
  219. }
  220. function createMenu(option) {
  221. var menuHtml = '<div style="height:100%!important;" class="pear-side-scroll layui-side-scroll ' + option.theme + '"><ul lay-filter="' + option.elem +
  222. '" class="layui-nav arrow pear-menu layui-nav-tree pear-nav-tree">'
  223. $.each(option.data, function(i, item) {
  224. var content = '<li class="layui-nav-item" >';
  225. if (i == option.defaultOpen) {
  226. content = '<li class="layui-nav-item layui-nav-itemed" >';
  227. }
  228. var href = "javascript:;";
  229. var target = "";
  230. var className = "site-demo-active"
  231. if (item.openType == "_blank" && item.type == 1) {
  232. href = item.href;
  233. target = "target='_blank'";
  234. className = "";
  235. }
  236. if (item.type == 0) {
  237. // 创 建 目 录 结 构
  238. content += '<a href="javascript:;" menu-type="' + item.type + '" menu-id="' + item.id + '" href="' + href +
  239. '" ' + target + '><i class="' + item.icon + '"></i><span>' + item.title +
  240. '</span></a>';
  241. } else if (item.type == 1) {
  242. content += '<a class="' + className + '" menu-type="' + item.type + '" menu-url="' + item.href + '" menu-id="' +
  243. item.id +
  244. '" menu-title="' + item.title + '" href="' + href + '" ' + target + '><i class="' + item.icon +
  245. '"></i><span>' + item.title + '</span></a>';
  246. }
  247. // 调 用 递 归 方 法 加 载 无 限 层 级 的 子 菜 单
  248. content += loadchild(item);
  249. // 结 束 一 个 根 菜 单 项
  250. content += '</li>';
  251. menuHtml += content;
  252. });
  253. // 结 束 菜 单 结 构 的 初 始 化
  254. menuHtml += "</ul></div>";
  255. // 将 菜 单 拼 接 到 初 始 化 容 器 中
  256. $("#" + option.elem).html(menuHtml);
  257. }
  258. function createMenuAndControl(option) {
  259. var control = '<div style="width: '+ option.controlWidth +'px;white-space: nowrap;overflow-x: scroll;overflow: hidden;" class="control"><ul class="layui-nav pear-nav-control pc layui-hide-xs" style="width: fit-content;">';
  260. var controlPe = '<ul class="layui-nav pear-nav-control layui-hide-sm">';
  261. // 声 明 头 部
  262. var menu = '<div class="layui-side-scroll ' + option.theme + '">'
  263. // 开 启 同 步 操 作
  264. var index = 0;
  265. var controlItemPe = '<dl class="layui-nav-child">';
  266. $.each(option.data, function(i, item) {
  267. var menuItem = '';
  268. var controlItem = '';
  269. if (i === option.defaultMenu) {
  270. controlItem = '<li pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  271. '" class="layui-this layui-nav-item"><a href="#">' + item.title + '</a></li>';
  272. menuItem = '<ul pear-id="' + item.id + '" lay-filter="' + option.elem +
  273. '" class="layui-nav arrow layui-nav-tree pear-nav-tree">';
  274. controlPe += '<li class="layui-nav-item"><a class="pe-title" href="javascript:;" >' + item.title + '</a>';
  275. controlItemPe += '<dd pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  276. '"><a href="javascript:void(0);">' + item.title + '</a></dd>';
  277. } else {
  278. controlItem = '<li pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  279. '" class="layui-nav-item"><a href="#">' + item.title + '</a></li>';
  280. menuItem = '<ul style="display:none" pear-id="' + item.id + '" lay-filter="' + option.elem +
  281. '" class="layui-nav arrow layui-nav-tree pear-nav-tree">';
  282. controlItemPe += '<dd pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  283. '"><a href="javascript:void(0);">' + item.title + '</a></dd>';
  284. }
  285. index++;
  286. $.each(item.children, function(i, note) {
  287. // 创 建 每 一 个 菜 单 项
  288. var content = '<li class="layui-nav-item" >';
  289. var href = "javascript:;";
  290. var target = "";
  291. var className = "site-demo-active";
  292. if (note.openType == "_blank" && note.type == 1) {
  293. href = note.href;
  294. target = "target='_blank'";
  295. className = "";
  296. }
  297. // 判 断 菜 单 类 型 0 是 不可跳转的目录 1 是 可 点 击 跳 转 的 菜 单
  298. if (note.type == 0) {
  299. // 创 建 目 录 结 构
  300. content += '<a href="' + href + '" ' + target + ' menu-type="' + note.type + '" menu-id="' + note.id +
  301. '" ><i class="' + note.icon + '"></i><span>' + note.title +
  302. '</span></a>';
  303. } else if (note.type == 1) {
  304. // 创 建 菜 单 结 构
  305. content += '<a ' + target + ' class="' + className + '" menu-type="' + note.type + '" menu-url="' + note.href +
  306. '" menu-id="' + note.id +
  307. '" menu-title="' + note.title + '" href="' + href + '"><i class="' + note.icon +
  308. '"></i><span>' + note.title + '</span></a>';
  309. }
  310. content += loadchild(note);
  311. content += '</li>';
  312. menuItem += content;
  313. })
  314. menu += menuItem + '</ul>';
  315. control += controlItem;
  316. })
  317. controlItemPe += "</li></dl></ul>"
  318. controlPe += controlItemPe;
  319. $("#" + option.control).html(control+"</div>");
  320. $("#" + option.control).append(controlPe);
  321. $("#" + option.elem).html(menu);
  322. $("#" + option.control + " .pear-nav-control").on("click", "[pear-id]", function() {
  323. $("#" + option.elem).find(".pear-nav-tree").css({
  324. display: 'none'
  325. });
  326. $("#" + option.elem).find(".pear-nav-tree[pear-id='" + $(this).attr("pear-id") + "']").css({
  327. display: 'block'
  328. });
  329. $("#" + option.control).find(".pe-title").html($(this).attr("pear-title"));
  330. $("#" + option.control).find("")
  331. option.change($(this).attr("pear-id"), $(this).attr("pear-title"), $(this).attr("pear-href"))
  332. })
  333. }
  334. /** 加载子菜单 (递归)*/
  335. function loadchild(obj) {
  336. // 判 单 是 否 是 菜 单, 如 果 是 菜 单 直 接 返 回
  337. if (obj.type == 1) {
  338. return "";
  339. }
  340. // 创 建 子 菜 单 结 构
  341. var content = '<dl class="layui-nav-child">';
  342. // 如 果 嵌 套 不 等 于 空
  343. if (obj.children != null && obj.children.length > 0) {
  344. // 遍 历 子 项 目
  345. $.each(obj.children, function(i, note) {
  346. // 创 建 子 项 结 构
  347. content += '<dd>';
  348. var href = "javascript:;";
  349. var target = "";
  350. var className = "site-demo-active";
  351. if (note.openType == "_blank" && note.type == 1) {
  352. href = note.href;
  353. target = "target='_blank'";
  354. className = "";
  355. }
  356. // 判 断 子 项 类 型
  357. if (note.type == 0) {
  358. // 创 建 目 录 结 构
  359. content += '<a ' + target + ' href="' + href + '" menu-type="' + note.type + '" menu-id="' + note.id +
  360. '"><i class="' + note.icon + '"></i><span>' + note.title + '</span></a>';
  361. } else if (note.type == 1) {
  362. // 创 建 菜 单 结 构
  363. content += '<a ' + target + ' class="' + className + '" menu-type="' + note.type + '" menu-url="' + note.href +
  364. '" menu-id="' + note.id + '" menu-title="' + note.title + '" menu-icon="' + note.icon + '" href="' + href +
  365. '" ><i class="' + note.icon + '"></i><span>' + note.title + '</span></a>';
  366. }
  367. // 加 载 子 项 目 录
  368. content += loadchild(note);
  369. // 结 束 当 前 子 菜 单
  370. content += '</dd>';
  371. });
  372. // 封 装
  373. } else {
  374. content += '<dd style="background-color: transparent!important;"><a style="background-color: transparent!important;margin-left: 26px">目录为空</a></dd>';
  375. }
  376. content += '</dl>';
  377. return content;
  378. }
  379. function downShow(option) {
  380. $("body #" + option.elem).on("click", "a[menu-type='0']", function() {
  381. if (!$("#" + option.elem).is(".pear-nav-mini")) {
  382. var superEle = $(this).parent();
  383. var ele = $(this).next('.layui-nav-child');
  384. var heights = ele.children("dd").length * 48;
  385. if ($(this).parent().is(".layui-nav-itemed")) {
  386. if (option.accordion) {
  387. $(this).parent().parent().find(".layui-nav-itemed").removeClass("layui-nav-itemed");
  388. $(this).parent().addClass("layui-nav-itemed");
  389. }
  390. ele.height(0);
  391. ele.animate({
  392. height: heights + "px"
  393. }, 200, function() {
  394. ele.css({
  395. height: "auto"
  396. });
  397. });
  398. } else {
  399. $(this).parent().addClass("layui-nav-itemed");
  400. ele.animate({
  401. height: "0px"
  402. }, 200, function() {
  403. ele.css({
  404. height: "auto"
  405. });
  406. $(this).parent().removeClass("layui-nav-itemed");
  407. });
  408. }
  409. }
  410. })
  411. }
  412. /** 二 级 悬 浮 菜 单*/
  413. function isHoverMenu(b, option) {
  414. if (b) {
  415. $("#" + option.elem + ".pear-nav-mini .layui-nav-item,#" + option.elem + ".pear-nav-mini dd").hover(function(e) {
  416. e.stopPropagation();
  417. var _this = $(this);
  418. _this.siblings().find(".layui-nav-child")
  419. .removeClass("layui-nav-hover").css({
  420. left: 0,
  421. top: 0
  422. });
  423. _this.children(".layui-nav-child").addClass("layui-nav-hover");
  424. _this.closest('.layui-nav-item').data('time') && clearTimeout(_this.closest('.layui-nav-item').data('time'));
  425. var height = $(window).height();
  426. var topLength = _this.offset().top;
  427. var thisHeight = _this.children(".layui-nav-child").height();
  428. if ((thisHeight + topLength) > height) {
  429. topLength = height - thisHeight - 10;
  430. }
  431. var left = _this.offset().left + 60;
  432. if (!_this.hasClass("layui-nav-item")) {
  433. left = _this.offset().left + _this.width();
  434. }
  435. _this.children(".layui-nav-child").offset({
  436. top: topLength,
  437. left: left + 3
  438. });
  439. }, function(e) {
  440. e.stopPropagation();
  441. var _this = $(this);
  442. _this.closest('.layui-nav-item').data('time', setTimeout(function() {
  443. _this.closest('.layui-nav-item')
  444. .find(".layui-nav-child")
  445. .removeClass("layui-nav-hover")
  446. .css({
  447. left: 0,
  448. top: 0
  449. });
  450. }, 50));
  451. })
  452. } else {
  453. $("#" + option.elem + " .layui-nav-item").off('mouseenter').unbind('mouseleave');
  454. $("#" + option.elem + " dd").off('mouseenter').unbind('mouseleave');
  455. }
  456. }
  457. exports(MOD_NAME, new pearMenu());
  458. })