index.js 19 KB

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