navbar.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /** kit_admin-v1.0.5 MIT License By http://kit/zhengjinfan.cn e-mail:zheng_jinfan@126.com */
  2. ;
  3. layui.define(["layer", "laytpl", "element"],
  4. function(i) {
  5. var e = layui.jquery,
  6. a = layui.layer,
  7. t = (e(window), e(document)),
  8. n = layui.laytpl,
  9. l = layui.element;
  10. i("navbar", {
  11. v: "1.0.2",
  12. config: {
  13. data: void 0,
  14. remote: {
  15. url: void 0,
  16. type: "GET",
  17. jsonp: !1
  18. },
  19. cached: !1,
  20. elem: void 0,
  21. filter: "kitNavbar"
  22. },
  23. set: function(i) {
  24. var a = this;
  25. return a.config.data = void 0,
  26. e.extend(!0, a.config, i),
  27. a
  28. },
  29. hasElem: function() {
  30. var i = this.config;
  31. return void 0 !== i.elem || 0 !== t.find("ul[kit-navbar]").length || !e(i.elem) || (layui.hint().error("Navbar error:请配置Navbar容器."), !1)
  32. },
  33. getElem: function() {
  34. var i = this.config;
  35. return void 0 !== i.elem && e(i.elem).length > 0 ? e(i.elem) : t.find("ul[kit-navbar]")
  36. },
  37. bind: function(i) {
  38. var n = this;
  39. n.config;
  40. return n.hasElem() ? (n.getElem().find("a[kit-target]").each(function() {
  41. var t = e(this),
  42. n = void 0;
  43. t.hover(function() {
  44. n = a.tips(e(this).children("span").text(), this)
  45. },
  46. function() {
  47. n && a.close(n)
  48. }),
  49. t.off("click").on("click",
  50. function() {
  51. var e, a = t.data("options");
  52. if (void 0 !== a) try {
  53. e = new Function("return " + a)()
  54. } catch(i) {
  55. layui.hint().error("Navbar 组件a[data-options]配置项存在语法错误:" + a)
  56. } else e = {
  57. icon: t.data("icon"),
  58. id: t.data("id"),
  59. title: t.data("title"),
  60. url: t.data("url")
  61. };
  62. "function" == typeof i && i(e)
  63. })
  64. }), e(".kit-side-fold").off("click").on("click",
  65. function() {
  66. var i = t.find("div.kit-side");
  67. i.hasClass("kit-sided") ? (i.removeClass("kit-sided"), t.find("div.layui-body").removeClass("kit-body-folded"), t.find("div.layui-footer").removeClass("kit-footer-folded")) : (i.addClass("kit-sided"), t.find("div.layui-body").addClass("kit-body-folded"), t.find("div.layui-footer").addClass("kit-footer-folded"))
  68. }), n) : n
  69. },
  70. render: function(i) {
  71. var t = this,
  72. d = t.config,
  73. o = d.remote,
  74. r = ["{{# layui.each(d,function(index, item){ }}", "{{# if(item.spread){ }}", '<li class="layui-nav-item layui-nav-itemed">', "{{# }else{ }}", '<li class="layui-nav-item">', "{{# } }}", "{{# var hasChildren = item.children!==undefined && item.children.length>0; }}", "{{# if(hasChildren){ }}", '<a href="javascript:;">', '{{# if (item.icon.indexOf("fa-") !== -1) { }}', '<i class="fa {{item.icon}}" aria-hidden="true"></i>', "{{# } else { }}", '<i class="layui-icon">{{item.icon}}</i>', "{{# } }}", "<span> {{item.title}}</span>", "</a>", "{{# var children = item.children; }}", '<dl class="layui-nav-child">', "{{# layui.each(children,function(childIndex, child){ }}", "<dd>", "<a href=\"javascript:;\" kit-target data-options=\"{url:'{{child.url}}',icon:'{{child.icon}}',title:'{{child.title}}',id:'{{child.id}}'}\">", '{{# if (child.icon.indexOf("fa-") !== -1) { }}', '<i class="fa {{child.icon}}" aria-hidden="true"></i>', "{{# } else { }}", '<i class="layui-icon">{{child.icon}}</i>', "{{# } }}", "<span> {{child.title}}</span>", "</a>", "</dd>", "{{# }); }}", "</dl>", "{{# }else{ }}", "<a href=\"javascript:;\" kit-target data-options=\"{url:'{{item.url}}',icon:'{{item.icon}}',title:'{{item.title}}',id:'{{item.id}}'}\">", '{{# if (item.icon.indexOf("fa-") !== -1) { }}', '<i class="fa {{item.icon}}" aria-hidden="true"></i>', "{{# } else { }}", '<i class="layui-icon">{{item.icon}}</i>', "{{# } }}", "<span> {{item.title}}</span>", "</a>", "{{# } }}", "</li>", "{{# }); }}"],
  75. c = [],
  76. s = a.load(2);
  77. if (!t.hasElem()) return t;
  78. var f = t.getElem();
  79. if (void 0 !== d.data && d.data.length > 0) c = d.data;
  80. else {
  81. o.jsonp;
  82. var u = {
  83. url: o.url,
  84. type: o.type,
  85. error: function(i, e, a) {
  86. layui.hint().error("Navbar error:AJAX请求出错." + a)
  87. },
  88. success: function(i) {
  89. c = i
  90. }
  91. };
  92. e.extend(!0, u, o.jsonp ? {
  93. dataType: "jsonp",
  94. jsonp: "callback",
  95. jsonpCallback: "jsonpCallback"
  96. }: {
  97. dataType: "json"
  98. }),
  99. e.support.cors = !0,
  100. e.ajax(u)
  101. }
  102. var h = setInterval(function() {
  103. c.length > 0 && (clearInterval(h), n(r.join("")).render(c,
  104. function(e) {
  105. f.html(e),
  106. l.init(),
  107. t.bind(function(e) {
  108. "function" == typeof i && i(e)
  109. }),
  110. s && a.close(s)
  111. }))
  112. },
  113. 50);
  114. return t
  115. }
  116. })
  117. });