index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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).length > 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. var nextul = $(this).next("ul");
  55. if (nextul.length == 0 && (!$(this).parent("li").hasClass("treeview") || ($("body").hasClass("multiplenav") && $(this).parent().parent().hasClass("sidebar-menu")))) {
  56. $(".sidebar-menu li").not($(this).parents("li")).removeClass("active");
  57. }
  58. //当外部触发隐藏的a时,触发父辈a的事件
  59. if (!$(this).closest("ul").is(":visible")) {
  60. //如果不需要左侧的菜单栏联动可以注释下面一行即可
  61. $(this).closest("ul").prev().trigger("click");
  62. }
  63. var visible = nextul.is(":visible");
  64. if (nextul.length == 0) {
  65. $(this).parents("li").addClass("active");
  66. } else {
  67. }
  68. e.stopPropagation();
  69. });
  70. //清除缓存
  71. $(document).on('click', "ul.wipecache li a,a.wipecache", function () {
  72. $.ajax({
  73. url: 'ajax/wipecache',
  74. dataType: 'json',
  75. data: {type: $(this).data("type")},
  76. cache: false,
  77. success: function (ret) {
  78. if (ret.hasOwnProperty("code")) {
  79. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  80. if (ret.code === 1) {
  81. Toastr.success(msg ? msg : __('Wipe cache completed'));
  82. } else {
  83. Toastr.error(msg ? msg : __('Wipe cache failed'));
  84. }
  85. } else {
  86. Toastr.error(__('Unknown data format'));
  87. }
  88. }, error: function () {
  89. Toastr.error(__('Network error'));
  90. }
  91. });
  92. });
  93. //全屏事件
  94. $(document).on('click', "[data-toggle='fullscreen']", function () {
  95. var doc = document.documentElement;
  96. if ($(document.body).hasClass("full-screen")) {
  97. $(document.body).removeClass("full-screen");
  98. document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen();
  99. } else {
  100. $(document.body).addClass("full-screen");
  101. doc.requestFullscreen ? doc.requestFullscreen() : doc.mozRequestFullScreen ? doc.mozRequestFullScreen() : doc.webkitRequestFullscreen ? doc.webkitRequestFullscreen() : doc.msRequestFullscreen && doc.msRequestFullscreen();
  102. }
  103. });
  104. var multiplenav = $("body").hasClass("multiplenav") > 0 ? true : false;
  105. var firstnav = $("#firstnav .nav-addtabs");
  106. var nav = multiplenav ? $("#secondnav .nav-addtabs") : firstnav;
  107. //刷新菜单事件
  108. $(document).on('refresh', '.sidebar-menu', function () {
  109. Fast.api.ajax({
  110. url: 'index/index',
  111. data: {action: 'refreshmenu'},
  112. loading: false
  113. }, function (data) {
  114. $(".sidebar-menu li:not([data-rel='external'])").remove();
  115. $(".sidebar-menu").prepend(data.menulist);
  116. if (multiplenav) {
  117. firstnav.html(data.navlist);
  118. }
  119. $("li[role='presentation'].active a", nav).trigger('click');
  120. $(window).trigger("resize");
  121. return false;
  122. }, function () {
  123. return false;
  124. });
  125. });
  126. if (multiplenav) {
  127. firstnav.css("overflow", "inherit");
  128. //一级菜单自适应
  129. $(window).resize(function () {
  130. var siblingsWidth = 0;
  131. firstnav.siblings().each(function () {
  132. siblingsWidth += $(this).outerWidth();
  133. });
  134. firstnav.width(firstnav.parent().width() - siblingsWidth);
  135. firstnav.refreshAddtabs();
  136. });
  137. //点击顶部第一级菜单栏
  138. firstnav.on("click", "li a", function () {
  139. $("li", firstnav).removeClass("active");
  140. $(this).closest("li").addClass("active");
  141. $(".sidebar-menu > li[pid]").addClass("hidden");
  142. if ($(this).attr("url") == "javascript:;") {
  143. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  144. sonlist.removeClass("hidden");
  145. var sidenav;
  146. var last_id = $(this).attr("last-id");
  147. if (last_id) {
  148. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "'] a[addtabs='" + last_id + "']");
  149. } else {
  150. sidenav = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']:first > a");
  151. }
  152. if (sidenav) {
  153. sidenav.attr("href") != "javascript:;" && sidenav.trigger('click');
  154. }
  155. } else {
  156. }
  157. });
  158. var mobilenav = $(".mobilenav");
  159. $("#firstnav .nav-addtabs li a").each(function () {
  160. mobilenav.append($(this).clone().addClass("btn btn-app"));
  161. });
  162. //点击移动端一级菜单
  163. mobilenav.on("click", "a", function () {
  164. $("a", mobilenav).removeClass("active");
  165. $(this).addClass("active");
  166. $(".sidebar-menu > li[pid]").addClass("hidden");
  167. if ($(this).attr("url") == "javascript:;") {
  168. var sonlist = $(".sidebar-menu > li[pid='" + $(this).attr("addtabs") + "']");
  169. sonlist.removeClass("hidden");
  170. }
  171. });
  172. //点击左侧菜单栏
  173. $(document).on('click', '.sidebar-menu li a[addtabs]', function (e) {
  174. var parents = $(this).parentsUntil("ul.sidebar-menu", "li");
  175. var top = parents[parents.length - 1];
  176. var pid = $(top).attr("pid");
  177. if (pid) {
  178. var obj = $("li a[addtabs=" + pid + "]", firstnav);
  179. var last_id = obj.attr("last-id");
  180. if (!last_id || last_id != pid) {
  181. obj.attr("last-id", $(this).attr("addtabs"));
  182. if (!obj.closest("li").hasClass("active")) {
  183. obj.trigger("click");
  184. }
  185. }
  186. mobilenav.find("a").removeClass("active");
  187. mobilenav.find("a[addtabs='" + pid + "']").addClass("active");
  188. }
  189. });
  190. }
  191. //这一行需要放在点击左侧链接事件之前
  192. var addtabs = Config.referer ? localStorage.getItem("addtabs") : null;
  193. //绑定tabs事件,如果需要点击强制刷新iframe,则请将iframeForceRefresh置为true,iframeForceRefreshTable只强制刷新表格
  194. nav.addtabs({iframeHeight: "100%", iframeForceRefresh: false, iframeForceRefreshTable: true, nav: nav});
  195. if ($("ul.sidebar-menu li.active a").length > 0) {
  196. $("ul.sidebar-menu li.active a").trigger("click");
  197. } else {
  198. if (multiplenav) {
  199. $("li:first > a", firstnav).trigger("click");
  200. } else {
  201. $("ul.sidebar-menu li a[url!='javascript:;']:first").trigger("click");
  202. }
  203. }
  204. //如果是刷新操作则直接返回刷新前的页面
  205. if (Config.referer) {
  206. if (Config.referer === $(addtabs).attr("url")) {
  207. var active = $("ul.sidebar-menu li a[addtabs=" + $(addtabs).attr("addtabs") + "]");
  208. if (multiplenav && active.length == 0) {
  209. active = $("ul li a[addtabs='" + $(addtabs).attr("addtabs") + "']");
  210. }
  211. if (active.length > 0) {
  212. active.trigger("click");
  213. } else {
  214. $(addtabs).appendTo(document.body).addClass("hide").trigger("click");
  215. }
  216. } else {
  217. //刷新页面后跳到到刷新前的页面
  218. Backend.api.addtabs(Config.referer);
  219. }
  220. }
  221. var createCookie = function (name, value) {
  222. var date = new Date();
  223. date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  224. var path = Config.moduleurl;
  225. document.cookie = encodeURIComponent(Config.cookie.prefix + name) + "=" + encodeURIComponent(value) + "; path=" + path + "; expires=" + date.toGMTString();
  226. };
  227. var my_skins = [
  228. "skin-blue",
  229. "skin-black",
  230. "skin-red",
  231. "skin-yellow",
  232. "skin-purple",
  233. "skin-green",
  234. "skin-blue-light",
  235. "skin-black-light",
  236. "skin-red-light",
  237. "skin-yellow-light",
  238. "skin-purple-light",
  239. "skin-green-light",
  240. "skin-black-blue",
  241. "skin-black-purple",
  242. "skin-black-red",
  243. "skin-black-green",
  244. "skin-black-yellow",
  245. "skin-black-pink",
  246. ];
  247. // 皮肤切换
  248. $("[data-skin]").on('click', function (e) {
  249. var skin = $(this).data('skin');
  250. if (!$("body").hasClass(skin)) {
  251. $("body").removeClass(my_skins.join(' ')).addClass(skin);
  252. var cssfile = Config.site.cdnurl + "/assets/css/skins/" + skin + ".css";
  253. $('head').append('<link rel="stylesheet" href="' + cssfile + '" type="text/css" />');
  254. $(".skin-list li.active").removeClass("active");
  255. $(".skin-list li a[data-skin='" + skin + "']").parent().addClass("active");
  256. createCookie('adminskin', skin);
  257. }
  258. return false;
  259. });
  260. // 收起菜单栏切换
  261. $("[data-layout='sidebar-collapse']").on('click', function () {
  262. $(".sidebar-toggle").trigger("click");
  263. });
  264. // 切换子菜单显示和菜单小图标的显示
  265. $("[data-menu='show-submenu']").on('click', function () {
  266. createCookie('show_submenu', $(this).prop("checked") ? 1 : 0);
  267. location.reload();
  268. });
  269. // 右侧控制栏切换
  270. $("[data-controlsidebar]").on('click', function () {
  271. var cls = $(this).data('controlsidebar');
  272. $("body").toggleClass(cls);
  273. AdminLTE.layout.fixSidebar();
  274. //Fix the problem with right sidebar and layout boxed
  275. if (cls == "layout-boxed")
  276. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  277. if ($('body').hasClass('fixed') && cls == 'fixed') {
  278. AdminLTE.pushMenu.expandOnHover();
  279. AdminLTE.layout.activate();
  280. }
  281. AdminLTE.controlSidebar._fix($(".control-sidebar-bg"));
  282. AdminLTE.controlSidebar._fix($(".control-sidebar"));
  283. var slide = !AdminLTE.options.controlSidebarOptions.slide;
  284. AdminLTE.options.controlSidebarOptions.slide = slide;
  285. if (!slide)
  286. $('.control-sidebar').removeClass('control-sidebar-open');
  287. });
  288. // 右侧控制栏背景切换
  289. $("[data-sidebarskin='toggle']").on('click', function () {
  290. var sidebar = $(".control-sidebar");
  291. if (sidebar.hasClass("control-sidebar-dark")) {
  292. sidebar.removeClass("control-sidebar-dark")
  293. sidebar.addClass("control-sidebar-light")
  294. } else {
  295. sidebar.removeClass("control-sidebar-light")
  296. sidebar.addClass("control-sidebar-dark")
  297. }
  298. });
  299. // 菜单栏展开或收起
  300. $("[data-enable='expandOnHover']").on('click', function () {
  301. $.AdminLTE.options.sidebarExpandOnHover = $(this).prop("checked") ? 1 : 0;
  302. localStorage.setItem('sidebarExpandOnHover', $.AdminLTE.options.sidebarExpandOnHover);
  303. AdminLTE.pushMenu.expandOnHover();
  304. $.AdminLTE.layout.fixSidebar();
  305. });
  306. // 切换菜单栏
  307. $(document).on("click", ".sidebar-toggle", function () {
  308. var value = $("body").hasClass("sidebar-collapse") ? 1 : 0;
  309. setTimeout(function () {
  310. $(window).trigger("resize");
  311. }, 300);
  312. createCookie('sidebar_collapse', value);
  313. });
  314. // 切换多级菜单
  315. $(document).on("click", "[data-config='multiplenav']", function () {
  316. var value = $(this).prop("checked") ? 1 : 0;
  317. createCookie('multiplenav', value);
  318. location.reload();
  319. });
  320. // 切换多选项卡
  321. $(document).on("click", "[data-config='multipletab']", function () {
  322. var value = $(this).prop("checked") ? 1 : 0;
  323. $("body").toggleClass("multipletab", value);
  324. createCookie('multipletab', value);
  325. });
  326. // 重设选项
  327. if ($('body').hasClass('fixed')) {
  328. $("[data-layout='fixed']").attr('checked', 'checked');
  329. }
  330. if ($('body').hasClass('layout-boxed')) {
  331. $("[data-layout='layout-boxed']").attr('checked', 'checked');
  332. }
  333. if ($('body').hasClass('sidebar-collapse')) {
  334. $("[data-layout='sidebar-collapse']").attr('checked', 'checked');
  335. }
  336. if ($('ul.sidebar-menu').hasClass('show-submenu')) {
  337. $("[data-menu='show-submenu']").attr('checked', 'checked');
  338. }
  339. var sidebarExpandOnHover = localStorage.getItem('sidebarExpandOnHover');
  340. if (sidebarExpandOnHover == '1') {
  341. $("[data-enable='expandOnHover']").trigger("click");
  342. }
  343. $.each(my_skins, function (i, j) {
  344. if ($("body").hasClass(j)) {
  345. $(".skin-list li a[data-skin='" + j + "']").parent().addClass("active");
  346. }
  347. });
  348. $(window).resize();
  349. },
  350. login: function () {
  351. var lastlogin = localStorage.getItem("lastlogin");
  352. if (lastlogin) {
  353. lastlogin = JSON.parse(lastlogin);
  354. $("#profile-img").attr("src", Backend.api.cdnurl(lastlogin.avatar));
  355. $("#profile-name").val(lastlogin.username);
  356. }
  357. //让错误提示框居中
  358. Fast.config.toastr.positionClass = "toast-top-center";
  359. //本地验证未通过时提示
  360. $("#login-form").data("validator-options", {
  361. invalid: function (form, errors) {
  362. $.each(errors, function (i, j) {
  363. Toastr.error(j);
  364. });
  365. },
  366. target: '#errtips'
  367. });
  368. //为表单绑定事件
  369. Form.api.bindevent($("#login-form"), function (data) {
  370. localStorage.setItem("lastlogin", JSON.stringify({
  371. id: data.id,
  372. username: data.username,
  373. avatar: data.avatar
  374. }));
  375. location.href = Backend.api.fixurl(data.url);
  376. }, function (data) {
  377. $("input[name=captcha]").next(".input-group-addon").find("img").trigger("click");
  378. });
  379. }
  380. };
  381. return Controller;
  382. });