menu.js 16 KB

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