index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. define(['jquery', 'bootstrap', 'backend', 'addtabs', 'adminlte', 'form'], function ($, undefined, Backend, undefined, AdminLTE, Form) {
  2. var Controller = {
  3. index: function () {
  4. //双击重新加载页面
  5. $(document).on("dblclick", ".sidebar-menu li > a", function (e) {
  6. $("#con_" + $(this).attr("addtabs") + " iframe").attr('src', function (i, val) {
  7. return val;
  8. });
  9. e.stopPropagation();
  10. });
  11. //修复在移除窗口时下拉框不隐藏的BUG
  12. $(window).on("blur", function () {
  13. $("[data-toggle='dropdown']").parent().removeClass("open");
  14. if ($("body").hasClass("sidebar-open")) {
  15. $(".sidebar-toggle").trigger("click");
  16. }
  17. });
  18. //快捷搜索
  19. $(".menuresult").width($("form.sidebar-form > .input-group").width());
  20. var searchResult = $(".menuresult");
  21. $("form.sidebar-form").on("blur", "input[name=q]", function () {
  22. searchResult.addClass("hide");
  23. }).on("focus", "input[name=q]", function () {
  24. if ($("a", searchResult).size() > 0) {
  25. searchResult.removeClass("hide");
  26. }
  27. }).on("keyup", "input[name=q]", function () {
  28. searchResult.html('');
  29. var val = $(this).val();
  30. var html = [];
  31. if (val != '') {
  32. $("ul.sidebar-menu li a[addtabs]:not([href^='javascript:;'])").each(function () {
  33. if ($("span:first", this).text().indexOf(val) > -1 || $(this).attr("py").indexOf(val) > -1 || $(this).attr("pinyin").indexOf(val) > -1) {
  34. html.push('<a data-url="' + $(this).attr("href") + '" href="javascript:;">' + $("span:first", this).text() + '</a>');
  35. if (html.length >= 100) {
  36. return false;
  37. }
  38. }
  39. });
  40. }
  41. $(searchResult).append(html.join(""));
  42. if (html.length > 0) {
  43. searchResult.removeClass("hide");
  44. } else {
  45. searchResult.addClass("hide");
  46. }
  47. });
  48. //快捷搜索点击事件
  49. $("form.sidebar-form").on('mousedown click', '.menuresult a[data-url]', function () {
  50. Backend.api.addtabs($(this).data("url"));
  51. });
  52. //切换左侧sidebar显示隐藏
  53. $(document).on("click fa.event.toggleitem", ".sidebar-menu li > a", function (e) {
  54. $(".sidebar-menu li").removeClass("active");
  55. //当外部触发隐藏的a时,触发父辈a的事件
  56. if (!$(this).closest("ul").is(":visible")) {
  57. //如果不需要左侧的菜单栏联动可以注释下面一行即可
  58. $(this).closest("ul").prev().trigger("click");
  59. }
  60. var visible = $(this).next("ul").is(":visible");
  61. if (!visible) {
  62. $(this).parents("li").addClass("active");
  63. } else {
  64. }
  65. e.stopPropagation();
  66. });
  67. //清除缓存
  68. $(document).on('click', "ul.wipecache li a", function () {
  69. $.ajax({
  70. url: 'ajax/wipecache',
  71. dataType: 'json',
  72. data: {type: $(this).data("type")},
  73. cache: false,
  74. success: function (ret) {
  75. if (ret.hasOwnProperty("code")) {
  76. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  77. if (ret.code === 1) {
  78. Toastr.success(msg ? msg : __('Wipe cache completed'));
  79. } else {
  80. Toastr.error(msg ? msg : __('Wipe cache failed'));
  81. }
  82. } else {
  83. Toastr.error(__('Unknown data format'));
  84. }
  85. }, error: function () {
  86. Toastr.error(__('Network error'));
  87. }
  88. });
  89. });
  90. //全屏事件
  91. $(document).on('click', "[data-toggle='fullscreen']", function () {
  92. var doc = document.documentElement;
  93. if ($(document.body).hasClass("full-screen")) {
  94. $(document.body).removeClass("full-screen");
  95. document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen();
  96. } else {
  97. $(document.body).addClass("full-screen");
  98. doc.requestFullscreen ? doc.requestFullscreen() : doc.mozRequestFullScreen ? doc.mozRequestFullScreen() : doc.webkitRequestFullscreen ? doc.webkitRequestFullscreen() : doc.msRequestFullscreen && doc.msRequestFullscreen();
  99. }
  100. });
  101. var multiplenav = $("#secondnav").size() > 0 ? true : false;
  102. var firstnav = $("#firstnav .nav-addtabs");
  103. var nav = multiplenav ? $("#secondnav .nav-addtabs") : firstnav;
  104. //刷新菜单事件
  105. $(document).on('refresh', '.sidebar-menu', function () {
  106. Fast.api.ajax({
  107. url: 'index/index',
  108. data: {action: 'refreshmenu'}
  109. }, function (data) {
  110. $(".sidebar-menu li:not([data-rel='external'])").remove();
  111. $(".sidebar-menu").prepend(data.menulist);
  112. if (multiplenav) {
  113. firstnav.html(data.navlist);
  114. }
  115. $("li[role='presentation'].active a", nav).trigger('click');
  116. return false;
  117. }, function () {
  118. return false;
  119. });
  120. });
  121. if (multiplenav) {
  122. //一级菜单自适应
  123. $(window).resize(function () {
  124. var siblingsWidth = 0;
  125. firstnav.siblings().each(function () {
  126. siblingsWidth += $(this).outerWidth();
  127. });
  128. firstnav.width(firstnav.parent().width() - siblingsWidth);
  129. firstnav.refreshAddtabs();
  130. });
  131. //点击顶部第一级菜单栏
  132. firstnav.on("click", "li a", function () {
  133. $("li", firstnav).removeClass("active");
  134. $(this).closest("li").addClass("active");
  135. $(".sidebar-menu > li.treeview").addClass("hidden");
  136. if ($(this).attr("url") == "javascript:;") {
  137. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  138. sonlist.removeClass("hidden");
  139. var sidenav;
  140. var last_id = $(this).attr("last-id");
  141. if (last_id) {
  142. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "'] a[addtabs='" + last_id + "']");
  143. } else {
  144. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']:first > a");
  145. }
  146. if (sidenav) {
  147. sidenav.attr("href") != "javascript:;" && sidenav.trigger('click');
  148. }
  149. } else {
  150. }
  151. });
  152. //点击左侧菜单栏
  153. $(document).on('click', '.sidebar-menu li a[addtabs]', function (e) {
  154. var parents = $(this).parentsUntil("ul.sidebar-menu", "li");
  155. var top = parents[parents.length - 1];
  156. var pid = $(top).attr("pid");
  157. if (pid) {
  158. var obj = $("li a[addtabs=" + pid + "]", firstnav);
  159. var last_id = obj.attr("last-id");
  160. if (!last_id || last_id != pid) {
  161. obj.attr("last-id", $(this).attr("addtabs"));
  162. if (!obj.closest("li").hasClass("active")) {
  163. obj.trigger("click");
  164. }
  165. }
  166. }
  167. });
  168. var mobilenav = $(".mobilenav");
  169. $("#firstnav .nav-addtabs li a").each(function () {
  170. mobilenav.append($(this).clone().addClass("btn btn-app"));
  171. });
  172. //点击移动端一级菜单
  173. mobilenav.on("click", "a", function () {
  174. $("a", mobilenav).removeClass("active");
  175. $(this).addClass("active");
  176. $(".sidebar-menu > li.treeview").addClass("hidden");
  177. if ($(this).attr("url") == "javascript:;") {
  178. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  179. sonlist.removeClass("hidden");
  180. }
  181. });
  182. }
  183. //这一行需要放在点击左侧链接事件之前
  184. var addtabs = Config.referer ? localStorage.getItem("addtabs") : null;
  185. //绑定tabs事件,如果需要点击强制刷新iframe,则请将iframeForceRefresh置为true,iframeForceRefreshTable只强制刷新表格
  186. nav.addtabs({iframeHeight: "100%", iframeForceRefresh: false, iframeForceRefreshTable: true, nav: nav});
  187. if ($("ul.sidebar-menu li.active a").size() > 0) {
  188. $("ul.sidebar-menu li.active a").trigger("click");
  189. } else {
  190. if (multiplenav) {
  191. $("li:first > a", firstnav).trigger("click");
  192. } else {
  193. $("ul.sidebar-menu li a[url!='javascript:;']:first").trigger("click");
  194. }
  195. }
  196. //如果是刷新操作则直接返回刷新前的页面
  197. if (Config.referer) {
  198. if (Config.referer === $(addtabs).attr("url")) {
  199. var active = $("ul.sidebar-menu li a[addtabs=" + $(addtabs).attr("addtabs") + "]");
  200. if (multiplenav && active.size() == 0) {
  201. active = $("ul li a[addtabs='" + $(addtabs).attr("addtabs") + "']");
  202. }
  203. if (active.size() > 0) {
  204. active.trigger("click");
  205. } else {
  206. $(addtabs).appendTo(document.body).addClass("hide").trigger("click");
  207. }
  208. } else {
  209. //刷新页面后跳到到刷新前的页面
  210. Backend.api.addtabs(Config.referer);
  211. }
  212. }
  213. var my_skins = [
  214. "skin-blue",
  215. "skin-white",
  216. "skin-red",
  217. "skin-yellow",
  218. "skin-purple",
  219. "skin-green",
  220. "skin-blue-light",
  221. "skin-white-light",
  222. "skin-red-light",
  223. "skin-yellow-light",
  224. "skin-purple-light",
  225. "skin-green-light"
  226. ];
  227. setup();
  228. function change_layout(cls) {
  229. $("body").toggleClass(cls);
  230. AdminLTE.layout.fixSidebar();
  231. //Fix the problem with right sidebar and layout boxed
  232. if (cls == "layout-boxed")
  233. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  234. if ($('body').hasClass('fixed') && cls == 'fixed') {
  235. AdminLTE.pushMenu.expandOnHover();
  236. AdminLTE.layout.activate();
  237. }
  238. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  239. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  240. }
  241. function change_skin(cls) {
  242. if (!$("body").hasClass(cls)) {
  243. $("body").removeClass(my_skins.join(' ')).addClass(cls);
  244. localStorage.setItem('skin', cls);
  245. var cssfile = Config.site.cdnurl + "/assets/css/skins/" + cls + ".css";
  246. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  247. }
  248. return false;
  249. }
  250. function setup() {
  251. var tmp = localStorage.getItem('skin');
  252. if (tmp && $.inArray(tmp, my_skins) != -1)
  253. change_skin(tmp);
  254. // 皮肤切换
  255. $("[data-skin]").on('click', function (e) {
  256. if ($(this).hasClass('knob'))
  257. return;
  258. e.preventDefault();
  259. change_skin($(this).data('skin'));
  260. });
  261. // 布局切换
  262. $("[data-layout]").on('click', function () {
  263. change_layout($(this).data('layout'));
  264. });
  265. // 切换子菜单显示和菜单小图标的显示
  266. $("[data-menu]").on('click', function () {
  267. if ($(this).data("menu") == 'show-submenu') {
  268. $("ul.sidebar-menu").toggleClass("show-submenu");
  269. } else {
  270. nav.toggleClass("disable-top-badge");
  271. }
  272. });
  273. // 右侧控制栏切换
  274. $("[data-controlsidebar]").on('click', function () {
  275. change_layout($(this).data('controlsidebar'));
  276. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  277. AdminLTE.options.controlSidebarOptions.slide = slide;
  278. if (!slide)
  279. $('.control-sidebar').removeClass('control-sidebar-open');
  280. });
  281. // 右侧控制栏背景切换
  282. $("[data-sidebarskin='toggle']").on('click', function () {
  283. var sidebar = $(".control-sidebar");
  284. if (sidebar.hasClass("control-sidebar-dark")) {
  285. sidebar.removeClass("control-sidebar-dark")
  286. sidebar.addClass("control-sidebar-light")
  287. } else {
  288. sidebar.removeClass("control-sidebar-light")
  289. sidebar.addClass("control-sidebar-dark")
  290. }
  291. });
  292. // 菜单栏展开或收起
  293. $("[data-enable='expandOnHover']").on('click', function () {
  294. $(this).attr('disabled', true);
  295. AdminLTE.pushMenu.expandOnHover();
  296. if (!$('body').hasClass('sidebar-collapse'))
  297. $("[data-layout='sidebar-collapse']").click();
  298. });
  299. // 重设选项
  300. if ($('body').hasClass('fixed')) {
  301. $("[data-layout='fixed']").attr('checked', 'checked');
  302. }
  303. if ($('body').hasClass('layout-boxed')) {
  304. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  305. }
  306. if ($('body').hasClass('sidebar-collapse')) {
  307. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  308. }
  309. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  310. $("[data-menu='show-submenu']").attr('checked', 'checked');
  311. }
  312. if (nav.hasClass('disable-top-badge')) {
  313. $("[data-menu='disable-top-badge']").attr('checked', 'checked');
  314. }
  315. }
  316. $(window).resize();
  317. },
  318. login: function () {
  319. var lastlogin = localStorage.getItem("lastlogin");
  320. if (lastlogin) {
  321. lastlogin = JSON.parse(lastlogin);
  322. $("#profile-img").attr("src", Backend.api.cdnurl(lastlogin.avatar));
  323. $("#profile-name").val(lastlogin.username);
  324. }
  325. //让错误提示框居中
  326. Fast.config.toastr.positionClass = "toast-top-center";
  327. //本地验证未通过时提示
  328. $("#login-form").data("validator-options", {
  329. invalid: function (form, errors) {
  330. $.each(errors, function (i, j) {
  331. Toastr.error(j);
  332. });
  333. },
  334. target: '#errtips'
  335. });
  336. //为表单绑定事件
  337. Form.api.bindevent($("#login-form"), function (data) {
  338. localStorage.setItem("lastlogin", JSON.stringify({
  339. id: data.id,
  340. username: data.username,
  341. avatar: data.avatar
  342. }));
  343. location.href = Backend.api.fixurl(data.url);
  344. }, function (data) {
  345. $("input[name=captcha]").next(".input-group-addon").find("img").trigger("click");
  346. });
  347. }
  348. };
  349. return Controller;
  350. });