menu.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. define(['jquery', 'layui'], function($, layui) {
  2. "use strict";
  3. var element = layui.element;
  4. var pearMenu = function (opt) {
  5. this.option = opt;
  6. };
  7. pearMenu.render = function (opt) {
  8. var option = {
  9. elem: opt.elem,
  10. async: typeof opt.async === 'undefined' ? true : opt.async,
  11. parseData: opt.parseData,
  12. url: opt.url,
  13. method: opt.method ? opt.method : "GET",
  14. defaultOpen: opt.defaultOpen,
  15. defaultSelect: opt.defaultSelect,
  16. control: opt.control,
  17. defaultMenu: opt.defaultMenu,
  18. accordion: opt.accordion,
  19. height: opt.height,
  20. theme: opt.theme,
  21. data: opt.data ? opt.data : [],
  22. change: opt.change ? opt.change : function () { },
  23. done: opt.done ? opt.done : function () { }
  24. }
  25. var tempDone = option.done;
  26. option.done = function(){
  27. if (option.control) {
  28. rationalizeHeaderControlWidthAuto(option);
  29. }
  30. tempDone();
  31. }
  32. if (option.async) {
  33. if (option.method === "GET") {
  34. getData(option.url).then(function (data) {
  35. option.data = data;
  36. renderMenu(option);
  37. });
  38. } else {
  39. postData(option.url).then(function (data) {
  40. option.data = data;
  41. renderMenu(option);
  42. });
  43. }
  44. } else {
  45. // 延时返回,和 javascript 执行时序关联
  46. window.setTimeout(function () { renderMenu(option); }, 500);
  47. }
  48. // 处理高度
  49. $("#" + opt.elem).height(option.height)
  50. return new pearMenu(option);
  51. }
  52. pearMenu.prototype.refresh = function() {
  53. var option = this.option;
  54. if (option.async) {
  55. if (option.method === "GET") {
  56. getData(option.url).then(function(data) {
  57. option.data = data;
  58. renderMenu(option);
  59. });
  60. } else {
  61. postData(option.url).then(function(data) {
  62. option.data = data;
  63. renderMenu(option);
  64. });
  65. }
  66. }
  67. return this;
  68. }
  69. pearMenu.prototype.click = function (clickEvent) {
  70. var _this = this;
  71. //左侧子菜单点击
  72. $("body").on("click", "#" + _this.option.elem + " .site-demo-active", function () {
  73. var dom = $(this);
  74. var data = {
  75. menuId: dom.attr("menu-id"),
  76. menuTitle: dom.attr("menu-title"),
  77. menuPath: dom.attr("menu-title"),
  78. menuIcon: dom.attr("menu-icon"),
  79. menuUrl: dom.attr("menu-url"),
  80. openType: dom.attr("open-type")
  81. };
  82. var doms = hash(dom);
  83. if (doms != null) {
  84. if (doms.text() != '') {
  85. data['menuPath'] = doms.find("span").text() + " / " + data['menuPath'];
  86. }
  87. }
  88. if (doms != null) {
  89. var domss = hash(doms);
  90. if (domss != null) {
  91. if (domss.text() != '') {
  92. data['menuPath'] = domss.find("span").text() + " / " + data['menuPath'];
  93. }
  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. }
  104. if ($("#" + _this.option.elem).is(".pear-nav-mini")) {
  105. if (_this.option.accordion) {
  106. activeMenus = $(this).parent().parent().parent().children("a");
  107. } else {
  108. activeMenus.push($(this).parent().parent().parent().children("a"));
  109. }
  110. }
  111. clickEvent(dom, data);
  112. })
  113. //顶部菜单点击
  114. $("body").on("click","#" + _this.option.control + " .pear-nav-control [pear-id]", function () {
  115. var dom = $(this);
  116. var href = $(this).attr("pear-href");
  117. if (href == "javascript:;") {
  118. $("#" + _this.option.elem).find(".pear-nav-tree").css({
  119. display: 'none'
  120. });
  121. $("#" + _this.option.elem).find(".pear-nav-tree[pear-id='" + $(this).attr("pear-id") + "']").css({
  122. display: 'block'
  123. });
  124. //$("#" + _this.option.control).find(".pe-title").html($(this).attr("pear-title"));
  125. $("#" + _this.option.control).find("")
  126. _this.option.change($(this).attr("pear-id"), $(this).attr("pear-title"), $(this).attr("pear-href"))
  127. }else if(href){
  128. var data = {
  129. menuId: dom.attr("pear-id"),
  130. menuTitle: dom.attr("pear-title"),
  131. menuIcon: dom.attr("pear-icon"),
  132. menuUrl: dom.attr("pear-href"),
  133. openType: dom.attr("pear-type")
  134. };
  135. clickEvent(dom, data, false);
  136. }
  137. })
  138. }
  139. function hash(dom) {
  140. var d = dom.parent().parent().prev();
  141. if (d.prop("tagName") === "UL") {
  142. return null;
  143. }
  144. return d;
  145. }
  146. pearMenu.prototype.skin = function (skin) {
  147. var menu = $(".pear-nav-tree[lay-filter='" + this.option.elem + "']").parent();
  148. menu.removeClass("dark-theme");
  149. menu.removeClass("light-theme");
  150. menu.addClass(skin);
  151. }
  152. pearMenu.prototype.selectItem = function (pearId) {
  153. if (this.option.control != false) {
  154. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find("ul").css({
  155. display: "none"
  156. });
  157. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass(
  158. "layui-this");
  159. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").css({
  160. display: "block"
  161. });
  162. var controlId = $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").attr("pear-id");
  163. $("#" + this.option.control).find(".layui-this").removeClass("layui-this");
  164. if (controlId != undefined) {
  165. $("#" + this.option.control).find("[pear-id='" + controlId + "']").addClass("layui-this");
  166. }else{
  167. $("#" + this.option.control).find("[pear-id='" + pearId + "']").addClass("layui-this");
  168. }
  169. }
  170. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-this").removeClass(
  171. "layui-this");
  172. if (!$("#" + this.option.elem).is(".pear-nav-mini")) {
  173. var openEle = null;
  174. var openEleHeight = 0;
  175. $($("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents('.layui-nav-child').get().reverse()).each(function () {
  176. if (!$(this).parent().is('.layui-nav-itemed')) {
  177. if (openEleHeight == 0) {
  178. openEle = $(this);
  179. } else {
  180. $(this).parent().addClass('layui-nav-itemed');
  181. $(this).css({
  182. height: 'auto',
  183. });
  184. }
  185. openEleHeight += $(this).children("dd").length * 48;
  186. }
  187. });
  188. if (this.option.accordion) {
  189. if (openEleHeight > 0) {
  190. var currentDom = openEle.parent().siblings('.layui-nav-itemed').children(".layui-nav-child");
  191. currentDom.animate({
  192. height: "0px"
  193. }, 240, function () {
  194. currentDom.css({
  195. height: "auto"
  196. });
  197. $(this).parent().removeClass("layui-nav-itemed");
  198. $(this).find('.layui-nav-itemed').removeClass("layui-nav-itemed");
  199. });
  200. }
  201. }
  202. if (openEleHeight > 0) {
  203. openEle.parent().addClass("layui-nav-itemed");
  204. openEle.height(0);
  205. openEle.animate({
  206. height: openEleHeight + "px"
  207. }, 240, function () {
  208. $(this).css({ height: 'auto' });
  209. });
  210. }
  211. }
  212. $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parent().addClass("layui-this");
  213. }
  214. var activeMenus;
  215. pearMenu.prototype.collapse = function (time) {
  216. var elem = this.option.elem;
  217. var config = this.option;
  218. if ($("#" + this.option.elem).is(".pear-nav-mini")) {
  219. $.each(activeMenus, function (i, item) {
  220. $("#" + elem + " a[menu-id='" + $(this).attr("menu-id") + "']").parent().addClass("layui-nav-itemed");
  221. })
  222. $("#" + this.option.elem).removeClass("pear-nav-mini");
  223. $("#" + this.option.elem).animate({
  224. width: "220px"
  225. }, 180);
  226. isHoverMenu(false, config);
  227. var that = this;
  228. $("#" + this.option.elem)
  229. .promise()
  230. .done(function () {
  231. if (that.option.control) {
  232. rationalizeHeaderControlWidth(that.option);
  233. }
  234. })
  235. } else {
  236. activeMenus = $("#" + this.option.elem).find(".layui-nav-itemed>a");
  237. $("#" + this.option.elem).find(".layui-nav-itemed").removeClass("layui-nav-itemed");
  238. $("#" + this.option.elem).addClass("pear-nav-mini");
  239. $("#" + this.option.elem).animate({
  240. width: "60px"
  241. }, 400);
  242. var that = this;
  243. $("#" + this.option.elem)
  244. .promise()
  245. .done(function () {
  246. isHoverMenu(true, config);
  247. if (that.option.control) {
  248. rationalizeHeaderControlWidth(that.option);
  249. }
  250. })
  251. }
  252. }
  253. function getData(url) {
  254. var defer = $.Deferred();
  255. var symbol = url.indexOf('?') !== -1 ? '&' : '?';
  256. $.get(url + symbol + "fresh=" + Math.random(), function (result) {
  257. defer.resolve(result)
  258. });
  259. return defer.promise();
  260. }
  261. function postData(url) {
  262. var defer = $.Deferred();
  263. var symbol = url.indexOf('?') !== -1 ? '&' : '?';
  264. $.post(url + symbol + "fresh=" + Math.random(), function (result) {
  265. defer.resolve(result)
  266. }, "json");
  267. return defer.promise();
  268. }
  269. function renderMenu(option) {
  270. if (option.parseData != false) {
  271. option.parseData(option.data);
  272. }
  273. if (option.data.length > 0) {
  274. if (option.control != false) {
  275. createMenuAndControl(option);
  276. } else {
  277. createMenu(option);
  278. }
  279. }
  280. element.init();
  281. option.done();
  282. }
  283. function createMenu(option) {
  284. var menuHtml = '<div style="height:100%!important;" class="pear-side-scroll layui-side-scroll ' + option.theme + '"><ul lay-filter="' + option.elem +
  285. '" class="layui-nav arrow pear-menu layui-nav-tree pear-nav-tree" '+ (option.accordion ? "lay-accordion" : "") +'>'
  286. $.each(option.data, function (i, item) {
  287. var content = '<li class="layui-nav-item" >';
  288. if (i == option.defaultOpen) {
  289. content = '<li class="layui-nav-item layui-nav-itemed" >';
  290. }
  291. var href = "javascript:;";
  292. var target = "";
  293. var className = "site-demo-active"
  294. item.type = item.childlist != null && item.childlist.length > 0 ? 0 : 1;//此处修改为是否子栏目判断
  295. if (item.openType == "_blank" && item.type == 1) {
  296. href = item.href;
  297. target = "target='_blank'";
  298. className = "";
  299. }
  300. if (item.type == 0) {
  301. // 创 建 目 录 结 构
  302. content += '<a ' + item.extend + ' href="javascript:;" menu-type="' + item.type + '" menu-id="' + item.id + '" href="' + href +
  303. '" ' + target + '><i class="' + item.icon + '"></i><span>' + item.title +
  304. '</span></a>';
  305. } else if (item.type == 1) {
  306. content += '<a ' + item.extend + ' class="' + className + '" menu-type="' + item.type + '" menu-url="' + item.href + '" menu-id="' +
  307. item.id +
  308. '" menu-title="' + item.title + '" href="' + href + '" ' + target + '><i class="' + item.icon +
  309. '"></i><span>' + item.title + '</span><div class="pull-right-container">' + item.badge + '</div></a>';
  310. }
  311. // 调 用 递 归 方 法 加 载 无 限 层 级 的 子 菜 单
  312. content += loadchild(item);
  313. // 结 束 一 个 根 菜 单 项
  314. content += '</li>';
  315. menuHtml += content;
  316. });
  317. // 结 束 菜 单 结 构 的 初 始 化
  318. menuHtml += "</ul></div>";
  319. // 将 菜 单 拼 接 到 初 始 化 容 器 中
  320. $("#" + option.elem).html(menuHtml);
  321. }
  322. function createMenuAndControl(option) {
  323. var control = '<div class="control"><ul class="layui-nav pear-nav-control pc layui-hide-xs">';
  324. control+= '<li class="layui-nav-item tabdrop layui-hide" style="float:right !important;"><a href="javascript:;"><i class="layui-icon layui-icon-more layui-font-20"></i></a><dl class="layui-nav-child"></dl></li>';
  325. var controlPe = '<ul class="layui-nav pear-nav-control layui-hide-sm">';
  326. // 声 明 头 部
  327. var menu = '<div class="layui-side-scroll ' + option.theme + '">'
  328. // 开 启 同 步 操 作
  329. var index = 0;
  330. var controlItemPe = '<dl class="layui-nav-child">';
  331. $.each(option.data, function (i, item) {
  332. if(item.childlist != null && item.childlist.length > 0){
  333. item.href = 'javascript:;';
  334. }
  335. var menuItem = '';
  336. var controlItem = '';
  337. var target = "";
  338. var href = "javascript:;";
  339. if (item.openType == "_blank" && item.childlist.length == 0) {
  340. href = item.href;
  341. item.href = '';
  342. target = "target='_blank'";
  343. }
  344. if (i === option.defaultMenu) {
  345. controlItem = '<li pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  346. '" class="layui-this layui-nav-item"><a ' + item.extend + ' href="'+ href +'" ' + target + '>' + item.title + '</a></li>';
  347. menuItem = '<ul pear-id="' + item.id + '" lay-filter="' + option.elem +
  348. '" class="layui-nav arrow layui-nav-tree pear-nav-tree">';
  349. //controlPe += '<li class="layui-nav-item"><a class="pe-title" href="javascript:;" >' + item.title + '</a>';
  350. controlPe += '<li class="layui-nav-item"><a class="pe-title" href="javascript:;" ><i class="icon iconfont icon-list-unordered"></i>&nbsp;选择模块</a>';
  351. controlItemPe += '<dd pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  352. '"><a href="javascript:void(0);">' + item.title + '</a></dd>';
  353. } else {
  354. controlItem = '<li pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  355. '" class="layui-nav-item"><a ' + item.extend + ' href="'+ href +'" ' + target + '>' + item.title + '</a></li>';
  356. menuItem = '<ul style="display:none" pear-id="' + item.id + '" lay-filter="' + option.elem +
  357. '" class="layui-nav arrow layui-nav-tree pear-nav-tree">';
  358. controlItemPe += '<dd pear-href="' + item.href + '" pear-title="' + item.title + '" pear-id="' + item.id +
  359. '"><a href="javascript:void(0);">' + item.title + '</a></dd>';
  360. }
  361. index++;
  362. $.each(item.childlist, function (i, note) {
  363. // 创 建 每 一 个 菜 单 项
  364. var content = '<li class="layui-nav-item" >';
  365. var href = "javascript:;";
  366. var target = "";
  367. var className = "site-demo-active";
  368. note.type = note.childlist != null && note.childlist.length > 0 ? 0 : 1;//此处修改为是否子栏目判断
  369. if (note.openType == "_blank" && note.type == 1) {
  370. href = note.href;
  371. target = "target='_blank'";
  372. className = "";
  373. }
  374. // 判 断 菜 单 类 型 0 是 不可跳转的目录 1 是 可 点 击 跳 转 的 菜 单
  375. if (note.type == 0) {
  376. // 创 建 目 录 结 构
  377. content += '<a href="' + href + '" ' + target + ' menu-type="' + note.type + '" menu-id="' + note.id +
  378. '" ><i class="' + note.icon + '"></i><span>' + note.title +
  379. '</span></a>';
  380. } else if (note.type == 1) {
  381. // 创 建 菜 单 结 构
  382. content += '<a ' + note.extend + ' ' + target + ' class="' + className + '" menu-type="' + note.type + '" menu-url="' + note.href +
  383. '" menu-id="' + note.id +
  384. '" menu-title="' + note.title + '" href="' + href + '"><i class="' + note.icon +
  385. '"></i><span>' + note.title + '</span><div class="pull-right-container">' + note.badge + '</div></a>';
  386. }
  387. content += loadchild(note);
  388. content += '</li>';
  389. menuItem += content;
  390. })
  391. menu += menuItem + '</ul>';
  392. control += controlItem;
  393. })
  394. controlItemPe += "</li></dl></ul>"
  395. controlPe += controlItemPe;
  396. $("#" + option.control).html(control + "</div>");
  397. $("#" + option.control).append(controlPe);
  398. $("#" + option.elem).html(menu);
  399. }
  400. /** 加载子菜单 (递归)*/
  401. function loadchild(obj) {
  402. // 判 单 是 否 是 菜 单, 如 果 是 菜 单 直 接 返 回
  403. obj.type = obj.childlist != null && obj.childlist.length > 0 ? 0 : 1;//此处修改为是否子栏目判断
  404. if (obj.type == 1) {
  405. return "";
  406. }
  407. // 创 建 子 菜 单 结 构
  408. var content = '<dl class="layui-nav-child">';
  409. // 如 果 嵌 套 不 等 于 空
  410. if (obj.childlist != null && obj.childlist.length > 0) {
  411. // 遍 历 子 项 目
  412. $.each(obj.childlist, function (i, note) {
  413. // 创 建 子 项 结 构
  414. content += '<dd>';
  415. var href = "javascript:;";
  416. var target = "";
  417. var className = "site-demo-active";
  418. if (note.openType == "_blank" && note.type == 1) {
  419. href = note.href;
  420. target = "target='_blank'";
  421. className = "";
  422. }
  423. note.type = note.childlist != null && note.childlist.length > 0 ? 0 : 1;//此处修改为是否子栏目判断
  424. // 判 断 子 项 类 型
  425. if (note.type == 0) {
  426. // 创 建 目 录 结 构
  427. content += '<a ' + target + ' href="' + href + '" menu-type="' + note.type + '" menu-id="' + note.id +
  428. '"><i class="' + note.icon + '"></i><span>' + note.title + '</span></a>';
  429. } else if (note.type == 1) {
  430. // 创 建 菜 单 结 构
  431. content += '<a ' + target + ' ' + note.extend + ' class="' + className + '" menu-type="' + note.type + '" menu-url="' + note.href +
  432. '" menu-id="' + note.id + '" menu-title="' + note.title + '" menu-icon="' + note.icon + '" href="' + href +
  433. '" ><i class="' + note.icon + '"></i><span>' + note.title + '</span><div class="pull-right-container">' + note.badge + '</div></a>';
  434. }
  435. // 加 载 子 项 目 录
  436. content += loadchild(note);
  437. // 结 束 当 前 子 菜 单
  438. content += '</dd>';
  439. });
  440. // 封 装
  441. } else {
  442. content += '<dd style="background-color: transparent!important;"><a style="background-color: transparent!important;margin-left: 26px">目录为空</a></dd>';
  443. }
  444. content += '</dl>';
  445. return content;
  446. }
  447. /** 二 级 悬 浮 菜 单*/
  448. function isHoverMenu(b, option) {
  449. if (b) {
  450. var navItem = "#" + option.elem + ".pear-nav-mini .layui-nav-item";
  451. var navChildDl = navItem + " .layui-nav-child>dl";
  452. var navChildDd = navItem + " .layui-nav-child>dd";
  453. $(navItem + "," + navChildDd).mouseenter(function () {
  454. var _this = $(this);
  455. _this.siblings().find(".layui-nav-child")
  456. .removeClass("layui-nav-hover").css({
  457. left: 0,
  458. top: 0
  459. });
  460. _this.children(".layui-nav-child").addClass("layui-nav-hover");
  461. var height = $(window).height();
  462. var topLength = _this.offset().top;
  463. var thisHeight = _this.children(".layui-nav-child").height();
  464. if ((thisHeight + topLength) > height) {
  465. topLength = height - thisHeight - 10;
  466. }
  467. var left = _this.offset().left + 60;
  468. if (!_this.hasClass("layui-nav-item")) {
  469. left = _this.offset().left + _this.width();
  470. }
  471. _this.children(".layui-nav-child").offset({
  472. top: topLength,
  473. left: left
  474. });
  475. });
  476. $(navItem + "," + navChildDl).mouseleave(function () {
  477. var _this = $(this);
  478. _this.closest('.layui-nav-item')
  479. .find(".layui-nav-child")
  480. .removeClass("layui-nav-hover")
  481. .css({
  482. left: 0,
  483. top: 0
  484. });
  485. });
  486. } else {
  487. $("#" + option.elem + " .layui-nav-item").off('mouseenter').unbind('mouseleave');
  488. $("#" + option.elem + " dd").off('mouseenter').unbind('mouseleave');
  489. }
  490. }
  491. function rationalizeHeaderControlWidth(option) {
  492. var $headerControl = $("#" + option.control);
  493. var $nextEl = $headerControl.next();
  494. var rationalizeWidth;
  495. if ($nextEl.length) {
  496. rationalizeWidth = $nextEl.position().left - $headerControl.position().left;
  497. } else {
  498. rationalizeWidth = $headerControl.parent().innerWidth() - $headerControl.position().left;
  499. }
  500. $("#" + option.control + " .control").css({"width": rationalizeWidth});
  501. var navobj = $("#" + option.control+' ul.pear-nav-control.pc');
  502. var dropdown = $(".tabdrop", navobj);
  503. var collection = 0;
  504. var maxwidth = rationalizeWidth - 60;
  505. var liwidth = 0;
  506. //检查超过一行的标签页
  507. $('.tabdrop').find('dd').each(function(){
  508. var newLI = $('<li></li>').html($(this).html());
  509. newLI.addClass('layui-nav-item');
  510. newLI.attr('pear-href', $(this).attr('pear-href'));
  511. newLI.attr('pear-title', $(this).attr('pear-title'));
  512. newLI.attr('pear-id', $(this).attr('pear-id'));
  513. navobj.append(newLI);
  514. $(this).remove();
  515. })
  516. //var litabs = navobj.append(dropdown.find('dd')).find('>li').not('.tabdrop');
  517. var litabs = navobj.find('>li').not('.tabdrop');
  518. var totalwidth = 0;
  519. litabs.each(function () {
  520. totalwidth += $(this).outerWidth(true);
  521. });
  522. if (rationalizeWidth < totalwidth) {
  523. litabs.each(function () {
  524. liwidth += $(this).outerWidth(true);
  525. if (liwidth > maxwidth) {
  526. var newDD = $('<dd></dd>').html($(this).html());
  527. newDD.attr('pear-href', $(this).attr('pear-href'));
  528. newDD.attr('pear-title', $(this).attr('pear-title'));
  529. newDD.attr('pear-id', $(this).attr('pear-id'));
  530. dropdown.find('dl').append(newDD);
  531. collection++;
  532. $(this).remove();
  533. }
  534. });
  535. if (collection > 0) {
  536. dropdown.removeClass('layui-hide');
  537. if (dropdown.find('.active').length === 1) {
  538. dropdown.addClass('active');
  539. } else {
  540. dropdown.removeClass('active');
  541. }
  542. }
  543. }else {
  544. dropdown.addClass('layui-hide');
  545. }
  546. }
  547. function rationalizeHeaderControlWidthAuto(option){
  548. $(window).on('resize', function () {
  549. rationalizeHeaderControlWidth(option);
  550. })
  551. $(document).ready(function () {
  552. rationalizeHeaderControlWidth(option);
  553. });
  554. }
  555. return pearMenu;
  556. })