tab.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /** kit_admin-v1.0.5 MIT License By http://kit/zhengjinfan.cn e-mail:zheng_jinfan@126.com */
  2. ;
  3. layui.define(["jquery", "element", "nprogress"],
  4. function(i) {
  5. var t = layui.jquery,
  6. e = layui.element,
  7. a = t(document),
  8. l = t(window),
  9. n = function() {
  10. this.config = {
  11. elem: void 0,
  12. mainUrl: "/home/start"
  13. },
  14. this.v = "1.0.3"
  15. }; (n.fn = n.prototype).set = function(i) {
  16. var e = this;
  17. return t.extend(!0, e.config, i),
  18. e
  19. },
  20. n.fn.render = function() {
  21. var i = this,
  22. t = i.config;
  23. return void 0 === t.elem ? (layui.hint().error("Tab error:请配置选择卡容器."), i) : (r._config = t, r.createTabDom(), i)
  24. },
  25. n.fn.tabAdd = function(i) {
  26. r.tabAdd(i)
  27. },
  28. n.fn.close = function(i) {
  29. r.tabDelete(i)
  30. },
  31. n.fn.getId = function() {
  32. return r.getCurrLayId()
  33. };
  34. var r = {
  35. _config: {},
  36. _filter: "kitTab",
  37. _title: void 0,
  38. _content: void 0,
  39. _parentElem: void 0,
  40. tabDomExists: function() {
  41. var i = this;
  42. return a.find("div.kit-tab").length > 0 && (i._title = t(".kit-tab ul.layui-tab-title"), i._content = t(".kit-tab div.layui-tab-content"), !0)
  43. },
  44. createTabDom: function() {
  45. var i = this,
  46. e = i._config;
  47. if (i._parentElem = e.elem, !i.tabDomExists()) {
  48. var a = ['<div class="layui-tab layui-tab-card kit-tab" lay-filter="' + i._filter + '">', '<ul class="layui-tab-title">', '<li class="layui-this" lay-id="-1"><i class="layui-icon">&#xe68e;</i> 控制面板</li>', "</ul>", '<div class="kit-tab-tool kit-tab-bg"><i class="fa fa-ellipsis-v"></i></div>', '<div class="kit-tab-tool-body layui-anim layui-anim-upbit">', "<ul>", '<li class="kit-item" data-target="closeCurrent">关闭当前选项卡</li>', '<li class="kit-item" data-target="closeOther">关闭其他选项卡</li>', '<li class="kit-line"></li>', '<li class="kit-item" data-target="closeAll">关闭所有选项卡</li>', "</ul>", "</div>", '<div class="layui-tab-content">', '<div class="layui-tab-item layui-show" lay-item-id="-1"><iframe src="' + e.mainUrl + '"></iframe></div>', "</div>", "</div>"];
  49. t(e.elem).html(a.join("")),
  50. i._title = t(".kit-tab ul.layui-tab-title"),
  51. i._content = t(".kit-tab div.layui-tab-content");
  52. var l = t(".kit-tab-tool"),
  53. n = t(".kit-tab-tool-body");
  54. l.on("click",
  55. function() {
  56. n.toggle()
  57. }),
  58. n.find("li.kit-item").each(function() {
  59. var e = t(this),
  60. a = e.data("target");
  61. e.off("click").on("click",
  62. function() {
  63. var e = i._title.children("li[class=layui-this]").attr("lay-id");
  64. switch (a) {
  65. case "refresh":
  66. var n = i._content.children("div[lay-item-id=" + e + "]").children("iframe");
  67. n.attr("src", n.attr("src"));
  68. break;
  69. case "closeCurrent":
  70. -1 != e && i.tabDelete(e);
  71. break;
  72. case "closeOther":
  73. i._title.children("li[lay-id]").each(function() {
  74. var a = t(this).attr("lay-id");
  75. a != e && -1 != a && i.tabDelete(a)
  76. });
  77. break;
  78. case "closeAll":
  79. i._title.children("li[lay-id]").each(function() {
  80. var e = t(this).attr("lay-id"); - 1 != e && i.tabDelete(e)
  81. })
  82. }
  83. l.click()
  84. })
  85. }),
  86. i.winResize()
  87. }
  88. },
  89. winResize: function() {
  90. var i = this;
  91. l.on("resize",
  92. function() {
  93. var e = t(i._parentElem).height();
  94. t(".kit-tab .layui-tab-content iframe").height(e - 45)
  95. }).resize()
  96. },
  97. tabExists: function(i) {
  98. return this._title.find("li[lay-id=" + i + "]").length > 0
  99. },
  100. tabDelete: function(i) {
  101. e.tabDelete(this._filter, i)
  102. },
  103. tabChange: function(i) {
  104. e.tabChange(this._filter, i)
  105. },
  106. getTab: function(i) {
  107. return this._title.find("li[lay-id=" + i + "]")
  108. },
  109. tabAdd: function(i) {
  110. var t = this,
  111. a = t._config,
  112. l = (i = i || {
  113. id: (new Date).getTime(),
  114. title: "新标签页",
  115. icon: "fa-file",
  116. url: "404.html"
  117. }).title,
  118. n = i.icon,
  119. r = i.url,
  120. c = i.id;
  121. if (t.tabExists(c)) t.tabChange(c);
  122. else {
  123. NProgress.start();
  124. var s = ['<li class="layui-this" lay-id="' + c + '" >']; - 1 !== n.indexOf("fa-") ? s.push('<i class="fa ' + n + '" aria-hidden="true"></i>') : s.push('<i class="layui-icon">' + n + "</i>"),
  125. s.push("&nbsp;" + l),
  126. s.push('<i class="layui-icon layui-unselect layui-tab-close">&#x1006;</i>'),
  127. s.push("</li>");
  128. var o = '<div class="layui-tab-item layui-show" lay-item-id="' + c + '"><iframe src="' + r + '"></iframe></div>';
  129. t._title.append(s.join("")),
  130. t._content.append(o),
  131. t.getTab(c).find("i.layui-tab-close").off("click").on("click",
  132. function() {
  133. a.closeBefore ? a.closeBefore(i) && t.tabDelete(c) : t.tabDelete(c)
  134. }),
  135. t.tabChange(c),
  136. t.winResize(),
  137. t._content.find("div[lay-item-id=" + c + "]").find("iframe").on("load",
  138. function() {
  139. NProgress.done()
  140. }),
  141. a.onSwitch && e.on("tab(" + t._filter + ")",
  142. function(i) {
  143. a.onSwitch({
  144. index: i.index,
  145. elem: i.elem,
  146. layId: t._title.children("li").eq(i.index).attr("lay-id")
  147. })
  148. })
  149. }
  150. },
  151. getCurrLayId: function() {
  152. return this._title.find("li.layui-this").attr("lay-id")
  153. }
  154. };
  155. i("tab", new n)
  156. });